1 #define SOL_CHECK_ARGUMENTS
2 
3 #include <catch.hpp>
4 #include <sol.hpp>
5 
6 #include <unordered_map>
7 #include <vector>
8 
9 struct two_things {
10 	int a;
11 	bool b;
12 };
13 
14 namespace sol {
15 
16 	// First, the expected size
17 	// Specialization of a struct
18 	template <>
19 	struct lua_size<two_things> : std::integral_constant<int, 2> {};
20 
21 	// Now, specialize various stack structures
22 	namespace stack {
23 
24 		template <>
25 		struct checker<two_things> {
26 			template <typename Handler>
checksol::stack::checker27 			static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
28 				// Check first and second second index for being the proper types
29 				bool success = stack::check<int>(L, index, handler)
30 					&& stack::check<bool>(L, index + 1, handler);
31 				tracking.use(2);
32 				return success;
33 			}
34 		};
35 
36 		template <>
37 		struct getter<two_things> {
getsol::stack::getter38 			static two_things get(lua_State* L, int index, record& tracking) {
39 				// Get the first element
40 				int a = stack::get<int>(L, index);
41 				// Get the second element,
42 				// in the +1 position from the first
43 				bool b = stack::get<bool>(L, index + 1);
44 				// we use 2 slots, each of the previous takes 1
45 				tracking.use(2);
46 				return two_things{ a, b };
47 			}
48 		};
49 
50 		template <>
51 		struct pusher<two_things> {
pushsol::stack::pusher52 			static int push(lua_State* L, const two_things& things) {
53 				int amount = stack::push(L, things.a);
54 				amount += stack::push(L, things.b);
55 				// Return 2 things
56 				return amount;
57 			}
58 		};
59 
60 	}
61 }
62 
63 TEST_CASE("customization/split-struct", "using the newly documented customization points to handle different kinds of classes") {
64 	sol::state lua;
65 
66 	// Create a pass-through style of function
67 	lua.script("function f ( a, b, c ) return a + c, b end");
__anon4df98a5d0102(int a, bool b, int c, double d) 68 	lua.set_function("g", [](int a, bool b, int c, double d) {
69 		return std::make_tuple(a + c, b, d + 2.5);
70 	});
71 
72 	// get the function out of Lua
73 	sol::function f = lua["f"];
74 	sol::function g = lua["g"];
75 
76 	two_things thingsf = f(two_things{ 24, true }, 1);
77 	two_things thingsg;
78 	double d;
79 	sol::tie( thingsg, d ) = g(two_things{ 25, false }, 2, 34.0);
80 	REQUIRE(thingsf.a == 25);
81 	REQUIRE(thingsf.b);
82 
83 	REQUIRE(thingsg.a == 27);
84 	REQUIRE_FALSE(thingsg.b);
85 	REQUIRE(d == 36.5);
86 }
87