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) { inline std::function<Signature> get(types<std::function<Signature>>, lua_State* L, int index = -1) {
typedef typename function_traits<Signature> fx_t; typedef typename function_traits<Signature> fx_t;
typedef typename fx_t::args_type args_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); return get_std_func<Signature>(args_t(), ret_t(), L, index);
} }
} // detail } // detail

View File

@ -297,7 +297,7 @@ struct userdata_function : public base_function {
template<typename... Ret, typename... Args> template<typename... Ret, typename... Args>
int operator()(types<Ret...>, types<Args...> t, lua_State* L) { int operator()(types<Ret...>, types<Args...> t, lua_State* L) {
typedef typename return_type<Ret...>::type return_type; typedef typename return_type<Ret...>::type return_type;
return_type r = stack::get_call(L, fx, t); return_type r = stack::get_call(L, 2, fx, t);
std::ptrdiff_t nargs = sizeof...(Args); std::ptrdiff_t nargs = sizeof...(Args);
lua_pop(L, nargs); lua_pop(L, nargs);
// stack::push(L, std::move(r)); // stack::push(L, std::move(r));

View File

@ -28,12 +28,6 @@
namespace sol { namespace sol {
namespace detail { 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) { inline int atpanic(lua_State* L) {
std::string err = lua_tostring(L, -1); std::string err = lua_tostring(L, -1);
throw error(err); throw error(err);
@ -70,7 +64,7 @@ public:
template<typename... Args> template<typename... Args>
void open_libraries(Args&&... 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) { if(sizeof...(args) == 0) {
luaL_openlibs(L.get()); luaL_openlibs(L.get());
return; return;

View File

@ -26,6 +26,12 @@
#include <type_traits> #include <type_traits>
namespace sol { 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> template<typename T, typename R = void>
using EnableIf = typename std::enable_if<T::value, R>::type; using EnableIf = typename std::enable_if<T::value, R>::type;
@ -53,6 +59,12 @@ struct return_type<> : types<>{
typedef void type; 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> template <typename T, template <typename...> class Templ>
struct is_specialization_of : std::false_type { }; struct is_specialization_of : std::false_type { };
template <typename... T, template <typename...> class Templ> template <typename... T, template <typename...> class Templ>

View File

@ -57,7 +57,7 @@ template<size_t... Ns>
struct build_reverse_indices<0, Ns...> : indices<Ns...> {}; struct build_reverse_indices<0, Ns...> : indices<Ns...> {};
template<typename... Args> 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> template<class Acc, class... Args>
struct reversed_ : Acc{}; struct reversed_ : Acc{};
@ -69,10 +69,10 @@ template<typename... Args>
struct reversed : reversed_<types<>, Args...>{}; struct reversed : reversed_<types<>, Args...>{};
template<typename... Args> template<typename... Args>
struct tuple_types : types<Args...>, std::false_type {}; struct tuple_types : types<Args...> {};
template<typename... 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> template<typename... Tn>
struct constructors {}; struct constructors {};

View File

@ -2,6 +2,15 @@
#include <catch.hpp> #include <catch.hpp>
#include <sol.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::string free_function() {
std::cout << "free_function()" << std::endl; std::cout << "free_function()" << std::endl;
return "test"; return "test";
@ -324,6 +333,25 @@ TEST_CASE("functions/return_order_and_multi_get", "Check if return order is in t
REQUIRE(tluaget == triple); 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") { TEST_CASE("tables/operator[]", "Check if operator[] retrieval and setting works properly") {
sol::state lua; sol::state lua;
lua.open_libraries(sol::lib::base); lua.open_libraries(sol::lib::base);