fix doc example typo
[boost.git] / boost / serialization / void_cast.hpp
blobf8ebe2b4e9bb7a351778bcfdb430fe9805da61a7
1 #ifndef BOOST_SERIALIZATION_VOID_CAST_HPP
2 #define BOOST_SERIALIZATION_VOID_CAST_HPP
4 // MS compatible compilers support #pragma once
5 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
6 # pragma once
7 #endif
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10 // void_cast.hpp: interface for run-time casting of void pointers.
12 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
13 // Use, modification and distribution is subject to the Boost Software
14 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
15 // http://www.boost.org/LICENSE_1_0.txt)
16 // gennadiy.rozental@tfn.com
18 // See http://www.boost.org for updates, documentation, and revision history.
20 #include <cstddef> // for ptrdiff_t
21 #include <boost/serialization/config.hpp>
22 #include <boost/serialization/smart_cast.hpp>
23 #include <boost/serialization/singleton.hpp>
24 #include <boost/serialization/force_include.hpp>
25 #include <boost/serialization/type_info_implementation.hpp>
26 #include <boost/type_traits/is_virtual_base_of.hpp>
28 #include <boost/config/abi_prefix.hpp> // must be the last header
30 #ifdef BOOST_MSVC
31 # pragma warning(push)
32 # pragma warning(disable : 4251 4231 4660 4275)
33 #endif
35 namespace boost {
36 namespace serialization {
38 class extended_type_info;
40 // Given a void *, assume that it really points to an instance of one type
41 // and alter it so that it would point to an instance of a related type.
42 // Return the altered pointer. If there exists no sequence of casts that
43 // can transform from_type to to_type, return a NULL.
45 BOOST_SERIALIZATION_DECL(void const *)
46 void_upcast(
47 extended_type_info const & derived,
48 extended_type_info const & base,
49 void const * const t
52 inline void *
53 void_upcast(
54 extended_type_info const & derived,
55 extended_type_info const & base,
56 void * const t
58 return const_cast<void*>(void_upcast(
59 derived,
60 base,
61 const_cast<void const *>(t)
62 ));
65 BOOST_SERIALIZATION_DECL(void const *)
66 void_downcast(
67 extended_type_info const & derived,
68 extended_type_info const & base,
69 void const * const t
72 inline void *
73 void_downcast(
74 extended_type_info const & derived,
75 extended_type_info const & base,
76 void * const t
78 return const_cast<void*>(void_downcast(
79 derived,
80 base,
81 const_cast<void const *>(t)
82 ));
85 namespace void_cast_detail {
87 class BOOST_SERIALIZATION_DECL(BOOST_PP_EMPTY()) void_caster
89 friend
90 BOOST_SERIALIZATION_DECL(void const *)
91 boost::serialization::void_upcast(
92 extended_type_info const & derived,
93 extended_type_info const & base,
94 void const * const
96 friend
97 BOOST_SERIALIZATION_DECL(void const *)
98 boost::serialization::void_downcast(
99 extended_type_info const & derived,
100 extended_type_info const & base,
101 void const * const
103 // cw 8.3 requires this!!
104 void_caster& operator=(void_caster const &);
105 protected:
106 void recursive_register(bool includes_virtual_base = false) const;
107 void recursive_unregister() const;
108 public:
109 // Data members
110 const extended_type_info * m_derived;
111 const extended_type_info * m_base;
112 const std::ptrdiff_t m_difference;
113 virtual bool is_shortcut() const {
114 return false;
116 // note that void_casters are keyed on value of
117 // addresses to member extended type info records
118 bool operator<(const void_caster & lhs) const {
119 if(m_derived < lhs.m_derived)
120 return true;
121 if(m_derived == lhs.m_derived)
122 if(m_base < lhs.m_base)
123 return true;
124 return false;
126 // each derived class must re-implement these;
127 virtual void const * upcast(void const * const t) const = 0;
128 virtual void const * downcast(void const * const t) const = 0;
129 // Constructor
130 void_caster(
131 extended_type_info const * derived,
132 extended_type_info const * base,
133 std::ptrdiff_t difference = 0
135 m_derived(derived),
136 m_base(base),
137 m_difference(difference)
139 virtual ~void_caster(){}
142 template <class Derived, class Base>
143 class void_caster_primitive :
144 public void_caster
146 virtual void const * downcast(void const * const t) const {
147 const Derived * d =
148 boost::serialization::smart_cast<const Derived *, const Base *>(
149 static_cast<const Base *>(t)
151 return d;
153 virtual void const * upcast(void const * const t) const {
154 const Base * b =
155 boost::serialization::smart_cast<const Base *, const Derived *>(
156 static_cast<const Derived *>(t)
158 return b;
160 public:
161 void_caster_primitive();
162 ~void_caster_primitive();
165 template <class Derived, class Base>
166 void_caster_primitive<Derived, Base>::void_caster_primitive() :
167 void_caster(
168 & type_info_implementation<Derived>::type::get_const_instance(),
169 & type_info_implementation<Base>::type::get_const_instance(),
170 // note:I wanted to display from 0 here, but at least one compiler
171 // treated 0 by not shifting it at all.
172 reinterpret_cast<std::ptrdiff_t>(
173 static_cast<Derived *>(
174 reinterpret_cast<Base *>(1)
176 ) - 1
179 recursive_register();
182 template <class Derived, class Base>
183 void_caster_primitive<Derived, Base>::~void_caster_primitive(){
184 recursive_unregister();
187 template <class Derived, class Base>
188 class void_caster_virtual_base :
189 public void_caster
191 public:
192 virtual void const * downcast(void const * const t) const {
193 const Derived * d =
194 dynamic_cast<const Derived *>(
195 static_cast<const Base *>(t)
197 return d;
199 virtual void const * upcast(void const * const t) const {
200 const Base * b =
201 dynamic_cast<const Base *>(
202 static_cast<const Derived *>(t)
204 return b;
206 void_caster_virtual_base();
207 ~void_caster_virtual_base();
210 template <class Derived, class Base>
211 void_caster_virtual_base<Derived,Base>::void_caster_virtual_base() :
212 void_caster(
213 & type_info_implementation<Derived>::type::get_const_instance(),
214 & type_info_implementation<Base>::type::get_const_instance()
217 recursive_register(true);
220 template <class Derived, class Base>
221 void_caster_virtual_base<Derived,Base>::~void_caster_virtual_base(){
222 recursive_unregister();
225 } // void_cast_detail
227 // Register a base/derived pair. This indicates that it is possible
228 // to upcast a void pointer from Derived to Base and downcast a
229 // void pointer from Base to Derived. Note bogus arguments to workaround
230 // bug in msvc 6.0
231 template<class Derived, class Base>
232 BOOST_DLLEXPORT
233 inline const void_cast_detail::void_caster & void_cast_register(
234 const Derived * dnull,
235 const Base * bnull
236 ) BOOST_USED;
238 template<class Derived, class Base>
239 BOOST_DLLEXPORT
240 inline const void_cast_detail::void_caster & void_cast_register(
241 Derived const * /* dnull = NULL */,
242 Base const * /* bnull = NULL */
244 typedef
245 BOOST_DEDUCED_TYPENAME mpl::eval_if<boost::is_virtual_base_of<Base,Derived>,
246 mpl::identity<
247 void_cast_detail::void_caster_virtual_base<Derived, Base>
249 ,// else
250 mpl::identity<
251 void_cast_detail::void_caster_primitive<Derived, Base>
253 >::type typex;
254 return singleton<typex>::get_const_instance();
257 } // namespace serialization
258 } // namespace boost
260 #ifdef BOOST_MSVC
261 # pragma warning(pop)
262 #endif
264 #include <boost/config/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
266 #endif // BOOST_SERIALIZATION_VOID_CAST_HPP