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