Fixed for VC6.5.
[luabind.git] / test / test_held_type.cpp
blob243dd41df5f4ee4169717c7f8a607052d2e78056
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 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 struct base : counted_type<base>
42 base(): n(4) {}
43 virtual ~base() {}
45 void f(int)
49 int n;
52 // this is here to make sure the pointer offsetting works
53 struct first_base : counted_type<first_base>
55 virtual ~first_base() {}
56 virtual void a() {}
57 int padding;
60 struct derived : first_base, base
62 derived(): n2(7) {}
63 void f() {}
64 int n2;
67 COUNTER_GUARD(first_base);
68 COUNTER_GUARD(base);
70 int feedback = 0;
72 void tester(base* t)
74 if (t->n == 4) feedback = 1;
77 void tester_(derived* t)
79 if (t->n2 == 7) feedback = 2;
82 void tester2(boost::shared_ptr<base> t)
84 if (t->n == 4) feedback = 3;
87 void tester3(boost::shared_ptr<const base> t)
89 if (t->n == 4) feedback = 4;
92 void tester4(const boost::shared_ptr<const base>& t)
94 if (t->n == 4) feedback = 5;
97 void tester5(const boost::shared_ptr<const base>* t)
99 if ((*t)->n == 4) feedback = 6;
102 void tester6(const boost::shared_ptr<base>* t)
104 if ((*t)->n == 4) feedback = 7;
107 void tester7(boost::shared_ptr<base>* t)
109 if ((*t)->n == 4) feedback = 8;
112 boost::shared_ptr<base> tester9()
114 feedback = 9;
115 return boost::shared_ptr<base>(new base());
118 void tester10(boost::shared_ptr<base> const& r)
120 if (r->n == 4) feedback = 10;
123 void tester11(boost::shared_ptr<const base> const& r)
125 if (r->n == 4) feedback = 11;
128 void tester12(boost::shared_ptr<derived> const& r)
130 if (r->n2 == 7) feedback = 12;
133 void test_main(lua_State* L)
135 boost::shared_ptr<base> base_ptr(new base());
137 using namespace luabind;
139 module(L)
141 def("tester", &tester),
142 def("tester", &tester_),
143 def("tester2", &tester2),
144 def("tester3", &tester3),
145 def("tester4", &tester4),
146 def("tester5", &tester5),
147 def("tester6", &tester6),
148 def("tester7", &tester7),
149 def("tester9", &tester9),
150 def("tester10", &tester10),
151 def("tester11", &tester11),
152 def("tester12", &tester12),
154 class_<base, boost::shared_ptr<base> >("base")
155 .def(constructor<>())
156 .def("f", &base::f),
158 class_<derived, base, boost::shared_ptr<base> >("derived")
159 .def(constructor<>())
160 .def("f", &derived::f)
163 object g = globals(L);
164 g["ptr"] = base_ptr;
166 DOSTRING(L, "tester(ptr)");
167 TEST_CHECK(feedback == 1);
169 DOSTRING(L,
170 "a = base()\n"
171 "b = derived()\n");
173 DOSTRING(L, "tester(b)");
174 TEST_CHECK(feedback == 2);
176 DOSTRING(L, "tester(a)");
177 TEST_CHECK(feedback == 1);
179 DOSTRING(L, "tester2(b)");
180 TEST_CHECK(feedback == 3);
182 DOSTRING(L, "tester3(b)");
183 TEST_CHECK(feedback == 4);
185 DOSTRING(L, "tester4(b)");
186 TEST_CHECK(feedback == 5);
188 feedback = 0;
190 DOSTRING(L, "tester4(a)");
191 TEST_CHECK(feedback == 5);
193 DOSTRING(L, "tester10(b)");
194 TEST_CHECK(feedback == 10);
196 DOSTRING(L, "tester11(b)");
197 TEST_CHECK(feedback == 11);
198 /* this test is messed up, shared_ptr<derived> isn't even registered
199 DOSTRING_EXPECTED(
201 , "tester12(b)"
202 , "no match for function call 'tester12' with the parameters (derived)\n"
203 "candidates are:\n"
204 "tester12(const custom&)\n");
206 object nil = globals(L)["non_existing_variable_is_nil"];
207 TEST_CHECK(object_cast<boost::shared_ptr<base> >(nil).get() == 0);
208 TEST_CHECK(object_cast<boost::shared_ptr<const base> >(nil).get() == 0);