Replace ref/unref with luaL_ref/luaL_unref.
[luabind.git] / test / test_adopt.cpp
blobb6d1f433886f42a8ea1e61115a826a9e7dab5a0d
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 test_main(lua_State* L)
44 using namespace luabind;
46 disable_super_deprecation();
48 module(L) [
49 class_<Base, Base_wrap>("Base")
50 .def(constructor<>()),
52 def("take_ownership", &take_ownership, adopt(_1))
55 DOSTRING(L,
56 "x = Base()\n"
59 TEST_CHECK(Base::count == 1);
61 DOSTRING(L,
62 "x = nil\n"
63 "collectgarbage('collect')\n"
66 TEST_CHECK(Base::count == 0);
68 DOSTRING(L,
69 "class 'Derived' (Base)\n"
70 " function Derived:__init()\n"
71 " super()\n"
72 " self.x = Base()\n"
73 " end\n"
76 DOSTRING(L,
77 "x = Derived()\n"
80 TEST_CHECK(Base::count == 2);
82 DOSTRING(L,
83 "x = nil\n"
84 "collectgarbage('collect')\n"
85 "collectgarbage('collect')\n"
88 TEST_CHECK(Base::count == 0);
90 DOSTRING(L,
91 "x = nil\n"
92 "collectgarbage('collect')\n"
95 TEST_CHECK(Base::count == 0);
97 DOSTRING(L,
98 "class 'Derived2' (Derived)\n"
99 " function Derived2:__init()\n"
100 " super()\n"
101 " end\n"
104 DOSTRING(L,
105 "x = Derived2()\n"
108 TEST_CHECK(Base::count == 2);
110 DOSTRING(L,
111 "x = nil\n"
112 "collectgarbage('collect')\n"
113 "collectgarbage('collect')\n"
116 TEST_CHECK(Base::count == 0);
118 DOSTRING(L,
119 "x = Derived()\n"
122 TEST_CHECK(Base::count == 2);
124 DOSTRING(L,
125 "take_ownership(x)\n"
126 "x = nil\n"
127 "collectgarbage('collect')\n"
128 "collectgarbage('collect')\n"
131 TEST_CHECK(Base::count == 2);
133 delete adopted;
135 DOSTRING(L,
136 "collectgarbage('collect')\n"
137 "collectgarbage('collect')\n"
140 TEST_CHECK(Base::count == 0);
142 DOSTRING(L,
143 "x = Derived2()\n"
146 TEST_CHECK(Base::count == 2);
148 DOSTRING(L,
149 "take_ownership(x)\n"
150 "x = nil\n"
151 "collectgarbage('collect')\n"
152 "collectgarbage('collect')\n"
155 TEST_CHECK(Base::count == 2);
157 delete adopted;
159 DOSTRING(L,
160 "collectgarbage('collect')\n"
161 "collectgarbage('collect')\n"
164 TEST_CHECK(Base::count == 0);