Branched for 0.7.1 release.
[luabind.git] / test / test_held_type.cpp
blob587f6b7abefa6329d291f63ef53437666cfaf698
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>
25 #include <boost/shared_ptr.hpp>
26 #include <memory>
28 namespace luabind {
30 #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
31 template<class T>
32 T* get_pointer(boost::shared_ptr<T> const& p) { return p.get(); }
33 #endif
35 template<class A>
36 boost::shared_ptr<const A>* get_const_holder(boost::shared_ptr<A> const*)
38 return 0;
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 virtual ~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 COUNTER_GUARD(first_base);
70 COUNTER_GUARD(base);
72 int feedback = 0;
74 void tester(base* t)
76 if (t->n == 4) feedback = 1;
79 void tester_(derived* t)
81 if (t->n2 == 7) feedback = 2;
84 void tester2(boost::shared_ptr<base> t)
86 if (t->n == 4) feedback = 3;
89 void tester3(boost::shared_ptr<const base> t)
91 if (t->n == 4) feedback = 4;
94 void tester4(const boost::shared_ptr<const base>& t)
96 if (t->n == 4) feedback = 5;
99 void tester5(const boost::shared_ptr<const base>* t)
101 if ((*t)->n == 4) feedback = 6;
104 void tester6(const boost::shared_ptr<base>* t)
106 if ((*t)->n == 4) feedback = 7;
109 void tester7(boost::shared_ptr<base>* t)
111 if ((*t)->n == 4) feedback = 8;
114 boost::shared_ptr<base> tester9()
116 feedback = 9;
117 return boost::shared_ptr<base>(new base());
120 void tester10(boost::shared_ptr<base> const& r)
122 if (r->n == 4) feedback = 10;
125 void tester11(boost::shared_ptr<const base> const& r)
127 if (r->n == 4) feedback = 11;
130 void tester12(boost::shared_ptr<derived> const& r)
132 if (r->n2 == 7) feedback = 12;
135 void test_main(lua_State* L)
137 boost::shared_ptr<base> base_ptr(new base());
139 using namespace luabind;
141 module(L)
143 def("tester", &tester),
144 def("tester", &tester_),
145 def("tester2", &tester2),
146 def("tester3", &tester3),
147 def("tester4", &tester4),
148 def("tester5", &tester5),
149 def("tester6", &tester6),
150 def("tester7", &tester7),
151 def("tester9", &tester9),
152 def("tester10", &tester10),
153 def("tester11", &tester11),
154 def("tester12", &tester12),
156 class_<base, boost::shared_ptr<base> >("base")
157 .def(constructor<>())
158 .def("f", &base::f),
160 class_<derived, base, boost::shared_ptr<base> >("derived")
161 .def(constructor<>())
162 .def("f", &derived::f)
165 object g = globals(L);
166 g["ptr"] = base_ptr;
168 DOSTRING(L, "tester(ptr)");
169 TEST_CHECK(feedback == 1);
171 DOSTRING(L,
172 "a = base()\n"
173 "b = derived()\n");
175 DOSTRING(L, "tester(b)");
176 TEST_CHECK(feedback == 2);
178 DOSTRING(L, "tester(a)");
179 TEST_CHECK(feedback == 1);
181 DOSTRING(L, "tester2(b)");
182 TEST_CHECK(feedback == 3);
184 DOSTRING(L, "tester3(b)");
185 TEST_CHECK(feedback == 4);
187 DOSTRING(L, "tester4(b)");
188 TEST_CHECK(feedback == 5);
190 feedback = 0;
192 DOSTRING(L, "tester4(a)");
193 TEST_CHECK(feedback == 5);
195 DOSTRING(L, "tester10(b)");
196 TEST_CHECK(feedback == 10);
198 DOSTRING(L, "tester11(b)");
199 TEST_CHECK(feedback == 11);
200 /* this test is messed up, shared_ptr<derived> isn't even registered
201 DOSTRING_EXPECTED(
203 , "tester12(b)"
204 , "no match for function call 'tester12' with the parameters (derived)\n"
205 "candidates are:\n"
206 "tester12(const custom&)\n");
208 object nil = globals(L)["non_existing_variable_is_nil"];
209 TEST_CHECK(object_cast<boost::shared_ptr<base> >(nil).get() == 0);
210 TEST_CHECK(object_cast<boost::shared_ptr<const base> >(nil).get() == 0);