From ab9126d8927e31ea97c12c56c8328a288eca0037 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Mon, 20 Feb 2017 18:07:02 -0500 Subject: [PATCH] update customization points and track un-specialized structs to trigger some safe-guards as a back-compatible safety net --- docs/source/tutorial/customization.rst | 5 +++++ examples/customization.cpp | 17 +++++++++++----- single/sol/sol.hpp | 28 +++++++++++++++++++++----- sol/types.hpp | 24 +++++++++++++++++++--- 4 files changed, 61 insertions(+), 13 deletions(-) diff --git a/docs/source/tutorial/customization.rst b/docs/source/tutorial/customization.rst index 5479c804..d17379bf 100644 --- a/docs/source/tutorial/customization.rst +++ b/docs/source/tutorial/customization.rst @@ -24,6 +24,11 @@ These are template class/structs, so you'll override them using a technique C++ template <> struct lua_size : std::integral_constant {}; + // Then, specialize the type to match: + // this has multiple types, so we consider it a "poly" type. + template <> + struct lua_type_of : std::integral_constant {}; + // Now, specialize various stack structures namespace stack { diff --git a/examples/customization.cpp b/examples/customization.cpp index 45280e59..f9228951 100644 --- a/examples/customization.cpp +++ b/examples/customization.cpp @@ -16,6 +16,11 @@ namespace sol { template <> struct lua_size : std::integral_constant {}; + // Then, specialize the type + // this makes sure Sol can return it properly + template <> + struct lua_type_of : std::integral_constant {}; + // Now, specialize various stack structures namespace stack { @@ -67,22 +72,24 @@ namespace sol { } int main() { + std::cout << "=== customization example ===" << std::endl; + std::cout << std::boolalpha; + sol::state lua; + lua.open_libraries(sol::lib::base); // Create a pass-through style of function - lua.script("function f ( a, b ) return a, b end"); + lua.script("function f ( a, b ) print(a, b) return a, b end"); // get the function out of Lua sol::function f = lua["f"]; - two_things things = f(two_things{ 24, true }); + two_things things = f(two_things{ 24, false }); assert(things.a == 24); - assert(things.b == true); + assert(things.b == false); // things.a == 24 // things.b == true - std::cout << "=== customization example ===" << std::endl; - std::cout << std::boolalpha; std::cout << "things.a: " << things.a << std::endl; std::cout << "things.b: " << things.b << std::endl; std::cout << std::endl; diff --git a/single/sol/sol.hpp b/single/sol/sol.hpp index 75de773e..34709d82 100644 --- a/single/sol/sol.hpp +++ b/single/sol/sol.hpp @@ -20,8 +20,8 @@ // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // This file was generated with a script. -// Generated 2017-02-20 08:45:30.715552 UTC -// This header was generated with sol v2.15.9 (revision b7b6366) +// Generated 2017-02-20 23:06:29.737387 UTC +// This header was generated with sol v2.15.9 (revision 889a45d) // https://github.com/ThePhD/sol2 #ifndef SOL_SINGLE_INCLUDE_HPP @@ -3686,10 +3686,14 @@ namespace sol { struct is_unique_usertype : std::integral_constant::value> {}; template - struct lua_type_of : detail::lua_type_of {}; + struct lua_type_of : detail::lua_type_of { + typedef int SOL_INTERNAL_UNSPECIALIZED_MARKER_; + }; template - struct lua_size : std::integral_constant { }; + struct lua_size : std::integral_constant { + typedef int SOL_INTERNAL_UNSPECIALIZED_MARKER_; + }; template struct lua_size> : std::integral_constant::value + lua_size::value> { }; @@ -3697,10 +3701,24 @@ namespace sol { template struct lua_size> : std::integral_constant::value> { }; + namespace detail { + template + struct void_ { typedef void type; }; + template + struct has_internal_marker_impl : std::false_type {}; + template + struct has_internal_marker_impl::type> : std::true_type {}; + + template + struct has_internal_marker : has_internal_marker_impl {}; + } + template struct is_lua_primitive : std::integral_constant>::value - || (lua_size::value > 1) + || ((type::userdata == lua_type_of>::value) + && detail::has_internal_marker>>::value + && !detail::has_internal_marker>>::value) || std::is_base_of>::value || std::is_base_of>::value || meta::is_specialization_of>::value diff --git a/sol/types.hpp b/sol/types.hpp index be4b5b87..843b2503 100644 --- a/sol/types.hpp +++ b/sol/types.hpp @@ -704,10 +704,14 @@ namespace sol { struct is_unique_usertype : std::integral_constant::value> {}; template - struct lua_type_of : detail::lua_type_of {}; + struct lua_type_of : detail::lua_type_of { + typedef int SOL_INTERNAL_UNSPECIALIZED_MARKER_; + }; template - struct lua_size : std::integral_constant { }; + struct lua_size : std::integral_constant { + typedef int SOL_INTERNAL_UNSPECIALIZED_MARKER_; + }; template struct lua_size> : std::integral_constant::value + lua_size::value> { }; @@ -715,10 +719,24 @@ namespace sol { template struct lua_size> : std::integral_constant::value> { }; + namespace detail { + template + struct void_ { typedef void type; }; + template + struct has_internal_marker_impl : std::false_type {}; + template + struct has_internal_marker_impl::type> : std::true_type {}; + + template + struct has_internal_marker : has_internal_marker_impl {}; + } + template struct is_lua_primitive : std::integral_constant>::value - || (lua_size::value > 1) + || ((type::userdata == lua_type_of>::value) + && detail::has_internal_marker>>::value + && !detail::has_internal_marker>>::value) || std::is_base_of>::value || std::is_base_of>::value || meta::is_specialization_of>::value