1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // test_void_cast.cpp: test implementation of run-time casting of void pointers
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>
10 #include <cstddef> // NULL
11 #include "test_tools.hpp"
12 #include <boost/serialization/extended_type_info_typeid.hpp>
13 #include <boost/serialization/void_cast.hpp>
14 #include <boost/serialization/singleton.hpp>
26 class Derived
: public Base1
, public Base2
31 class MostDerived
: public Derived
37 const boost::serialization::extended_type_info
& eti(){
38 return boost::serialization::singleton
<
39 boost::serialization::extended_type_info_typeid
< T
>
40 >::get_const_instance();
44 test_main( int /* argc */, char* /* argv */[] )
47 MostDerived
* pmd
=& md
;
48 Derived
* pd
= static_cast<Derived
*>(pmd
);
50 Base2
* pb2
= static_cast<Base2
*>(pmd
);
51 Base1
* pb1
= static_cast<Base1
*>(pd
);
53 void* vpmd
= static_cast<void*>(pmd
);
54 void* vpb1
= static_cast<void*>(pb1
);
55 void* vpb2
= static_cast<void*>(pb2
);
56 void* vpd
= static_cast<void*>(pd
);
58 // simple casts only requiring table lookup
59 BOOST_CHECK(vpd
== boost::serialization::void_downcast(
64 BOOST_CHECK(vpb1
== boost::serialization::void_upcast(
69 BOOST_CHECK(vpd
== boost::serialization::void_downcast(
74 BOOST_CHECK(vpb2
== boost::serialization::void_upcast(
79 BOOST_CHECK(vpmd
== boost::serialization::void_downcast(
84 BOOST_CHECK(vpd
== boost::serialization::void_upcast(
89 // note relationship between MostDerived and Base1 is automatically derived
90 BOOST_CHECK(vpmd
== boost::serialization::void_downcast(
95 BOOST_CHECK(vpb1
== boost::serialization::void_upcast(
101 // note relationship between MostDerived and Base2 is automatically derived
102 BOOST_CHECK(vpmd
== boost::serialization::void_downcast(
107 BOOST_CHECK(vpb2
== boost::serialization::void_upcast(
113 // note: currently derivations are not optimised. See void_cast.cpp
114 // for and explanation. These should still work though.
116 // need to double check to validate speed up optimization of derivations
117 BOOST_CHECK(vpmd
== boost::serialization::void_downcast(
122 BOOST_CHECK(vpb1
== boost::serialization::void_upcast(
127 BOOST_CHECK(vpmd
== boost::serialization::void_downcast(
132 BOOST_CHECK(vpb2
== boost::serialization::void_upcast(
138 // check things that should fail
139 BOOST_CHECK(NULL
== boost::serialization::void_downcast(
145 // note that a fundamental feature is that derived/base pairs are created
146 // at compiler time so that all are registered before the main program starts
147 // so leave the registration here at the end to verify this. Note bogus arguments
148 // to workaround msvc 6 bug
149 boost::serialization::void_cast_register
<Derived
, Base1
>(
150 static_cast<Derived
*>(NULL
),
151 static_cast<Base1
*>(NULL
)
153 boost::serialization::void_cast_register
<Derived
, Base2
>(
154 static_cast<Derived
*>(NULL
),
155 static_cast<Base2
*>(NULL
)
157 boost::serialization::void_cast_register
<MostDerived
, Derived
>(
158 static_cast<MostDerived
*>(NULL
),
159 static_cast<Derived
*>(NULL
)