Merge pull request #8 from ThePhD/master

Makes everything build with VC++ and also adds a void overload for `sol::function`.
This commit is contained in:
Danny 2013-12-13 20:54:55 -08:00
commit bec0406ce6
5 changed files with 45 additions and 36 deletions

View File

@ -61,6 +61,11 @@ public:
function(const function&) = default; function(const function&) = default;
function& operator=(const function&) = default; function& operator=(const function&) = default;
template<typename... Args>
void operator()(Args&&... args) {
call<>(std::forward<Args>(args)...);
}
template<typename... Ret, typename... Args> template<typename... Ret, typename... Args>
auto call(Args&&... args) -> decltype(invoke(types<Ret...>(), sizeof...(Args))) { auto call(Args&&... args) -> decltype(invoke(types<Ret...>(), sizeof...(Args))) {
push(); push();

View File

@ -208,8 +208,8 @@ inline int push_user(lua_State* L, T& item) {
namespace detail { namespace detail {
template<typename T, std::size_t... I> template<typename T, std::size_t... I>
inline void push(lua_State* L, indices<I...>, const T& tuplen) { inline void push(lua_State* L, indices<I...>, const T& tuplen) {
using swallow = char[]; using swallow = char[ 1 + sizeof...(I) ];
void(swallow{'\0', (sol::stack::push(L, std::get<I>(tuplen)), '\0')... }); swallow {'\0', (sol::stack::push(L, std::get<I>(tuplen)), '\0')... };
} }
template<typename F, typename... Vs> template<typename F, typename... Vs>

View File

@ -41,7 +41,7 @@ T* get_ptr(T* val) {
} // detail } // detail
class table : virtual public reference { class table : virtual public reference {
template<typename T, typename U> struct proxy; template<typename Table, typename T> struct proxy;
public: public:
table() noexcept : reference() {} table() noexcept : reference() {}
table(lua_State* L, int index = -1) : reference(L, index) { table(lua_State* L, int index = -1) : reference(L, index) {
@ -82,13 +82,13 @@ public:
} }
template<typename T> template<typename T>
auto operator[](T&& key) -> decltype(proxy<decltype(*this), T>(*this, std::forward<T>(key))) { proxy<table&, T> operator[](T&& key) {
return proxy<decltype(*this), T>(*this, std::forward<T>(key)); return proxy<table&, T>(*this, std::forward<T>(key));
} }
template<typename T> template<typename T>
auto operator[](T&& key) const -> decltype(proxy<decltype(*this), T>(*this, std::forward<T>(key))) { proxy<const table&, T> operator[](T&& key) const {
return proxy<decltype(*this), T>(*this, std::forward<T>(key)); return proxy<const table&, T>(*this, std::forward<T>(key));
} }
size_t size() const { size_t size() const {

View File

@ -72,56 +72,56 @@ template<typename TFuncSignature>
struct function_traits; struct function_traits;
template<typename T, typename R, typename... Args> template<typename T, typename R, typename... Args>
struct function_traits<R( T::* )( Args... )> { struct function_traits<R(T::*)(Args...)> {
static const std::size_t arity = sizeof...( Args ); static const std::size_t arity = sizeof...(Args);
static const bool is_member_function = true; static const bool is_member_function = true;
typedef std::tuple<Args...> arg_tuple_type; typedef std::tuple<Args...> arg_tuple_type;
typedef types<Args...> args_type; typedef types<Args...> args_type;
typedef R( T::* function_pointer_type )( Args... ); typedef R(T::* function_pointer_type)(Args...);
typedef typename std::remove_pointer<function_pointer_type>::type function_type; typedef typename std::remove_pointer<function_pointer_type>::type function_type;
typedef R( *free_function_pointer_type )( Args... ); typedef R(*free_function_pointer_type)(Args...);
typedef R return_type; typedef R return_type;
template<std::size_t i> template<std::size_t i>
using arg = typename std::tuple_element<i, arg_tuple_type>::type; using arg = typename std::tuple_element<i, arg_tuple_type>::type;
}; };
template<typename T, typename R, typename... Args> template<typename T, typename R, typename... Args>
struct function_traits<R( T::* )( Args... ) const> { struct function_traits<R(T::*)(Args...) const> {
static const std::size_t arity = sizeof...( Args ); static const std::size_t arity = sizeof...(Args);
static const bool is_member_function = true; static const bool is_member_function = true;
typedef std::tuple<Args...> arg_tuple_type; typedef std::tuple<Args...> arg_tuple_type;
typedef types<Args...> args_type; typedef types<Args...> args_type;
typedef R( T::* function_pointer_type )( Args... ); typedef R(T::* function_pointer_type)(Args...);
typedef typename std::remove_pointer<function_pointer_type>::type function_type; typedef typename std::remove_pointer<function_pointer_type>::type function_type;
typedef R( *free_function_pointer_type )( Args... ); typedef R(*free_function_pointer_type)(Args...);
typedef R return_type; typedef R return_type;
template<std::size_t i> template<std::size_t i>
using arg = typename std::tuple_element<i, arg_tuple_type>::type; using arg = typename std::tuple_element<i, arg_tuple_type>::type;
}; };
template<typename R, typename... Args> template<typename R, typename... Args>
struct function_traits<R( Args... )> { struct function_traits<R(Args...)> {
static const std::size_t arity = sizeof...( Args ); static const std::size_t arity = sizeof...(Args);
static const bool is_member_function = false; static const bool is_member_function = false;
typedef std::tuple<Args...> arg_tuple_type; typedef std::tuple<Args...> arg_tuple_type;
typedef types<Args...> args_type; typedef types<Args...> args_type;
typedef R( function_type )( Args... ); typedef R(function_type)(Args...);
typedef R( *function_pointer_type )( Args... ); typedef R(*function_pointer_type)(Args...);
typedef R( *free_function_pointer_type )( Args... ); typedef R(*free_function_pointer_type)(Args...);
typedef R return_type; typedef R return_type;
template<std::size_t i> template<std::size_t i>
using arg = typename std::tuple_element<i, arg_tuple_type>::type; using arg = typename std::tuple_element<i, arg_tuple_type>::type;
}; };
template<typename R, typename... Args> template<typename R, typename... Args>
struct function_traits<R( *)( Args... )> { struct function_traits<R(*)(Args...)> {
static const std::size_t arity = sizeof...( Args ); static const std::size_t arity = sizeof...(Args);
static const bool is_member_function = false; static const bool is_member_function = false;
typedef std::tuple<Args...> arg_tuple_type; typedef std::tuple<Args...> arg_tuple_type;
typedef types<Args...> args_type; typedef types<Args...> args_type;
typedef R( function_type )( Args... ); typedef R(function_type)(Args...);
typedef R( *function_pointer_type )( Args... ); typedef R(*function_pointer_type)(Args...);
typedef R( *free_function_pointer_type )( Args... ); typedef R(*free_function_pointer_type)(Args...);
typedef R return_type; typedef R return_type;
template<std::size_t i> template<std::size_t i>
using arg = typename std::tuple_element<i, arg_tuple_type>::type; using arg = typename std::tuple_element<i, arg_tuple_type>::type;

View File

@ -81,7 +81,10 @@ TEST_CASE("simple/callWithParameters", "Lua function is called with a few parame
REQUIRE_NOTHROW(lua.script("function my_add(i, j, k) return i + j + k end")); REQUIRE_NOTHROW(lua.script("function my_add(i, j, k) return i + j + k end"));
auto f = lua.get<sol::function>("my_add"); auto f = lua.get<sol::function>("my_add");
REQUIRE_NOTHROW(lua.script("function my_nothing(i, j, k) end"));
auto fvoid = lua.get<sol::function>("my_nothing");
int a; int a;
REQUIRE_NOTHROW(fvoid(1, 2, 3));
REQUIRE_NOTHROW(a = f.call<int>(1, 2, 3)); REQUIRE_NOTHROW(a = f.call<int>(1, 2, 3));
REQUIRE(a == 6); REQUIRE(a == 6);
REQUIRE_THROWS(a = f.call<int>(1, 2, "arf")); REQUIRE_THROWS(a = f.call<int>(1, 2, "arf"));
@ -111,15 +114,16 @@ TEST_CASE("simple/callLambda", "A C++ lambda is exposed to lua and called") {
TEST_CASE("advanced/callLambdaReturns", "Checks for lambdas returning values") { TEST_CASE("advanced/callLambdaReturns", "Checks for lambdas returning values") {
sol::state lua; sol::state lua;
lua.set_function("a", [ ] { return 42; }); REQUIRE_NOTHROW(lua.set_function("a", [ ] { return 42; }));
lua.set_function("b", [ ] { return 42u; }); REQUIRE_NOTHROW(lua.set_function("b", [ ] { return 42u; }));
lua.set_function("c", [ ] { return 3.14; }); REQUIRE_NOTHROW(lua.set_function("c", [ ] { return 3.14; }));
lua.set_function("d", [ ] { return 6.28f; }); REQUIRE_NOTHROW(lua.set_function("d", [ ] { return 6.28f; }));
lua.set_function("e", [ ] { return "lol"; }); REQUIRE_NOTHROW(lua.set_function("e", [ ] { return "lol"; }));
lua.set_function("f", [ ] { return true; }); REQUIRE_NOTHROW(lua.set_function("f", [ ] { return true; }));
lua.set_function("g", [ ] { return std::string("str"); }); REQUIRE_NOTHROW(lua.set_function("g", [ ] { return std::string("str"); }));
lua.set_function("h", [ ] { }); REQUIRE_NOTHROW(lua.set_function("h", [ ] { }));
lua.set_function("i", [ ] { return sol::nil; }); REQUIRE_NOTHROW(lua.set_function("i", [ ] { return sol::nil; }));
REQUIRE_NOTHROW(lua.set_function("j", [ ] { return std::make_tuple(1, 6.28f, 3.14, std::string( "heh" )); } ));
} }
TEST_CASE("advanced/callLambda2", "A C++ lambda is exposed to lua and called") { TEST_CASE("advanced/callLambda2", "A C++ lambda is exposed to lua and called") {
@ -165,7 +169,7 @@ TEST_CASE("tables/functions_variables", "Check if tables and function calls work
std::cout << "stateless lambda()" << std::endl; std::cout << "stateless lambda()" << std::endl;
return "test"; return "test";
} }
); );
REQUIRE_NOTHROW(run_script(lua)); REQUIRE_NOTHROW(run_script(lua));
lua.get<sol::table>("os").set_function("fun", &free_function); lua.get<sol::table>("os").set_function("fun", &free_function);
@ -183,7 +187,7 @@ TEST_CASE("tables/functions_variables", "Check if tables and function calls work
std::cout << "stateless lambda()" << std::endl; std::cout << "stateless lambda()" << std::endl;
return "test"; return "test";
} }
); );
REQUIRE_NOTHROW(run_script(lua)); REQUIRE_NOTHROW(run_script(lua));
// r-value, cannot optomize // r-value, cannot optomize