serialization.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. namespace boost {
  38. namespace serialization {
  39. BOOST_STRONG_TYPEDEF(unsigned int, version_type)
  40. // default implementation - call the member function "serialize"
  41. template<class Archive, class T>
  42. inline void serialize(
  43. Archive & ar, T & t, const unsigned int file_version
  44. ){
  45. access::serialize(ar, t, static_cast<unsigned int>(file_version));
  46. }
  47. // save data required for construction
  48. template<class Archive, class T>
  49. inline void save_construct_data(
  50. Archive & /*ar*/,
  51. const T * /*t*/,
  52. const unsigned int /*file_version */
  53. ){
  54. // default is to save no data because default constructor
  55. // requires no arguments.
  56. }
  57. // load data required for construction and invoke constructor in place
  58. template<class Archive, class T>
  59. inline void load_construct_data(
  60. Archive & /*ar*/,
  61. T * t,
  62. const unsigned int /*file_version*/
  63. ){
  64. // default just uses the default constructor. going
  65. // through access permits usage of otherwise private default
  66. // constructor
  67. access::construct(t);
  68. }
  69. /////////////////////////////////////////////////////////////////////////////
  70. // layer 3 - move call into serialization namespace so that ADL will function
  71. // in the manner we desire.
  72. //
  73. // on compilers which don't implement ADL. only the current namespace
  74. // i.e. boost::serialization will be searched.
  75. //
  76. // on compilers which DO implement ADL
  77. // serialize overrides can be in any of the following
  78. //
  79. // 1) same namepace as Archive
  80. // 2) same namespace as T
  81. // 3) boost::serialization
  82. //
  83. // Due to Martin Ecker
  84. template<class Archive, class T>
  85. inline void serialize_adl(
  86. Archive & ar,
  87. T & t,
  88. const unsigned int file_version
  89. ){
  90. const version_type v(file_version);
  91. serialize(ar, t, v);
  92. }
  93. template<class Archive, class T>
  94. inline void save_construct_data_adl(
  95. Archive & ar,
  96. const T * t,
  97. const unsigned int file_version
  98. ){
  99. const version_type v(file_version);
  100. save_construct_data(ar, t, v);
  101. }
  102. template<class Archive, class T>
  103. inline void load_construct_data_adl(
  104. Archive & ar,
  105. T * t,
  106. const unsigned int file_version
  107. ){
  108. // see above comment
  109. const version_type v(file_version);
  110. load_construct_data(ar, t, v);
  111. }
  112. } // namespace serialization
  113. } // namespace boost
  114. #endif //BOOST_SERIALIZATION_SERIALIZATION_HPP