Added `get_const_holder()` overload for `boost::shared_ptr`.
[luabind.git] / test / test_held_type.cpp
blobe049002845423b399644a727480e5d442387a5c4
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
37 struct base : counted_type<base>
39 base(): n(4) {}
40 virtual ~base() {}
42 void f(int)
46 int n;
49 // this is here to make sure the pointer offsetting works
50 struct first_base : counted_type<first_base>
52 virtual ~first_base() {}
53 virtual void a() {}
54 int padding;
57 struct derived : first_base, base
59 derived(): n2(7) {}
60 void f() {}
61 int n2;
64 COUNTER_GUARD(first_base);
65 COUNTER_GUARD(base);
67 int feedback = 0;
69 void tester(base* t)
71 if (t->n == 4) feedback = 1;
74 void tester_(derived* t)
76 if (t->n2 == 7) feedback = 2;
79 void tester2(boost::shared_ptr<base> t)
81 if (t->n == 4) feedback = 3;
84 void tester3(boost::shared_ptr<const base> t)
86 if (t->n == 4) feedback = 4;
89 void tester4(const boost::shared_ptr<const base>& t)
91 if (t->n == 4) feedback = 5;
94 void tester5(const boost::shared_ptr<const base>* t)
96 if ((*t)->n == 4) feedback = 6;
99 void tester6(const boost::shared_ptr<base>* t)
101 if ((*t)->n == 4) feedback = 7;
104 void tester7(boost::shared_ptr<base>* t)
106 if ((*t)->n == 4) feedback = 8;
109 boost::shared_ptr<base> tester9()
111 feedback = 9;
112 return boost::shared_ptr<base>(new base());
115 void tester10(boost::shared_ptr<base> const& r)
117 if (r->n == 4) feedback = 10;
120 void tester11(boost::shared_ptr<const base> const& r)
122 if (r->n == 4) feedback = 11;
125 void tester12(boost::shared_ptr<derived> const& r)
127 if (r->n2 == 7) feedback = 12;
130 void test_main(lua_State* L)
132 boost::shared_ptr<base> base_ptr(new base());
134 using namespace luabind;
136 module(L)
138 def("tester", &tester),
139 def("tester", &tester_),
140 def("tester2", &tester2),
141 def("tester3", &tester3),
142 def("tester4", &tester4),
143 def("tester5", &tester5),
144 def("tester6", &tester6),
145 def("tester7", &tester7),
146 def("tester9", &tester9),
147 def("tester10", &tester10),
148 def("tester11", &tester11),
149 def("tester12", &tester12),
151 class_<base, boost::shared_ptr<base> >("base")
152 .def(constructor<>())
153 .def("f", &base::f),
155 class_<derived, base, boost::shared_ptr<base> >("derived")
156 .def(constructor<>())
157 .def("f", &derived::f)
160 object g = globals(L);
161 g["ptr"] = base_ptr;
163 DOSTRING(L, "tester(ptr)");
164 TEST_CHECK(feedback == 1);
166 DOSTRING(L,
167 "a = base()\n"
168 "b = derived()\n");
170 DOSTRING(L, "tester(b)");
171 TEST_CHECK(feedback == 2);
173 DOSTRING(L, "tester(a)");
174 TEST_CHECK(feedback == 1);
176 DOSTRING(L, "tester2(b)");
177 TEST_CHECK(feedback == 3);
179 DOSTRING(L, "tester3(b)");
180 TEST_CHECK(feedback == 4);
182 DOSTRING(L, "tester4(b)");
183 TEST_CHECK(feedback == 5);
185 feedback = 0;
187 DOSTRING(L, "tester4(a)");
188 TEST_CHECK(feedback == 5);
190 DOSTRING(L, "tester10(b)");
191 TEST_CHECK(feedback == 10);
193 DOSTRING(L, "tester11(b)");
194 TEST_CHECK(feedback == 11);
195 /* this test is messed up, shared_ptr<derived> isn't even registered
196 DOSTRING_EXPECTED(
198 , "tester12(b)"
199 , "no match for function call 'tester12' with the parameters (derived)\n"
200 "candidates are:\n"
201 "tester12(const custom&)\n");
203 object nil = globals(L)["non_existing_variable_is_nil"];
204 TEST_CHECK(object_cast<boost::shared_ptr<base> >(nil).get() == 0);
205 TEST_CHECK(object_cast<boost::shared_ptr<const base> >(nil).get() == 0);