1 | #ifndef BOOST_EXTENDED_TYPE_INFO_NO_RTTI_HPP |
2 | #define BOOST_EXTENDED_TYPE_INFO_NO_RTTI_HPP |
3 | |
4 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
5 | // MS compatible compilers support #pragma once |
6 | #if defined(_MSC_VER) |
7 | # pragma once |
8 | #endif |
9 | |
10 | // extended_type_info_no_rtti.hpp: implementation for version that depends |
11 | // on runtime typing (rtti - typeid) but uses a user specified string |
12 | // as the portable class identifier. |
13 | |
14 | // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . |
15 | // Use, modification and distribution is subject to the Boost Software |
16 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
17 | // http://www.boost.org/LICENSE_1_0.txt) |
18 | |
19 | // See http://www.boost.org for updates, documentation, and revision history. |
20 | #include <boost/assert.hpp> |
21 | |
22 | #include <boost/config.hpp> |
23 | #include <boost/static_assert.hpp> |
24 | |
25 | #include <boost/mpl/if.hpp> |
26 | #include <boost/type_traits/is_polymorphic.hpp> |
27 | #include <boost/type_traits/remove_const.hpp> |
28 | |
29 | #include <boost/detail/workaround.hpp> |
30 | |
31 | #include <boost/serialization/static_warning.hpp> |
32 | #include <boost/serialization/singleton.hpp> |
33 | #include <boost/serialization/extended_type_info.hpp> |
34 | #include <boost/serialization/factory.hpp> |
35 | #include <boost/serialization/throw_exception.hpp> |
36 | |
37 | #include <boost/serialization/config.hpp> |
38 | // hijack serialization access |
39 | #include <boost/serialization/access.hpp> |
40 | |
41 | #include <boost/config/abi_prefix.hpp> // must be the last header |
42 | #ifdef BOOST_MSVC |
43 | # pragma warning(push) |
44 | # pragma warning(disable : 4251 4231 4660 4275 4511 4512) |
45 | #endif |
46 | |
47 | namespace boost { |
48 | namespace serialization { |
49 | /////////////////////////////////////////////////////////////////////// |
50 | // define a special type_info that doesn't depend on rtti which is not |
51 | // available in all situations. |
52 | |
53 | namespace no_rtti_system { |
54 | |
55 | // common base class to share type_info_key. This is used to |
56 | // identify the method used to keep track of the extended type |
57 | class BOOST_SYMBOL_VISIBLE extended_type_info_no_rtti_0 : |
58 | public extended_type_info |
59 | { |
60 | protected: |
61 | BOOST_SERIALIZATION_DECL extended_type_info_no_rtti_0(const char * key); |
62 | BOOST_SERIALIZATION_DECL ~extended_type_info_no_rtti_0() BOOST_OVERRIDE; |
63 | public: |
64 | BOOST_SERIALIZATION_DECL bool |
65 | is_less_than(const boost::serialization::extended_type_info &rhs) const BOOST_OVERRIDE; |
66 | BOOST_SERIALIZATION_DECL bool |
67 | is_equal(const boost::serialization::extended_type_info &rhs) const BOOST_OVERRIDE; |
68 | }; |
69 | |
70 | } // no_rtti_system |
71 | |
72 | template<class T> |
73 | class extended_type_info_no_rtti : |
74 | public no_rtti_system::extended_type_info_no_rtti_0, |
75 | public singleton<extended_type_info_no_rtti< T > > |
76 | { |
77 | template<bool tf> |
78 | struct action { |
79 | struct defined { |
80 | static const char * invoke(){ |
81 | return guid< T >(); |
82 | } |
83 | }; |
84 | struct undefined { |
85 | // if your program traps here - you failed to |
86 | // export a guid for this type. the no_rtti |
87 | // system requires export for types serialized |
88 | // as pointers. |
89 | BOOST_STATIC_ASSERT(0 == sizeof(T)); |
90 | static const char * invoke(); |
91 | }; |
92 | static const char * invoke(){ |
93 | typedef |
94 | typename boost::mpl::if_c< |
95 | tf, |
96 | defined, |
97 | undefined |
98 | >::type type; |
99 | return type::invoke(); |
100 | } |
101 | }; |
102 | public: |
103 | extended_type_info_no_rtti() : |
104 | no_rtti_system::extended_type_info_no_rtti_0( |
105 | action<guid_defined< T >::value >::invoke()) |
106 | { |
107 | key_register(); |
108 | } |
109 | ~extended_type_info_no_rtti() BOOST_OVERRIDE { |
110 | key_unregister(); |
111 | } |
112 | const extended_type_info * |
113 | get_derived_extended_type_info(const T & t) const { |
114 | // find the type that corresponds to the most derived type. |
115 | // this implementation doesn't depend on typeid() but assumes |
116 | // that the specified type has a function of the following signature. |
117 | // A common implementation of such a function is to define as a virtual |
118 | // function. So if the type is not a polymorphic type it's likely an error |
119 | BOOST_STATIC_WARNING(boost::is_polymorphic< T >::value); |
120 | const char * derived_key = t.get_key(); |
121 | BOOST_ASSERT(NULL != derived_key); |
122 | return boost::serialization::extended_type_info::find(key: derived_key); |
123 | } |
124 | const char * get_key() const{ |
125 | return action<guid_defined< T >::value >::invoke(); |
126 | } |
127 | const char * get_debug_info() const BOOST_OVERRIDE { |
128 | return action<guid_defined< T >::value >::invoke(); |
129 | } |
130 | void * construct(unsigned int count, ...) const BOOST_OVERRIDE { |
131 | // count up the arguments |
132 | std::va_list ap; |
133 | va_start(ap, count); |
134 | switch(count){ |
135 | case 0: |
136 | return factory<typename boost::remove_const< T >::type, 0>(ap); |
137 | case 1: |
138 | return factory<typename boost::remove_const< T >::type, 1>(ap); |
139 | case 2: |
140 | return factory<typename boost::remove_const< T >::type, 2>(ap); |
141 | case 3: |
142 | return factory<typename boost::remove_const< T >::type, 3>(ap); |
143 | case 4: |
144 | return factory<typename boost::remove_const< T >::type, 4>(ap); |
145 | default: |
146 | BOOST_ASSERT(false); // too many arguments |
147 | // throw exception here? |
148 | return NULL; |
149 | } |
150 | } |
151 | void destroy(void const * const p) const BOOST_OVERRIDE { |
152 | boost::serialization::access::destroy( |
153 | static_cast<T const *>(p) |
154 | ); |
155 | //delete static_cast<T const * const>(p) ; |
156 | } |
157 | }; |
158 | |
159 | } // namespace serialization |
160 | } // namespace boost |
161 | |
162 | /////////////////////////////////////////////////////////////////////////////// |
163 | // If no other implementation has been designated as default, |
164 | // use this one. To use this implementation as the default, specify it |
165 | // before any of the other headers. |
166 | |
167 | #ifndef BOOST_SERIALIZATION_DEFAULT_TYPE_INFO |
168 | #define BOOST_SERIALIZATION_DEFAULT_TYPE_INFO |
169 | namespace boost { |
170 | namespace serialization { |
171 | template<class T> |
172 | struct extended_type_info_impl { |
173 | typedef typename |
174 | boost::serialization::extended_type_info_no_rtti< T > type; |
175 | }; |
176 | } // namespace serialization |
177 | } // namespace boost |
178 | #endif |
179 | |
180 | #ifdef BOOST_MSVC |
181 | # pragma warning(pop) |
182 | #endif |
183 | #include <boost/config/abi_suffix.hpp> // pops abi_suffix.hpp pragmas |
184 | |
185 | #endif // BOOST_EXTENDED_TYPE_INFO_NO_RTTI_HPP |
186 | |