File: test_policies.cpp

package info (click to toggle)
luabind 0.9.1%2Bdfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,844 kB
  • ctags: 2,833
  • sloc: cpp: 13,796; makefile: 126; sh: 24; ansic: 11
file content (227 lines) | stat: -rw-r--r-- 5,573 bytes parent folder | download | duplicates (3)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright (c) 2003 Daniel Wallin and Arvid Norberg

// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
// ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
// OR OTHER DEALINGS IN THE SOFTWARE.

#include "test.hpp"

#include <luabind/out_value_policy.hpp>
#include <luabind/return_reference_to_policy.hpp>
#include <luabind/copy_policy.hpp>
#include <luabind/adopt_policy.hpp>
#include <luabind/discard_result_policy.hpp>
#include <luabind/dependency_policy.hpp>
#include <luabind/luabind.hpp>

struct test_copy {};


struct secret_type {};

secret_type sec_;


struct policies_test_class
{
	policies_test_class(const char* name): name_(name)
	{ ++count; }
	policies_test_class() { ++count; }
	~policies_test_class()
	{ --count; }

	std::string name_;

	policies_test_class* make(const char* name) const
	{
		return new policies_test_class(name);
	}

	void f(policies_test_class* p)
	{
		delete p;
	}
	const policies_test_class* internal_ref() { return this; }
	policies_test_class* self_ref()
	{ return this; }

	static int count;

	//	private:
	policies_test_class(policies_test_class const& c): name_(c.name_)
	{ ++count; }

	void member_out_val(int a, int* v) { *v = a * 2; }
	secret_type* member_secret() { return &sec_; }
};

int policies_test_class::count = 0;

policies_test_class global;

void out_val(float* f) { *f = 3.f; }
policies_test_class* copy_val() { return &global; }
policies_test_class const* copy_val_const() { return &global; }

secret_type* secret() { return &sec_; }

void aux_test();

struct test_t {
	test_t *make(int) { return new test_t(); }
	void take(test_t*) {}
};

struct MI2;

struct MI1
{
	void add(MI2 *) {}
};

struct MI2 : public MI1
{
	virtual ~MI2()
	{}
};

struct MI2W : public MI2, public luabind::wrap_base {};

void test_main(lua_State* L)
{
	using namespace luabind;

	module(L)
	[
		class_<test_t>("test_t")
		.def("make", &test_t::make, adopt(return_value))
		.def("take", &test_t::take, adopt(_2))
	];
	
	module(L)
	[
		class_<policies_test_class>("test")
			.def(constructor<>())
			.def("member_out_val", &policies_test_class::member_out_val, pure_out_value(_3))
			.def("member_secret", &policies_test_class::member_secret, discard_result)
			.def("f", &policies_test_class::f, adopt(_2))
			.def("make", &policies_test_class::make, adopt(return_value))
			.def("internal_ref", &policies_test_class::internal_ref, dependency(result, _1))
			.def("self_ref", &policies_test_class::self_ref, return_reference_to(_1)),

		def("out_val", &out_val, pure_out_value(_1)),
		def("copy_val", &copy_val, copy(result)),
		def("copy_val_const", &copy_val_const, copy(result)),
		def("secret", &secret, discard_result),

		class_<MI1>("mi1")
			.def(constructor<>())
			.def("add",&MI1::add,adopt(_2)),

		class_<MI2,MI2W,MI1>("mi2")
			.def(constructor<>())
	];

	// test copy
	DOSTRING(L,	"a = secret()\n");

	TEST_CHECK(policies_test_class::count == 1);

	DOSTRING(L, "a = copy_val()\n");
	TEST_CHECK(policies_test_class::count == 2);

	DOSTRING(L, "b = copy_val_const()\n");
	TEST_CHECK(policies_test_class::count == 3);

	DOSTRING(L,
		"a = nil\n"
		"b = nil\n"
		"collectgarbage()\n");

	// only the global variable left here
	TEST_CHECK(policies_test_class::count == 1);

	// out_value
	DOSTRING(L,
		"a = out_val()\n"
		"assert(a == 3)");

	// return_reference_to
	DOSTRING(L,
		"a = test()\n"
		"b = a:self_ref()\n"
		"a = nil\n"
		"collectgarbage()");

	// a is kept alive as long as b is alive
	TEST_CHECK(policies_test_class::count == 2);

	DOSTRING(L,
		"b = nil\n"
		"collectgarbage()");

	TEST_CHECK(policies_test_class::count == 1);

	DOSTRING(L, "a = test()");

	TEST_CHECK(policies_test_class::count == 2);

	DOSTRING(L,
		"b = a:internal_ref()\n"
		"a = nil\n"
		"collectgarbage()");

	// a is kept alive as long as b is alive
	TEST_CHECK(policies_test_class::count == 2);

	// two gc-cycles because dependency-table won't be collected in the
	// same cycle as the object_rep
	DOSTRING(L,
		"b = nil\n"
		"collectgarbage()\n"
		"collectgarbage()");

	TEST_CHECK(policies_test_class::count == 1);

	// adopt
	DOSTRING(L, "a = test()");

	TEST_CHECK(policies_test_class::count == 2);

	DOSTRING(L, "b = a:make('tjosan')");
	DOSTRING(L, "assert(a:member_out_val(3) == 6)");
	DOSTRING(L, "a:member_secret()");

	// make instantiated a new policies_test_class
	TEST_CHECK(policies_test_class::count == 3);

	DOSTRING(L, "a:f(b)\n");

	// b was adopted by c++ and deleted the object
	TEST_CHECK(policies_test_class::count == 2);

	DOSTRING(L, "a = nil\n"
		"collectgarbage()");

	TEST_CHECK(policies_test_class::count == 1);

	// adopt with wrappers
	DOSTRING(L, "mi1():add(mi2())");
}