From 045d9371df03c8015f5d3f1c9fcf55dbde7d33c0 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Fri, 29 Jul 2016 00:57:47 -0400 Subject: [PATCH] customization points are now live --- docs/source/api/stack.rst | 36 ++++++- docs/source/tutorial/customization.rst | 104 ++++++++++++++++++ docs/source/tutorial/cxx-in-lua.rst | 2 +- docs/source/tutorial/tutorial-top.rst | 1 + sol/call.hpp | 4 +- sol/function.hpp | 14 +-- sol/stack.hpp | 22 +++- sol/stack_check.hpp | 123 ++++++++++----------- sol/stack_check_get.hpp | 40 ++++--- sol/stack_core.hpp | 108 ++++++++++++++++--- sol/stack_get.hpp | 143 +++++++++++++++---------- sol/stack_pop.hpp | 23 +--- sol/stack_proxy.hpp | 27 +++++ sol/tie.hpp | 25 +++-- sol/types.hpp | 31 ++++-- sol/variadic_args.hpp | 3 +- test_customizations.cpp | 83 ++++++++++++++ test_usertypes.cpp | 3 +- 18 files changed, 594 insertions(+), 198 deletions(-) create mode 100644 docs/source/tutorial/customization.rst create mode 100644 test_customizations.cpp diff --git a/docs/source/api/stack.rst b/docs/source/api/stack.rst index ed07d000..0ebb2d2e 100644 --- a/docs/source/api/stack.rst +++ b/docs/source/api/stack.rst @@ -11,6 +11,26 @@ If you find that the higher level abstractions are not meeting your needs, you m There are, however, a few :ref:`template customization points` that you may use for your purposes and a handful of potentially handy functions. These may help if you're trying to slim down the code you have to write, or if you want to make your types behave differently throughout the Sol stack. Note that overriding the defaults **can** throw out many of the safety guarantees Sol provides: therefore, modify the :ref:`extension points` at your own discretion. +structures +---------- + +.. code-block:: cpp + :caption: struct: record + :name: stack-record + + struct record { + int last; + int used; + + void use(int count); + }; + +This structure is for advanced usage with :ref:`stack::get` and :ref:`stack::check_get`. When overriding the customization points, it is important to call the ``use`` member function on this class with the amount of things you are pulling from the stack. ``used`` contains the total accumulation of items produced. ``last`` is the number of items gotten from the stack with the last operation (not necessarily popped from the stack). In all trivial cases for types, ``last == 1`` and ``used == 1`` after an operation; structures such as ``std::pair`` and ``std::tuple`` may pull more depending on the classes it contains. + +When overriding the :doc:`customization points<../tutorial/customization>`, please note that this structure should enable you to push multiple return values and get multiple return values to the stack, and thus be able to seamlessly pack/unpack return values from Lua into a single C++ struct, and vice-versa. This functionality is only recommended for people who need to customize the library further than the basics. It is also a good way to add support for the type and propose it back to the original library so that others may benefit from your work. + +Note that customizations can also be put up on a separate page here, if individuals decide to make in-depth custom ones for their framework or other places. + members ------- @@ -20,9 +40,13 @@ members template auto get( lua_State* L, int index = -1 ) + template + auto get( lua_State* L, int index, record& tracking ) Retrieves the value of the object at ``index`` in the stack. The return type varies based on ``T``: with primitive types, it is usually ``T``: for all unrecognized ``T``, it is generally a ``T&`` or whatever the extension point :ref:`stack::getter\` implementation returns. The type ``T`` has top-level ``const`` qualifiers and reference modifiers removed before being forwarded to the extension point :ref:`stack::getter\` struct. ``stack::get`` will default to forwarding all arguments to the :ref:`stack::check_get` function with a handler of ``type_panic`` to strongly alert for errors, if you ask for the :doc:`safety<../safety>`. +`record` + You may also retrieve an :doc:`sol::optional\` from this as well, to have it attempt to not throw errors when performing the get and the type is not correct. .. code-block:: cpp @@ -44,7 +68,7 @@ Checks if the object at ``index`` is of type ``T``. If it is not, it will call t template auto check_get( lua_State* L, int index = -1 ) template - auto check_get( lua_State* L, int index, Handler&& handler ) + auto check_get( lua_State* L, int index, Handler&& handler, record& tracking ) Retrieves the value of the object at ``index`` in the stack, but does so safely. It returns an ``optional``, where ``U`` in this case is the return type deduced from ``stack::get``. This allows a person to properly check if the type they're getting is what they actually want, and gracefully handle errors when working with the stack if they so choose to. You can define ``SOL_CHECK_ARGUMENTS`` to turn on additional :doc:`safety<../safety>`, in which ``stack::get`` will default to calling this version of the function with a handler of ``type_panic`` to strongly alert for errors and help you track bugs if you suspect something might be going wrong in your system. @@ -117,6 +141,10 @@ This struct is used for showing whether or not a :ref:`probing get_field`. + +Below is more extensive information for the curious. + The structs below are already overriden for a handful of types. If you try to mess with them for the types ``sol`` has already overriden them for, you're in for a world of thick template error traces and headaches. Overriding them for your own user defined types should be just fine, however. .. code-block:: cpp @@ -125,7 +153,7 @@ The structs below are already overriden for a handful of types. If you try to me template struct getter { - static T get (int index = -1) { + static T get (lua_State* L, int index, record& tracking) { // ... return // T, or something related to T. } @@ -156,7 +184,7 @@ This is an SFINAE-friendly struct that is meant to expose static function ``push template , typename = void> struct checker { template - static bool check ( lua_State* L, int index, Handler&& handler ) { + static bool check ( lua_State* L, int index, Handler&& handler, record& tracking ) { // if the object in the Lua stack at index is a T, return true if ( ... ) return true; // otherwise, call the handler function, @@ -166,6 +194,6 @@ This is an SFINAE-friendly struct that is meant to expose static function ``push } }; -This is an SFINAE-friendly struct that is meant to expose static function ``check`` that returns the number of things pushed onto the stack. The default implementation simply checks whether the expected type passed in through the template is equal to the type of the object at the specified index in the Lua stack. The default implementation for types which are considered ``userdata`` go through a myriad of checks to support checking if a type is *actually* of type ``T`` or if its the base class of what it actually stored as a userdata in that index. Down-casting from a base class to a mroe derived type is, unfortunately, impossible to do. +This is an SFINAE-friendly struct that is meant to expose static function ``check`` that returns the number of things pushed onto the stack. The default implementation simply checks whether the expected type passed in through the template is equal to the type of the object at the specified index in the Lua stack. The default implementation for types which are considered ``userdata`` go through a myriad of checks to support checking if a type is *actually* of type ``T`` or if its the base class of what it actually stored as a userdata in that index. Down-casting from a base class to a more derived type is, unfortunately, impossible to do. .. _lua_CFunction: http://www.Lua.org/manual/5.3/manual.html#lua_CFunction \ No newline at end of file diff --git a/docs/source/tutorial/customization.rst b/docs/source/tutorial/customization.rst new file mode 100644 index 00000000..7f15cef6 --- /dev/null +++ b/docs/source/tutorial/customization.rst @@ -0,0 +1,104 @@ +adding your own types +===================== + +Sometimes, overriding Sol to make it handle certain ``struct``s and ``class``es as something other than just userdata is desirable. The way to do this is to take advantage of the 4 customization points for Sol. These are ``sol::lua_size``, ``sol::stack::pusher``, ``sol::stack::getter``, ``sol::stack::checker``. + +The first thing to do is decide whether or not your type can be gotten from the stack, and whether or not it should also be pushed as arguments or such into Lua. If you need to retrieve it (as a return using one or multiple values from a Lua return), you should override but ``sol::stack::getter`` and ``sol::stack::checker``. If you need to push it into Lua at some point, then you'll want to override ``sol::stack::pusher``. For both cases, you need to override ``sol::lua_size``. + +These are structures, so you'll override them using a technique C++ calls *class/struct specialization*. Below is an example of a struct that gets broken apart into 2 pieces when being pushed into Lua, and then pulled back into a struct when retrieved from Lua: + +.. code-block:: cpp + :caption: two_things.hpp + :name: customization-overriding + + #include + + struct two_things { + int a; + bool b; + }; + + namespace sol { + + // First, the expected size + // Specialization of a struct + template <> + struct lua_size : std::integral_constant {}; + + // Now, specialize various stack structures + namespace stack { + + template <> + struct checker { + template + static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { + // Check first and second second index for being the proper types + bool success = stack::check(L, index, handler) + && stack::check(L, index + 1, handler); + tracking.use(2); + return success; + } + }; + + template <> + struct getter { + static two_things get(lua_State* L, int index, record& tracking) { + // Get the first element + int a = stack::get(L, index); + // Get the second element, + // in the +1 position from the first + bool b = stack::get(L, index + 1); + // we use 2 slots, each of the previous takes 1 + tracking.use(2); + return two_things{ a, b }; + } + }; + + template <> + struct pusher { + static int push(lua_State* L, const two_things& things) { + int amount = stack::push(L, things.a); + amount += stack::push(L, things.b); + // Return 2 things + return amount; + } + }; + + } + } + + +This is the base formula that you can follow to extend to your own classes. Using it in the rest of the framework should then be seamless: + +.. code-block:: cpp + :caption: customization: using it + :name: customization-using + + #include + #include + + int main (int argc, char* argv[]) { + + sol::state lua; + + // Create a pass-through style of function + lua.script("function f ( 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}); + // things.a == 24 + // things.b == true + + return 0; + } + + +And that's it! + +A few things of note about the implementation: First, there's an auxiliary parameter of type :doc:`sol::stack::record<../api/stack>` for the getters and checkers. This keeps track of what the last complete operation performed. Since we retrieved 2 members, we use ``tracking.use(2);`` to indicate that we used 2 stack positions (one for ``bool``, one for ``int``). The second thing to note here is that we made sure to use the ``index`` parameter, and then proceeded to add 1 to it for the next one. + +In general, this is fine since most getters/checkers only use 1 stack point. But, if you're doing more complex nested classes, it would be useful to use ``tracking.last`` to understand how many stack indices the last getter/checker operation did and increment it by ``index + tracking.last`` after using a ``stack::check<..>( L, index, tracking)`` call. + +You can read more about the structs themselves :ref:`over on the API page for stack`, and if there's something that goes wrong or you have anymore questions, please feel free to drop a line on the Github Issues page or send an e-mail! \ No newline at end of file diff --git a/docs/source/tutorial/cxx-in-lua.rst b/docs/source/tutorial/cxx-in-lua.rst index 9a3b8cb1..25e38f48 100644 --- a/docs/source/tutorial/cxx-in-lua.rst +++ b/docs/source/tutorial/cxx-in-lua.rst @@ -126,4 +126,4 @@ To do this, you bind things using the ``new_usertype`` and ``set_usertype`` meth That script should run fine now, and you can observe and play around with the values. Even more stuff :doc:`you can do<../api/usertype>` is described elsewhere, like initializer functions (private constructors / destructors support), "static" functions callable with ``name.my_function( ... )``, and overloaded member functions. -This is a powerful way to allow reuse of C++ code from Lua beyond just registering functions, and should get you on your way to having more complex classes and data structures! \ No newline at end of file +This is a powerful way to allow reuse of C++ code from Lua beyond just registering functions, and should get you on your way to having more complex classes and data structures! In the case that you need more customization than just usertypes, however, you can customize Sol to behave more fit to your desires by using the desired :doc:`customization and extension structures`. \ No newline at end of file diff --git a/docs/source/tutorial/tutorial-top.rst b/docs/source/tutorial/tutorial-top.rst index 1e88e9a4..398aa5ae 100644 --- a/docs/source/tutorial/tutorial-top.rst +++ b/docs/source/tutorial/tutorial-top.rst @@ -16,3 +16,4 @@ Take some time to learn the framework with thse tutorials. But, if you need to g functions cxx-in-lua ownership + customization diff --git a/sol/call.hpp b/sol/call.hpp index f443f3a2..b4973039 100644 --- a/sol/call.hpp +++ b/sol/call.hpp @@ -85,7 +85,6 @@ namespace sol { typedef lua_bind_traits> traits; typedef meta::tuple_types return_types; typedef typename traits::free_args_list args_list; - typedef typename args_list::indices args_indices; // compile-time eliminate any functions that we know ahead of time are of improper arity if (meta::find_in_pack_v, index_value...>::value) { return overload_match_arity(types(), std::index_sequence(), std::index_sequence(), std::forward(matchfx), L, fxarity, start, std::forward(args)...); @@ -93,7 +92,8 @@ namespace sol { if (traits::free_arity != fxarity) { return overload_match_arity(types(), std::index_sequence(), std::index_sequence(), std::forward(matchfx), L, fxarity, start, std::forward(args)...); } - if (!stack::stack_detail::check_types().check(args_list(), args_indices(), L, start, no_panic)) { + stack::record tracking{}; + if (!stack::stack_detail::check_types{}.check(args_list(), L, start, no_panic, tracking)) { return overload_match_arity(types(), std::index_sequence(), std::index_sequence(), std::forward(matchfx), L, fxarity, start, std::forward(args)...); } return matchfx(types(), index_value(), return_types(), args_list(), L, fxarity, start, std::forward(args)...); diff --git a/sol/function.hpp b/sol/function.hpp index 6e26d455..4da237dd 100644 --- a/sol/function.hpp +++ b/sol/function.hpp @@ -40,13 +40,13 @@ namespace sol { template auto invoke(types, std::index_sequence, std::ptrdiff_t n) const { - luacall(n, sizeof...(Ret)); + luacall(n, lua_size>::value); return stack::pop>(base_t::lua_state()); } template Ret invoke(types, std::index_sequence, std::ptrdiff_t n) const { - luacall(n, 1); + luacall(n, lua_size::value); return stack::pop(base_t::lua_state()); } @@ -104,7 +104,7 @@ namespace sol { typedef meta::tuple_types return_types; template - static std::function get_std_func(types, types, lua_State* L, int index = -1) { + static std::function get_std_func(types, types, lua_State* L, int index) { sol::function f(L, index); auto fx = [f, L, index](Args&&... args) -> meta::return_type_t { return f.call(std::forward(args)...); @@ -113,7 +113,7 @@ namespace sol { } template - static std::function get_std_func(types, types, lua_State* L, int index = -1) { + static std::function get_std_func(types, types, lua_State* L, int index) { sol::function f(L, index); auto fx = [f, L, index](FxArgs&&... args) -> void { f(std::forward(args)...); @@ -122,11 +122,13 @@ namespace sol { } template - static std::function get_std_func(types<>, types t, lua_State* L, int index = -1) { + static std::function get_std_func(types<>, types t, lua_State* L, int index) { return get_std_func(types(), t, L, index); } - static std::function get(lua_State* L, int index) { + static std::function get(lua_State* L, int index, record& tracking) { + tracking.last = 1; + tracking.used += 1; type t = type_of(L, index); if (t == type::none || t == type::nil) { return nullptr; diff --git a/sol/stack.hpp b/sol/stack.hpp index 5f6939ff..9d1ab8d7 100644 --- a/sol/stack.hpp +++ b/sol/stack.hpp @@ -66,13 +66,26 @@ namespace sol { return std::pair(*reinterpret_cast(static_cast(voiddata.data())), index); } + struct evaluator { + template + static decltype(auto) eval(types<>, std::index_sequence<>, lua_State*, int, record&, Fx&& fx, Args&&... args) { + return std::forward(fx)(std::forward(args)...); + } + + template + static decltype(auto) eval(types, std::index_sequence, lua_State* L, int start, record& tracking, Fx&& fx, FxArgs&&... fxargs) { + return eval(types(), std::index_sequence(), L, start, tracking, std::forward(fx), std::forward(fxargs)..., stack_detail::unchecked_get(L, start + tracking.used, tracking)); + } + }; + template ::value>> inline decltype(auto) call(types, types ta, std::index_sequence tai, lua_State* L, int start, Fx&& fx, FxArgs&&... args) { #ifndef _MSC_VER static_assert(meta::all...>::value, "One of the arguments being bound is a move-only type, and it is not being taken by reference: this will break your code. Please take a reference and std::move it manually if this was your intention."); #endif // This compiler make me so fucking sad - check_types{}.check(ta, tai, L, start, type_panic); - return fx(std::forward(args)..., stack_detail::unchecked_get(L, start + I - meta::count_for_to_pack::value)...); + multi_check(L, start, type_panic); + record tracking{}; + return evaluator{}.eval(ta, tai, L, start, tracking, std::forward(fx), std::forward(args)...); } template @@ -80,8 +93,9 @@ namespace sol { #ifndef _MSC_VER static_assert(meta::all...>::value, "One of the arguments being bound is a move-only type, and it is not being taken by reference: this will break your code. Please take a reference and std::move it manually if this was your intention."); #endif // This compiler make me so fucking sad - check_types{}.check(ta, tai, L, start, type_panic); - fx(std::forward(args)..., stack_detail::unchecked_get(L, start + I - meta::count_for_to_pack::value)...); + multi_check(L, start, type_panic); + record tracking{}; + evaluator{}.eval(ta, tai, L, start, tracking, std::forward(fx), std::forward(args)...); } } // stack_detail diff --git a/sol/stack_check.hpp b/sol/stack_check.hpp index 5888ea3e..4c0986a1 100644 --- a/sol/stack_check.hpp +++ b/sol/stack_check.hpp @@ -50,7 +50,8 @@ namespace sol { template struct basic_check { template - static bool check(lua_State* L, int index, Handler&& handler) { + static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { + tracking.use(1); bool success = check_func(L, index) == 1; if (!success) { // expected type, actual type @@ -59,35 +60,13 @@ namespace sol { return success; } }; - - template - struct check_types { - template - static bool check(types, std::index_sequence, lua_State* L, int firstargument, Handler&& handler) { - if (!stack::check(L, firstargument + I0, handler)) - return false; - return check(types(), std::index_sequence(), L, firstargument - static_cast(is_transparent_argument>::value), std::forward(handler)); - } - - template - static bool check(types<>, std::index_sequence<>, lua_State*, int, Handler&&) { - return true; - } - }; - - template <> - struct check_types { - template - static bool check(types, std::index_sequence, lua_State*, int, Handler&&) { - return true; - } - }; } // stack_detail template struct checker { template - static bool check(lua_State* L, int index, Handler&& handler) { + static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { + tracking.use(1); const type indextype = type_of(L, index); bool success = expected == indextype; if (!success) { @@ -98,19 +77,17 @@ namespace sol { } }; - template - struct checker { - template - static bool check(lua_State*, int, Handler&&) { - return true; - } - }; - template struct checker { template - static bool check(lua_State* L, int index, Handler&& handler) { - bool success = lua_isnoneornil(L, index); + static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { + bool success = lua_isnil(L, index); + if (success) { + tracking.use(1); + return success; + } + tracking.use(0); + success = lua_isnone(L, index); if (!success) { // expected type, actual type handler(L, index, expected, type_of(L, index)); @@ -123,9 +100,10 @@ namespace sol { struct checker : checker {}; template - struct checker { + struct checker { template - static bool check(lua_State*, int, Handler&&) { + static bool check(lua_State*, int, Handler&&, record& tracking) { + tracking.use(0); return true; } }; @@ -133,7 +111,17 @@ namespace sol { template struct checker { template - static bool check(lua_State*, int, Handler&&) { + static bool check(lua_State*, int, Handler&&, record& tracking) { + tracking.use(0); + return true; + } + }; + + template + struct checker { + template + static bool check(lua_State*, int, Handler&&, record& tracking) { + tracking.use(0); return true; } }; @@ -141,7 +129,8 @@ namespace sol { template struct checker { template - static bool check(lua_State* L, int index, Handler&& handler) { + static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { + tracking.use(1); bool success = !lua_isnone(L, index); if (!success) { // expected type, actual type @@ -154,7 +143,8 @@ namespace sol { template struct checker { template - static bool check(lua_State* L, int index, Handler&& handler) { + static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { + tracking.use(1); type t = type_of(L, index); bool success = t == type::userdata || t == type::lightuserdata; if (!success) { @@ -168,7 +158,8 @@ namespace sol { template struct checker { template - static bool check(lua_State* L, int index, Handler&& handler) { + static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { + tracking.use(1); type t = type_of(L, index); bool success = t == type::userdata; if (!success) { @@ -195,7 +186,8 @@ namespace sol { template struct checker { template - static bool check(lua_State* L, int index, Handler&& handler) { + static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { + tracking.use(1); type t = type_of(L, index); if (t == type::nil || t == type::none || t == type::function) { // allow for nil to be returned @@ -228,7 +220,8 @@ namespace sol { template struct checker { template - static bool check(lua_State* L, int index, Handler&& handler) { + static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { + tracking.use(1); type t = type_of(L, index); if (t == type::table) { return true; @@ -244,20 +237,22 @@ namespace sol { template struct checker { template - static bool check(lua_State* L, int index, Handler&& handler) { + static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { + tracking.use(1); const type indextype = type_of(L, index); // Allow nil to be transformed to nullptr if (indextype == type::nil) { return true; } - return checker{}.check(types(), L, indextype, index, std::forward(handler)); + return checker{}.check(types(), L, indextype, index, std::forward(handler), tracking); } }; template struct checker { template - static bool check(types, lua_State* L, type indextype, int index, Handler&& handler) { + static bool check(types, lua_State* L, type indextype, int index, Handler&& handler, record& tracking) { + tracking.use(1); if (indextype != type::userdata) { handler(L, index, type::userdata, indextype); return false; @@ -291,56 +286,54 @@ namespace sol { } template - static bool check(lua_State* L, int index, Handler&& handler) { + static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { const type indextype = type_of(L, index); - return check(types(), L, indextype, index, std::forward(handler)); + return check(types(), L, indextype, index, std::forward(handler), tracking); } }; template struct checker::value>> { template - static bool check(lua_State* L, int index, Handler&& handler) { - return checker::type, type::userdata>{}.check(L, index, std::forward(handler)); + static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { + return checker::type, type::userdata>{}.check(L, index, std::forward(handler), tracking); } }; template struct checker, type::userdata, C> { template - static bool check(lua_State* L, int index, Handler&& handler) { - return checker{}.check(L, index, std::forward(handler)); + static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { + return checker{}.check(L, index, std::forward(handler), tracking); } }; template struct checker, type::poly, C> { - template - static bool apply(std::index_sequence is, lua_State* L, int index, Handler&& handler) { - index = index < 0 ? lua_absindex(L, index) - (sizeof...(I)-1) : index; - return stack_detail::check_types{}.check(types(), is, L, index, handler); - } - template - static bool check(lua_State* L, int index, Handler&& handler) { - return apply(std::make_index_sequence(), L, index, std::forward(handler)); + static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { + return stack::multi_check(L, index, std::forward(handler), tracking); } }; template struct checker, type::poly, C> { template - static bool check(lua_State* L, int index, Handler&& handler) { - index = index < 0 ? lua_absindex(L, index) - 1 : index; - return stack::check(L, index, handler) && stack::check(L, index + 1, handler); + static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { + return stack::multi_check(L, index, std::forward(handler), tracking); } }; template struct checker, type::poly, C> { template - static bool check(lua_State* L, int index, Handler&& handler) { - return lua_isnoneornil(L, index) || stack::check(L, index, std::forward(handler)); + static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { + type t = type_of(L, index); + if (t == type::none) { + tracking.use(0); + return true; + } + return t == type::nil || stack::check(L, index, std::forward(handler), tracking); } }; } // stack diff --git a/sol/stack_check_get.hpp b/sol/stack_check_get.hpp index ebab0734..728d7d9a 100644 --- a/sol/stack_check_get.hpp +++ b/sol/stack_check_get.hpp @@ -31,35 +31,39 @@ namespace sol { namespace stack { template struct check_getter { - typedef stack_detail::strip_t U; - typedef std::conditional_t::value, U, U&> R; + typedef decltype(stack_detail::unchecked_get(nullptr, 0, std::declval())) R; template - static optional get(lua_State* L, int index, Handler&& handler) { - if (!check(L, index, std::forward(handler))) + static optional get(lua_State* L, int index, Handler&& handler, record& tracking) { + if (!check(L, index, std::forward(handler))) { + tracking.use(static_cast(!lua_isnone(L, index))); return nullopt; - return stack_detail::unchecked_get(L, index); + } + return stack_detail::unchecked_get(L, index, tracking); } }; template struct check_getter> { template - static decltype(auto) get(lua_State* L, int index, Handler&&) { - return check_get(L, index, no_panic); + static decltype(auto) get(lua_State* L, int index, Handler&&, record& tracking) { + return check_get(L, index, no_panic, tracking); } }; template struct check_getter::value && lua_type_of::value == type::number>> { template - static optional get(lua_State* L, int index, Handler&& handler) { + static optional get(lua_State* L, int index, Handler&& handler, record& tracking) { int isnum = 0; lua_Integer value = lua_tointegerx(L, index, &isnum); if (isnum == 0) { - handler(L, index, type::number, type_of(L, index)); + type t = type_of(L, index); + tracking.use(static_cast(t != type::none)); + handler(L, index, type::number, t); return nullopt; } + tracking.use(1); return static_cast(value); } }; @@ -67,13 +71,16 @@ namespace sol { template struct check_getter::value && !meta::any_same::value>> { template - static optional get(lua_State* L, int index, Handler&& handler) { + static optional get(lua_State* L, int index, Handler&& handler, record& tracking) { int isnum = 0; lua_Integer value = lua_tointegerx(L, index, &isnum); if (isnum == 0) { - handler(L, index, type::number, type_of(L, index)); + type t = type_of(L, index); + tracking.use(static_cast(t != type::none)); + handler(L, index, type::number, t); return nullopt; } + tracking.use(1); return static_cast(value); } }; @@ -81,21 +88,24 @@ namespace sol { template struct check_getter::value>> { template - static optional get(lua_State* L, int index, Handler&& handler) { + static optional get(lua_State* L, int index, Handler&& handler, record& tracking) { int isnum = 0; lua_Number value = lua_tonumberx(L, index, &isnum); if (isnum == 0) { - handler(L, index, type::number, type_of(L, index)); + type t = type_of(L, index); + tracking.use(static_cast(t != type::none)); + handler(L, index, type::number, t); return nullopt; } + tracking.use(1); return static_cast(value); } }; template struct getter> { - static decltype(auto) get(lua_State* L, int index) { - return check_get(L, index, no_panic); + static decltype(auto) get(lua_State* L, int index, record& tracking) { + return check_get(L, index, no_panic, tracking); } }; } // stack diff --git a/sol/stack_core.hpp b/sol/stack_core.hpp index 76ed4c25..dfc9e43d 100644 --- a/sol/stack_core.hpp +++ b/sol/stack_core.hpp @@ -82,6 +82,17 @@ namespace sol { operator bool() const { return success; }; }; + struct record { + int last; + int used; + + record() : last(), used() {} + void use(int count) { + last = count; + used += count; + } + }; + namespace stack_detail { template struct strip { @@ -108,8 +119,8 @@ namespace sol { false; #endif template - inline decltype(auto) unchecked_get(lua_State* L, int index = -1) { - return getter>{}.get(L, index); + inline decltype(auto) unchecked_get(lua_State* L, int index, record& tracking) { + return getter>{}.get(L, index, tracking); } } // stack_detail @@ -164,12 +175,18 @@ namespace sol { } template - bool check(lua_State* L, int index, Handler&& handler) { + bool check(lua_State* L, int index, Handler&& handler, record& tracking) { typedef meta::unqualified_t Tu; checker c; // VC++ has a bad warning here: shut it up (void)c; - return c.check(L, index, std::forward(handler)); + return c.check(L, index, std::forward(handler), tracking); + } + + template + bool check(lua_State* L, int index, Handler&& handler) { + record tracking{}; + return check(L, index, std::forward(handler), tracking); } template @@ -178,9 +195,15 @@ namespace sol { return check(L, index, handler); } + template + inline decltype(auto) check_get(lua_State* L, int index, Handler&& handler, record& tracking) { + return check_getter>{}.get(L, index, std::forward(handler), tracking); + } + template inline decltype(auto) check_get(lua_State* L, int index, Handler&& handler) { - return check_getter>{}.get(L, index, std::forward(handler)); + record tracking{}; + return check_get(L, index, handler, tracking); } template @@ -193,20 +216,20 @@ namespace sol { #ifdef SOL_CHECK_ARGUMENTS template - inline auto tagged_get(types, lua_State* L, int index = -1) -> decltype(stack_detail::unchecked_get(L, index)) { - auto op = check_get(L, index, type_panic); + inline auto tagged_get(types, lua_State* L, int index, record& tracking) -> decltype(stack_detail::unchecked_get(L, index, tracking)) { + auto op = check_get(L, index, type_panic, tracking); return *op; } #else template - inline decltype(auto) tagged_get(types, lua_State* L, int index = -1) { - return stack_detail::unchecked_get(L, index); + inline decltype(auto) tagged_get(types, lua_State* L, int index, record& tracking) { + return stack_detail::unchecked_get(L, index, tracking); } #endif template - inline decltype(auto) tagged_get(types>, lua_State* L, int index = -1) { - return stack_detail::unchecked_get>(L, index); + inline decltype(auto) tagged_get(types>, lua_State* L, int index, record& tracking) { + return stack_detail::unchecked_get>(L, index, tracking); } template @@ -218,11 +241,72 @@ namespace sol { return 0; } + template + struct check_types { + template + static bool check(types, lua_State* L, int firstargument, Handler&& handler, record& tracking) { + if (!stack::check(L, firstargument + tracking.used, handler, tracking)) + return false; + return check(types(), L, firstargument, std::forward(handler), tracking); + } + + template + static bool check(types<>, lua_State*, int, Handler&&, record&) { + return true; + } + }; + + template <> + struct check_types { + template + static bool check(types, lua_State*, int, Handler&&, record&) { + return true; + } + }; + } // stack_detail + template + bool multi_check(lua_State* L, int index, Handler&& handler, record& tracking) { + return stack_detail::check_types{}.check(types...>(), L, index, std::forward(handler), tracking); + } + + template + bool multi_check(lua_State* L, int index, Handler&& handler) { + record tracking{}; + return multi_check(L, index, std::forward(handler), tracking); + } + + template + bool multi_check(lua_State* L, int index) { + auto handler = no_panic; + return multi_check(L, index, handler); + } + + template + bool multi_check(lua_State* L, int index, Handler&& handler, record& tracking) { + return multi_check(L, index, std::forward(handler), tracking); + } + + template + bool multi_check(lua_State* L, int index, Handler&& handler) { + return multi_check(L, index, std::forward(handler)); + } + + template + bool multi_check(lua_State* L, int index) { + return multi_check(L, index); + } + + template + inline decltype(auto) get(lua_State* L, int index, record& tracking) { + return stack_detail::tagged_get(types(), L, index, tracking); + } + template inline decltype(auto) get(lua_State* L, int index = -1) { - return stack_detail::tagged_get(types(), L, index); + record tracking{}; + return get(L, index, tracking); } template diff --git a/sol/stack_get.hpp b/sol/stack_get.hpp index c917c092..dc2f9dd1 100644 --- a/sol/stack_get.hpp +++ b/sol/stack_get.hpp @@ -36,98 +36,111 @@ namespace sol { template struct getter { - static T& get(lua_State* L, int index = -1) { - return getter{}.get(L, index); + static T& get(lua_State* L, int index, record& tracking) { + return getter{}.get(L, index, tracking); } }; template struct getter::value>> { - static T get(lua_State* L, int index = -1) { + static T get(lua_State* L, int index, record& tracking) { + tracking.use(1); return static_cast(lua_tonumber(L, index)); } }; template struct getter, std::is_signed>::value>> { - static T get(lua_State* L, int index = -1) { + static T get(lua_State* L, int index, record& tracking) { + tracking.use(1); return static_cast(lua_tointeger(L, index)); } }; template struct getter, std::is_unsigned>::value>> { - static T get(lua_State* L, int index = -1) { + static T get(lua_State* L, int index, record& tracking) { + tracking.use(1); return static_cast(lua_tointeger(L, index)); } }; template struct getter::value>> { - static T get(lua_State* L, int index = -1) { + static T get(lua_State* L, int index, record& tracking) { + tracking.use(1); return static_cast(lua_tointegerx(L, index, nullptr)); } }; template struct getter::value || std::is_base_of::value>> { - static T get(lua_State* L, int index = -1) { + static T get(lua_State* L, int index, record& tracking) { + tracking.use(1); return T(L, index); } }; template<> struct getter { - static userdata_value get(lua_State* L, int index = -1) { + static userdata_value get(lua_State* L, int index, record& tracking) { + tracking.use(1); return userdata_value(lua_touserdata(L, index)); } }; template<> struct getter { - static lightuserdata_value get(lua_State* L, int index = -1) { + static lightuserdata_value get(lua_State* L, int index, record& tracking) { + tracking.use(1); return lightuserdata_value(lua_touserdata(L, index)); } }; template struct getter> { - static light get(lua_State* L, int index = -1) { + static light get(lua_State* L, int index, record& tracking) { + tracking.use(1); return light(static_cast(lua_touserdata(L, index))); } }; template struct getter> { - static T& get(lua_State* L, int index = -1) { + static T& get(lua_State* L, int index, record& tracking) { + tracking.use(1); return *static_cast(lua_touserdata(L, index)); } }; template struct getter> { - static T* get(lua_State* L, int index = -1) { + static T* get(lua_State* L, int index, record& tracking) { + tracking.use(1); return static_cast(lua_touserdata(L, index)); } }; template<> struct getter { - static type get(lua_State *L, int index) { + static type get(lua_State *L, int index, record& tracking) { + tracking.use(1); return static_cast(lua_type(L, index)); } }; template<> struct getter { - static bool get(lua_State* L, int index) { + static bool get(lua_State* L, int index, record& tracking) { + tracking.use(1); return lua_toboolean(L, index) != 0; } }; template<> struct getter { - static std::string get(lua_State* L, int index = -1) { + static std::string get(lua_State* L, int index, record& tracking) { + tracking.use(1); std::size_t len; auto str = lua_tolstring(L, index, &len); return{ str, len }; @@ -136,7 +149,8 @@ namespace sol { template <> struct getter { - string_detail::string_shim get(lua_State* L, int index) { + string_detail::string_shim get(lua_State* L, int index, record& tracking) { + tracking.use(1); size_t len; const char* p = lua_tolstring(L, index, &len); return string_detail::string_shim(p, len); @@ -145,15 +159,17 @@ namespace sol { template<> struct getter { - static const char* get(lua_State* L, int index = -1) { + static const char* get(lua_State* L, int index, record& tracking) { + tracking.use(1); return lua_tostring(L, index); } }; template<> struct getter { - static meta_function get(lua_State *L, int index) { - const char* name = getter{}.get(L, index); + static meta_function get(lua_State *L, int index, record& tracking) { + tracking.use(1); + const char* name = getter{}.get(L, index, tracking); for (std::size_t i = 0; i < meta_function_names.size(); ++i) if (meta_function_names[i] == name) return static_cast(i); @@ -163,7 +179,8 @@ namespace sol { template<> struct getter { - static char get(lua_State* L, int index = -1) { + static char get(lua_State* L, int index, record& tracking) { + tracking.use(1); size_t len; auto str = lua_tolstring(L, index, &len); return len > 0 ? str[0] : '\0'; @@ -174,28 +191,32 @@ namespace sol { template<> struct getter { - static std::wstring get(lua_State* L, int index = -1) { + static std::wstring get(lua_State* L, int index, record& tracking) { + tracking.use(1); return{}; } }; template<> struct getter { - static std::u16string get(lua_State* L, int index = -1) { + static std::u16string get(lua_State* L, int index, record& tracking) { + tracking.use(1); return{}; } }; template<> struct getter { - static std::u32string get(lua_State* L, int index = -1) { + static std::u32string get(lua_State* L, int index, record& tracking) { + tracking.use(1); return{}; } }; template<> struct getter { - static wchar_t get(lua_State* L, int index = -1) { + static wchar_t get(lua_State* L, int index, record& tracking) { + tracking.use(1); auto str = getter{}.get(L, index); return str.size() > 0 ? str[0] : '\0'; } @@ -203,7 +224,8 @@ namespace sol { template<> struct getter { - static char get(lua_State* L, int index = -1) { + static char get(lua_State* L, int index, record& tracking) { + tracking.use(1); auto str = getter{}.get(L, index); return str.size() > 0 ? str[0] : '\0'; } @@ -211,7 +233,8 @@ namespace sol { template<> struct getter { - static char32_t get(lua_State* L, int index = -1) { + static char32_t get(lua_State* L, int index, record& tracking) { + tracking.use(1); auto str = getter{}.get(L, index); return str.size() > 0 ? str[0] : '\0'; } @@ -221,49 +244,56 @@ namespace sol { template<> struct getter { - static nil_t get(lua_State*, int = -1) { + static nil_t get(lua_State*, int, record& tracking) { + tracking.use(1); return nil; } }; template<> struct getter { - static std::nullptr_t get(lua_State*, int = -1) { + static std::nullptr_t get(lua_State*, int, record& tracking) { + tracking.use(1); return nullptr; } }; template<> struct getter { - static nullopt_t get(lua_State*, int = -1) { + static nullopt_t get(lua_State*, int, record& tracking) { + tracking.use(1); return nullopt; } }; template<> struct getter { - static this_state get(lua_State* L, int = -1) { + static this_state get(lua_State* L, int, record& tracking) { + tracking.use(0); return this_state{ L }; } }; template<> struct getter { - static lua_CFunction get(lua_State* L, int index = -1) { + static lua_CFunction get(lua_State* L, int index, record& tracking) { + tracking.use(1); return lua_tocfunction(L, index); } }; template<> struct getter { - static c_closure get(lua_State* L, int index = -1) { + static c_closure get(lua_State* L, int index, record& tracking) { + tracking.use(1); return c_closure(lua_tocfunction(L, index), -1); } }; template<> struct getter { - static error get(lua_State* L, int index = -1) { + static error get(lua_State* L, int index, record& tracking) { + tracking.use(1); size_t sz = 0; const char* err = lua_tolstring(L, index, &sz); if (err == nullptr) { @@ -275,20 +305,22 @@ namespace sol { template<> struct getter { - static void* get(lua_State* L, int index = -1) { + static void* get(lua_State* L, int index, record& tracking) { + tracking.use(1); return lua_touserdata(L, index); } }; template struct getter { - static T* get_no_nil(lua_State* L, int index = -1) { + static T* get_no_nil(lua_State* L, int index, record& tracking) { + tracking.use(1); void** pudata = static_cast(lua_touserdata(L, index)); void* udata = *pudata; - return get_no_nil_from(L, udata, index); + return get_no_nil_from(L, udata, index, tracking); } - static T* get_no_nil_from(lua_State* L, void* udata, int index = -1) { + static T* get_no_nil_from(lua_State* L, void* udata, int index, record&) { if (detail::has_derived::value && luaL_getmetafield(L, index, &detail::base_class_cast_key()[0]) != 0) { void* basecastdata = lua_touserdata(L, -1); detail::inheritance_cast_function ic = (detail::inheritance_cast_function)basecastdata; @@ -300,25 +332,27 @@ namespace sol { return obj; } - static T* get(lua_State* L, int index = -1) { + static T* get(lua_State* L, int index, record& tracking) { type t = type_of(L, index); - if (t == type::nil) + if (t == type::nil) { + tracking.use(1); return nullptr; - return get_no_nil(L, index); + } + return get_no_nil(L, index, tracking); } }; template struct getter> { - static T* get(lua_State* L, int index = -1) { - return getter::get_no_nil(L, index); + static T* get(lua_State* L, int index, record& tracking) { + return getter::get_no_nil(L, index, tracking); } }; template struct getter { - static T& get(lua_State* L, int index = -1) { - return *getter::get_no_nil(L, index); + static T& get(lua_State* L, int index, record& tracking) { + return *getter::get_no_nil(L, index, tracking); } }; @@ -327,7 +361,8 @@ namespace sol { typedef typename unique_usertype_traits::type P; typedef typename unique_usertype_traits::actual_type Real; - static Real& get(lua_State* L, int index = -1) { + static Real& get(lua_State* L, int index, record& tracking) { + tracking.use(1); P** pref = static_cast(lua_touserdata(L, index)); detail::special_destruct_func* fx = static_cast(static_cast(pref + 1)); Real* mem = static_cast(static_cast(fx + 1)); @@ -337,29 +372,27 @@ namespace sol { template struct getter> { - static T& get(lua_State* L, int index = -1) { - return getter{}.get(L, index); + static T& get(lua_State* L, int index, record& tracking) { + return getter{}.get(L, index, tracking); } }; template struct getter> { template - static decltype(auto) apply(std::index_sequence, lua_State* L, int index = -1) { - index = index < 0 ? lua_absindex(L, index) - (sizeof...(I)-1) : index; - return std::tuple(L, index + I))...>(stack::get(L, index + I)...); + static decltype(auto) apply(std::index_sequence, lua_State* L, int index, record& tracking) { + return std::tuple(L, index))...>{stack::get(L, index + tracking.used, tracking)...}; } - static decltype(auto) get(lua_State* L, int index = -1) { - return apply(std::make_index_sequence(), L, index); + static decltype(auto) get(lua_State* L, int index, record& tracking) { + return apply(std::make_index_sequence(), L, index, tracking); } }; template struct getter> { - static decltype(auto) get(lua_State* L, int index = -1) { - index = index < 0 ? lua_absindex(L, index) - 1 : index; - return std::pair(L, index)), decltype(stack::get(L, index))>(stack::get(L, index), stack::get(L, index + 1)); + static decltype(auto) get(lua_State* L, int index, record& tracking) { + return std::pair(L, index)), decltype(stack::get(L, index))>{stack::get(L, index, tracking), stack::get(L, index + tracking.used, tracking)}; } }; diff --git a/sol/stack_pop.hpp b/sol/stack_pop.hpp index 235a025b..f6970131 100644 --- a/sol/stack_pop.hpp +++ b/sol/stack_pop.hpp @@ -32,26 +32,9 @@ namespace sol { template struct popper { inline static decltype(auto) pop(lua_State* L) { - decltype(auto) r = get(L); - lua_pop(L, 1); - return r; - } - }; - - template - struct popper> { - inline static decltype(auto) pop(lua_State* L) { - decltype(auto) r = get>(L); - lua_pop(L, static_cast(sizeof...(Args))); - return r; - } - }; - - template - struct popper> { - inline static decltype(auto) pop(lua_State* L) { - decltype(auto) r = get>(L); - lua_pop(L, 2); + record tracking{}; + decltype(auto) r = get(L, -lua_size::value, tracking); + lua_pop(L, tracking.used); return r; } }; diff --git a/sol/stack_proxy.hpp b/sol/stack_proxy.hpp index de399653..c1959233 100644 --- a/sol/stack_proxy.hpp +++ b/sol/stack_proxy.hpp @@ -77,6 +77,23 @@ namespace sol { }; } // stack + namespace detail { + template <> + struct is_speshul : std::true_type {}; + template <> + struct is_speshul : std::true_type {}; + + template + stack_proxy get(types, index_value<0>, index_value, const T& fr) { + return stack_proxy(fr.lua_state(), static_cast(fr.stack_index() + I)); + } + + template 0)>> = meta::enabler> + stack_proxy get(types, index_value, index_value, const T& fr) { + return get(types(), index_value(), index_value::value>(), fr); + } + } + template <> struct tie_size : std::integral_constant {}; @@ -85,6 +102,11 @@ namespace sol { return stack_proxy(fr.lua_state(), static_cast(fr.stack_index() + I)); } + template + stack_proxy get(types t, const function_result& fr) { + return detail::get(t, index_value(), index_value<0>(), fr); + } + template <> struct tie_size : std::integral_constant {}; @@ -92,6 +114,11 @@ namespace sol { stack_proxy get(const protected_function_result& fr) { return stack_proxy(fr.lua_state(), static_cast(fr.stack_index() + I)); } + + template + stack_proxy get(types t, const protected_function_result& fr) { + return detail::get(t, index_value(), index_value<0>(), fr); + } } // sol #endif // SOL_STACK_PROXY_HPP diff --git a/sol/tie.hpp b/sol/tie.hpp index 62d9503b..9f131324 100644 --- a/sol/tie.hpp +++ b/sol/tie.hpp @@ -26,6 +26,11 @@ namespace sol { + namespace detail { + template + struct is_speshul : std::false_type {}; + } + template struct tie_size : std::tuple_size {}; @@ -48,15 +53,23 @@ namespace sol { typedef tie_size> tie_size; typedef std::conditional_t<(value_size::value < tie_size::value), value_size, tie_size> indices_size; typedef std::make_index_sequence indices; - set(indices(), std::forward(target)); + set_extra(detail::is_speshul>(), indices(), std::forward(target)); } template - void set(std::index_sequence, T&& target) { + void set_extra(std::true_type, std::index_sequence, T&& target) { using std::get; (void)detail::swallow{ 0, - (get(*this) = get(target), 0)... - , 0 }; + (get(*this) = get(types(), target), 0)... + , 0 }; + } + + template + void set_extra(std::false_type, std::index_sequence, T&& target) { + using std::get; + (void)detail::swallow{ 0, + (get(*this) = get(target), 0)... + , 0 }; } public: @@ -64,8 +77,8 @@ namespace sol { template tie_t& operator= (T&& value) { - typedef is_tieable> bondable; - set(bondable(), std::forward(value)); + typedef is_tieable> tieable; + set(tieable(), std::forward(value)); return *this; } diff --git a/sol/types.hpp b/sol/types.hpp index c37a8850..0cafa82e 100644 --- a/sol/types.hpp +++ b/sol/types.hpp @@ -515,6 +515,9 @@ namespace sol { template <> struct lua_type_of : std::integral_constant { }; + template <> + struct lua_type_of : std::integral_constant { }; + template <> struct lua_type_of : std::integral_constant { }; @@ -581,6 +584,12 @@ namespace sol { template <> struct lua_type_of : std::integral_constant {}; + template <> + struct lua_type_of : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + template struct lua_type_of : std::integral_constant {}; @@ -593,12 +602,6 @@ namespace sol { template <> struct lua_type_of : std::integral_constant {}; - template <> - struct lua_type_of : std::integral_constant {}; - - template <> - struct lua_type_of : std::integral_constant {}; - template struct lua_type_of> >::value >> : std::integral_constant {}; + + template class V, typename... Args> + struct accumulate : std::integral_constant {}; + + template class V, typename T, typename... Args> + struct accumulate : accumulate::value, V, Args...> {}; } // detail template struct lua_type_of : detail::lua_type_of {}; + template + struct lua_size : std::integral_constant { }; + + template + struct lua_size> : std::integral_constant::value + lua_size::value> { }; + + template + struct lua_size> : std::integral_constant::value> { }; + template struct is_lua_primitive : std::integral_constant>::value + || (lua_size::value > 1) || std::is_base_of>::value || std::is_base_of>::value || meta::is_specialization_of>::value diff --git a/sol/variadic_args.hpp b/sol/variadic_args.hpp index a8dab5bc..36fa3cda 100644 --- a/sol/variadic_args.hpp +++ b/sol/variadic_args.hpp @@ -218,7 +218,8 @@ namespace sol { namespace stack { template <> struct getter { - static variadic_args get(lua_State* L, int index = -1) { + static variadic_args get(lua_State* L, int index, record& tracking) { + tracking.last = 0; return variadic_args(L, index); } }; diff --git a/test_customizations.cpp b/test_customizations.cpp new file mode 100644 index 00000000..74d7a25d --- /dev/null +++ b/test_customizations.cpp @@ -0,0 +1,83 @@ +#define SOL_CHECK_ARGUMENTS + +#include +#include + +struct two_things { + int a; + bool b; +}; + +namespace sol { + + // First, the expected size + // Specialization of a struct + template <> + struct lua_size : std::integral_constant {}; + + // Now, specialize various stack structures + namespace stack { + + template <> + struct checker { + template + static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { + // Check first and second second index for being the proper types + bool success = stack::check(L, index, handler) + && stack::check(L, index + 1, handler); + tracking.use(2); + return success; + } + }; + + template <> + struct getter { + static two_things get(lua_State* L, int index, record& tracking) { + // Get the first element + int a = stack::get(L, index); + // Get the second element, + // in the +1 position from the first + bool b = stack::get(L, index + 1); + // we use 2 slots, each of the previous takes 1 + tracking.use(2); + return two_things{ a, b }; + } + }; + + template <> + struct pusher { + static int push(lua_State* L, const two_things& things) { + int amount = stack::push(L, things.a); + amount += stack::push(L, things.b); + // Return 2 things + return amount; + } + }; + + } +} + +TEST_CASE("customization/split-struct", "using the newly documented customization points to handle different kinds of classes") { + sol::state lua; + + // Create a pass-through style of function + lua.script("function f ( a, b, c ) return a + c, b end"); + lua.set_function("g", [](int a, bool b, int c, double d) { + return std::make_tuple(a + c, b, d + 2.5); + }); + + // get the function out of Lua + sol::function f = lua["f"]; + sol::function g = lua["g"]; + + two_things thingsf = f(two_things{ 24, true }, 1); + two_things thingsg; + double d; + sol::tie( thingsg, d ) = g(two_things{ 25, false }, 2, 34.0); + REQUIRE(thingsf.a == 25); + REQUIRE(thingsf.b); + + REQUIRE(thingsg.a == 27); + REQUIRE_FALSE(thingsg.b); + REQUIRE(d == 36.5); +} diff --git a/test_usertypes.cpp b/test_usertypes.cpp index 0cf4abf7..af04dd15 100644 --- a/test_usertypes.cpp +++ b/test_usertypes.cpp @@ -500,7 +500,8 @@ TEST_CASE("usertype/nonmember-functions", "let users set non-member functions th "print(tostring(t))\n" "t:gief()\n" "t:gief_stuff(20)\n")); - REQUIRE((lua.get("t").a == 20)); + giver& g = lua.get("t"); + REQUIRE(g.a == 20); } TEST_CASE("usertype/unique-shared-ptr", "manage the conversion and use of unique and shared pointers ('unique usertypes')") {