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)
14 # pragma once
15 #endif
16 
17 #include <boost/config.hpp>
18 
19 #include <boost/optional.hpp>
20 
21 #include <boost/serialization/item_version_type.hpp>
22 #include <boost/serialization/library_version_type.hpp>
23 #include <boost/serialization/version.hpp>
24 #include <boost/serialization/split_free.hpp>
25 #include <boost/serialization/nvp.hpp>
26 #include <boost/type_traits/is_pointer.hpp>
27 #include <boost/serialization/detail/is_default_constructible.hpp>
28 
29 // function specializations must be defined in the appropriate
30 // namespace - boost::serialization
31 namespace boost {
32 namespace serialization {
33 
34 template<class Archive, class T>
save(Archive & ar,const boost::optional<T> & t,const unsigned int)35 void save(
36     Archive & ar,
37     const boost::optional< T > & t,
38     const unsigned int /*version*/
39 ){
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
51     const bool tflag = t.is_initialized();
52     ar << boost::serialization::make_nvp("initialized", tflag);
53     if (tflag){
54         ar << boost::serialization::make_nvp("value", *t);
55     }
56 }
57 
58 template<class Archive, class T>
load(Archive & ar,boost::optional<T> & t,const unsigned int version)59 void load(
60     Archive & ar,
61     boost::optional< T > & t,
62     const unsigned int version
63 ){
64     bool tflag;
65     ar >> boost::serialization::make_nvp("initialized", tflag);
66     if(! tflag){
67         t.reset();
68         return;
69     }
70 
71     if(0 == version){
72         boost::serialization::item_version_type item_version(0);
73         boost::serialization::library_version_type library_version(
74             ar.get_library_version()
75         );
76         if(boost::serialization::library_version_type(3) < library_version){
77             ar >> BOOST_SERIALIZATION_NVP(item_version);
78         }
79     }
80     if(! t.is_initialized())
81         t = T();
82     ar >> boost::serialization::make_nvp("value", *t);
83 }
84 
85 template<class Archive, class T>
serialize(Archive & ar,boost::optional<T> & t,const unsigned int version)86 void serialize(
87     Archive & ar,
88     boost::optional< T > & t,
89     const unsigned int version
90 ){
91     boost::serialization::split_free(ar, t, version);
92 }
93 
94 template<class T>
95 struct version<boost::optional<T> > {
96     BOOST_STATIC_CONSTANT(int, value = 1);
97 };
98 
99 } // serialization
100 } // boost
101 
102 #endif // BOOST_SERIALIZATION_OPTIONAL_HPP_
103