6a003be1a0a18b96eb0c597d3bee5b564cc1c25d
[platform/upstream/boost.git] / boost / serialization / extended_type_info_typeid.hpp
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_SERIALIZATION_DECL(BOOST_PP_EMPTY()) extended_type_info_typeid_0 : 
53     public extended_type_info
54 {
55     virtual const char * get_debug_info() const {
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     extended_type_info_typeid_0(const char * key);
63     ~extended_type_info_typeid_0();
64     void type_register(const std::type_info & ti);
65     void type_unregister();
66     const extended_type_info *
67     get_extended_type_info(const std::type_info & ti) const;
68 public:
69     virtual bool
70     is_less_than(const extended_type_info &rhs) const;
71     virtual bool
72     is_equal(const extended_type_info &rhs) const;
73     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:
86     extended_type_info_typeid() :
87         typeid_system::extended_type_info_typeid_0(get_key())
88     {
89         type_register(typeid(T));
90         key_register();
91     }
92     ~extended_type_info_typeid(){
93         key_unregister();
94         type_unregister();
95     }
96     // get the eti record for the true type of this record
97     // relying upon standard type info implemenation (rtti)
98     const extended_type_info *
99     get_derived_extended_type_info(const T & t) const {
100         // note: this implementation - based on usage of typeid (rtti)
101         // only does something if the class has at least one virtual function.
102         BOOST_STATIC_WARNING(boost::is_polymorphic< T >::value);
103         return 
104             typeid_system::extended_type_info_typeid_0::get_extended_type_info(
105                 typeid(t)
106             );
107     }
108     const char * get_key() const {
109         return boost::serialization::guid< T >();
110     }
111     virtual void * construct(unsigned int count, ...) const{
112         // count up the arguments
113         std::va_list ap;
114         va_start(ap, count);
115         switch(count){
116         case 0:
117             return factory<typename boost::remove_const< T >::type, 0>(ap);
118         case 1:
119             return factory<typename boost::remove_const< T >::type, 1>(ap);
120         case 2:
121             return factory<typename boost::remove_const< T >::type, 2>(ap);
122         case 3:
123             return factory<typename boost::remove_const< T >::type, 3>(ap);
124         case 4:
125             return factory<typename boost::remove_const< T >::type, 4>(ap);
126         default:
127             BOOST_ASSERT(false); // too many arguments
128             // throw exception here?
129             return NULL;
130         }
131     }
132     virtual void destroy(void const * const p) const {
133         boost::serialization::access::destroy(
134             static_cast<T const *>(p)
135         );
136         //delete static_cast<T const * const>(p);
137     }
138 };
139
140 } // namespace serialization
141 } // namespace boost
142
143 ///////////////////////////////////////////////////////////////////////////////
144 // If no other implementation has been designated as default, 
145 // use this one.  To use this implementation as the default, specify it
146 // before any of the other headers.
147 #ifndef BOOST_SERIALIZATION_DEFAULT_TYPE_INFO
148     #define BOOST_SERIALIZATION_DEFAULT_TYPE_INFO
149     namespace boost {
150     namespace serialization {
151     template<class T>
152     struct extended_type_info_impl {
153         typedef typename 
154             boost::serialization::extended_type_info_typeid< T > type;
155     };
156     } // namespace serialization
157     } // namespace boost
158 #endif
159
160 #ifdef BOOST_MSVC
161 #pragma warning(pop)
162 #endif
163 #include <boost/config/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
164
165 #endif // BOOST_SERIALIZATION_EXTENDED_TYPE_INFO_TYPEID_HPP