Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/serialization/test/test_void_cast.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 5.8 KB
Line 
1/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2// test_void_cast.cpp: test implementation of run-time casting of void pointers
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 <boost/test/test_tools.hpp>
11#include <boost/serialization/extended_type_info_typeid.hpp>
12#include <boost/serialization/void_cast.hpp>
13
14class Base1
15{
16    char a;
17};
18
19class Base2
20{
21    int b;
22};
23
24class Derived : public Base1, public Base2
25{
26    long c;
27};
28
29class MostDerived : public Derived
30{
31    char d[32];
32};
33
34int
35test_main( int /* argc */, char* /* argv */[] )
36{
37    MostDerived d;
38    MostDerived* pd =& d;
39    Derived* pc = static_cast<Derived*>(pd);
40
41    Base2* pb = static_cast<Base2*>(pd);
42    Base1* pa = static_cast<Base1*>(pd);
43
44    void* vpd = static_cast<void*>(pd);
45    void* vpc = static_cast<void*>(pc);
46    void* vpb = static_cast<void*>(pb);
47    void* vpa = static_cast<void*>(pa);
48
49    // simple casts only requiring table lookup
50    BOOST_CHECK(vpc == boost::serialization::void_downcast(
51        * boost::serialization::extended_type_info_typeid<Derived>::get_instance(),
52        * boost::serialization::extended_type_info_typeid<Base1>::get_instance(), 
53        vpa
54    ));
55    BOOST_CHECK(vpa == boost::serialization::void_upcast(
56        * boost::serialization::extended_type_info_typeid<Derived>::get_instance(),
57        * boost::serialization::extended_type_info_typeid<Base1>::get_instance(), 
58        vpc 
59    ));
60    BOOST_CHECK(vpc == boost::serialization::void_downcast( 
61        * boost::serialization::extended_type_info_typeid<Derived>::get_instance(), 
62        * boost::serialization::extended_type_info_typeid<Base2>::get_instance(), 
63        vpb
64    ));
65    BOOST_CHECK(vpb == boost::serialization::void_upcast(
66        * boost::serialization::extended_type_info_typeid<Derived>::get_instance(), 
67        * boost::serialization::extended_type_info_typeid<Base2>::get_instance(), 
68        vpc
69    ));
70    BOOST_CHECK(vpd == boost::serialization::void_downcast( 
71        * boost::serialization::extended_type_info_typeid<MostDerived>::get_instance(), 
72        * boost::serialization::extended_type_info_typeid<Derived>::get_instance(), 
73        vpc
74    ));
75    BOOST_CHECK(vpc == boost::serialization::void_upcast(
76        * boost::serialization::extended_type_info_typeid<MostDerived>::get_instance(), 
77        * boost::serialization::extended_type_info_typeid<Derived>::get_instance(), 
78        vpd
79    ));
80
81    // note relationship between MostDerived and Base1 is automatically derived
82    BOOST_CHECK(vpd == boost::serialization::void_downcast( 
83        * boost::serialization::extended_type_info_typeid<MostDerived>::get_instance(), 
84        * boost::serialization::extended_type_info_typeid<Base1>::get_instance(), 
85        vpa
86    ));
87    BOOST_CHECK(vpa == boost::serialization::void_upcast(
88        * boost::serialization::extended_type_info_typeid<MostDerived>::get_instance(), 
89        * boost::serialization::extended_type_info_typeid<Base1>::get_instance(), 
90        vpd
91    ));
92
93    // note relationship between MostDerived and Base2 is automatically derived
94    BOOST_CHECK(vpd == boost::serialization::void_downcast( 
95        * boost::serialization::extended_type_info_typeid<MostDerived>::get_instance(), 
96        * boost::serialization::extended_type_info_typeid<Base2>::get_instance(), 
97        vpb
98    ));
99    BOOST_CHECK(vpb == boost::serialization::void_upcast(
100        * boost::serialization::extended_type_info_typeid<MostDerived>::get_instance(), 
101        * boost::serialization::extended_type_info_typeid<Base2>::get_instance(), 
102        vpd
103    ));
104
105    // need to double check to validate speed up optimization of derivations
106    BOOST_CHECK(vpd == boost::serialization::void_downcast( 
107        * boost::serialization::extended_type_info_typeid<MostDerived>::get_instance(), 
108        * boost::serialization::extended_type_info_typeid<Base1>::get_instance(), 
109        vpa
110    ));
111    BOOST_CHECK(vpa == boost::serialization::void_upcast(
112        * boost::serialization::extended_type_info_typeid<MostDerived>::get_instance(), 
113        * boost::serialization::extended_type_info_typeid<Base1>::get_instance(), 
114        vpd
115    ));
116    BOOST_CHECK(vpd == boost::serialization::void_downcast(
117        * boost::serialization::extended_type_info_typeid<MostDerived>::get_instance(), 
118        * boost::serialization::extended_type_info_typeid<Base2>::get_instance(), 
119        vpb
120    ));
121    BOOST_CHECK(vpb == boost::serialization::void_upcast(
122        * boost::serialization::extended_type_info_typeid<MostDerived>::get_instance(), 
123        * boost::serialization::extended_type_info_typeid<Base2>::get_instance(), 
124        vpd
125    ));
126
127    // check things that should fail
128    BOOST_CHECK(NULL == boost::serialization::void_downcast(
129        * boost::serialization::extended_type_info_typeid<Base2>::get_instance(),
130        * boost::serialization::extended_type_info_typeid<Base1>::get_instance(), 
131        vpa
132    ));
133
134    // note that a fundamental feature is that derived/base pairs are created
135    // at compiler time so that all are registered before the main program starts
136    // so leave the registration here at the end to verify this. Note bogus arguments
137    // to workaround msvc 6 bug
138    boost::serialization::void_cast_register<Derived, Base1>(
139        static_cast<Derived *>(NULL),
140        static_cast<Base1 *>(NULL)
141    );
142    boost::serialization::void_cast_register<Derived, Base2>(
143        static_cast<Derived *>(NULL),
144        static_cast<Base2 *>(NULL)
145    );
146    boost::serialization::void_cast_register<MostDerived, Derived>(
147        static_cast<MostDerived *>(NULL),
148        static_cast<Derived *>(NULL)
149    );
150    return EXIT_SUCCESS;
151}
152
153// EOF
Note: See TracBrowser for help on using the repository browser.