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