1 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
---|
2 | // test_smart_cast.cpp: |
---|
3 | |
---|
4 | // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . |
---|
5 | // Use, modification and distribution is subject to the Boost Software |
---|
6 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
7 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
8 | // <gennadiy.rozental@tfn.com> |
---|
9 | |
---|
10 | #include <exception> |
---|
11 | #include <boost/smart_cast.hpp> |
---|
12 | |
---|
13 | #include <boost/test/test_tools.hpp> |
---|
14 | #include <boost/noncopyable.hpp> |
---|
15 | |
---|
16 | using namespace boost; |
---|
17 | |
---|
18 | class Base1 : public boost::noncopyable |
---|
19 | { |
---|
20 | char a; |
---|
21 | }; |
---|
22 | |
---|
23 | class Base2 |
---|
24 | { |
---|
25 | int b; |
---|
26 | }; |
---|
27 | |
---|
28 | class Derived : public Base1, public Base2 |
---|
29 | { |
---|
30 | long c; |
---|
31 | }; |
---|
32 | |
---|
33 | BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(Base1) |
---|
34 | BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(Base2) |
---|
35 | BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(Derived) |
---|
36 | |
---|
37 | // if compiler doesn't support TPS, the smart_cast syntax doesn't |
---|
38 | // work for references. One has to use the smart_cast_reference |
---|
39 | // syntax (tested below ) instead. |
---|
40 | |
---|
41 | void test_static_reference_cast_2(){ |
---|
42 | Derived d; |
---|
43 | Base1 & b1 = static_cast<Base1 &>(d); |
---|
44 | Base2 & b2 = static_cast<Base2 &>(d); |
---|
45 | |
---|
46 | Base1 & scb1 = smart_cast<Base1 &, Derived &>(d); |
---|
47 | Base2 & scb2 = smart_cast<Base2 &, Derived &>(d); |
---|
48 | BOOST_CHECK_EQUAL(& b1, & scb1); |
---|
49 | BOOST_CHECK_EQUAL(& b2, & scb2); |
---|
50 | |
---|
51 | // downcast |
---|
52 | // BOOST_CHECK_EQUAL(& d, & (smart_cast<Derived &, Base1 &>(b1))); |
---|
53 | // BOOST_CHECK_EQUAL(& d, & (smart_cast<Derived &, Base2 &>(b2))); |
---|
54 | |
---|
55 | // crosscast pointers fails at compiler time |
---|
56 | // BOOST_CHECK_EQUAL(pB2,smart_cast<B2 *>(pB1)); |
---|
57 | // though explicit cross cast will always work |
---|
58 | BOOST_CHECK_EQUAL(& b2,( |
---|
59 | & smart_cast<Base2 &, Derived &>( |
---|
60 | smart_cast<Derived &, Base1 &>(b1) |
---|
61 | )) |
---|
62 | ); |
---|
63 | } |
---|
64 | |
---|
65 | void test_static_reference_cast_1(){ |
---|
66 | Derived d; |
---|
67 | Base1 & b1 = static_cast<Base1 &>(d); |
---|
68 | Base2 & b2 = static_cast<Base2 &>(d); |
---|
69 | |
---|
70 | Base1 & scb1 = smart_cast_reference<Base1 &>(d); |
---|
71 | Base2 & scb2 = smart_cast_reference<Base2 &>(d); |
---|
72 | BOOST_CHECK_EQUAL(& b1, & scb1); |
---|
73 | BOOST_CHECK_EQUAL(& b2, & scb2); |
---|
74 | |
---|
75 | // downcast |
---|
76 | BOOST_CHECK_EQUAL(& d, & (smart_cast_reference<Derived &>(b1))); |
---|
77 | BOOST_CHECK_EQUAL(& d, & (smart_cast_reference<Derived &>(b2))); |
---|
78 | |
---|
79 | // crosscast pointers fails at compiler time |
---|
80 | // BOOST_CHECK_EQUAL(pB2,smart_cast<B2 *>(pB1)); |
---|
81 | // though explicit cross cast will always work |
---|
82 | BOOST_CHECK_EQUAL(& b2,( |
---|
83 | & smart_cast_reference<Base2 &>( |
---|
84 | smart_cast_reference<Derived &>(b1) |
---|
85 | )) |
---|
86 | ); |
---|
87 | } |
---|
88 | |
---|
89 | void test_static_pointer_cast(){ |
---|
90 | // pointers |
---|
91 | Derived d; |
---|
92 | Derived *pD = & d; |
---|
93 | Base1 *pB1 = pD; |
---|
94 | Base2 *pB2 = pD; |
---|
95 | |
---|
96 | // upcast |
---|
97 | BOOST_CHECK_EQUAL(pB1, smart_cast<Base1 *>(pD)); |
---|
98 | BOOST_CHECK_EQUAL(pB2, smart_cast<Base2 *>(pD)); |
---|
99 | |
---|
100 | // downcast |
---|
101 | BOOST_CHECK_EQUAL(pD, smart_cast<Derived *>(pB1)); |
---|
102 | BOOST_CHECK_EQUAL(pD, smart_cast<Derived *>(pB2)); |
---|
103 | |
---|
104 | // crosscast pointers fails at compiler time |
---|
105 | // BOOST_CHECK_EQUAL(pB2, smart_cast<Base2 *>(pB1)); |
---|
106 | |
---|
107 | // though explicit cross cast will always work |
---|
108 | BOOST_CHECK_EQUAL(pB2, |
---|
109 | smart_cast<Base2 *>( |
---|
110 | smart_cast<Derived *>(pB1) |
---|
111 | ) |
---|
112 | ); |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | class VBase1 : public boost::noncopyable |
---|
117 | { |
---|
118 | char a; |
---|
119 | public: |
---|
120 | virtual ~VBase1(){}; |
---|
121 | }; |
---|
122 | |
---|
123 | class VBase2 |
---|
124 | { |
---|
125 | int b; |
---|
126 | public: |
---|
127 | virtual ~VBase2(){}; |
---|
128 | }; |
---|
129 | |
---|
130 | class VDerived : public VBase1, public VBase2 |
---|
131 | { |
---|
132 | long c; |
---|
133 | public: |
---|
134 | virtual ~VDerived(){}; |
---|
135 | }; |
---|
136 | |
---|
137 | BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(VBase1) |
---|
138 | BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(VBase2) |
---|
139 | BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(VDerived) |
---|
140 | |
---|
141 | // see above |
---|
142 | |
---|
143 | void test_dynamic_reference_cast_2(){ |
---|
144 | VDerived d; |
---|
145 | VBase1 &b1 = dynamic_cast<VBase1 &>(d); |
---|
146 | VBase2 &b2 = static_cast<VBase2 &>(d); |
---|
147 | |
---|
148 | VBase1 & vb1 = smart_cast<VBase1 &, VDerived &>(d); |
---|
149 | BOOST_CHECK_EQUAL(& b1, & vb1); |
---|
150 | BOOST_CHECK_EQUAL(& b2, (& smart_cast<VBase2 &, VDerived &>(d))); |
---|
151 | |
---|
152 | // downcast |
---|
153 | BOOST_CHECK_EQUAL(& d, (& smart_cast<VDerived &, VBase1 &>(b1))); |
---|
154 | BOOST_CHECK_EQUAL(& d, (& smart_cast<VDerived &, VBase2 &>(b2))); |
---|
155 | |
---|
156 | // crosscast |
---|
157 | BOOST_CHECK_EQUAL(& b2, (& smart_cast<VBase2 &, VBase1 &>(b1))); |
---|
158 | |
---|
159 | // explicit cross cast should always work |
---|
160 | BOOST_CHECK_EQUAL(& b2, ( |
---|
161 | & smart_cast<VBase2 &, VDerived &>( |
---|
162 | smart_cast<VDerived &, VBase1 &>(b1) |
---|
163 | )) |
---|
164 | ); |
---|
165 | } |
---|
166 | |
---|
167 | void test_dynamic_reference_cast_1(){ |
---|
168 | VDerived d; |
---|
169 | VBase1 &b1 = dynamic_cast<VBase1 &>(d); |
---|
170 | VBase2 &b2 = static_cast<VBase2 &>(d); |
---|
171 | |
---|
172 | VBase1 & vb1 = smart_cast_reference<VBase1 &>(d); |
---|
173 | BOOST_CHECK_EQUAL(& b1, & vb1); |
---|
174 | BOOST_CHECK_EQUAL(& b2, (& smart_cast_reference<VBase2 &>(d))); |
---|
175 | |
---|
176 | // downcast |
---|
177 | BOOST_CHECK_EQUAL(& d, (& smart_cast_reference<VDerived &>(b1))); |
---|
178 | BOOST_CHECK_EQUAL(& d, (& smart_cast_reference<VDerived &>(b2))); |
---|
179 | |
---|
180 | // crosscast |
---|
181 | BOOST_CHECK_EQUAL(& b2, (& smart_cast_reference<VBase2 &>(b1))); |
---|
182 | |
---|
183 | // explicit cross cast should always work |
---|
184 | BOOST_CHECK_EQUAL(& b2, ( |
---|
185 | & smart_cast_reference<VBase2 &>( |
---|
186 | smart_cast_reference<VDerived &>(b1) |
---|
187 | )) |
---|
188 | ); |
---|
189 | } |
---|
190 | |
---|
191 | void test_dynamic_pointer_cast(){ |
---|
192 | // pointers |
---|
193 | VDerived d; |
---|
194 | VDerived *pD = & d; |
---|
195 | VBase1 *pB1 = pD; |
---|
196 | VBase2 *pB2 = pD; |
---|
197 | |
---|
198 | // upcast |
---|
199 | BOOST_CHECK_EQUAL(pB1, smart_cast<VBase1 *>(pD)); |
---|
200 | BOOST_CHECK_EQUAL(pB2, smart_cast<VBase2 *>(pD)); |
---|
201 | |
---|
202 | // downcast |
---|
203 | BOOST_CHECK_EQUAL(pD, smart_cast<VDerived *>(pB1)); |
---|
204 | BOOST_CHECK_EQUAL(pD, smart_cast<VDerived *>(pB2)); |
---|
205 | |
---|
206 | // crosscast pointers fails at compiler time |
---|
207 | BOOST_CHECK_EQUAL(pB2, smart_cast<VBase2 *>(pB1)); |
---|
208 | // though explicit cross cast will always work |
---|
209 | BOOST_CHECK_EQUAL(pB2, |
---|
210 | smart_cast<VBase2 *>( |
---|
211 | smart_cast<VDerived *>(pB1) |
---|
212 | ) |
---|
213 | ); |
---|
214 | } |
---|
215 | |
---|
216 | int |
---|
217 | test_main(int /* argc */, char * /* argv */[]) |
---|
218 | { |
---|
219 | test_static_reference_cast_2(); |
---|
220 | test_static_reference_cast_1(); |
---|
221 | test_static_pointer_cast(); |
---|
222 | test_dynamic_reference_cast_2(); |
---|
223 | test_dynamic_reference_cast_1(); |
---|
224 | test_dynamic_pointer_cast(); |
---|
225 | |
---|
226 | return EXIT_SUCCESS; |
---|
227 | } |
---|