From c5863c8c3156ff2b405b4f8005e8f8edeb1a094a Mon Sep 17 00:00:00 2001 From: ThePhD Date: Fri, 24 Jan 2020 20:24:50 -0500 Subject: [PATCH] Improve comments and example code. --- .../source/customization_base_object_catch.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/examples/source/customization_base_object_catch.cpp b/examples/source/customization_base_object_catch.cpp index 67af265f..099cc51c 100644 --- a/examples/source/customization_base_object_catch.cpp +++ b/examples/source/customization_base_object_catch.cpp @@ -92,9 +92,16 @@ sol::object BaseObject::getAsRetyped(lua_State* L, BaseObjectLifetime Lifetime) return sol::make_object(L, std::make_shared(*static_cast(this))); } default: - // we have a normal type here, so that means we - // must bypass customization points std::cout << "Unknown type: falling back to base object." << std::endl; + // we have a normal type here, so that means we + // must bypass typical customization points by using + // sol::make_object_userdata/sol::make_reference_userdata + // WARNING: IF THIS TYPE IS IN FACT NOT A BASE OBJECT, + // BUT SOME DERIVED OBJECT, THEN RUNNING THIS CODE FOR + // VALUE TYPES AND SHARED TYPES WILL "SLICE" + // THE DERIVED BITS OFF THE BASE BITS PERMANENTLY + // NEVER FORGET TO UPDATE THE SWITCH IF YOU ADD + // NEW TYPES!! switch (Lifetime) { case BaseObjectLifetime::Value: return sol::make_object_userdata(L, *this); @@ -123,8 +130,8 @@ int sol_lua_push(sol::types>, lua_State* L, const st } int main() { - // test our customization points out - std::cout << "=== Base object customization points ===" << std::endl; + // test our customization points out + std::cout << "=== Base object customization points ===" << std::endl; sol::state lua; lua.open_libraries(sol::lib::base, sol::lib::string, sol::lib::table); @@ -157,6 +164,7 @@ int main() { // Same objects but as base objects to test mapping. std::cout << "Base-cast pointers..." << std::endl; + lua["ptrBaseAsBase"] = static_cast(&base); lua["ptrArmorAsBase"] = static_cast(&armor); lua["ptrWeaponAsBase"] = static_cast(&weapon); std::cout << std::endl; @@ -168,6 +176,7 @@ int main() { std::cout << std::endl; std::cout << "Smart pointers put as the base class..." << std::endl; + lua["sharedBaseAsBase"] = (std::shared_ptr)std::make_shared(); lua["sharedArmorAsBase"] = (std::shared_ptr)std::make_shared(); lua["sharedArmorAsBase"] = (std::shared_ptr)std::make_shared(); std::cout << std::endl;