nvp.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #ifndef BOOST_SERIALIZATION_NVP_HPP
  2. #define BOOST_SERIALIZATION_NVP_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. // nvp.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 <utility>
  15. #include <boost/config.hpp>
  16. #include <boost/detail/workaround.hpp>
  17. #include <boost/serialization/level.hpp>
  18. #include <boost/serialization/tracking.hpp>
  19. #include <boost/serialization/split_member.hpp>
  20. #include <boost/serialization/base_object.hpp>
  21. #include <boost/serialization/traits.hpp>
  22. #include <boost/serialization/wrapper.hpp>
  23. #include <boost/core/addressof.hpp>
  24. namespace boost {
  25. namespace serialization {
  26. template<class T>
  27. struct nvp :
  28. public std::pair<const char *, T *>,
  29. public wrapper_traits<const nvp< T > >
  30. {
  31. //private:
  32. nvp(const nvp & rhs) :
  33. std::pair<const char *, T *>(rhs.first, rhs.second)
  34. {}
  35. public:
  36. explicit nvp(const char * name_, T & t) :
  37. // note: added _ to suppress useless gcc warning
  38. std::pair<const char *, T *>(name_, boost::addressof(t))
  39. {}
  40. const char * name() const {
  41. return this->first;
  42. }
  43. T & value() const {
  44. return *(this->second);
  45. }
  46. const T & const_value() const {
  47. return *(this->second);
  48. }
  49. template<class Archive>
  50. void save(
  51. Archive & ar,
  52. const unsigned int /* file_version */
  53. ) const {
  54. ar.operator<<(const_value());
  55. }
  56. template<class Archive>
  57. void load(
  58. Archive & ar,
  59. const unsigned int /* file_version */
  60. ){
  61. ar.operator>>(value());
  62. }
  63. BOOST_SERIALIZATION_SPLIT_MEMBER()
  64. };
  65. template<class T>
  66. inline
  67. const nvp< T > make_nvp(const char * name, T & t){
  68. return nvp< T >(name, t);
  69. }
  70. // to maintain efficiency and portability, we want to assign
  71. // specific serialization traits to all instances of this wrappers.
  72. // we can't strait forward method below as it depends upon
  73. // Partial Template Specialization and doing so would mean that wrappers
  74. // wouldn't be treated the same on different platforms. This would
  75. // break archive portability. Leave this here as reminder not to use it !!!
  76. template <class T>
  77. struct implementation_level<nvp< T > >
  78. {
  79. typedef mpl::integral_c_tag tag;
  80. typedef mpl::int_<object_serializable> type;
  81. BOOST_STATIC_CONSTANT(int, value = implementation_level::type::value);
  82. };
  83. // nvp objects are generally created on the stack and are never tracked
  84. template<class T>
  85. struct tracking_level<nvp< T > >
  86. {
  87. typedef mpl::integral_c_tag tag;
  88. typedef mpl::int_<track_never> type;
  89. BOOST_STATIC_CONSTANT(int, value = tracking_level::type::value);
  90. };
  91. } // seralization
  92. } // boost
  93. #include <boost/preprocessor/stringize.hpp>
  94. #define BOOST_SERIALIZATION_NVP(name) \
  95. boost::serialization::make_nvp(BOOST_PP_STRINGIZE(name), name)
  96. /**/
  97. #define BOOST_SERIALIZATION_BASE_OBJECT_NVP(name) \
  98. boost::serialization::make_nvp( \
  99. BOOST_PP_STRINGIZE(name), \
  100. boost::serialization::base_object<name >(*this) \
  101. )
  102. /**/
  103. #endif // BOOST_SERIALIZATION_NVP_HPP