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.
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>
36 struct secret_type
{};
41 struct policies_test_class
43 policies_test_class(const char* name
): name_(name
)
45 policies_test_class() { ++count
; }
46 ~policies_test_class()
51 policies_test_class
* make(const char* name
) const
53 return new policies_test_class(name
);
56 void f(policies_test_class
* p
)
60 const policies_test_class
* internal_ref() { return this; }
61 policies_test_class
* self_ref()
67 policies_test_class(policies_test_class
const& c
): name_(c
.name_
)
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_
; }
87 test_t
*make(int) { return new test_t(); }
98 struct MI2
: public MI1
104 struct MI2W
: public MI2
, public luabind::wrap_base
{};
106 void test_main(lua_State
* L
)
108 using namespace luabind
;
112 class_
<test_t
>("test_t")
113 .def("make", &test_t::make
, adopt(return_value
))
114 .def("take", &test_t::take
, adopt(_2
))
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", ©_val
, copy(result
)),
130 def("copy_val_const", ©_val_const
, copy(result
)),
131 def("secret", &secret
, discard_result
),
134 .def(constructor
<>())
135 .def("add",&MI1::add
,adopt(_2
)),
137 class_
<MI2
,MI2W
,MI1
>("mi2")
138 .def(constructor
<>())
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);
155 "collectgarbage()\n");
157 // only the global variable left here
158 TEST_CHECK(policies_test_class::count
== 1);
165 // return_reference_to
172 // a is kept alive as long as b is alive
173 TEST_CHECK(policies_test_class::count
== 2);
179 TEST_CHECK(policies_test_class::count
== 1);
181 DOSTRING(L
, "a = test()");
183 TEST_CHECK(policies_test_class::count
== 2);
186 "b = a:internal_ref()\n"
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
200 TEST_CHECK(policies_test_class::count
== 1);
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"
222 TEST_CHECK(policies_test_class::count
== 1);
224 // adopt with wrappers
225 DOSTRING(L
, "mi1():add(mi2())");