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_
13 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
17 #include <boost/config.hpp>
19 #include <boost/archive/detail/basic_iarchive.hpp>
21 #include <boost/optional.hpp>
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/version.hpp>
27 #include <boost/serialization/detail/stack_constructor.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 const bool tflag = t.is_initialized();
41 ar << boost::serialization::make_nvp("initialized", tflag);
43 const boost::serialization::item_version_type item_version(version< T >::value);
45 const boost::archive::library_version_type library_version(
46 ar.get_library_version()
48 if(boost::archive::library_version_type(3) < library_version){
49 ar << BOOST_SERIALIZATION_NVP(item_version);
52 ar << BOOST_SERIALIZATION_NVP(item_version);
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);
67 boost::serialization::item_version_type item_version(0);
68 boost::archive::library_version_type library_version(
69 ar.get_library_version()
71 if(boost::archive::library_version_type(3) < library_version){
72 // item_version is handled as an attribute so it doesnt need an NVP
73 ar >> BOOST_SERIALIZATION_NVP(item_version);
75 detail::stack_construct<Archive, T> aux(ar, item_version);
76 ar >> boost::serialization::make_nvp("value", aux.reference());
77 t.reset(aux.reference());
84 template<class Archive, class T>
87 boost::optional< T > & t,
88 const unsigned int version
90 boost::serialization::split_free(ar, t, version);
93 // the following would be slightly more efficient. But it
94 // would mean that archives created with programs that support
95 // TPS wouldn't be readable by programs that don't support TPS.
96 // Hence we decline to support this otherwise convenient optimization.
97 //#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
101 struct implementation_level<optional< T > >
103 typedef mpl::integral_c_tag tag;
104 typedef mpl::int_<boost::serialization::object_serializable> type;
105 BOOST_STATIC_CONSTANT(
107 value = boost::serialization::implementation_level::type::value
112 struct tracking_level<optional< T > >
114 typedef mpl::integral_c_tag tag;
115 typedef mpl::int_<boost::serialization::track_never> type;
116 BOOST_STATIC_CONSTANT(
118 value = boost::serialization::tracking_level::type::value
127 #endif // BOOST_SERIALIZATION_OPTIONAL_HPP_