extended_type_info_typeid.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #ifndef BOOST_SERIALIZATION_EXTENDED_TYPE_INFO_TYPEID_HPP
  2. #define BOOST_SERIALIZATION_EXTENDED_TYPE_INFO_TYPEID_HPP
  3. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  4. // MS compatible compilers support #pragma once
  5. #if defined(_MSC_VER)
  6. # pragma once
  7. #endif
  8. // extended_type_info_typeid.hpp: implementation for version that depends
  9. // on runtime typing (rtti - typeid) but uses a user specified string
  10. // as the portable class identifier.
  11. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  12. // Use, modification and distribution is subject to the Boost Software
  13. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. // See http://www.boost.org for updates, documentation, and revision history.
  16. #include <typeinfo>
  17. #include <cstdarg>
  18. #include <boost/assert.hpp>
  19. #include <boost/config.hpp>
  20. #include <boost/static_assert.hpp>
  21. #include <boost/serialization/static_warning.hpp>
  22. #include <boost/type_traits/is_polymorphic.hpp>
  23. #include <boost/type_traits/remove_const.hpp>
  24. #include <boost/serialization/config.hpp>
  25. #include <boost/serialization/singleton.hpp>
  26. #include <boost/serialization/extended_type_info.hpp>
  27. #include <boost/serialization/factory.hpp>
  28. // hijack serialization access
  29. #include <boost/serialization/access.hpp>
  30. #include <boost/mpl/if.hpp>
  31. #include <boost/config/abi_prefix.hpp> // must be the last header
  32. #ifdef BOOST_MSVC
  33. # pragma warning(push)
  34. # pragma warning(disable : 4251 4231 4660 4275 4511 4512)
  35. #endif
  36. namespace boost {
  37. namespace serialization {
  38. namespace typeid_system {
  39. class BOOST_SYMBOL_VISIBLE extended_type_info_typeid_0 :
  40. public extended_type_info
  41. {
  42. virtual const char * get_debug_info() const {
  43. if(static_cast<const std::type_info *>(0) == m_ti)
  44. return static_cast<const char *>(0);
  45. return m_ti->name();
  46. }
  47. protected:
  48. const std::type_info * m_ti;
  49. BOOST_SERIALIZATION_DECL extended_type_info_typeid_0(const char * key);
  50. BOOST_SERIALIZATION_DECL ~extended_type_info_typeid_0();
  51. BOOST_SERIALIZATION_DECL void type_register(const std::type_info & ti);
  52. BOOST_SERIALIZATION_DECL void type_unregister();
  53. BOOST_SERIALIZATION_DECL const extended_type_info *
  54. get_extended_type_info(const std::type_info & ti) const;
  55. public:
  56. virtual BOOST_SERIALIZATION_DECL bool
  57. is_less_than(const extended_type_info &rhs) const;
  58. virtual BOOST_SERIALIZATION_DECL bool
  59. is_equal(const extended_type_info &rhs) const;
  60. const std::type_info & get_typeid() const {
  61. return *m_ti;
  62. }
  63. };
  64. } // typeid_system
  65. template<class T>
  66. class extended_type_info_typeid :
  67. public typeid_system::extended_type_info_typeid_0,
  68. public singleton<extended_type_info_typeid< T > >
  69. {
  70. public:
  71. extended_type_info_typeid() :
  72. typeid_system::extended_type_info_typeid_0(
  73. boost::serialization::guid< T >()
  74. )
  75. {
  76. type_register(typeid(T));
  77. key_register();
  78. }
  79. ~extended_type_info_typeid(){
  80. key_unregister();
  81. type_unregister();
  82. }
  83. // get the eti record for the true type of this record
  84. // relying upon standard type info implemenation (rtti)
  85. const extended_type_info *
  86. get_derived_extended_type_info(const T & t) const {
  87. // note: this implementation - based on usage of typeid (rtti)
  88. // only does something if the class has at least one virtual function.
  89. BOOST_STATIC_WARNING(boost::is_polymorphic< T >::value);
  90. return
  91. typeid_system::extended_type_info_typeid_0::get_extended_type_info(
  92. typeid(t)
  93. );
  94. }
  95. const char * get_key() const {
  96. return boost::serialization::guid< T >();
  97. }
  98. virtual void * construct(unsigned int count, ...) const{
  99. // count up the arguments
  100. std::va_list ap;
  101. va_start(ap, count);
  102. switch(count){
  103. case 0:
  104. return factory<typename boost::remove_const< T >::type, 0>(ap);
  105. case 1:
  106. return factory<typename boost::remove_const< T >::type, 1>(ap);
  107. case 2:
  108. return factory<typename boost::remove_const< T >::type, 2>(ap);
  109. case 3:
  110. return factory<typename boost::remove_const< T >::type, 3>(ap);
  111. case 4:
  112. return factory<typename boost::remove_const< T >::type, 4>(ap);
  113. default:
  114. BOOST_ASSERT(false); // too many arguments
  115. // throw exception here?
  116. return NULL;
  117. }
  118. }
  119. virtual void destroy(void const * const p) const {
  120. boost::serialization::access::destroy(
  121. static_cast<T const *>(p)
  122. );
  123. //delete static_cast<T const * const>(p);
  124. }
  125. };
  126. } // namespace serialization
  127. } // namespace boost
  128. ///////////////////////////////////////////////////////////////////////////////
  129. // If no other implementation has been designated as default,
  130. // use this one. To use this implementation as the default, specify it
  131. // before any of the other headers.
  132. #ifndef BOOST_SERIALIZATION_DEFAULT_TYPE_INFO
  133. #define BOOST_SERIALIZATION_DEFAULT_TYPE_INFO
  134. namespace boost {
  135. namespace serialization {
  136. template<class T>
  137. struct extended_type_info_impl {
  138. typedef typename
  139. boost::serialization::extended_type_info_typeid< T > type;
  140. };
  141. } // namespace serialization
  142. } // namespace boost
  143. #endif
  144. #ifdef BOOST_MSVC
  145. #pragma warning(pop)
  146. #endif
  147. #include <boost/config/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
  148. #endif // BOOST_SERIALIZATION_EXTENDED_TYPE_INFO_TYPEID_HPP