]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/serialization/test/test_smart_cast.cpp
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // test_smart_cast.cpp:
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>
11 #include <boost/serialization/smart_cast.hpp>
13 #include "test_tools.hpp"
14 #include <boost/noncopyable.hpp>
16 using namespace boost::serialization
;
18 class Base1
: public boost::noncopyable
29 # pragma warning(push)
30 # pragma warning(disable : 4511 4512)
33 class Derived
: public Base1
, public Base2
42 // if compiler doesn't support TPS, the smart_cast syntax doesn't
43 // work for references. One has to use the smart_cast_reference
44 // syntax (tested below ) instead.
46 void test_static_reference_cast_2(){
48 Base1
& b1
= static_cast<Base1
&>(d
);
49 Base2
& b2
= static_cast<Base2
&>(d
);
51 Base1
& scb1
= smart_cast
<Base1
&, Derived
&>(d
);
52 Base2
& scb2
= smart_cast
<Base2
&, Derived
&>(d
);
53 BOOST_CHECK_EQUAL(& b1
, & scb1
);
54 BOOST_CHECK_EQUAL(& b2
, & scb2
);
57 // BOOST_CHECK_EQUAL(& d, & (smart_cast<Derived &, Base1 &>(b1)));
58 // BOOST_CHECK_EQUAL(& d, & (smart_cast<Derived &, Base2 &>(b2)));
60 // crosscast pointers fails at compiler time
61 // BOOST_CHECK_EQUAL(pB2,smart_cast<B2 *>(pB1));
62 // though explicit cross cast will always work
63 BOOST_CHECK_EQUAL(& b2
,(
64 & smart_cast
<Base2
&, Derived
&>(
65 smart_cast
<Derived
&, Base1
&>(b1
)
70 void test_static_reference_cast_1(){
72 Base1
& b1
= static_cast<Base1
&>(d
);
73 Base2
& b2
= static_cast<Base2
&>(d
);
75 Base1
& scb1
= smart_cast_reference
<Base1
&>(d
);
76 Base2
& scb2
= smart_cast_reference
<Base2
&>(d
);
77 BOOST_CHECK_EQUAL(& b1
, & scb1
);
78 BOOST_CHECK_EQUAL(& b2
, & scb2
);
81 BOOST_CHECK_EQUAL(& d
, & (smart_cast_reference
<Derived
&>(b1
)));
82 BOOST_CHECK_EQUAL(& d
, & (smart_cast_reference
<Derived
&>(b2
)));
84 // crosscast pointers fails at compiler time
85 // BOOST_CHECK_EQUAL(pB2,smart_cast<B2 *>(pB1));
86 // though explicit cross cast will always work
87 BOOST_CHECK_EQUAL(& b2
,(
88 & smart_cast_reference
<Base2
&>(
89 smart_cast_reference
<Derived
&>(b1
)
94 void test_static_pointer_cast(){
102 BOOST_CHECK_EQUAL(pB1
, smart_cast
<Base1
*>(pD
));
103 BOOST_CHECK_EQUAL(pB2
, smart_cast
<Base2
*>(pD
));
106 BOOST_CHECK_EQUAL(pD
, smart_cast
<Derived
*>(pB1
));
107 BOOST_CHECK_EQUAL(pD
, smart_cast
<Derived
*>(pB2
));
109 // crosscast pointers fails at compiler time
110 // BOOST_CHECK_EQUAL(pB2, smart_cast<Base2 *>(pB1));
112 // though explicit cross cast will always work
113 BOOST_CHECK_EQUAL(pB2
,
115 smart_cast
<Derived
*>(pB1
)
120 class VBase1
: public boost::noncopyable
135 # pragma warning(push)
136 # pragma warning(disable : 4511 4512)
139 class VDerived
: public VBase1
, public VBase2
143 virtual ~VDerived(){};
152 void test_dynamic_reference_cast_2(){
154 VBase1
&b1
= dynamic_cast<VBase1
&>(d
);
155 VBase2
&b2
= static_cast<VBase2
&>(d
);
157 VBase1
& vb1
= smart_cast
<VBase1
&, VDerived
&>(d
);
158 BOOST_CHECK_EQUAL(& b1
, & vb1
);
159 BOOST_CHECK_EQUAL(& b2
, (& smart_cast
<VBase2
&, VDerived
&>(d
)));
162 BOOST_CHECK_EQUAL(& d
, (& smart_cast
<VDerived
&, VBase1
&>(b1
)));
163 BOOST_CHECK_EQUAL(& d
, (& smart_cast
<VDerived
&, VBase2
&>(b2
)));
166 BOOST_CHECK_EQUAL(& b2
, (& smart_cast
<VBase2
&, VBase1
&>(b1
)));
168 // explicit cross cast should always work
169 BOOST_CHECK_EQUAL(& b2
, (
170 & smart_cast
<VBase2
&, VDerived
&>(
171 smart_cast
<VDerived
&, VBase1
&>(b1
)
176 void test_dynamic_reference_cast_1(){
178 VBase1
&b1
= dynamic_cast<VBase1
&>(d
);
179 VBase2
&b2
= static_cast<VBase2
&>(d
);
181 VBase1
& vb1
= smart_cast_reference
<VBase1
&>(d
);
182 BOOST_CHECK_EQUAL(& b1
, & vb1
);
183 BOOST_CHECK_EQUAL(& b2
, (& smart_cast_reference
<VBase2
&>(d
)));
186 BOOST_CHECK_EQUAL(& d
, (& smart_cast_reference
<VDerived
&>(b1
)));
187 BOOST_CHECK_EQUAL(& d
, (& smart_cast_reference
<VDerived
&>(b2
)));
190 BOOST_CHECK_EQUAL(& b2
, (& smart_cast_reference
<VBase2
&>(b1
)));
192 // explicit cross cast should always work
193 BOOST_CHECK_EQUAL(& b2
, (
194 & smart_cast_reference
<VBase2
&>(
195 smart_cast_reference
<VDerived
&>(b1
)
200 void test_dynamic_pointer_cast(){
208 BOOST_CHECK_EQUAL(pB1
, smart_cast
<VBase1
*>(pD
));
209 BOOST_CHECK_EQUAL(pB2
, smart_cast
<VBase2
*>(pD
));
212 BOOST_CHECK_EQUAL(pD
, smart_cast
<VDerived
*>(pB1
));
213 BOOST_CHECK_EQUAL(pD
, smart_cast
<VDerived
*>(pB2
));
215 // crosscast pointers fails at compiler time
216 BOOST_CHECK_EQUAL(pB2
, smart_cast
<VBase2
*>(pB1
));
217 // though explicit cross cast will always work
218 BOOST_CHECK_EQUAL(pB2
,
219 smart_cast
<VBase2
*>(
220 smart_cast
<VDerived
*>(pB1
)
226 test_main(int /* argc */, char * /* argv */[])
228 test_static_reference_cast_2();
229 test_static_reference_cast_1();
230 test_static_pointer_cast();
231 test_dynamic_reference_cast_2();
232 test_dynamic_reference_cast_1();
233 test_dynamic_pointer_cast();