diff --git a/sol/lua_function.hpp b/sol/function_types.hpp similarity index 80% rename from sol/lua_function.hpp rename to sol/function_types.hpp index 06e3979a..4f855d6b 100644 --- a/sol/lua_function.hpp +++ b/sol/function_types.hpp @@ -28,7 +28,7 @@ namespace sol { template -struct static_lua_func { +struct static_function { typedef typename std::remove_pointer::type>::type function_type; typedef function_traits traits_type; @@ -64,7 +64,7 @@ struct static_lua_func { }; template -struct static_object_lua_func { +struct static_member_function { typedef typename std::remove_pointer::type>::type function_type; typedef function_traits traits_type; @@ -103,15 +103,15 @@ struct static_object_lua_func { } }; -struct lua_func { +struct base_function { static int call(lua_State* L) { void** pinheritancedata = static_cast(stack::get(L, 1).value); void* inheritancedata = *pinheritancedata; if (inheritancedata == nullptr) { throw sol_error("call from Lua to C++ function has null data"); } - lua_func* pfx = static_cast(inheritancedata); - lua_func& fx = *pfx; + base_function* pfx = static_cast(inheritancedata); + base_function& fx = *pfx; int r = fx(L); return r; } @@ -119,8 +119,8 @@ struct lua_func { static int gc(lua_State* L) { void** puserdata = static_cast(stack::get(L, 1).value); void* userdata = *puserdata; - lua_func* ptr = static_cast(userdata); - std::default_delete dx{}; + base_function* ptr = static_cast(userdata); + std::default_delete dx{}; dx(ptr); return 0; } @@ -129,17 +129,17 @@ struct lua_func { throw sol_error("failure to call specialized wrapped C++ function from Lua"); } - virtual ~lua_func() {} + virtual ~base_function() {} }; template -struct functor_lua_func : public lua_func { +struct functor_function : public base_function { typedef decltype(&Function::operator()) function_type; typedef function_traits traits_type; Function fx; template - functor_lua_func(FxArgs&&... fxargs): fx(std::forward(fxargs)...) {} + functor_function(FxArgs&&... fxargs): fx(std::forward(fxargs)...) {} template int operator()(types, types t, lua_State* L) { @@ -165,41 +165,8 @@ struct functor_lua_func : public lua_func { } }; -template::value> -struct function_lua_func : public lua_func { - typedef typename std::remove_pointer::type>::type function_type; - typedef function_traits traits_type; - function_type fx; - - template - function_lua_func(FxArgs&&... fxargs): fx(std::forward(fxargs)...) {} - - template - int operator()(types, types t, lua_State* L) { - stack::pop_call(L, fx, t); - return 0; - } - - template - int operator()(types<>, types t, lua_State* L) { - return (*this)(types(), t, L); - } - - template - int operator()(types, types t, lua_State* L) { - typedef typename multi_return::type return_type; - return_type r = stack::pop_call(L, fx, t); - stack::push(L, std::move(r)); - return sizeof...(Ret); - } - - virtual int operator()(lua_State* L) override { - return (*this)(tuple_types(), typename traits_type::args_type(), L); - } -}; - template -struct function_lua_func : public lua_func { +struct member_function : public base_function { typedef typename std::remove_pointer::type>::type function_type; typedef function_traits traits_type; struct functor { @@ -216,7 +183,7 @@ struct function_lua_func : public lua_func { } fx; template - function_lua_func(T m, FxArgs&&... fxargs): fx(std::move(m), std::forward(fxargs)...) {} + member_function(T m, FxArgs&&... fxargs): fx(std::move(m), std::forward(fxargs)...) {} template int operator()(types, types t, lua_State* L) { @@ -243,7 +210,7 @@ struct function_lua_func : public lua_func { }; template -struct class_lua_func : public lua_func { +struct userdata_function : public base_function { typedef typename std::remove_pointer::type>::type function_type; typedef function_traits traits_type; struct functor { @@ -266,7 +233,7 @@ struct class_lua_func : public lua_func { } fx; template - class_lua_func(FxArgs&&... fxargs): fx(std::forward(fxargs)...) {} + userdata_function(FxArgs&&... fxargs): fx(std::forward(fxargs)...) {} template int operator()(types, types t, lua_State* L) { diff --git a/sol/reference.hpp b/sol/reference.hpp index b6808960..9acc75ed 100644 --- a/sol/reference.hpp +++ b/sol/reference.hpp @@ -50,10 +50,6 @@ public: lua_rawgeti(L, LUA_REGISTRYINDEX, ref); } - void pop() const noexcept { - lua_pop(L, 1); - } - reference(reference&& o) noexcept { L = o.L; ref = o.ref; diff --git a/sol/table.hpp b/sol/table.hpp index 170e6817..73611050 100644 --- a/sol/table.hpp +++ b/sol/table.hpp @@ -24,7 +24,7 @@ #include "proxy.hpp" #include "stack.hpp" -#include "lua_function.hpp" +#include "function_types.hpp" #include "userdata.hpp" namespace sol { @@ -168,6 +168,9 @@ public: return proxy(*this, std::forward(key)); } + void pop(int n = 1) const noexcept { + lua_pop(state(), n); + } private: template table& set_isfunction_fx(std::true_type, T&& key, TFx&& fx) { @@ -192,7 +195,7 @@ private: template table& set_isconvertible_fx(std::false_type, T&& key, TFx&& fx) { typedef typename std::remove_pointer>::type clean_fx; - std::unique_ptr sptr(new functor_lua_func(std::forward(fx))); + std::unique_ptr sptr(new functor_function(std::forward(fx))); return set_fx(std::forward(key), std::move(sptr)); } @@ -204,7 +207,7 @@ private: template table& set_lvalue_fx(std::false_type, T&& key, TFx&& fx, TObj&& obj) { typedef typename std::remove_pointer>::type clean_fx; - std::unique_ptr sptr(new function_lua_func(std::forward(obj), std::forward(fx))); + std::unique_ptr sptr(new member_function(std::forward(obj), std::forward(fx))); return set_fx(std::forward(key), std::move(sptr)); } @@ -219,7 +222,7 @@ private: // with the same member function pointer type Decay fxptr(std::forward(fx)); void* userobjdata = static_cast(detail::get_ptr(obj)); - lua_CFunction freefunc = &static_object_lua_func, TFx>::call; + lua_CFunction freefunc = &static_member_function, TFx>::call; const char* freefuncname = fkey.c_str(); const luaL_Reg funcreg[2] = { { freefuncname, freefunc }, @@ -241,7 +244,7 @@ private: table& set_fx(std::false_type, T&& key, TFx&& fx) { std::string fkey(key); Decay target(std::forward(fx)); - lua_CFunction freefunc = &static_lua_func::call; + lua_CFunction freefunc = &static_function::call; const char* freefuncname = fkey.c_str(); const luaL_Reg funcreg[2] = { { freefuncname, freefunc }, @@ -257,14 +260,14 @@ private: } template - table& set_fx(T&& key, std::unique_ptr luafunc) { + table& set_fx(T&& key, std::unique_ptr luafunc) { std::string fkey(key); std::string metakey("sol.stateful."); metakey += fkey; metakey += ".meta"; - lua_func* target = luafunc.release(); + base_function* target = luafunc.release(); void* userdata = reinterpret_cast(target); - lua_CFunction freefunc = &lua_func::call; + lua_CFunction freefunc = &base_function::call; const char* freefuncname = fkey.c_str(); const char* metatablename = metakey.c_str(); const luaL_Reg funcreg[2] = { @@ -274,7 +277,7 @@ private: if (luaL_newmetatable(state(), metatablename) == 1) { lua_pushstring(state(), "__gc"); - lua_pushcclosure(state(), &lua_func::gc, 0); + lua_pushcclosure(state(), &base_function::gc, 0); lua_settable(state(), -3); } diff --git a/sol/userdata.hpp b/sol/userdata.hpp index 70433367..e898a51d 100644 --- a/sol/userdata.hpp +++ b/sol/userdata.hpp @@ -23,7 +23,7 @@ #define SOL_USERDATA_HPP #include "state.hpp" -#include "lua_function.hpp" +#include "function_types.hpp" #include "demangle.hpp" #include @@ -44,7 +44,7 @@ private: std::string luaname; std::vector functionnames; - std::vector> functions; + std::vector> functions; std::vector functiontable; std::vector metatable; @@ -128,8 +128,8 @@ private: void* inheritancedata = stack::get(L, i + 1); if (inheritancedata == nullptr) throw sol_error("call from Lua to C++ function has null data"); - lua_func* pfx = static_cast(inheritancedata); - lua_func& fx = *pfx; + base_function* pfx = static_cast(inheritancedata); + base_function& fx = *pfx; int r = fx(L); return r; } @@ -144,7 +144,7 @@ private: void build_function_tables(Ret(T::* func)(MArgs...), std::string name, Args&&... args) { typedef typename std::decay::type fx_t; functionnames.push_back(std::move(name)); - functions.emplace_back(detail::make_unique>(std::move(func))); + functions.emplace_back(detail::make_unique>(std::move(func))); functiontable.push_back({ functionnames.back().c_str(), &class_func::call }); build_function_tables(std::forward(args)...); } diff --git a/tests.cpp b/tests.cpp index c916a79d..298bd236 100644 --- a/tests.cpp +++ b/tests.cpp @@ -17,23 +17,23 @@ struct object { }; struct fuser { - int x; - - fuser( ) : x( 0 ) { - - } - - fuser( int x ) : x( x ) { - - } + int x; + + fuser( ) : x( 0 ) { + + } + + fuser( int x ) : x( x ) { + + } - int add( int y ) { - return x + y; - } - - int add2( int y ) { - return x + y + 2; - } + int add( int y ) { + return x + y; + } + + int add2( int y ) { + return x + y + 2; + } }; int plop_xyz(int x, int y, std::string z) { @@ -345,30 +345,29 @@ TEST_CASE("tables/operator[]", "Check if operator[] retrieval and setting works REQUIRE_NOTHROW(assert1(lua.global_table())); } -TEST_CASE( "tables/userdata", "Show that we can create classes from userdata and use them" ) { +TEST_CASE("tables/userdata", "Show that we can create classes from userdata and use them") { - sol::state lua; + sol::state lua; - sol::userdata lc{ &fuser::add, "add", &fuser::add2, "add2" }; - lua.set_class( lc ); + sol::userdata lc{ &fuser::add, "add", &fuser::add2, "add2" }; + lua.set_class( lc ); - lua.script( "a = fuser:new()\n" - "b = a:add(1)\n" - "c = a:add2(1)\n" - "\n" ); + lua.script("a = fuser:new()\n" + "b = a:add(1)\n" + "c = a:add2(1)\n"); - sol::object a = lua.get( "a" ); - sol::object b = lua.get( "b" ); - sol::object c = lua.get( "c" ); - REQUIRE( ( a.is( ) ) ); - auto atype = a.get_type( ); - auto btype = b.get_type( ); - auto ctype = c.get_type( ); - REQUIRE( ( atype == sol::type::table ) ); - REQUIRE( ( btype == sol::type::number ) ); - REQUIRE( ( ctype == sol::type::number ) ); - int bresult = b.as( ); - int cresult = c.as( ); - REQUIRE( bresult == 1 ); - REQUIRE( cresult == 3 ); + sol::object a = lua.get("a"); + sol::object b = lua.get("b"); + sol::object c = lua.get("c"); + REQUIRE((a.is())); + auto atype = a.get_type(); + auto btype = b.get_type(); + auto ctype = c.get_type(); + REQUIRE((atype == sol::type::table)); + REQUIRE((btype == sol::type::number)); + REQUIRE((ctype == sol::type::number)); + int bresult = b.as(); + int cresult = c.as(); + REQUIRE(bresult == 1); + REQUIRE(cresult == 3); } \ No newline at end of file