Teach adopt() to hold the adopted pointer in custom pointer type.
[luabind.git] / test / test_private_destructors.cpp
blob9855914d18a48bd7645151041f4e67d3891e8c71
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>
8 struct X
10 private:
11 ~X() {}
14 int ptr_count = 0;
16 template <class T>
17 struct ptr
19 ptr()
20 : p(0)
22 ptr_count++;
25 ptr(T* p)
26 : p(p)
28 ptr_count++;
31 ptr(ptr const& other)
32 : p(other.p)
34 ptr_count++;
37 template <class U>
38 ptr(ptr<U> const& other)
39 : p(other.p)
41 ptr_count++;
44 ~ptr()
46 ptr_count--;
49 T* p;
52 template <class T>
53 T* get_pointer(ptr<T> const& x)
55 return x.p;
58 template <class T>
59 ptr<T const>* get_const_holder(ptr<T>*)
61 return 0;
64 void f1(X const&)
67 void f2(X&)
70 void f3(X const*)
73 void f4(X*)
76 void g1(ptr<X> p)
78 TEST_CHECK(ptr_count == (p.p ? 2 : 3));
81 void g2(ptr<X> const& p)
83 TEST_CHECK(ptr_count == (p.p ? 1 : 2));
86 void g3(ptr<X>*)
88 TEST_CHECK(ptr_count == 1);
91 void g4(ptr<X> const*)
93 TEST_CHECK(ptr_count == 1);
96 ptr<X> get()
98 return ptr<X>(new X);
101 void test_main(lua_State* L)
103 using namespace luabind;
105 module(L) [
106 class_<X, ptr<X> >("X"),
108 def("get", &get),
110 def("f1", &f1),
111 def("f2", &f2),
112 def("f3", &f3),
113 def("f4", &f4),
115 def("g1", &g1),
116 def("g2", &g2),
117 def("g3", &g3),
118 def("g4", &g4)
121 DOSTRING(L, "x = get()\n");
122 TEST_CHECK(ptr_count == 1);
124 DOSTRING(L, "f1(x)\n");
125 TEST_CHECK(ptr_count == 1);
127 DOSTRING(L, "f2(x)\n");
128 TEST_CHECK(ptr_count == 1);
130 DOSTRING(L, "f3(x)\n");
131 TEST_CHECK(ptr_count == 1);
133 DOSTRING(L, "f4(x)\n");
134 TEST_CHECK(ptr_count == 1);
136 DOSTRING(L, "g1(x)\n");
137 TEST_CHECK(ptr_count == 1);
139 DOSTRING(L, "g2(x)\n");
140 TEST_CHECK(ptr_count == 1);
142 DOSTRING(L, "g3(x)\n");
143 TEST_CHECK(ptr_count == 1);
145 DOSTRING(L, "g4(x)\n");
146 TEST_CHECK(ptr_count == 1);
148 DOSTRING(L,
149 "x = nil\n"
152 lua_gc(L, LUA_GCCOLLECT, 0);
153 TEST_CHECK(ptr_count == 0);