File: variables_demo.cpp

package info (click to toggle)
sol2 3.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,096 kB
  • sloc: cpp: 43,816; ansic: 1,018; python: 356; sh: 288; makefile: 202
file content (79 lines) | stat: -rw-r--r-- 2,029 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>

#include <tuple>
#include <utility> // for std::pair

int main() {

	sol::state lua;
	/*
	lua.script_file("variables.lua");
	*/
	lua.script(R"(
config = {
	fullscreen = false,
	resolution = { x = 1024, y = 768 }
}
	)");
	// the type "sol::state" behaves
	// exactly like a table!
	bool isfullscreen
	     = lua["config"]
	          ["fullscreen"]; // can get nested variables
	sol::table config = lua["config"];
	SOL_ASSERT(!isfullscreen);

	// can also get it using the "get" member function
	// auto replaces the unqualified type name
	auto resolution = config.get<sol::table>("resolution");

	// table and state can have multiple things pulled out of it
	// too
	std::tuple<int, int> xyresolutiontuple
	     = resolution.get<int, int>("x", "y");
	SOL_ASSERT(std::get<0>(xyresolutiontuple) == 1024);
	SOL_ASSERT(std::get<1>(xyresolutiontuple) == 768);

	// test variable
	auto bark = lua["config"]["bark"];
	if (bark.valid()) {
		// branch not taken: config and/or bark are not
		// variables
	}
	else {
		// Branch taken: config and bark are existing variables
	}

	// can also use optional
	sol::optional<int> not_an_integer
	     = lua["config"]["fullscreen"];
	if (not_an_integer) {
		// Branch not taken: value is not an integer
	}

	sol::optional<bool> is_a_boolean
	     = lua["config"]["fullscreen"];
	if (is_a_boolean) {
		// Branch taken: the value is a boolean
	}

	sol::optional<double> does_not_exist
	     = lua["not_a_variable"];
	if (does_not_exist) {
		// Branch not taken: that variable is not present
	}

	// this will result in a value of '24'
	// (it tries to get a number, and fullscreen is
	// not a number
	int is_defaulted = lua["config"]["fullscreen"].get_or(24);
	SOL_ASSERT(is_defaulted == 24);

	// This will result in the value of the config, which is
	// 'false'
	bool is_not_defaulted = lua["config"]["fullscreen"];
	SOL_ASSERT(!is_not_defaulted);

	return 0;
}