Teach adopt() to hold the adopted pointer in custom pointer type.
[luabind.git] / test / test_attributes.cpp
bloba18f1ea387ddee94ed7fcaecfdeb68291d98658a
1 // Copyright (c) 2004 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"
24 #include <luabind/luabind.hpp>
26 struct A
28 int get() const
30 return 5;
34 struct B : A
38 struct C
40 int a;
43 struct property_test : counted_type<property_test>
45 property_test(): o(6), c_ref(&c) {}
46 ~property_test() { c.a = 0; }
48 std::string str_;
49 int a_;
50 float o;
51 signed char b;
52 C c;
53 C* c_ref;
55 void set(int a) { a_ = a; }
56 int get() const { return a_; }
58 void setA(A* a) {}
59 A* getA() const { return 0; }
61 void set_str(const char* str)
62 { str_ = str; }
64 const char* get_str() const
65 { return str_.c_str(); }
68 COUNTER_GUARD(property_test);
70 void free_setter(property_test& p, int a)
71 { p.set(a); }
73 int free_getter(const property_test& p)
74 { return p.get(); }
76 struct borrowed_attribute : counted_type<borrowed_attribute>
80 struct attribute_holder : counted_type<attribute_holder>
82 attribute_holder(borrowed_attribute* b)
83 : borrowed(b)
86 borrowed_attribute* borrowed;
89 void test_main(lua_State* L)
91 using namespace luabind;
93 module(L)
95 class_<property_test>("property")
96 .def(luabind::constructor<>())
97 .def("get", &property_test::get)
98 .property("a", &property_test::get, &property_test::set)
99 .property("str", &property_test::get_str, &property_test::set_str)
100 .property("A", &property_test::getA, &property_test::setA)
101 .def_readonly("o", &property_test::o)
102 .property("free", &free_getter, &free_setter)
103 .def_readwrite("b", &property_test::b)
104 .def_readwrite("c", &property_test::c)
105 .def_readwrite("c_ref", &property_test::c_ref),
107 class_<A>("A")
108 .def(constructor<>())
109 .property("a", &A::get),
111 class_<B, A>("B")
112 .def(constructor<>())
113 .property("x", &A::get),
115 class_<C>("C")
116 .def(constructor<>())
117 .def_readwrite("a", &C::a)
120 module(L) [
121 class_<borrowed_attribute>("borrowed_attribute")
122 .def(constructor<>()),
124 class_<attribute_holder>("attribute_holder")
125 .def(constructor<borrowed_attribute*>())
126 .def_readwrite(
127 "borrowed", &attribute_holder::borrowed, no_dependency)
130 DOSTRING(L, "test = property()\n");
132 DOSTRING(L,
133 "test.a = 5\n"
134 "assert(test.a == 5)");
136 DOSTRING(L, "assert(test.o == 6)");
138 DOSTRING(L,
139 "test.new_member = 'foo'\n"
140 "assert(test.new_member == 'foo')");
142 DOSTRING(L,
143 "property.new_member2 = 'bar'\n"
144 "assert(property.new_member2 == 'bar')");
146 DOSTRING(L,
147 "b = property()\n"
148 "assert(b.new_member2 == 'bar')");
150 DOSTRING(L,
151 "test.str = 'foobar'\n"
152 "assert(test.str == 'foobar')\n");
154 DOSTRING(L,
155 "test.free = 6\n"
156 "assert(test.free == 6)\n");
158 DOSTRING(L,
159 "test.b = 3\n"
160 "assert(test.b == 3)\n");
162 DOSTRING(L, "test.c.a = 1\n"
163 "assert(test.c.a == 1)\n"
164 "assert(test.c_ref.a == 1)");
166 DOSTRING(L, "t1 = property()\n"
167 "c = t1.c\n"
168 "c2 = t1.c_ref\n"
169 "c.a = 1\n"
170 "t1 = nil\n"
171 "collectgarbage()\n"
172 "collectgarbage()\n"
173 "assert(c.a == 1)\n"
174 "assert(c2.a == 1)");
176 DOSTRING(L,
177 "a = B()\n");
179 DOSTRING(L,
180 "assert(a.a == 5)\n");
182 DOSTRING(L,
183 "assert(a.x == 5)\n");
185 DOSTRING(L,
186 "borrowed = borrowed_attribute()\n"
187 "holder = attribute_holder(borrowed)\n"
190 TEST_CHECK(borrowed_attribute::count == 1);
191 TEST_CHECK(attribute_holder::count == 1);
193 DOSTRING(L,
194 "holder.borrowed = borrowed\n"
195 "borrowed2 = holder.borrowed\n"
196 "holder = nil\n"
197 "collectgarbage()\n"
200 TEST_CHECK(borrowed_attribute::count == 1);
201 TEST_CHECK(attribute_holder::count == 0);