serialization.hpp 5.1 KB

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