Add define that strips extern "C" {} around Lua includes.
[luabind.git] / test / test_policies.cpp
blob09e62fcc6721b51ec4b3619b95e1602171996d62
1 // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
14 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
18 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21 // OR OTHER DEALINGS IN THE SOFTWARE.
23 #include "test.hpp"
25 #include <luabind/out_value_policy.hpp>
26 #include <luabind/return_reference_to_policy.hpp>
27 #include <luabind/copy_policy.hpp>
28 #include <luabind/adopt_policy.hpp>
29 #include <luabind/discard_result_policy.hpp>
30 #include <luabind/dependency_policy.hpp>
31 #include <luabind/luabind.hpp>
33 struct test_copy {};
36 struct secret_type {};
38 secret_type sec_;
41 struct policies_test_class
43 policies_test_class(const char* name): name_(name)
44 { ++count; }
45 policies_test_class() { ++count; }
46 ~policies_test_class()
47 { --count; }
49 std::string name_;
51 policies_test_class* make(const char* name) const
53 return new policies_test_class(name);
56 void f(policies_test_class* p)
58 delete p;
60 const policies_test_class* internal_ref() { return this; }
61 policies_test_class* self_ref()
62 { return this; }
64 static int count;
66 // private:
67 policies_test_class(policies_test_class const& c): name_(c.name_)
68 { ++count; }
70 void member_out_val(int a, int* v) { *v = a * 2; }
71 secret_type* member_secret() { return &sec_; }
74 int policies_test_class::count = 0;
76 policies_test_class global;
78 void out_val(float* f) { *f = 3.f; }
79 policies_test_class* copy_val() { return &global; }
80 policies_test_class const* copy_val_const() { return &global; }
82 secret_type* secret() { return &sec_; }
84 void aux_test();
86 struct test_t {
87 test_t *make(int) { return new test_t(); }
88 void take(test_t*) {}
91 struct MI2;
93 struct MI1
95 void add(MI2 *) {}
98 struct MI2 : public MI1
100 virtual ~MI2()
104 struct MI2W : public MI2, public luabind::wrap_base {};
106 void test_main(lua_State* L)
108 using namespace luabind;
110 module(L)
112 class_<test_t>("test_t")
113 .def("make", &test_t::make, adopt(return_value))
114 .def("take", &test_t::take, adopt(_2))
117 module(L)
119 class_<policies_test_class>("test")
120 .def(constructor<>())
121 .def("member_out_val", &policies_test_class::member_out_val, pure_out_value(_3))
122 .def("member_secret", &policies_test_class::member_secret, discard_result)
123 .def("f", &policies_test_class::f, adopt(_2))
124 .def("make", &policies_test_class::make, adopt(return_value))
125 .def("internal_ref", &policies_test_class::internal_ref, dependency(result, _1))
126 .def("self_ref", &policies_test_class::self_ref, return_reference_to(_1)),
128 def("out_val", &out_val, pure_out_value(_1)),
129 def("copy_val", &copy_val, copy(result)),
130 def("copy_val_const", &copy_val_const, copy(result)),
131 def("secret", &secret, discard_result),
133 class_<MI1>("mi1")
134 .def(constructor<>())
135 .def("add",&MI1::add,adopt(_2)),
137 class_<MI2,MI2W,MI1>("mi2")
138 .def(constructor<>())
141 // test copy
142 DOSTRING(L, "a = secret()\n");
144 TEST_CHECK(policies_test_class::count == 1);
146 DOSTRING(L, "a = copy_val()\n");
147 TEST_CHECK(policies_test_class::count == 2);
149 DOSTRING(L, "b = copy_val_const()\n");
150 TEST_CHECK(policies_test_class::count == 3);
152 DOSTRING(L,
153 "a = nil\n"
154 "b = nil\n"
155 "collectgarbage()\n");
157 // only the global variable left here
158 TEST_CHECK(policies_test_class::count == 1);
160 // out_value
161 DOSTRING(L,
162 "a = out_val()\n"
163 "assert(a == 3)");
165 // return_reference_to
166 DOSTRING(L,
167 "a = test()\n"
168 "b = a:self_ref()\n"
169 "a = nil\n"
170 "collectgarbage()");
172 // a is kept alive as long as b is alive
173 TEST_CHECK(policies_test_class::count == 2);
175 DOSTRING(L,
176 "b = nil\n"
177 "collectgarbage()");
179 TEST_CHECK(policies_test_class::count == 1);
181 DOSTRING(L, "a = test()");
183 TEST_CHECK(policies_test_class::count == 2);
185 DOSTRING(L,
186 "b = a:internal_ref()\n"
187 "a = nil\n"
188 "collectgarbage()");
190 // a is kept alive as long as b is alive
191 TEST_CHECK(policies_test_class::count == 2);
193 // two gc-cycles because dependency-table won't be collected in the
194 // same cycle as the object_rep
195 DOSTRING(L,
196 "b = nil\n"
197 "collectgarbage()\n"
198 "collectgarbage()");
200 TEST_CHECK(policies_test_class::count == 1);
202 // adopt
203 DOSTRING(L, "a = test()");
205 TEST_CHECK(policies_test_class::count == 2);
207 DOSTRING(L, "b = a:make('tjosan')");
208 DOSTRING(L, "assert(a:member_out_val(3) == 6)");
209 DOSTRING(L, "a:member_secret()");
211 // make instantiated a new policies_test_class
212 TEST_CHECK(policies_test_class::count == 3);
214 DOSTRING(L, "a:f(b)\n");
216 // b was adopted by c++ and deleted the object
217 TEST_CHECK(policies_test_class::count == 2);
219 DOSTRING(L, "a = nil\n"
220 "collectgarbage()");
222 TEST_CHECK(policies_test_class::count == 1);
224 // adopt with wrappers
225 DOSTRING(L, "mi1():add(mi2())");