1 #ifndef BOOST_SERIALIZATION_EXTENDED_TYPE_INFO_TYPEID_HPP 2 #define BOOST_SERIALIZATION_EXTENDED_TYPE_INFO_TYPEID_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_typeid.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 21 #include <typeinfo> 22 #include <cstdarg> 23 #include <boost/assert.hpp> 24 #include <boost/config.hpp> 25 26 #include <boost/static_assert.hpp> 27 #include <boost/serialization/static_warning.hpp> 28 #include <boost/type_traits/is_polymorphic.hpp> 29 #include <boost/type_traits/remove_const.hpp> 30 31 #include <boost/serialization/config.hpp> 32 #include <boost/serialization/singleton.hpp> 33 #include <boost/serialization/extended_type_info.hpp> 34 #include <boost/serialization/factory.hpp> 35 36 // hijack serialization access 37 #include <boost/serialization/access.hpp> 38 39 #include <boost/mpl/if.hpp> 40 41 #include <boost/config/abi_prefix.hpp> // must be the last header 42 43 #ifdef BOOST_MSVC 44 # pragma warning(push) 45 # pragma warning(disable : 4251 4231 4660 4275 4511 4512) 46 #endif 47 48 namespace boost { 49 namespace serialization { 50 namespace typeid_system { 51 52 class BOOST_SYMBOL_VISIBLE extended_type_info_typeid_0 : 53 public extended_type_info 54 { get_debug_info() const55 const char * get_debug_info() const BOOST_OVERRIDE { 56 if(static_cast<const std::type_info *>(0) == m_ti) 57 return static_cast<const char *>(0); 58 return m_ti->name(); 59 } 60 protected: 61 const std::type_info * m_ti; 62 BOOST_SERIALIZATION_DECL extended_type_info_typeid_0(const char * key); 63 BOOST_SERIALIZATION_DECL ~extended_type_info_typeid_0() BOOST_OVERRIDE; 64 BOOST_SERIALIZATION_DECL void type_register(const std::type_info & ti); 65 BOOST_SERIALIZATION_DECL void type_unregister(); 66 BOOST_SERIALIZATION_DECL const extended_type_info * 67 get_extended_type_info(const std::type_info & ti) const; 68 public: 69 BOOST_SERIALIZATION_DECL bool 70 is_less_than(const extended_type_info &rhs) const BOOST_OVERRIDE; 71 BOOST_SERIALIZATION_DECL bool 72 is_equal(const extended_type_info &rhs) const BOOST_OVERRIDE; get_typeid() const73 const std::type_info & get_typeid() const { 74 return *m_ti; 75 } 76 }; 77 78 } // typeid_system 79 80 template<class T> 81 class extended_type_info_typeid : 82 public typeid_system::extended_type_info_typeid_0, 83 public singleton<extended_type_info_typeid< T > > 84 { 85 public: extended_type_info_typeid()86 extended_type_info_typeid() : 87 typeid_system::extended_type_info_typeid_0( 88 boost::serialization::guid< T >() 89 ) 90 { 91 type_register(typeid(T)); 92 key_register(); 93 } ~extended_type_info_typeid()94 ~extended_type_info_typeid() BOOST_OVERRIDE { 95 key_unregister(); 96 type_unregister(); 97 } 98 // get the eti record for the true type of this record 99 // relying upon standard type info implemenation (rtti) 100 const extended_type_info * get_derived_extended_type_info(const T & t) const101 get_derived_extended_type_info(const T & t) const { 102 // note: this implementation - based on usage of typeid (rtti) 103 // only does something if the class has at least one virtual function. 104 BOOST_STATIC_WARNING(boost::is_polymorphic< T >::value); 105 return 106 typeid_system::extended_type_info_typeid_0::get_extended_type_info( 107 typeid(t) 108 ); 109 } get_key() const110 const char * get_key() const { 111 return boost::serialization::guid< T >(); 112 } construct(unsigned int count,...) const113 void * construct(unsigned int count, ...) const BOOST_OVERRIDE { 114 // count up the arguments 115 std::va_list ap; 116 va_start(ap, count); 117 switch(count){ 118 case 0: 119 return factory<typename boost::remove_const< T >::type, 0>(ap); 120 case 1: 121 return factory<typename boost::remove_const< T >::type, 1>(ap); 122 case 2: 123 return factory<typename boost::remove_const< T >::type, 2>(ap); 124 case 3: 125 return factory<typename boost::remove_const< T >::type, 3>(ap); 126 case 4: 127 return factory<typename boost::remove_const< T >::type, 4>(ap); 128 default: 129 BOOST_ASSERT(false); // too many arguments 130 // throw exception here? 131 return NULL; 132 } 133 } destroy(void const * const p) const134 void destroy(void const * const p) const BOOST_OVERRIDE { 135 boost::serialization::access::destroy( 136 static_cast<T const *>(p) 137 ); 138 //delete static_cast<T const * const>(p); 139 } 140 }; 141 142 } // namespace serialization 143 } // namespace boost 144 145 /////////////////////////////////////////////////////////////////////////////// 146 // If no other implementation has been designated as default, 147 // use this one. To use this implementation as the default, specify it 148 // before any of the other headers. 149 #ifndef BOOST_SERIALIZATION_DEFAULT_TYPE_INFO 150 #define BOOST_SERIALIZATION_DEFAULT_TYPE_INFO 151 namespace boost { 152 namespace serialization { 153 template<class T> 154 struct extended_type_info_impl { 155 typedef typename 156 boost::serialization::extended_type_info_typeid< T > type; 157 }; 158 } // namespace serialization 159 } // namespace boost 160 #endif 161 162 #ifdef BOOST_MSVC 163 #pragma warning(pop) 164 #endif 165 #include <boost/config/abi_suffix.hpp> // pops abi_suffix.hpp pragmas 166 167 #endif // BOOST_SERIALIZATION_EXTENDED_TYPE_INFO_TYPEID_HPP 168