doc tweaks
[luabind.git] / test / test_held_type.cpp
blobe9b5e230d22d2d011288760091fd1abddd2e7476
1 // Copyright (c) 2004 Daniel Wallin
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>
25 #include <boost/shared_ptr.hpp>
26 #include <memory>
28 namespace luabind {
30 template<class T>
31 T* get_pointer(boost::shared_ptr<T>& p) { return p.get(); }
33 template<class A>
34 boost::shared_ptr<const A>* get_const_holder(boost::shared_ptr<A>*)
36 return 0;
40 namespace {
42 struct base : counted_type<base>
44 base(): n(4) {}
45 virtual ~base() {}
47 void f(int)
51 int n;
54 // this is here to make sure the pointer offsetting works
55 struct first_base : counted_type<first_base>
57 ~first_base() {}
58 virtual void a() {}
59 int padding;
62 struct derived : first_base, base
64 derived(): n2(7) {}
65 void f() {}
66 int n2;
69 int feedback = 0;
71 void tester(base* t)
73 if (t->n == 4) feedback = 1;
76 void tester_(derived* t)
78 if (t->n2 == 7) feedback = 2;
81 void tester2(boost::shared_ptr<base> t)
83 if (t->n == 4) feedback = 3;
86 void tester3(boost::shared_ptr<const base> t)
88 if (t->n == 4) feedback = 4;
91 void tester4(const boost::shared_ptr<const base>& t)
93 if (t->n == 4) feedback = 5;
96 void tester5(const boost::shared_ptr<const base>* t)
98 if ((*t)->n == 4) feedback = 6;
101 void tester6(const boost::shared_ptr<base>* t)
103 if ((*t)->n == 4) feedback = 7;
106 void tester7(boost::shared_ptr<base>* t)
108 if ((*t)->n == 4) feedback = 8;
111 boost::shared_ptr<base> tester9()
113 feedback = 9;
114 return boost::shared_ptr<base>(new base());
117 } // namespace unnamed
119 void test_held_type()
121 COUNTER_GUARD(first_base);
122 COUNTER_GUARD(base);
124 boost::shared_ptr<base> base_ptr(new base());
126 lua_state L;
128 using namespace luabind;
130 module(L)
132 def("tester", &tester),
133 def("tester", &tester_),
134 def("tester2", &tester2),
135 def("tester3", &tester3),
136 def("tester4", &tester4),
137 def("tester5", &tester5),
138 def("tester6", &tester6),
139 def("tester7", &tester7),
140 def("tester9", &tester9),
142 class_<base, boost::shared_ptr<base> >("base")
143 .def(constructor<>())
144 .def("f", &base::f),
146 class_<derived, base, boost::shared_ptr<base> >("derived")
147 .def(constructor<>())
148 .def("f", &derived::f)
151 object g = get_globals(L);
152 g["ptr"] = base_ptr;
154 DOSTRING(L, "tester(ptr)");
155 BOOST_CHECK(feedback == 1);
157 DOSTRING(L,
158 "a = base()\n"
159 "b = derived()\n");
161 DOSTRING(L, "tester(b)");
162 BOOST_CHECK(feedback == 2);
164 DOSTRING(L, "tester(a)");
165 BOOST_CHECK(feedback == 1);
167 DOSTRING(L, "tester2(b)");
168 BOOST_CHECK(feedback == 3);
170 DOSTRING(L, "tester3(b)");
171 BOOST_CHECK(feedback == 4);
173 DOSTRING(L, "tester4(b)");
174 BOOST_CHECK(feedback == 5);
176 feedback = 0;
178 DOSTRING(L, "tester4(a)");
179 BOOST_CHECK(feedback == 5);