Disabled dll-interface warning on MSVC.
[luabind.git] / test / test_policies.cpp
blob13bafc138379cd9e1c7798acb59dfda83aa36f9b
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; }
81 secret_type* secret() { return &sec_; }
83 void aux_test();
85 struct test_t {
86 test_t *make(int) { return new test_t(); }
87 void take(test_t*) {}
90 struct MI2;
92 struct MI1
94 void add(MI2 *) {}
97 struct MI2 : public MI1 {};
98 struct MI2W : public MI2, public luabind::wrap_base {};
100 void test_main(lua_State* L)
102 using namespace luabind;
104 module(L)
106 class_<test_t>("test_t")
107 .def("make", &test_t::make, adopt(return_value))
108 .def("take", &test_t::take, adopt(_2))
111 module(L)
113 class_<policies_test_class>("test")
114 .def(constructor<>())
115 .def("member_out_val", &policies_test_class::member_out_val, pure_out_value(_3))
116 .def("member_secret", &policies_test_class::member_secret, discard_result)
117 .def("f", &policies_test_class::f, adopt(_2))
118 .def("make", &policies_test_class::make, adopt(return_value))
119 .def("internal_ref", &policies_test_class::internal_ref, dependency(result, _1))
120 .def("self_ref", &policies_test_class::self_ref, return_reference_to(_1)),
122 def("out_val", &out_val, pure_out_value(_1)),
123 def("copy_val", &copy_val, copy(result)),
124 def("secret", &secret, discard_result),
126 class_<MI1>("mi1")
127 .def(constructor<>())
128 .def("add",&MI1::add,adopt(_2)),
130 class_<MI2,MI2W,MI1>("mi2")
131 .def(constructor<>())
134 // test copy
135 DOSTRING(L, "a = secret()\n");
137 TEST_CHECK(policies_test_class::count == 1);
139 DOSTRING(L, "a = copy_val()\n");
141 TEST_CHECK(policies_test_class::count == 2);
143 DOSTRING(L,
144 "a = nil\n"
145 "collectgarbage()\n");
147 // only the global variable left here
148 TEST_CHECK(policies_test_class::count == 1);
150 // out_value
151 DOSTRING(L,
152 "a = out_val()\n"
153 "assert(a == 3)");
155 // return_reference_to
156 DOSTRING(L,
157 "a = test()\n"
158 "b = a:self_ref()\n"
159 "a = nil\n"
160 "collectgarbage()");
162 // a is kept alive as long as b is alive
163 TEST_CHECK(policies_test_class::count == 2);
165 DOSTRING(L,
166 "b = nil\n"
167 "collectgarbage()");
169 TEST_CHECK(policies_test_class::count == 1);
171 DOSTRING(L, "a = test()");
173 TEST_CHECK(policies_test_class::count == 2);
175 DOSTRING(L,
176 "b = a:internal_ref()\n"
177 "a = nil\n"
178 "collectgarbage()");
180 // a is kept alive as long as b is alive
181 TEST_CHECK(policies_test_class::count == 2);
183 // two gc-cycles because dependency-table won't be collected in the
184 // same cycle as the object_rep
185 DOSTRING(L,
186 "b = nil\n"
187 "collectgarbage()\n"
188 "collectgarbage()");
190 TEST_CHECK(policies_test_class::count == 1);
192 // adopt
193 DOSTRING(L, "a = test()");
195 TEST_CHECK(policies_test_class::count == 2);
197 DOSTRING(L, "b = a:make('tjosan')");
198 DOSTRING(L, "assert(a:member_out_val(3) == 6)");
199 DOSTRING(L, "a:member_secret()");
201 // make instantiated a new policies_test_class
202 TEST_CHECK(policies_test_class::count == 3);
204 DOSTRING(L, "a:f(b)\n");
206 // b was adopted by c++ and deleted the object
207 TEST_CHECK(policies_test_class::count == 2);
209 DOSTRING(L, "a = nil\n"
210 "collectgarbage()");
212 TEST_CHECK(policies_test_class::count == 1);
214 // adopt with wrappers
215 DOSTRING(L, "mi1():add(mi2())");