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