serialization.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #ifndef BOOST_SERIALIZATION_SERIALIZATION_HPP
  2. #define BOOST_SERIALIZATION_SERIALIZATION_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. #if defined(_MSC_VER) && (_MSC_VER >= 1310)
  8. # pragma warning (disable : 4675) // suppress ADL warning
  9. #endif
  10. #include <boost/config.hpp>
  11. #include <boost/serialization/strong_typedef.hpp>
  12. #include <boost/serialization/pfto.hpp>
  13. #include <boost/serialization/throw_exception.hpp>
  14. #include <boost/serialization/nvp.hpp>
  15. // incremented for each "release"
  16. #define BOOST_SERIALIZATION_LIBRARY_VERSION 19
  17. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  18. // serialization.hpp: interface for serialization system.
  19. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  20. // Use, modification and distribution is subject to the Boost Software
  21. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  22. // http://www.boost.org/LICENSE_1_0.txt)
  23. // See http://www.boost.org for updates, documentation, and revision history.
  24. //////////////////////////////////////////////////////////////////////
  25. // public interface to serialization.
  26. /////////////////////////////////////////////////////////////////////////////
  27. // layer 0 - intrusive verison
  28. // declared and implemented for each user defined class to be serialized
  29. //
  30. // template<Archive>
  31. // serialize(Archive &ar, const unsigned int file_version){
  32. // ar & base_object<base>(*this) & member1 & member2 ... ;
  33. // }
  34. /////////////////////////////////////////////////////////////////////////////
  35. // layer 1 - layer that routes member access through the access class.
  36. // this is what permits us to grant access to private class member functions
  37. // by specifying friend class boost::serialization::access
  38. #include <boost/serialization/access.hpp>
  39. /////////////////////////////////////////////////////////////////////////////
  40. // layer 2 - default implementation of non-intrusive serialization.
  41. //
  42. // note the usage of function overloading to compensate that C++ does not
  43. // currently support Partial Template Specialization for function templates
  44. // We have declared the version number as "const unsigned long".
  45. // Overriding templates for specific data types should declare the version
  46. // number as "const unsigned int". Template matching will first be applied
  47. // to functions with the same version types - that is the overloads.
  48. // If there is no declared function prototype that matches, the second argument
  49. // will be converted to "const unsigned long" and a match will be made with
  50. // one of the default template functions below.
  51. namespace boost {
  52. namespace serialization {
  53. BOOST_STRONG_TYPEDEF(unsigned int, version_type)
  54. // default implemenation - call the member function "serialize"
  55. template<class Archive, class T>
  56. inline void serialize(
  57. Archive & ar, T & t, const BOOST_PFTO unsigned int file_version
  58. ){
  59. access::serialize(ar, t, static_cast<unsigned int>(file_version));
  60. }
  61. // save data required for construction
  62. template<class Archive, class T>
  63. inline void save_construct_data(
  64. Archive & /*ar*/,
  65. const T * /*t*/,
  66. const BOOST_PFTO unsigned int /*file_version */
  67. ){
  68. // default is to save no data because default constructor
  69. // requires no arguments.
  70. }
  71. // load data required for construction and invoke constructor in place
  72. template<class Archive, class T>
  73. inline void load_construct_data(
  74. Archive & ar,
  75. T * t,
  76. const BOOST_PFTO unsigned int /*file_version*/
  77. ){
  78. // default just uses the default constructor. going
  79. // through access permits usage of otherwise private default
  80. // constructor
  81. access::construct(t);
  82. }
  83. /////////////////////////////////////////////////////////////////////////////
  84. // layer 3 - move call into serialization namespace so that ADL will function
  85. // in the manner we desire.
  86. //
  87. // on compilers which don't implement ADL. only the current namespace
  88. // i.e. boost::serialization will be searched.
  89. //
  90. // on compilers which DO implement ADL
  91. // serialize overrides can be in any of the following
  92. //
  93. // 1) same namepace as Archive
  94. // 2) same namespace as T
  95. // 3) boost::serialization
  96. //
  97. // Due to Martin Ecker
  98. template<class Archive, class T>
  99. inline void serialize_adl(
  100. Archive & ar,
  101. T & t,
  102. const unsigned int file_version
  103. ){
  104. // note usage of function overloading to delay final resolution
  105. // until the point of instantiation. This works around the two-phase
  106. // lookup "feature" which inhibits redefintion of a default function
  107. // template implementation. Due to Robert Ramey
  108. //
  109. // Note that this trick generates problems for compiles which don't support
  110. // PFTO, suppress it here. As far as we know, there are no compilers
  111. // which fail to support PFTO while supporting two-phase lookup.
  112. #if ! defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
  113. const version_type v(file_version);
  114. serialize(ar, t, v);
  115. #else
  116. serialize(ar, t, file_version);
  117. #endif
  118. }
  119. template<class Archive, class T>
  120. inline void save_construct_data_adl(
  121. Archive & ar,
  122. const T * t,
  123. const unsigned int file_version
  124. ){
  125. // see above
  126. #if ! defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
  127. const version_type v(file_version);
  128. save_construct_data(ar, t, v);
  129. #else
  130. save_construct_data(ar, t, file_version);
  131. #endif
  132. }
  133. template<class Archive, class T>
  134. inline void load_construct_data_adl(
  135. Archive & ar,
  136. T * t,
  137. const unsigned int file_version
  138. ){
  139. // see above comment
  140. #if ! defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
  141. const version_type v(file_version);
  142. load_construct_data(ar, t, v);
  143. #else
  144. load_construct_data(ar, t, file_version);
  145. #endif
  146. }
  147. } // namespace serialization
  148. } // namespace boost
  149. #endif //BOOST_SERIALIZATION_SERIALIZATION_HPP