nvp.hpp 4.4 KB

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