Merge branch '0.8' to sync with changes made after 0.8.1 release.
[luabind.git] / test / test_adopt.cpp
blob99746e82f59c60c4919fba3a238e8206cce11e18
1 // Copyright Daniel Wallin 2008. Use, modification and distribution is
2 // subject to the Boost Software License, Version 1.0. (See accompanying
3 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 #include "test.hpp"
6 #include <luabind/luabind.hpp>
7 #include <luabind/adopt_policy.hpp>
8 #include <iostream>
10 struct Base
12 Base()
14 count++;
17 virtual ~Base()
19 count--;
22 static int count;
25 int Base::count = 0;
27 struct Base_wrap : Base, luabind::wrap_base
28 {};
30 void destroy(Base* p)
32 delete p;
35 Base* adopted = 0;
37 void take_ownership(Base* p)
39 adopted = p;
42 void not_null(Base* p)
44 TEST_CHECK(p);
47 void test_main(lua_State* L)
49 using namespace luabind;
51 disable_super_deprecation();
53 module(L) [
54 class_<Base, Base_wrap>("Base")
55 .def(constructor<>()),
57 def("take_ownership", &take_ownership, adopt(_1)),
58 def("not_null", &not_null)
61 DOSTRING(L,
62 "x = Base()\n"
65 TEST_CHECK(Base::count == 1);
67 DOSTRING(L,
68 "x = nil\n"
69 "collectgarbage('collect')\n"
72 TEST_CHECK(Base::count == 0);
74 DOSTRING(L,
75 "class 'Derived' (Base)\n"
76 " function Derived:__init()\n"
77 " super()\n"
78 " self.x = Base()\n"
79 " end\n"
82 DOSTRING(L,
83 "x = Derived()\n"
86 TEST_CHECK(Base::count == 2);
88 DOSTRING(L,
89 "x = nil\n"
90 "collectgarbage('collect')\n"
91 "collectgarbage('collect')\n"
94 TEST_CHECK(Base::count == 0);
96 DOSTRING(L,
97 "x = nil\n"
98 "collectgarbage('collect')\n"
101 TEST_CHECK(Base::count == 0);
103 DOSTRING(L,
104 "class 'Derived2' (Derived)\n"
105 " function Derived2:__init()\n"
106 " super()\n"
107 " end\n"
110 DOSTRING(L,
111 "x = Derived2()\n"
114 TEST_CHECK(Base::count == 2);
116 DOSTRING(L,
117 "x = nil\n"
118 "collectgarbage('collect')\n"
119 "collectgarbage('collect')\n"
122 TEST_CHECK(Base::count == 0);
124 DOSTRING(L,
125 "x = Derived()\n"
128 TEST_CHECK(Base::count == 2);
130 DOSTRING(L,
131 "take_ownership(x)\n"
132 "x = nil\n"
133 "collectgarbage('collect')\n"
134 "collectgarbage('collect')\n"
137 TEST_CHECK(Base::count == 2);
139 delete adopted;
141 DOSTRING(L,
142 "collectgarbage('collect')\n"
143 "collectgarbage('collect')\n"
146 TEST_CHECK(Base::count == 0);
148 DOSTRING(L,
149 "x = Derived2()\n"
152 TEST_CHECK(Base::count == 2);
154 DOSTRING(L,
155 "take_ownership(x)\n"
156 "x = nil\n"
157 "collectgarbage('collect')\n"
158 "collectgarbage('collect')\n"
161 TEST_CHECK(Base::count == 2);
163 delete adopted;
165 DOSTRING(L,
166 "collectgarbage('collect')\n"
167 "collectgarbage('collect')\n"
170 TEST_CHECK(Base::count == 0);
172 DOSTRING(L,
173 "x = Derived()\n"
174 "take_ownership(x)\n"
175 "not_null(x)\n"
178 delete adopted;
180 DOSTRING(L,
181 "x = nil\n"
182 "collectgarbage('collect')\n"
185 TEST_CHECK(Base::count == 0);