access.hpp 4.4 KB

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