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.
24 #include <luabind/luabind.hpp>
43 struct property_test
: counted_type
<property_test
>
45 property_test(): o(6), c_ref(&c
) {}
46 ~property_test() { c
.a
= 0; }
55 void set(int a
) { a_
= a
; }
56 int get() const { return a_
; }
59 A
* getA() const { return 0; }
61 void set_str(const char* 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
)
73 int free_getter(const property_test
& p
)
76 void test_main(lua_State
* L
)
78 using namespace luabind
;
82 class_
<property_test
>("property")
83 .def(luabind::constructor
<>())
84 .def("get", &property_test::get
)
85 .property("a", &property_test::get
, &property_test::set
)
86 .property("str", &property_test::get_str
, &property_test::set_str
)
87 .property("A", &property_test::getA
, &property_test::setA
)
88 .def_readonly("o", &property_test::o
)
89 .property("free", &free_getter
, &free_setter
)
90 .def_readwrite("b", &property_test::b
)
91 .def_readwrite("c", &property_test::c
)
92 .def_readwrite("c_ref", &property_test::c_ref
),
96 .property("a", &A::get
),
100 .property("x", &A::get
),
103 .def(constructor
<>())
104 .def_readwrite("a", &C::a
)
107 DOSTRING(L
, "test = property()\n");
111 "assert(test.a == 5)");
113 DOSTRING(L
, "assert(test.o == 6)");
116 "test.new_member = 'foo'\n"
117 "assert(test.new_member == 'foo')");
120 "property.new_member2 = 'bar'\n"
121 "assert(property.new_member2 == 'bar')");
125 "assert(b.new_member2 == 'bar')");
128 "test.str = 'foobar'\n"
129 "assert(test.str == 'foobar')\n");
133 "assert(test.free == 6)\n");
137 "assert(test.b == 3)\n");
139 DOSTRING(L
, "test.c.a = 1\n"
140 "assert(test.c.a == 1)\n"
141 "assert(test.c_ref.a == 1)");
143 DOSTRING(L
, "t1 = property()\n"
151 "assert(c2.a == 1)");
157 "assert(a.a == 5)\n");
160 "assert(a.x == 5)\n");