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
This commit is contained in:
ThePhD 2014-05-30 19:10:08 -04:00
parent 854d735410
commit a842060e4d
7 changed files with 66 additions and 32 deletions

View File

@ -111,7 +111,7 @@ template <typename Signature>
inline std::function<Signature> get(types<std::function<Signature>>, lua_State* L, int index = -1) {
typedef typename function_traits<Signature> fx_t;
typedef typename fx_t::args_type args_t;
typedef typename tuple_types<typename fx_t::return_type>::types_type ret_t;
typedef typename tuple_types<typename fx_t::return_type>::type ret_t;
return get_std_func<Signature>(args_t(), ret_t(), L, index);
}
} // detail

View File

@ -35,9 +35,9 @@ struct static_function {
template<typename... Args>
static int typed_call(types<void>, types<Args...> 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<typename... Args>
@ -49,9 +49,9 @@ struct static_function {
static int typed_call(types<Ret...>, types<Args...> t, function_type* fx, lua_State* L) {
typedef typename return_type<Ret...>::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<void>, types<Args...>, T& item, function_type& ifx, lua_State* L) {
auto fx = [&item, &ifx](Args&&... args) -> void { (item.*ifx)(std::forward<Args>(args)...); };
stack::get_call(L, fx, types<Args...>());
std::ptrdiff_t nargs = sizeof...(Args);
std::ptrdiff_t nargs = sizeof...(Args);
lua_pop(L, nargs);
return 0;
return 0;
}
template<typename... Args>
@ -91,9 +91,9 @@ struct static_member_function {
typedef typename return_type<Ret...>::type return_type;
auto fx = [&item, &ifx](Args&&... args) -> return_type { return (item.*ifx)(std::forward<Args>(args)...); };
return_type r = stack::get_call(L, fx, types<Args...>());
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<typename... Args>
int operator()(types<void>, types<Args...> 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<typename... Args>
@ -185,9 +185,9 @@ struct functor_function : public base_function {
int operator()(types<Ret...>, types<Args...> t, lua_State* L) {
typedef typename return_type<Ret...>::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<Ret...>, types<Args...> t, lua_State* L) {
typedef typename return_type<Ret...>::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<typename... Args>
int operator()(types<void>, types<Args...> 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<typename... Args>
@ -297,10 +297,10 @@ struct userdata_function : public base_function {
template<typename... Ret, typename... Args>
int operator()(types<Ret...>, types<Args...> t, lua_State* L) {
typedef typename return_type<Ret...>::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);
}

View File

@ -28,12 +28,6 @@
namespace sol {
namespace detail {
template<class T, class...>
struct are_same : std::true_type {};
template<class T, class U, class... Args>
struct are_same<T, U, Args...> : std::integral_constant<bool, std::is_same<T, U>::value && are_same<T, Args...>::value> {};
inline int atpanic(lua_State* L) {
std::string err = lua_tostring(L, -1);
throw error(err);
@ -70,7 +64,7 @@ public:
template<typename... Args>
void open_libraries(Args&&... args) {
static_assert(detail::are_same<lib, Args...>::value, "all types must be libraries");
static_assert(are_same<lib, Args...>::value, "all types must be libraries");
if(sizeof...(args) == 0) {
luaL_openlibs(L.get());
return;

View File

@ -26,6 +26,12 @@
#include <type_traits>
namespace sol {
template<class T, class...>
struct are_same : std::true_type { };
template<class T, class U, class... Args>
struct are_same<T, U, Args...> : std::integral_constant <bool, std::is_same<T, U>::value && are_same<T, Args...>::value> { };
template<typename T, typename R = void>
using EnableIf = typename std::enable_if<T::value, R>::type;
@ -53,6 +59,12 @@ struct return_type<> : types<>{
typedef void type;
};
template<typename... Args>
struct is_tuple : std::false_type{ };
template<typename... Args>
struct is_tuple<std::tuple<Args...>> : std::true_type{ };
template <typename T, template <typename...> class Templ>
struct is_specialization_of : std::false_type { };
template <typename... T, template <typename...> class Templ>
@ -147,4 +159,4 @@ struct function_traits<R(*)(Args...)> {
};
} // sol
#endif // SOL_TRAITS_HPP
#endif // SOL_TRAITS_HPP

View File

@ -57,7 +57,7 @@ template<size_t... Ns>
struct build_reverse_indices<0, Ns...> : indices<Ns...> {};
template<typename... Args>
struct types : build_indices<sizeof...(Args)> { typedef types type; typedef types types_type; };
struct types : build_indices<sizeof...(Args)> { typedef types type; };
template<class Acc, class... Args>
struct reversed_ : Acc{};
@ -69,10 +69,10 @@ template<typename... Args>
struct reversed : reversed_<types<>, Args...>{};
template<typename... Args>
struct tuple_types : types<Args...>, std::false_type {};
struct tuple_types : types<Args...> {};
template<typename... Args>
struct tuple_types<std::tuple<Args...>> : types<Args...>, std::true_type {};
struct tuple_types<std::tuple<Args...>> : types<Args...> {};
template<typename... Tn>
struct constructors {};

View File

@ -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

View File

@ -2,6 +2,15 @@
#include <catch.hpp>
#include <sol.hpp>
void test_free_func( std::function<void( )> f ) {
f( );
}
void test_free_func2( std::function<int( int )> 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);