# sol2 3.5.0 documentation

## lua_value

### Search the Documentation

# lua_value

easy creation of Lua values and tables at the cost of some safety and speed

    struct lua_value;
    struct array_value;

The goal of these types is to make it easy to describe tables and arrays in C++ code. It works by using a thread local `lua_State*` variable inside the class so that one can simply pass values. The thread local variable is initialized by creation of a sol::state, but can also be done manually<state-automatic-handlers> with `sol::set_default_state`. An example of usage is below:

lua_value.cpp

      1#define SOL_ALL_SAFETIES_ON 1
      2#include <sol/sol.hpp>
      3
      4#include <iostream>
      5#include <string>
      6
      7struct int_entry {
      8	int value;
      9
     10	int_entry() : value(0) {
     11	}
     12
     13	int_entry(int v) : value(v) {
     14	}
     15
     16	std::string to_string() const {
     17		return "int_entry(" + std::to_string(value) + ")";
     18	}
     19
     20	bool operator==(const int_entry& e) const {
     21		return value == e.value;
     22	}
     23};
     24
     25int main(int, char*[]) {
     26
     27	std::cout << "=== sol::lua_value/sol::array_value ==="
     28	          << std::endl;
     29
     30	sol::state lua;
     31	lua.open_libraries(sol::lib::base, sol::lib::io);
     32
     33	sol::lua_value lv_int(lua, 56);
     34	sol::lua_value lv_int_table(lua, { 1, 2, 3, 4, 5 });
     35	sol::lua_value lv_map(lua,
     36	     { { "bark bark", "meow hiss!" },
     37	          { 3, 4 },
     38	          { ":D", 6 } });
     39	sol::lua_value lv_mixed_table(lua,
     40	     sol::array_value {
     41	          1, int_entry(2), 3, int_entry(4), 5 });
     42	sol::lua_value lv_mixed_nested_table(lua,
     43	     sol::array_value { 1,
     44	          int_entry(2),
     45	          3,
     46	          int_entry(4),
     47	          sol::array_value { 5, 6, int_entry(7), "8" } });
     48
     49	const auto& code = R"(
     50		function real_print_recursive (e, level)
     51			local et = type(e)
     52			if et == 'table' then
     53				io.write("{ ")
     54				local iters = 0
     55				for k, v in pairs(e) do
     56					if iters ~= 0  then
     57						io.write(", ")
     58					end
     59					real_print_recursive(k, level + 1)
     60					io.write(": ")
     61					real_print_recursive(v, level + 1)
     62					iters = iters + 1
     63				end
     64				io.write(" }")
     65			elseif et == 'string' then
     66				io.write('"')
     67				io.write(e)
     68				io.write('"')
     69			else
     70				io.write(tostring(e))
     71			end
     72			if level == 0 then
     73				io.write("\n")
     74			end
     75		end
     76
     77		function print_recursive (e)
     78			real_print_recursive(e, 0)
     79		end
     80	)";
     81
     82	sol::optional<sol::error> maybe_error
     83	     = lua.safe_script(code, sol::script_pass_on_error);
     84	if (maybe_error) {
     85		std::cerr << maybe_error->what() << std::endl;
     86		return 1;
     87	}
     88	sol::function print_recursive = lua["print_recursive"];
     89
     90	// show it printed out
     91	std::cout << "lv_int: " << std::endl;
     92	print_recursive(lv_int);
     93	std::cout << std::endl;
     94
     95	std::cout << "lv_int_table: " << std::endl;
     96	print_recursive(lv_int_table);
     97	std::cout << std::endl;
     98
     99	std::cout << "lv_map: " << std::endl;
    100	print_recursive(lv_map);
    101	std::cout << std::endl;
    102
    103	std::cout << "lv_mixed_table: " << std::endl;
    104	print_recursive(lv_mixed_table);
    105	std::cout << std::endl;
    106
    107	std::cout << "lv_mixed_nested_table: " << std::endl;
    108	print_recursive(lv_mixed_nested_table);
    109	std::cout << std::endl;
    110
    111	std::cout << std::endl;
    112
    113	return 0;
    114}