1 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
---|
2 | |
---|
3 | // (C) Copyright 2002-4 Pavel Vozenilek . |
---|
4 | // Use, modification and distribution is subject to the Boost Software |
---|
5 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
6 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
7 | |
---|
8 | // Provides non-intrusive serialization for boost::optional. |
---|
9 | |
---|
10 | #ifndef BOOST_SERIALIZATION_OPTIONAL_HPP_ |
---|
11 | #define BOOST_SERIALIZATION_OPTIONAL_HPP_ |
---|
12 | |
---|
13 | #if defined(_MSC_VER) && (_MSC_VER >= 1020) |
---|
14 | # pragma once |
---|
15 | #endif |
---|
16 | |
---|
17 | #include <boost/config.hpp> |
---|
18 | |
---|
19 | #include <boost/optional.hpp> |
---|
20 | #include <boost/serialization/split_free.hpp> |
---|
21 | #include <boost/serialization/level.hpp> |
---|
22 | #include <boost/serialization/nvp.hpp> |
---|
23 | #include <boost/serialization/detail/stack_constructor.hpp> |
---|
24 | |
---|
25 | // function specializations must be defined in the appropriate |
---|
26 | // namespace - boost::serialization |
---|
27 | namespace boost { |
---|
28 | namespace serialization { |
---|
29 | |
---|
30 | template<class Archive, class T> |
---|
31 | void save( |
---|
32 | Archive & ar, |
---|
33 | const boost::optional<T> & t, |
---|
34 | const unsigned int /*version*/ |
---|
35 | ){ |
---|
36 | const bool tflag = t; |
---|
37 | ar << boost::serialization::make_nvp("initialized", tflag); |
---|
38 | if (tflag) |
---|
39 | ar << boost::serialization::make_nvp("value", *t); |
---|
40 | } |
---|
41 | |
---|
42 | template<class Archive, class T> |
---|
43 | void load( |
---|
44 | Archive & ar, |
---|
45 | boost::optional<T> & t, |
---|
46 | const unsigned int /*version*/ |
---|
47 | ){ |
---|
48 | bool tflag; |
---|
49 | ar >> boost::serialization::make_nvp("initialized", tflag); |
---|
50 | if (tflag){ |
---|
51 | detail::stack_construct<Archive, T> aux(ar); |
---|
52 | ar >> boost::serialization::make_nvp("value", aux.reference()); |
---|
53 | t.reset(aux.reference()); |
---|
54 | } |
---|
55 | else { |
---|
56 | t.reset(); |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | template<class Archive, class T> |
---|
61 | void serialize( |
---|
62 | Archive & ar, |
---|
63 | boost::optional<T> & t, |
---|
64 | const unsigned int version |
---|
65 | ){ |
---|
66 | boost::serialization::split_free(ar, t, version); |
---|
67 | } |
---|
68 | |
---|
69 | // the following would be slightly more efficient. But it |
---|
70 | // would mean that archives created with programs that support |
---|
71 | // TPS wouldn't be readable by programs that don't support TPS. |
---|
72 | // Hence we decline to support this otherwise convenient optimization. |
---|
73 | //#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION |
---|
74 | #if 0 |
---|
75 | |
---|
76 | template <class T> |
---|
77 | struct implementation_level<optional<T> > |
---|
78 | { |
---|
79 | typedef mpl::integral_c_tag tag; |
---|
80 | typedef mpl::int_<boost::serialization::object_serializable> type; |
---|
81 | BOOST_STATIC_CONSTANT( |
---|
82 | int , |
---|
83 | value = boost::serialization::implementation_level::type::value |
---|
84 | ); |
---|
85 | }; |
---|
86 | |
---|
87 | template<class T> |
---|
88 | struct tracking_level<optional<T> > |
---|
89 | { |
---|
90 | typedef mpl::integral_c_tag tag; |
---|
91 | typedef mpl::int_<boost::serialization::track_never> type; |
---|
92 | BOOST_STATIC_CONSTANT( |
---|
93 | int , |
---|
94 | value = boost::serialization::tracking_level::type::value |
---|
95 | ); |
---|
96 | }; |
---|
97 | |
---|
98 | #endif |
---|
99 | |
---|
100 | } // serialization |
---|
101 | } // namespace boost |
---|
102 | |
---|
103 | #endif // BOOST_SERIALIZATION_OPTIONAL_HPP_ |
---|