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>
32 #include <boost/serialization/force_include.hpp>
34 // function specializations must be defined in the appropriate
35 // namespace - boost::serialization
37 namespace serialization {
39 template<class Archive, class T>
42 const boost::optional< T > & t,
43 const unsigned int /*version*/
45 // It is an inherent limitation to the serialization of optional.hpp
46 // that the underlying type must be either a pointer or must have a
47 // default constructor. It's possible that this could change sometime
48 // in the future, but for now, one will have to work around it. This can
49 // be done by serialization the optional<T> as optional<T *>
50 #if ! defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
52 boost::serialization::detail::is_default_constructible<T>::value
53 || boost::is_pointer<T>::value
56 const bool tflag = t.is_initialized();
57 ar << boost::serialization::make_nvp("initialized", tflag);
59 ar << boost::serialization::make_nvp("value", *t);
63 template<class Archive, class T>
66 boost::optional< T > & t,
67 const unsigned int version
70 ar >> boost::serialization::make_nvp("initialized", tflag);
77 boost::serialization::item_version_type item_version(0);
78 boost::archive::library_version_type library_version(
79 ar.get_library_version()
81 if(boost::archive::library_version_type(3) < library_version){
82 ar >> BOOST_SERIALIZATION_NVP(item_version);
85 if(! t.is_initialized())
87 ar >> boost::serialization::make_nvp("value", *t);
90 template<class Archive, class T>
93 boost::optional< T > & t,
94 const unsigned int version
96 boost::serialization::split_free(ar, t, version);
100 struct version<boost::optional<T> > {
101 BOOST_STATIC_CONSTANT(int, value = 1);
107 #endif // BOOST_SERIALIZATION_OPTIONAL_HPP_