nvp.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #ifndef BOOST_SERIALIZATION_NVP_HPP
  2. #define BOOST_SERIALIZATION_NVP_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. // 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. // supress noise
  18. #if BOOST_WORKAROUND(BOOST_MSVC, <= 1200)
  19. # pragma warning (disable : 4786) // too long name, harmless warning
  20. #endif
  21. #include <boost/mpl/integral_c.hpp>
  22. #include <boost/mpl/integral_c_tag.hpp>
  23. #include <boost/serialization/level.hpp>
  24. #include <boost/serialization/tracking.hpp>
  25. #include <boost/serialization/split_member.hpp>
  26. #include <boost/serialization/base_object.hpp>
  27. #include <boost/serialization/traits.hpp>
  28. #include <boost/serialization/wrapper.hpp>
  29. namespace boost {
  30. namespace serialization {
  31. template<class T>
  32. struct nvp :
  33. public std::pair<const char *, T *>,
  34. public wrapper_traits<const nvp< T > >
  35. {
  36. explicit nvp(const char * name_, T & t) :
  37. // note: redundant cast works around borland issue
  38. // note: added _ to suppress useless gcc warning
  39. std::pair<const char *, T *>(name_, (T*)(& t))
  40. {}
  41. nvp(const nvp & rhs) :
  42. // note: redundant cast works around borland issue
  43. std::pair<const char *, T *>(rhs.first, (T*)rhs.second)
  44. {}
  45. const char * name() const {
  46. return this->first;
  47. }
  48. T & value() const {
  49. return *(this->second);
  50. }
  51. const T & const_value() const {
  52. return *(this->second);
  53. }
  54. // True64 compiler complains with a warning about the use of
  55. // the name "Archive" hiding some higher level usage. I'm sure this
  56. // is an error but I want to accomodated as it generates a long warning
  57. // listing and might be related to a lot of test failures.
  58. // default treatment for name-value pairs. The name is
  59. // just discarded and only the value is serialized.
  60. template<class Archivex>
  61. void save(
  62. Archivex & ar,
  63. const unsigned int /* file_version */
  64. ) const {
  65. // CodeWarrior 8.x can't seem to resolve the << op for a rhs of "const T *"
  66. ar.operator<<(const_value());
  67. }
  68. template<class Archivex>
  69. void load(
  70. Archivex & ar,
  71. const unsigned int /* file_version */
  72. ){
  73. // CodeWarrior 8.x can't seem to resolve the >> op for a rhs of "const T *"
  74. ar.operator>>(value());
  75. }
  76. BOOST_SERIALIZATION_SPLIT_MEMBER()
  77. };
  78. template<class T>
  79. inline
  80. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  81. const
  82. #endif
  83. nvp< T > make_nvp(const char * name, T & t){
  84. return nvp< T >(name, t);
  85. }
  86. // to maintain efficiency and portability, we want to assign
  87. // specific serialization traits to all instances of this wrappers.
  88. // we can't strait forward method below as it depends upon
  89. // Partial Template Specialization and doing so would mean that wrappers
  90. // wouldn't be treated the same on different platforms. This would
  91. // break archive portability. Leave this here as reminder not to use it !!!
  92. #if 0 // #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  93. template <class T>
  94. struct implementation_level<nvp< T > >
  95. {
  96. typedef mpl::integral_c_tag tag;
  97. typedef mpl::int_<object_serializable> type;
  98. BOOST_STATIC_CONSTANT(int, value = implementation_level::type::value);
  99. };
  100. // nvp objects are generally created on the stack and are never tracked
  101. template<class T>
  102. struct tracking_level<nvp< T > >
  103. {
  104. typedef mpl::integral_c_tag tag;
  105. typedef mpl::int_<track_never> type;
  106. BOOST_STATIC_CONSTANT(int, value = tracking_level::type::value);
  107. };
  108. #endif
  109. } // seralization
  110. } // boost
  111. #include <boost/preprocessor/stringize.hpp>
  112. #define BOOST_SERIALIZATION_NVP(name) \
  113. boost::serialization::make_nvp(BOOST_PP_STRINGIZE(name), name)
  114. /**/
  115. #define BOOST_SERIALIZATION_BASE_OBJECT_NVP(name) \
  116. boost::serialization::make_nvp( \
  117. BOOST_PP_STRINGIZE(name), \
  118. boost::serialization::base_object<name >(*this) \
  119. )
  120. /**/
  121. #endif // BOOST_SERIALIZATION_NVP_HPP