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