Teach adopt() to hold the adopted pointer in custom pointer type.
[luabind.git] / test / test_virtual_inheritance.cpp
blob3087025db93ffd4825a5d500061e8f4d1df40de4
1 // Copyright Daniel Wallin 2009. Use, modification and distribution is
2 // subject to the Boost Software License, Version 1.0. (See accompanying
3 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 #include "test.hpp"
6 #include <luabind/luabind.hpp>
8 // Test the following hierarchy:
9 //
10 // X
11 // / \.
12 // Y Z
13 // \ /
14 // U
16 struct X
18 X(int x)
19 : value(x)
22 virtual ~X()
25 int f() const
27 return value;
30 int value;
33 struct Y : virtual X
35 Y(int value)
36 : X(value)
39 int g() const
41 return 2;
44 int dummy;
47 struct Z : virtual X
49 Z(int value)
50 : X(value)
53 int h() const
55 return 3;
58 int dummy;
61 struct U : Y, Z
63 U(int value)
64 : X(value)
65 , Y(value)
66 , Z(value)
69 int dummy;
72 X* upcast(U* p)
74 return p;
77 // This hiearchy tries to prove that conversion and caching works for all sub
78 // object pointers:
81 // Base Base
82 // | |
83 // Left Right
84 // \ /
85 // \ /
86 // Derived
89 struct Base
91 Base(int value)
92 : value(value)
95 virtual ~Base()
98 int value;
101 struct Left : Base
103 Left()
104 : Base(1)
107 int left() const
109 return value;
112 int dummy;
115 struct Right : Base
117 Right()
118 : Base(2)
121 int right() const
123 return value;
126 int dummy;
129 struct Derived : Left, Right
131 int f() const
133 return 3;
136 int dummy;
139 Base* left(Left* p)
141 return p;
144 Base* right(Right* p)
146 return p;
149 void test_main(lua_State* L)
151 using namespace luabind;
153 module(L) [
154 class_<X>("X")
155 .def("f", &X::f),
156 class_<Y, X>("Y")
157 .def("g", &Y::g),
158 class_<Z, X>("Z")
159 .def("h", &Z::h),
160 class_<U, bases<Y, Z> >("U")
161 .def(constructor<int>()),
162 def("upcast", &upcast)
165 // Do everything twice to verify that caching works.
167 DOSTRING(L,
168 "function assert2(x)\n"
169 " assert(x)\n"
170 " assert(x)\n"
171 "end\n"
174 DOSTRING(L,
175 "x = U(1)\n"
176 "assert2(x:f() == 1)\n"
177 "assert2(x:g() == 2)\n"
178 "assert2(x:h() == 3)\n"
181 DOSTRING(L,
182 "y = upcast(x)\n"
183 "assert2(y:f() == 1)\n"
184 "assert2(y:g() == 2)\n"
185 "assert2(y:h() == 3)\n"
188 module(L) [
189 class_<Base>("Base"),
190 class_<Left, Base>("Left")
191 .def("left", &Left::left),
192 class_<Right, Base>("Right")
193 .def("right", &Right::right),
194 class_<Derived, bases<Left, Right> >("Derived")
195 .def(constructor<>())
196 .def("f", &Derived::f),
197 def("left", &left),
198 def("right", &right)
201 DOSTRING(L,
202 "x = Derived()\n"
203 "assert2(x:left() == 1)\n"
204 "assert2(x:right() == 2)\n"
205 "assert2(x:f() == 3)\n"
208 DOSTRING(L,
209 "y = left(x)\n"
210 "assert2(y:left() == 1)\n"
211 "assert2(y:right() == 2)\n"
212 "assert2(y:f() == 3)\n"
215 DOSTRING(L,
216 "y = right(x)\n"
217 "assert2(y:left() == 1)\n"
218 "assert2(y:right() == 2)\n"
219 "assert2(y:f() == 3)\n"