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/optional.hpp>
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>
29 // function specializations must be defined in the appropriate
30 // namespace - boost::serialization
32 namespace serialization {
34 template<class Archive, class T>
37 const boost::optional< T > & t,
38 const unsigned int /*version*/
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)
47 boost::serialization::detail::is_default_constructible<T>::value
48 || boost::is_pointer<T>::value
51 const bool tflag = t.is_initialized();
52 ar << boost::serialization::make_nvp("initialized", tflag);
54 ar << boost::serialization::make_nvp("value", *t);
58 template<class Archive, class T>
61 boost::optional< T > & t,
62 const unsigned int version
65 ar >> boost::serialization::make_nvp("initialized", tflag);
72 boost::serialization::item_version_type item_version(0);
73 boost::serialization::library_version_type library_version(
74 ar.get_library_version()
76 if(boost::serialization::library_version_type(3) < library_version){
77 ar >> BOOST_SERIALIZATION_NVP(item_version);
80 if(! t.is_initialized())
82 ar >> boost::serialization::make_nvp("value", *t);
85 template<class Archive, class T>
88 boost::optional< T > & t,
89 const unsigned int version
91 boost::serialization::split_free(ar, t, version);
95 struct version<boost::optional<T> > {
96 BOOST_STATIC_CONSTANT(int, value = 1);
102 #endif // BOOST_SERIALIZATION_OPTIONAL_HPP_