From a842060e4d5e86693501981c4db4d725aae77c53 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Fri, 30 May 2014 19:10:08 -0400 Subject: [PATCH] Removed std::true_type/false_type from tuple_types and created a separate is_tuple trait, so that we can use ::type on tuple_types without it interfering with base typedefs in std::true/false_type Fixing some identation Moved are_same type traits to traits.hpp --- sol/function.hpp | 2 +- sol/function_types.hpp | 38 +++++++++++++++++++------------------- sol/state.hpp | 8 +------- sol/traits.hpp | 14 +++++++++++++- sol/tuple.hpp | 6 +++--- sol/types.hpp | 2 +- tests.cpp | 28 ++++++++++++++++++++++++++++ 7 files changed, 66 insertions(+), 32 deletions(-) diff --git a/sol/function.hpp b/sol/function.hpp index 65ed135f..94c7a959 100644 --- a/sol/function.hpp +++ b/sol/function.hpp @@ -111,7 +111,7 @@ template inline std::function get(types>, lua_State* L, int index = -1) { typedef typename function_traits fx_t; typedef typename fx_t::args_type args_t; - typedef typename tuple_types::types_type ret_t; + typedef typename tuple_types::type ret_t; return get_std_func(args_t(), ret_t(), L, index); } } // detail diff --git a/sol/function_types.hpp b/sol/function_types.hpp index 01b1325b..ffd62f90 100644 --- a/sol/function_types.hpp +++ b/sol/function_types.hpp @@ -35,9 +35,9 @@ struct static_function { template static int typed_call(types, types t, function_type* fx, lua_State* L) { stack::get_call(L, fx, t); - std::ptrdiff_t nargs = sizeof...(Args); + std::ptrdiff_t nargs = sizeof...(Args); lua_pop(L, nargs); - return 0; + return 0; } template @@ -49,9 +49,9 @@ struct static_function { static int typed_call(types, types t, function_type* fx, lua_State* L) { typedef typename return_type::type return_type; return_type r = stack::get_call(L, fx, t); - std::ptrdiff_t nargs = sizeof...(Args); + std::ptrdiff_t nargs = sizeof...(Args); lua_pop(L, nargs); - stack::push(L, std::move(r)); + stack::push(L, std::move(r)); return sizeof...(Ret); } @@ -76,9 +76,9 @@ struct static_member_function { static int typed_call(types, types, T& item, function_type& ifx, lua_State* L) { auto fx = [&item, &ifx](Args&&... args) -> void { (item.*ifx)(std::forward(args)...); }; stack::get_call(L, fx, types()); - std::ptrdiff_t nargs = sizeof...(Args); + std::ptrdiff_t nargs = sizeof...(Args); lua_pop(L, nargs); - return 0; + return 0; } template @@ -91,9 +91,9 @@ struct static_member_function { typedef typename return_type::type return_type; auto fx = [&item, &ifx](Args&&... args) -> return_type { return (item.*ifx)(std::forward(args)...); }; return_type r = stack::get_call(L, fx, types()); - std::ptrdiff_t nargs = sizeof...(Args); + std::ptrdiff_t nargs = sizeof...(Args); lua_pop(L, nargs); - stack::push(L, std::move(r)); + stack::push(L, std::move(r)); return sizeof...(Ret); } @@ -171,9 +171,9 @@ struct functor_function : public base_function { template int operator()(types, types t, lua_State* L) { stack::get_call(L, fx, t); - std::ptrdiff_t nargs = sizeof...(Args); + std::ptrdiff_t nargs = sizeof...(Args); lua_pop(L, nargs); - return 0; + return 0; } template @@ -185,9 +185,9 @@ struct functor_function : public base_function { int operator()(types, types t, lua_State* L) { typedef typename return_type::type return_type; return_type r = stack::get_call(L, fx, t); - std::ptrdiff_t nargs = sizeof...(Args); + std::ptrdiff_t nargs = sizeof...(Args); lua_pop(L, nargs); - stack::push(L, r); + stack::push(L, r); return sizeof...(Ret); } @@ -231,9 +231,9 @@ struct member_function : public base_function { int operator()(types, types t, lua_State* L) { typedef typename return_type::type return_type; return_type r = stack::get_call(L, fx, t); - std::ptrdiff_t nargs = sizeof...(Args); + std::ptrdiff_t nargs = sizeof...(Args); lua_pop(L, nargs); - stack::push(L, std::move(r)); + stack::push(L, std::move(r)); return sizeof...(Ret); } @@ -273,9 +273,9 @@ struct userdata_function : public base_function { template int operator()(types, types t, lua_State* L) { stack::get_call(L, fx, t); - std::ptrdiff_t nargs = sizeof...(Args); + std::ptrdiff_t nargs = sizeof...(Args); lua_pop(L, nargs); - return 0; + return 0; } template @@ -297,10 +297,10 @@ struct userdata_function : public base_function { template int operator()(types, types t, lua_State* L) { typedef typename return_type::type return_type; - return_type r = stack::get_call(L, fx, t); - std::ptrdiff_t nargs = sizeof...(Args); + return_type r = stack::get_call(L, 2, fx, t); + std::ptrdiff_t nargs = sizeof...(Args); lua_pop(L, nargs); - // stack::push(L, std::move(r)); + // stack::push(L, std::move(r)); special_push(L, r); return sizeof...(Ret); } diff --git a/sol/state.hpp b/sol/state.hpp index b5470eb1..5cf5630f 100644 --- a/sol/state.hpp +++ b/sol/state.hpp @@ -28,12 +28,6 @@ namespace sol { namespace detail { -template -struct are_same : std::true_type {}; - -template -struct are_same : std::integral_constant::value && are_same::value> {}; - inline int atpanic(lua_State* L) { std::string err = lua_tostring(L, -1); throw error(err); @@ -70,7 +64,7 @@ public: template void open_libraries(Args&&... args) { - static_assert(detail::are_same::value, "all types must be libraries"); + static_assert(are_same::value, "all types must be libraries"); if(sizeof...(args) == 0) { luaL_openlibs(L.get()); return; diff --git a/sol/traits.hpp b/sol/traits.hpp index 80f100b7..d87d2805 100644 --- a/sol/traits.hpp +++ b/sol/traits.hpp @@ -26,6 +26,12 @@ #include namespace sol { +template +struct are_same : std::true_type { }; + +template +struct are_same : std::integral_constant ::value && are_same::value> { }; + template using EnableIf = typename std::enable_if::type; @@ -53,6 +59,12 @@ struct return_type<> : types<>{ typedef void type; }; +template +struct is_tuple : std::false_type{ }; + +template +struct is_tuple> : std::true_type{ }; + template class Templ> struct is_specialization_of : std::false_type { }; template class Templ> @@ -147,4 +159,4 @@ struct function_traits { }; } // sol -#endif // SOL_TRAITS_HPP +#endif // SOL_TRAITS_HPP diff --git a/sol/tuple.hpp b/sol/tuple.hpp index d2b766bc..0b579397 100644 --- a/sol/tuple.hpp +++ b/sol/tuple.hpp @@ -57,7 +57,7 @@ template struct build_reverse_indices<0, Ns...> : indices {}; template -struct types : build_indices { typedef types type; typedef types types_type; }; +struct types : build_indices { typedef types type; }; template struct reversed_ : Acc{}; @@ -69,10 +69,10 @@ template struct reversed : reversed_, Args...>{}; template -struct tuple_types : types, std::false_type {}; +struct tuple_types : types {}; template -struct tuple_types> : types, std::true_type {}; +struct tuple_types> : types {}; template struct constructors {}; diff --git a/sol/types.hpp b/sol/types.hpp index 987f8ce7..348a17fd 100644 --- a/sol/types.hpp +++ b/sol/types.hpp @@ -160,4 +160,4 @@ inline bool operator==(nil_t, nil_t) { return true; } inline bool operator!=(nil_t, nil_t) { return false; } } // sol -#endif // SOL_TYPES_HPP +#endif // SOL_TYPES_HPP diff --git a/tests.cpp b/tests.cpp index c6ac269a..2e294c90 100644 --- a/tests.cpp +++ b/tests.cpp @@ -2,6 +2,15 @@ #include #include +void test_free_func( std::function f ) { + f( ); +} + +void test_free_func2( std::function f, int arg1 ) { + int val = f( arg1 ); + assert( arg1 == val ); +} + std::string free_function() { std::cout << "free_function()" << std::endl; return "test"; @@ -324,6 +333,25 @@ TEST_CASE("functions/return_order_and_multi_get", "Check if return order is in t REQUIRE(tluaget == triple); } +TEST_CASE( "functions/sol::function to std::function", "check if conversion to std::function works properly and calls with correct arguments" ) { + sol::state lua; + lua.open_libraries( sol::lib::base ); + + lua.set_function( "testFunc", test_free_func ); + lua.set_function( "testFunc2", test_free_func2 ); + lua.script( + "testFunc(function() print(\"hello std::function\") end)" + ); + lua.script( + "function m(a)\n" + " print(\"hello std::function with arg \", a)\n" + " return a\n" + "end\n" + "\n" + "testFunc2(m, 1)" + ); +} + TEST_CASE("tables/operator[]", "Check if operator[] retrieval and setting works properly") { sol::state lua; lua.open_libraries(sol::lib::base);