Standard-compliant function signature deduction (cannot use set_function<Args...> format as that creates an ambiguity)

state now has for_each (runs on global table like all other functions)
added for_each tests, per @Rapptz request
This commit is contained in:
ThePhD 2015-05-15 22:26:18 -04:00
parent 1792259685
commit e816d07121
5 changed files with 65 additions and 11 deletions

View File

@ -105,11 +105,6 @@ struct pusher<function_sig_t<Sigs...>> {
set_isconvertible_fx(is_convertible(), t, L, std::forward<Fx>(fx));
}
template<typename... Args, typename Fx, typename R = typename std::result_of<Fx(Args...)>::type>
static void set_memfx(types<Args...>, lua_State* L, Fx&& fx){
set_memfx(types<R(Args...)>(), L, std::forward<Fx>(fx));
}
template<typename Fx>
static void set_memfx(types<>, lua_State* L, Fx&& fx) {
typedef Unqualified<Unwrap<Fx>> fx_t;

View File

@ -77,6 +77,10 @@ public:
return get<function>();
}
operator Unqualified<Table>() const {
return get<Unqualified<Table>>();
}
operator std::string() const {
return get<std::string>();
}

View File

@ -192,6 +192,11 @@ public:
return *this;
}
template <typename Fx>
void for_each(Fx&& fx) {
global.for_each(std::forward<Fx>(fx));
}
template<typename T>
table create_table(T&& key, int narr = 0, int nrec = 0) {
lua_createtable(L.get(), narr, nrec);

View File

@ -113,7 +113,9 @@ public:
size_t size() const {
push();
return lua_rawlen(state(), -1);
size_t result = lua_rawlen(state(), -1);
pop();
return result;
}
template<typename T>
@ -166,11 +168,6 @@ private:
set_resolved_function<R(Args...)>(std::forward<Key>(key), std::forward<Fx>(fx));
}
template<typename... Args, typename Fx, typename Key, typename R = typename std::result_of<Fx(Args...)>::type>
void set_fx(types<Args...>, Key&& key, Fx&& fx){
set_fx(types<R(Args...)>(), std::forward<Key>(key), std::forward<Fx>(fx));
}
template<typename Fx, typename Key>
void set_fx(types<>, Key&& key, Fx&& fx) {
typedef Unqualified<Unwrap<Fx>> fx_t;

View File

@ -730,6 +730,59 @@ TEST_CASE("tables/arbitrary-creation", "tables should be created from standard c
REQUIRE(c.get<std::string>("project") == "sol");
}
TEST_CASE("tables/for_each", "Testing the use of for_each to get values from a lua table") {
sol::state lua;
lua.open_libraries(sol::lib::base);
lua.script("arr = {\n"
"[0] = \"Hi\",\n"
"[1] = 123.45,\n"
"[2] = \"String value\",\n"
// Does nothing
//"[3] = nil,\n"
//"[nil] = 3,\n"
"[\"WOOF\"] = 123,\n"
"}");
sol::table tbl = lua[ "arr" ];
std::size_t tablesize = 4;
std::size_t iterations = 0;
tbl.for_each(
[&iterations](sol::object key, sol::object value) {
++iterations;
sol::type keytype = key.get_type();
switch (keytype) {
case sol::type::number:
switch (key.as<int>()) {
case 0:
REQUIRE((value.as<std::string>() == "Hi"));
break;
case 1:
REQUIRE((value.as<double>() == 123.45));
break;
case 2:
REQUIRE((value.as<std::string>() == "String value"));
break;
case 3:
REQUIRE((value.is<sol::nil_t>()));
break;
}
break;
case sol::type::string:
if (key.as<std::string>() == "WOOF") {
REQUIRE((value.as<double>() == 123));
}
break;
case sol::type::nil:
REQUIRE((value.as<double>() == 3));
break;
default:
break;
}
}
);
REQUIRE(iterations == tablesize);
}
TEST_CASE("tables/issue-number-twenty-five", "Using pointers and references from C++ classes in Lua") {
struct test {
int x = 0;