File: usertype_constructors.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 (66 lines) | stat: -rw-r--r-- 1,639 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
#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>

#include <string>
#include <iostream>

class MyClass {
public:
	MyClass(double d) : data(d) {
	}
	double data;
};

int main() {
	std::cout << "=== usertype constructors ===" << std::endl;


	sol::state lua;
	lua.open_libraries(sol::lib::base);

	lua.new_usertype<MyClass>("MyClass",
	     sol::meta_function::construct,
	     sol::factories(
	          // MyClass.new(...) -- dot syntax, no "self" value
	          // passed in
	          [](const double& d) {
		          return std::make_shared<MyClass>(d);
	          },
	          // MyClass:new(...) -- colon syntax, passes in the
	          // "self" value as first argument implicitly
	          [](sol::object, const double& d) {
		          return std::make_shared<MyClass>(d);
	          }),
	     // MyClass(...) syntax, only
	     sol::call_constructor,
	     sol::factories([](const double& d) {
		     return std::make_shared<MyClass>(d);
	     }),
	     "data",
	     &MyClass::data);

	sol::optional<sol::error> maybe_error = lua.safe_script(R"(
		d1 = MyClass(2.1)
		d2 = MyClass:new(3.1)
		d3 = MyClass(4.1)
		assert(d1.data == 2.1)
		assert(d2.data == 3.1)
		assert(d3.data == 4.1)
	)",
	     sol::script_pass_on_error);

	if (maybe_error) {
		// something went wrong!
		std::cerr << "Something has gone horribly unexpected "
		             "and wrong:\n"
		          << maybe_error->what() << std::endl;
		return 1;
	}

	// everything is okay!
	std::cout
	     << "Everything went okay and all the asserts passed!"
	     << std::endl;

	return 0;
}