Skip to content

Commit

Permalink
Additional function creation tables and tests to keep them there.
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePhD committed Mar 15, 2016
1 parent c3bf2e0 commit f43adf9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
10 changes: 10 additions & 0 deletions sol/state_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,16 @@ class state_view {
return *this;
}

template <typename Name>
table create_table(Name&& name, int narr = 0, int nrec = 0) {
return global.create(std::forward<Name>(name), narr, nrec);
}

template <typename Name, typename Key, typename Value, typename... Args>
table create_table(Name&& name, int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
return global.create(std::forward<Name>(name), narr, nrec, std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
}

table create_table(int narr = 0, int nrec = 0) {
return create_table(lua_state(), narr, nrec);
}
Expand Down
14 changes: 14 additions & 0 deletions sol/table_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,20 @@ class table_core : public reference {
}

public:
template <typename Name>
table create(Name&& name, int narr = 0, int nrec = 0) {
table x = create(lua_state(), narr, nrec);
this->set(std::forward<Name>(name), x);
return x;
}

template <typename Name, typename Key, typename Value, typename... Args>
table create(Name&& name, int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
table x = create(lua_state(), narr, nrec, std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
this->set(std::forward<Name>(name), x);
return x;
}

table create(int narr = 0, int nrec = 0) {
return create(lua_state(), narr, nrec);
}
Expand Down
21 changes: 21 additions & 0 deletions tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,27 @@ TEST_CASE("tables/create", "Check if creating a table is kosher") {
REQUIRE((testtable[3] == 4));
}

TEST_CASE("tables/create-local", "Check if creating a table is kosher") {
sol::state lua;
lua["testtable"] = lua.create_table(0, 0, "Woof", "Bark", 1, 2, 3, 4);
sol::object testobj = lua["testtable"];
REQUIRE(testobj.is<sol::table>());
sol::table testtable = testobj.as<sol::table>();
REQUIRE((testtable["Woof"] == std::string("Bark")));
REQUIRE((testtable[1] == 2));
REQUIRE((testtable[3] == 4));
}

TEST_CASE("tables/create-local-named", "Check if creating a table is kosher") {
sol::state lua;
sol::table testtable = lua.create_table("testtable", 0, 0, "Woof", "Bark", 1, 2, 3, 4);
sol::object testobj = lua["testtable"];
REQUIRE(testobj.is<sol::table>());
REQUIRE((testtable["Woof"] == std::string("Bark")));
REQUIRE((testtable[1] == 2));
REQUIRE((testtable[3] == 4));
}

TEST_CASE("tables/functions-variables", "Check if tables and function calls work as intended") {
sol::state lua;
lua.open_libraries(sol::lib::base, sol::lib::os);
Expand Down

0 comments on commit f43adf9

Please sign in to comment.