| 5 | #include <iostream> |
| 6 | |
| 7 | int main(int, char**) { |
| 8 | std::cout << "=== containers ===" << std::endl; |
| 9 | |
| 10 | sol::state lua; |
| 11 | lua.open_libraries(); |
| 12 | |
| 13 | lua.script(R"( |
| 14 | function f (x) |
| 15 | print("container has:") |
| 16 | for k=1,#x do |
| 17 | v = x[k] |
| 18 | print("\t", k, v) |
| 19 | end |
| 20 | print() |
| 21 | end |
| 22 | )"); |
| 23 | |
| 24 | // Have the function we |
| 25 | // just defined in Lua |
| 26 | sol::function f = lua["f"]; |
| 27 | |
| 28 | // Set a global variable called |
| 29 | // "arr" to be a vector of 5 lements |
| 30 | lua["arr"] = std::vector<int> { 2, 4, 6, 8, 10 }; |
| 31 | |
| 32 | // Call it, see 5 elements |
| 33 | // printed out |
| 34 | f(lua["arr"]); |
| 35 | |
| 36 | // Mess with it in C++ |
| 37 | // Containers are stored as userdata, unless you |
| 38 | // use `sol::as_table()` and `sol::as_table_t`. |
| 39 | std::vector<int>& reference_to_arr = lua["arr"]; |
| 40 | reference_to_arr.push_back(12); |
| 41 | |
| 42 | // Call it, see *6* elements |
| 43 | // printed out |
| 44 | f(lua["arr"]); |
| 45 | |
| 46 | lua.script(R"( |
| 47 | arr:add(28) |
| 48 | )"); |
| 49 | |
| 50 | // Call it, see *7* elements |
| 51 | // printed out |
| 52 | f(lua["arr"]); |
| 53 | |
| 54 | lua.script(R"( |
| 55 | arr:clear() |
| 56 | )"); |
| 57 | |
| 58 | // Now it's empty |
| 59 | f(lua["arr"]); |
| 60 | |
| 61 | std::cout << std::endl; |
| 62 | |
| 63 | return 0; |
| 64 | } |
nothing calls this directly
no test coverage detected