1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
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)
8 // Provides non-intrusive serialization for boost::optional.
10 #ifndef BOOST_SERIALIZATION_OPTIONAL_HPP_
11 #define BOOST_SERIALIZATION_OPTIONAL_HPP_
17 #include <boost/config.hpp>
19 #include <boost/archive/detail/basic_iarchive.hpp>
21 #include <boost/optional.hpp>
22 #include <boost/move/utility_core.hpp>
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>
33 // function specializations must be defined in the appropriate
34 // namespace - boost::serialization
36 namespace serialization {
38 template<class Archive, class T>
41 const boost::optional< T > & t,
42 const unsigned int /*version*/
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 *>
50 boost::serialization::detail::is_default_constructible<T>::value
51 || boost::is_pointer<T>::value
53 const bool tflag = t.is_initialized();
54 ar << boost::serialization::make_nvp("initialized", tflag);
56 const boost::serialization::item_version_type item_version(version< T >::value);
58 const boost::archive::library_version_type library_version(
59 ar.get_library_version()
61 if(boost::archive::library_version_type(3) < library_version){
62 ar << BOOST_SERIALIZATION_NVP(item_version);
65 ar << BOOST_SERIALIZATION_NVP(item_version);
67 ar << boost::serialization::make_nvp("value", *t);
71 template<class Archive, class T>
74 boost::optional< T > & t,
75 const unsigned int /*version*/
78 ar >> boost::serialization::make_nvp("initialized", tflag);
84 boost::serialization::item_version_type item_version(0);
85 boost::archive::library_version_type library_version(
86 ar.get_library_version()
88 if(boost::archive::library_version_type(3) < library_version){
89 ar >> BOOST_SERIALIZATION_NVP(item_version);
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(
100 template<class Archive, class T>
103 boost::optional< T > & t,
104 const unsigned int version
106 boost::serialization::split_free(ar, t, version);
112 #endif // BOOST_SERIALIZATION_OPTIONAL_HPP_