fix doc example typo
[boost.git] / boost / serialization / nvp.hpp
blobade67b425bc8f69c042797243b10b647d64947cc
1 #ifndef BOOST_SERIALIZATION_NVP_HPP
2 #define BOOST_SERIALIZATION_NVP_HPP
4 // MS compatible compilers support #pragma once
5 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
6 # pragma once
7 #endif
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10 // nvp.hpp: interface for serialization system.
12 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
13 // Use, modification and distribution is subject to the Boost Software
14 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
15 // http://www.boost.org/LICENSE_1_0.txt)
17 // See http://www.boost.org for updates, documentation, and revision history.
19 #include <utility>
21 #include <boost/config.hpp>
22 #include <boost/detail/workaround.hpp>
23 // supress noise
24 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1200)
25 # pragma warning (disable : 4786) // too long name, harmless warning
26 #endif
28 #include <boost/mpl/integral_c.hpp>
29 #include <boost/mpl/integral_c_tag.hpp>
31 #include <boost/serialization/level.hpp>
32 #include <boost/serialization/tracking.hpp>
33 #include <boost/serialization/split_member.hpp>
34 #include <boost/serialization/base_object.hpp>
35 #include <boost/serialization/traits.hpp>
36 #include <boost/serialization/wrapper.hpp>
38 namespace boost {
39 namespace serialization {
41 template<class T>
42 struct nvp :
43 public std::pair<const char *, T *>,
44 public wrapper_traits<nvp<T> >
46 explicit nvp(const char * name, T & t) :
47 // note: redundant cast works around borland issue
48 std::pair<const char *, T *>(name, (T*)(& t))
50 nvp(const nvp & rhs) :
51 // note: redundant cast works around borland issue
52 std::pair<const char *, T *>(rhs.first, (T*)rhs.second)
55 const char * name() const {
56 return this->first;
58 T & value() const {
59 return *(this->second);
62 const T & const_value() const {
63 return *(this->second);
66 // True64 compiler complains with a warning about the use of
67 // the name "Archive" hiding some higher level usage. I'm sure this
68 // is an error but I want to accomodated as it generates a long warning
69 // listing and might be related to a lot of test failures.
70 // default treatment for name-value pairs. The name is
71 // just discarded and only the value is serialized.
72 template<class Archivex>
73 void save(
74 Archivex & ar,
75 const unsigned int /* file_version */
76 ) const {
77 // CodeWarrior 8.x can't seem to resolve the << op for a rhs of "const T *"
78 ar.operator<<(const_value());
80 template<class Archivex>
81 void load(
82 Archivex & ar,
83 const unsigned int /* file_version */
85 // CodeWarrior 8.x can't seem to resolve the >> op for a rhs of "const T *"
86 ar.operator>>(value());
88 BOOST_SERIALIZATION_SPLIT_MEMBER()
91 template<class T>
92 inline
93 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
94 const
95 #endif
96 nvp<T> make_nvp(const char * name, T & t){
97 return nvp<T>(name, t);
100 // to maintain efficiency and portability, we want to assign
101 // specific serialization traits to all instances of this wrappers.
102 // we can't strait forward method below as it depends upon
103 // Partial Template Specialization and doing so would mean that wrappers
104 // wouldn't be treated the same on different platforms. This would
105 // break archive portability. Leave this here as reminder not to use it !!!
106 #if 0 // #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
108 template <class T>
109 struct implementation_level<nvp<T> >
111 typedef mpl::integral_c_tag tag;
112 typedef mpl::int_<object_serializable> type;
113 BOOST_STATIC_CONSTANT(int, value = implementation_level::type::value);
116 // nvp objects are generally created on the stack and are never tracked
117 template<class T>
118 struct tracking_level<nvp<T> >
120 typedef mpl::integral_c_tag tag;
121 typedef mpl::int_<track_never> type;
122 BOOST_STATIC_CONSTANT(int, value = tracking_level::type::value);
125 #endif
127 } // seralization
128 } // boost
130 #include <boost/preprocessor/stringize.hpp>
132 #define BOOST_SERIALIZATION_NVP(name) \
133 boost::serialization::make_nvp(BOOST_PP_STRINGIZE(name), name)
134 /**/
136 #define BOOST_SERIALIZATION_BASE_OBJECT_NVP(name) \
137 boost::serialization::make_nvp( \
138 BOOST_PP_STRINGIZE(name), \
139 boost::serialization::base_object<name >(*this) \
141 /**/
143 #endif // BOOST_SERIALIZATION_NVP_HPP