access.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #ifndef BOOST_SERIALIZATION_ACCESS_HPP
  2. #define BOOST_SERIALIZATION_ACCESS_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER)
  5. # pragma once
  6. #endif
  7. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  8. // access.hpp: interface for serialization system.
  9. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  10. // Use, modification and distribution is subject to the Boost Software
  11. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. // See http://www.boost.org for updates, documentation, and revision history.
  14. #include <boost/config.hpp>
  15. namespace boost {
  16. namespace archive {
  17. namespace detail {
  18. template<class Archive, class T>
  19. class iserializer;
  20. template<class Archive, class T>
  21. class oserializer;
  22. } // namespace detail
  23. } // namespace archive
  24. namespace serialization {
  25. // forward declarations
  26. template<class Archive, class T>
  27. inline void serialize_adl(Archive &, T &, const unsigned int);
  28. namespace detail {
  29. template<class Archive, class T>
  30. struct member_saver;
  31. template<class Archive, class T>
  32. struct member_loader;
  33. } // namespace detail
  34. // use an "accessor class so that we can use:
  35. // "friend class boost::serialization::access;"
  36. // in any serialized class to permit clean, safe access to private class members
  37. // by the serialization system
  38. class access {
  39. public:
  40. // grant access to "real" serialization defaults
  41. #ifdef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  42. public:
  43. #else
  44. template<class Archive, class T>
  45. friend struct detail::member_saver;
  46. template<class Archive, class T>
  47. friend struct detail::member_loader;
  48. template<class Archive, class T>
  49. friend class archive::detail::iserializer;
  50. template<class Archive, class T>
  51. friend class archive::detail::oserializer;
  52. template<class Archive, class T>
  53. friend inline void serialize(
  54. Archive & ar,
  55. T & t,
  56. const unsigned int file_version
  57. );
  58. template<class Archive, class T>
  59. friend inline void save_construct_data(
  60. Archive & ar,
  61. const T * t,
  62. const unsigned int file_version
  63. );
  64. template<class Archive, class T>
  65. friend inline void load_construct_data(
  66. Archive & ar,
  67. T * t,
  68. const unsigned int file_version
  69. );
  70. #endif
  71. // pass calls to users's class implementation
  72. template<class Archive, class T>
  73. static void member_save(
  74. Archive & ar,
  75. //const T & t,
  76. T & t,
  77. const unsigned int file_version
  78. ){
  79. t.save(ar, file_version);
  80. }
  81. template<class Archive, class T>
  82. static void member_load(
  83. Archive & ar,
  84. T & t,
  85. const unsigned int file_version
  86. ){
  87. t.load(ar, file_version);
  88. }
  89. template<class Archive, class T>
  90. static void serialize(
  91. Archive & ar,
  92. T & t,
  93. const unsigned int file_version
  94. ){
  95. // note: if you get a compile time error here with a
  96. // message something like:
  97. // cannot convert parameter 1 from <file type 1> to <file type 2 &>
  98. // a likely possible cause is that the class T contains a
  99. // serialize function - but that serialize function isn't
  100. // a template and corresponds to a file type different than
  101. // the class Archive. To resolve this, don't include an
  102. // archive type other than that for which the serialization
  103. // function is defined!!!
  104. t.serialize(ar, file_version);
  105. }
  106. template<class T>
  107. static void destroy( const T * t) // const appropriate here?
  108. {
  109. // the const business is an MSVC 6.0 hack that should be
  110. // benign on everything else
  111. delete const_cast<T *>(t);
  112. }
  113. template<class T>
  114. static void construct(T * t){
  115. // default is inplace invocation of default constructor
  116. // Note the :: before the placement new. Required if the
  117. // class doesn't have a class-specific placement new defined.
  118. ::new(t)T;
  119. }
  120. template<class T, class U>
  121. static T & cast_reference(U & u){
  122. return static_cast<T &>(u);
  123. }
  124. template<class T, class U>
  125. static T * cast_pointer(U * u){
  126. return static_cast<T *>(u);
  127. }
  128. };
  129. } // namespace serialization
  130. } // namespace boost
  131. #endif // BOOST_SERIALIZATION_ACCESS_HPP