mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
A hefty slice of changes for functions on the proxy. Not necessary, but was fun to get around MSVC's ICE errors.
This commit is contained in:
parent
6c06bddd6e
commit
25f42b4bd3
|
@ -65,9 +65,14 @@ public:
|
||||||
void operator()(Args&&... args) {
|
void operator()(Args&&... args) {
|
||||||
call<>(std::forward<Args>(args)...);
|
call<>(std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename... Ret, typename... Args>
|
||||||
|
typename multi_return<Ret...>::type operator()(types<Ret...>, Args&&... args) {
|
||||||
|
return call<Ret...>(std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
template<typename... Ret, typename... Args>
|
template<typename... Ret, typename... Args>
|
||||||
auto call(Args&&... args) -> decltype(invoke(types<Ret...>(), sizeof...(Args))) {
|
typename multi_return<Ret...>::type call(Args&&... args) {
|
||||||
push();
|
push();
|
||||||
stack::push_args(state(), std::forward<Args>(args)...);
|
stack::push_args(state(), std::forward<Args>(args)...);
|
||||||
return invoke(types<Ret...>(), sizeof...(Args));
|
return invoke(types<Ret...>(), sizeof...(Args));
|
||||||
|
|
|
@ -45,7 +45,7 @@ inline T get_unsigned(lua_State* L, std::false_type, int index = -1) {
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline T get_arithmetic(lua_State* L, std::false_type, int index = -1) {
|
inline T get_arithmetic(lua_State* L, std::false_type, int index = -1) {
|
||||||
// T is a floating point
|
// T is a floating point
|
||||||
return lua_tonumber(L, index);
|
return static_cast<T>(lua_tonumber(L, index));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|
|
@ -118,6 +118,21 @@ private:
|
||||||
operator U() const {
|
operator U() const {
|
||||||
return t.get<U>(key);
|
return t.get<U>(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
void operator()( Args&&... args ) {
|
||||||
|
call<>( std::forward<Args>( args )... );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Ret, typename... Args>
|
||||||
|
typename multi_return<Ret...>::type operator()(types<Ret...>, Args&&... args) {
|
||||||
|
return call<Ret...>(std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Ret, typename... Args>
|
||||||
|
typename multi_return<Ret...>::type call(Args&&... args) {
|
||||||
|
return t.get<function>(key)(types<Ret...>(), std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T, typename TFx>
|
template<typename T, typename TFx>
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
#ifndef SOL_TRAITS_HPP
|
#ifndef SOL_TRAITS_HPP
|
||||||
#define SOL_TRAITS_HPP
|
#define SOL_TRAITS_HPP
|
||||||
|
|
||||||
|
#include "tuple.hpp"
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
namespace sol {
|
namespace sol {
|
||||||
|
@ -62,6 +63,21 @@ struct is_function_impl<T, true> {
|
||||||
};
|
};
|
||||||
} // detail
|
} // detail
|
||||||
|
|
||||||
|
template <typename... Args>
|
||||||
|
struct multi_return {
|
||||||
|
typedef std::tuple<Args...> type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct multi_return<T> {
|
||||||
|
typedef T type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct multi_return<> : types<>{
|
||||||
|
typedef void type;
|
||||||
|
};
|
||||||
|
|
||||||
template<bool B>
|
template<bool B>
|
||||||
using Bool = std::integral_constant<bool, B>;
|
using Bool = std::integral_constant<bool, B>;
|
||||||
|
|
||||||
|
|
|
@ -113,6 +113,8 @@ template<>
|
||||||
inline type type_of<bool>() {
|
inline type type_of<bool>() {
|
||||||
return type::boolean;
|
return type::boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline bool operator==(nil_t, nil_t) { return true; }
|
||||||
} // sol
|
} // sol
|
||||||
|
|
||||||
#endif // SOL_TYPES_HPP
|
#endif // SOL_TYPES_HPP
|
22
tests.cpp
22
tests.cpp
|
@ -112,18 +112,40 @@ 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") {
|
||||||
|
const static std::string lol = "lol", str = "str";
|
||||||
|
const static std::tuple<int, float, double, std::string> heh_tuple = std::make_tuple(1, 6.28f, 3.14, std::string("heh"));
|
||||||
sol::state lua;
|
sol::state lua;
|
||||||
|
|
||||||
REQUIRE_NOTHROW(lua.set_function("a", [ ] { return 42; }));
|
REQUIRE_NOTHROW(lua.set_function("a", [ ] { return 42; }));
|
||||||
|
REQUIRE(lua["a"]( sol::types<int>() ) == 42);
|
||||||
|
|
||||||
REQUIRE_NOTHROW(lua.set_function("b", [ ] { return 42u; }));
|
REQUIRE_NOTHROW(lua.set_function("b", [ ] { return 42u; }));
|
||||||
|
REQUIRE(lua["b"](sol::types<unsigned int>()) == 42u);
|
||||||
|
|
||||||
REQUIRE_NOTHROW(lua.set_function("c", [ ] { return 3.14; }));
|
REQUIRE_NOTHROW(lua.set_function("c", [ ] { return 3.14; }));
|
||||||
|
REQUIRE(lua["c"](sol::types<double>()) == 3.14);
|
||||||
|
|
||||||
REQUIRE_NOTHROW(lua.set_function("d", [ ] { return 6.28f; }));
|
REQUIRE_NOTHROW(lua.set_function("d", [ ] { return 6.28f; }));
|
||||||
|
REQUIRE(lua["d"](sol::types<float>()) == 6.28f);
|
||||||
|
|
||||||
REQUIRE_NOTHROW(lua.set_function("e", [ ] { return "lol"; }));
|
REQUIRE_NOTHROW(lua.set_function("e", [ ] { return "lol"; }));
|
||||||
|
REQUIRE(lua["e"](sol::types<std::string>()) == lol);
|
||||||
|
|
||||||
REQUIRE_NOTHROW(lua.set_function("f", [ ] { return true; }));
|
REQUIRE_NOTHROW(lua.set_function("f", [ ] { return true; }));
|
||||||
|
REQUIRE(lua["f"](sol::types<bool>()));
|
||||||
|
|
||||||
REQUIRE_NOTHROW(lua.set_function("g", [ ] { return std::string("str"); }));
|
REQUIRE_NOTHROW(lua.set_function("g", [ ] { return std::string("str"); }));
|
||||||
|
REQUIRE(lua["g"](sol::types<std::string>()) == str);
|
||||||
|
|
||||||
REQUIRE_NOTHROW(lua.set_function("h", [ ] { }));
|
REQUIRE_NOTHROW(lua.set_function("h", [ ] { }));
|
||||||
|
REQUIRE_NOTHROW(lua["h"](sol::types<>()));
|
||||||
|
REQUIRE_THROWS(lua["h"](sol::types<int>()));
|
||||||
|
|
||||||
REQUIRE_NOTHROW(lua.set_function("i", [ ] { return sol::nil; }));
|
REQUIRE_NOTHROW(lua.set_function("i", [ ] { return sol::nil; }));
|
||||||
|
REQUIRE(lua["i"](sol::types<sol::nil_t>()) == sol::nil);
|
||||||
|
|
||||||
REQUIRE_NOTHROW(lua.set_function("j", [ ] { return std::make_tuple(1, 6.28f, 3.14, std::string( "heh" )); } ));
|
REQUIRE_NOTHROW(lua.set_function("j", [ ] { return std::make_tuple(1, 6.28f, 3.14, std::string( "heh" )); } ));
|
||||||
|
//REQUIRE(lua["j"](sol::types<int, float, double, std::string>()) == heh_tuple);
|
||||||
}
|
}
|
||||||
|
|
||||||
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") {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user