From 560b5b2673b5ce42ea2a6499f1807e44170040e3 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Tue, 6 Mar 2018 17:36:33 -0500 Subject: [PATCH] =?UTF-8?q?=E1=95=95=E0=BC=BC=20=E2=80=A2=CC=80=EF=B8=BF?= =?UTF-8?q?=E2=80=A2=CC=81=E0=BC=BD=E1=95=97=20VC++=20COMING=20THROUGH=20W?= =?UTF-8?q?AHAAAY=20=E1=95=95=E0=BC=BC=20=E2=80=A2=CC=80=EF=B8=BF=E2=80=A2?= =?UTF-8?q?=CC=81=E0=BC=BD=E1=95=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../custom_global_transparent_argument.cpp | 70 +++++++++++++++++++ single/sol/sol.hpp | 10 ++- single/sol/sol_forward.hpp | 4 +- sol/load_result.hpp | 6 ++ 4 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 examples/custom_global_transparent_argument.cpp diff --git a/examples/custom_global_transparent_argument.cpp b/examples/custom_global_transparent_argument.cpp new file mode 100644 index 00000000..a507cc4e --- /dev/null +++ b/examples/custom_global_transparent_argument.cpp @@ -0,0 +1,70 @@ +#define SOL_CHECK_ARGUMENTS 1 +#include + +// Something that can't be collided with +constexpr static const auto& script_key = "GlobalResource.MySpecialIdentifier123"; + +struct GlobalResource { + int value = 2; +}; + +// Customize sol2 to handle this type +namespace sol { + template <> + struct lua_type_of : std::integral_constant {}; + + namespace stack { + template <> + struct checker { + template + static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { + // get the field from global storage + stack::get_field(L, script_key); + // verify type + type t = static_cast(lua_type(L, -1)); + lua_pop(L, 1); + return t == type::lightuserdata; + } + }; + + template <> + struct getter { + static GlobalResource* get(lua_State* L, int index, record& tracking) { + // retrieve the (light) userdata for this type + tracking.use(0); // not actually pulling anything off the stack + (void)index; // unused + stack::get_field(L, script_key); + GlobalResource* ls = static_cast(lua_touserdata(L, -1)); + lua_pop(L, 1); // clean up stack value returned by `get_field` + return ls; + }; + }; + template <> + struct pusher { + static int push(lua_State* L, GlobalResource* ls) { + // push light userdata + return stack::push(L, make_light(ls));; + } + }; + } +} + +int main() { + + sol::state lua; + lua.open_libraries(sol::lib::base); + + GlobalResource instance; + + // get GlobalResource + lua.set_function("f", [](GlobalResource* l, int value) { + return l->value + value; + }); + lua.set(script_key, &instance); + + // note only 1 argument, + // despite being 2 + lua.script("assert(f(1) == 3)"); + + return 0; +} diff --git a/single/sol/sol.hpp b/single/sol/sol.hpp index 1e0f14bf..72eef051 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 2018-03-06 03:55:05.726661 UTC -// This header was generated with sol v2.19.5 (revision 68738cd) +// Generated 2018-03-06 22:35:07.056602 UTC +// This header was generated with sol v2.19.5 (revision 0fe9b16) // https://github.com/ThePhD/sol2 #ifndef SOL_SINGLE_INCLUDE_HPP @@ -19868,7 +19868,13 @@ namespace sol { template decltype(auto) call(Args&&... args) { +#if defined(_MSC_VER) && _MSC_VER == 1913 + // This compiler is bananas + // B, A N A N A S + return get().call(std::forward(args)...); +#else return get().template call(std::forward(args)...); +#endif } template diff --git a/single/sol/sol_forward.hpp b/single/sol/sol_forward.hpp index fe304bc9..de87b4eb 100644 --- a/single/sol/sol_forward.hpp +++ b/single/sol/sol_forward.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 2018-03-06 03:55:05.939310 UTC -// This header was generated with sol v2.19.5 (revision 68738cd) +// Generated 2018-03-06 22:35:07.265771 UTC +// This header was generated with sol v2.19.5 (revision 0fe9b16) // https://github.com/ThePhD/sol2 #ifndef SOL_SINGLE_INCLUDE_FORWARD_HPP diff --git a/sol/load_result.hpp b/sol/load_result.hpp index 1d396f31..0d9dc803 100644 --- a/sol/load_result.hpp +++ b/sol/load_result.hpp @@ -122,7 +122,13 @@ namespace sol { template decltype(auto) call(Args&&... args) { +#if defined(_MSC_VER) && _MSC_VER == 1913 + // This compiler is bananas + // B, A N A N A S + return get().call(std::forward(args)...); +#else return get().template call(std::forward(args)...); +#endif } template