]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
2 | ||
f67539c2 | 3 | // (C) Copyright 2002-4 Pavel Vozenilek . |
7c673cae FG |
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) | |
14 | # pragma once | |
15 | #endif | |
16 | ||
17 | #include <boost/config.hpp> | |
18 | ||
7c673cae | 19 | #include <boost/optional.hpp> |
b32b8144 | 20 | |
7c673cae | 21 | #include <boost/serialization/item_version_type.hpp> |
20effc67 TL |
22 | #include <boost/serialization/library_version_type.hpp> |
23 | #include <boost/serialization/version.hpp> | |
7c673cae | 24 | #include <boost/serialization/split_free.hpp> |
7c673cae | 25 | #include <boost/serialization/nvp.hpp> |
b32b8144 | 26 | #include <boost/type_traits/is_pointer.hpp> |
b32b8144 | 27 | #include <boost/serialization/detail/is_default_constructible.hpp> |
7c673cae FG |
28 | |
29 | // function specializations must be defined in the appropriate | |
30 | // namespace - boost::serialization | |
f67539c2 | 31 | namespace boost { |
7c673cae FG |
32 | namespace serialization { |
33 | ||
34 | template<class Archive, class T> | |
35 | void save( | |
f67539c2 TL |
36 | Archive & ar, |
37 | const boost::optional< T > & t, | |
7c673cae FG |
38 | const unsigned int /*version*/ |
39 | ){ | |
b32b8144 FG |
40 | // It is an inherent limitation to the serialization of optional.hpp |
41 | // that the underlying type must be either a pointer or must have a | |
42 | // default constructor. It's possible that this could change sometime | |
43 | // in the future, but for now, one will have to work around it. This can | |
44 | // be done by serialization the optional<T> as optional<T *> | |
45 | #if ! defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS) | |
46 | BOOST_STATIC_ASSERT( | |
47 | boost::serialization::detail::is_default_constructible<T>::value | |
48 | || boost::is_pointer<T>::value | |
49 | ); | |
50 | #endif | |
7c673cae FG |
51 | const bool tflag = t.is_initialized(); |
52 | ar << boost::serialization::make_nvp("initialized", tflag); | |
53 | if (tflag){ | |
7c673cae FG |
54 | ar << boost::serialization::make_nvp("value", *t); |
55 | } | |
56 | } | |
57 | ||
58 | template<class Archive, class T> | |
59 | void load( | |
f67539c2 TL |
60 | Archive & ar, |
61 | boost::optional< T > & t, | |
b32b8144 | 62 | const unsigned int version |
7c673cae FG |
63 | ){ |
64 | bool tflag; | |
65 | ar >> boost::serialization::make_nvp("initialized", tflag); | |
b32b8144 FG |
66 | if(! tflag){ |
67 | t.reset(); | |
68 | return; | |
69 | } | |
70 | ||
71 | if(0 == version){ | |
7c673cae | 72 | boost::serialization::item_version_type item_version(0); |
20effc67 | 73 | boost::serialization::library_version_type library_version( |
7c673cae FG |
74 | ar.get_library_version() |
75 | ); | |
20effc67 | 76 | if(boost::serialization::library_version_type(3) < library_version){ |
b32b8144 | 77 | ar >> BOOST_SERIALIZATION_NVP(item_version); |
7c673cae | 78 | } |
7c673cae | 79 | } |
b32b8144 FG |
80 | if(! t.is_initialized()) |
81 | t = T(); | |
82 | ar >> boost::serialization::make_nvp("value", *t); | |
7c673cae FG |
83 | } |
84 | ||
85 | template<class Archive, class T> | |
86 | void serialize( | |
f67539c2 TL |
87 | Archive & ar, |
88 | boost::optional< T > & t, | |
7c673cae FG |
89 | const unsigned int version |
90 | ){ | |
91 | boost::serialization::split_free(ar, t, version); | |
92 | } | |
93 | ||
b32b8144 FG |
94 | template<class T> |
95 | struct version<boost::optional<T> > { | |
96 | BOOST_STATIC_CONSTANT(int, value = 1); | |
97 | }; | |
98 | ||
7c673cae | 99 | } // serialization |
b32b8144 | 100 | } // boost |
7c673cae FG |
101 | |
102 | #endif // BOOST_SERIALIZATION_OPTIONAL_HPP_ |