serialization.hpp 5.6 KB

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