Some spiffier table access syntax (and a stupid VC++ bug discovered along with it)

This commit is contained in:
ThePhD 2016-02-01 03:53:51 -05:00
parent 1d93f560f2
commit c97b3f2b81

View File

@ -18,19 +18,40 @@ int main() {
"}");
auto t2 = lua.get<sol::table>("table2");
auto nestedTable = t2.get<sol::table>("nestedTable");
/* Shorter Syntax: */
// using the values stored in table1
/*std::cout << (std::string)lua["table1"][1] << " "
<< (std::string)lua["table1"][2] << '\n';
*/
// some retrieval of values from the nested table
// the cleaner way of doing things
// chain off the the get<>() / [] results
std::string x = lua["table2"]["nestedTable"]["key2"];
std::cout << "nested table: key1 : " << nestedTable.get<std::string>("key1") << ", key2: "
<< x
<< '\n';
std::cout << "name of t2: " << t2.get<std::string>("name") << '\n';
#ifndef _MSC_VER
// VC++ has a bug in its implementation and
// I've filed a bug against the compiler at Microsoft,
// but the following
// works on g++ and clang++
std::cout << "name of t2: " << (std::string)t2["name"] << '\n';
#endif // VC++ being a dumb
/* Longer Syntax: */
// using the values stored in table1
std::cout << lua.get<sol::table>("table1").get<std::string>(1) << " "
<< lua.get<sol::table>("table1").get<std::string>(2) << '\n';
auto t2 = lua.get<sol::table>("table2");
auto nestedTable = t2.get<sol::table>("nestedTable");
// some retrieval of values from the nested table
// the cleaner way of doing things
std::cout << "nested table: key1 : " << nestedTable.get<std::string>("key1") << ", key2: "
// yes you can chain the get<>() results
<< lua.get<sol::table>("table2").get<sol::table>("nestedTable").get<std::string>("key2")
<< '\n';
std::cout << "name of t2: " << t2.get<std::string>("name") << '\n';
}