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/serialization/level.hpp> |
25 | #include <boost/serialization/tracking.hpp> |
26 | #include <boost/serialization/split_member.hpp> |
27 | #include <boost/serialization/base_object.hpp> |
28 | #include <boost/serialization/traits.hpp> |
29 | #include <boost/serialization/wrapper.hpp> |
30 | |
31 | namespace boost { |
32 | namespace serialization { |
33 | |
34 | template<class T> |
35 | struct nvp : |
36 | public std::pair<const char *, T *>, |
37 | public wrapper_traits<const nvp< T > > |
38 | { |
39 | //private: |
40 | nvp(const nvp & rhs) : |
41 | std::pair<const char *, T *>(rhs.first, rhs.second) |
42 | {} |
43 | public: |
44 | explicit nvp(const char * name_, T & t) : |
45 | // note: added _ to suppress useless gcc warning |
46 | std::pair<const char *, T *>(name_, & t) |
47 | {} |
48 | |
49 | const char * name() const { |
50 | return this->first; |
51 | } |
52 | T & value() const { |
53 | return *(this->second); |
54 | } |
55 | |
56 | const T & const_value() const { |
57 | return *(this->second); |
58 | } |
59 | |
60 | template<class Archive> |
61 | void save( |
62 | Archive & ar, |
63 | const unsigned int /* file_version */ |
64 | ) const { |
65 | ar.operator<<(const_value()); |
66 | } |
67 | template<class Archive> |
68 | void load( |
69 | Archive & ar, |
70 | const unsigned int /* file_version */ |
71 | ){ |
72 | ar.operator>>(value()); |
73 | } |
74 | BOOST_SERIALIZATION_SPLIT_MEMBER() |
75 | }; |
76 | |
77 | template<class T> |
78 | inline |
79 | const nvp< T > make_nvp(const char * name, T & t){ |
80 | return nvp< T >(name, t); |
81 | } |
82 | |
83 | // to maintain efficiency and portability, we want to assign |
84 | // specific serialization traits to all instances of this wrappers. |
85 | // we can't strait forward method below as it depends upon |
86 | // Partial Template Specialization and doing so would mean that wrappers |
87 | // wouldn't be treated the same on different platforms. This would |
88 | // break archive portability. Leave this here as reminder not to use it !!! |
89 | |
90 | template <class T> |
91 | struct implementation_level<nvp< T > > |
92 | { |
93 | typedef mpl::integral_c_tag tag; |
94 | typedef mpl::int_<object_serializable> type; |
95 | BOOST_STATIC_CONSTANT(int, value = implementation_level::type::value); |
96 | }; |
97 | |
98 | // nvp objects are generally created on the stack and are never tracked |
99 | template<class T> |
100 | struct tracking_level<nvp< T > > |
101 | { |
102 | typedef mpl::integral_c_tag tag; |
103 | typedef mpl::int_<track_never> type; |
104 | BOOST_STATIC_CONSTANT(int, value = tracking_level::type::value); |
105 | }; |
106 | |
107 | } // seralization |
108 | } // boost |
109 | |
110 | #include <boost/preprocessor/stringize.hpp> |
111 | |
112 | #define BOOST_SERIALIZATION_NVP(name) \ |
113 | boost::serialization::make_nvp(BOOST_PP_STRINGIZE(name), name) |
114 | /**/ |
115 | |
116 | #define BOOST_SERIALIZATION_BASE_OBJECT_NVP(name) \ |
117 | boost::serialization::make_nvp( \ |
118 | BOOST_PP_STRINGIZE(name), \ |
119 | boost::serialization::base_object<name >(*this) \ |
120 | ) |
121 | /**/ |
122 | |
123 | #endif // BOOST_SERIALIZATION_NVP_HPP |
124 | |