Add missing index_tuple.hpp header.
[luabind.git] / test / test_held_type.cpp
blobb5cc78d2c3ae3e2e0d8564d603fdd367d0f50e34
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 <luabind/version.hpp>
26 #include <boost/shared_ptr.hpp>
27 #include <memory>
29 namespace luabind {
31 #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
32 template<class T>
33 T* get_pointer(boost::shared_ptr<T> const& p) { return p.get(); }
34 #endif
38 struct base : counted_type<base>
40 base(): n(4) {}
41 virtual ~base() {}
43 void f(int)
47 int n;
50 // this is here to make sure the pointer offsetting works
51 struct first_base : counted_type<first_base>
53 virtual ~first_base() {}
54 virtual void a() {}
55 int padding;
58 struct derived : first_base, base
60 derived(): n2(7) {}
61 void f() {}
62 int n2;
65 COUNTER_GUARD(first_base);
66 COUNTER_GUARD(base);
68 int feedback = 0;
70 void tester(base* t)
72 if (t->n == 4) feedback = 1;
75 void tester_(derived* t)
77 if (t->n2 == 7) feedback = 2;
80 void tester2(boost::shared_ptr<base> t)
82 if (t->n == 4) feedback = 3;
85 void tester3(boost::shared_ptr<const base> t)
87 if (t->n == 4) feedback = 4;
90 void tester4(const boost::shared_ptr<const base>& t)
92 if (t->n == 4) feedback = 5;
95 void tester5(const boost::shared_ptr<const base>* t)
97 if ((*t)->n == 4) feedback = 6;
100 void tester6(const boost::shared_ptr<base>* t)
102 if ((*t)->n == 4) feedback = 7;
105 void tester7(boost::shared_ptr<base>* t)
107 if ((*t)->n == 4) feedback = 8;
110 boost::shared_ptr<base> tester9()
112 feedback = 9;
113 return boost::shared_ptr<base>(new base());
116 void tester10(boost::shared_ptr<base> const& r)
118 if (r->n == 4) feedback = 10;
121 void tester11(boost::shared_ptr<const base> const& r)
123 if (r->n == 4) feedback = 11;
126 void tester12(boost::shared_ptr<derived> const& r)
128 if (r->n2 == 7) feedback = 12;
131 derived tester13()
133 feedback = 13;
134 derived d;
135 d.n2 = 13;
136 return d;
139 void test_main(lua_State* L)
141 boost::shared_ptr<base> base_ptr(new base());
143 using namespace luabind;
145 module(L)
147 def("tester", &tester),
148 def("tester", &tester_),
149 def("tester2", &tester2),
150 def("tester3", &tester3),
151 def("tester4", &tester4),
152 def("tester5", &tester5),
153 def("tester6", &tester6),
154 def("tester7", &tester7),
155 def("tester9", &tester9),
156 def("tester10", &tester10),
157 def("tester11", &tester11),
158 def("tester12", &tester12),
159 def("tester13", &tester13),
161 class_<base, boost::shared_ptr<base> >("base")
162 .def(constructor<>())
163 .def("f", &base::f),
165 class_<derived, base, boost::shared_ptr<base> >("derived")
166 .def(constructor<>())
167 .def("f", &derived::f)
170 object g = globals(L);
171 g["ptr"] = base_ptr;
173 DOSTRING(L, "tester(ptr)");
174 TEST_CHECK(feedback == 1);
176 DOSTRING(L,
177 "a = base()\n"
178 "b = derived()\n");
180 #if LUABIND_VERSION != 900
181 DOSTRING(L, "tester(b)");
182 TEST_CHECK(feedback == 2);
183 #endif
185 DOSTRING(L, "tester(a)");
186 TEST_CHECK(feedback == 1);
188 DOSTRING(L, "tester2(b)");
189 TEST_CHECK(feedback == 3);
191 feedback = 0;
193 DOSTRING(L, "tester10(b)");
194 TEST_CHECK(feedback == 10);
196 /* this test is messed up, shared_ptr<derived> isn't even registered
197 DOSTRING_EXPECTED(
199 , "tester12(b)"
200 , "no match for function call 'tester12' with the parameters (derived)\n"
201 "candidates are:\n"
202 "tester12(const custom&)\n");
204 #if LUABIND_VERSION != 900
205 object nil = globals(L)["non_existing_variable_is_nil"];
206 TEST_CHECK(object_cast<boost::shared_ptr<base> >(nil).get() == 0);
207 TEST_CHECK(object_cast<boost::shared_ptr<const base> >(nil).get() == 0);
208 #endif
210 DOSTRING(L, "tester13()");
211 TEST_CHECK(feedback == 13);