fix doc example typo
[boost.git] / boost / serialization / optional.hpp
blob68d236b71df1252fdc4bfee673f0588b6bc04596
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)
14 # pragma once
15 #endif
17 #include <boost/config.hpp>
19 #include <boost/optional.hpp>
20 #include <boost/serialization/split_free.hpp>
21 #include <boost/serialization/level.hpp>
22 #include <boost/serialization/nvp.hpp>
23 #include <boost/serialization/version.hpp>
24 #include <boost/serialization/detail/stack_constructor.hpp>
26 // function specializations must be defined in the appropriate
27 // namespace - boost::serialization
28 namespace boost {
29 namespace serialization {
31 template<class Archive, class T>
32 void save(
33 Archive & ar,
34 const boost::optional<T> & t,
35 const unsigned int /*version*/
37 const bool tflag = t.is_initialized();
38 ar << boost::serialization::make_nvp("initialized", tflag);
39 if (tflag){
40 if(3 < ar.get_library_version()){
41 const int v = version<T>::value;
42 ar << boost::serialization::make_nvp("item_version", v);
44 ar << boost::serialization::make_nvp("value", *t);
48 template<class Archive, class T>
49 void load(
50 Archive & ar,
51 boost::optional<T> & t,
52 const unsigned int /*version*/
54 bool tflag;
55 ar >> boost::serialization::make_nvp("initialized", tflag);
56 if (tflag){
57 unsigned int v = 0;
58 if(3 < ar.get_library_version()){
59 ar >> boost::serialization::make_nvp("item_version", v);
61 detail::stack_construct<Archive, T> aux(ar, v);
62 ar >> boost::serialization::make_nvp("value", aux.reference());
63 t.reset(aux.reference());
65 else {
66 t.reset();
70 template<class Archive, class T>
71 void serialize(
72 Archive & ar,
73 boost::optional<T> & t,
74 const unsigned int version
76 boost::serialization::split_free(ar, t, version);
79 // the following would be slightly more efficient. But it
80 // would mean that archives created with programs that support
81 // TPS wouldn't be readable by programs that don't support TPS.
82 // Hence we decline to support this otherwise convenient optimization.
83 //#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
84 #if 0
86 template <class T>
87 struct implementation_level<optional<T> >
89 typedef mpl::integral_c_tag tag;
90 typedef mpl::int_<boost::serialization::object_serializable> type;
91 BOOST_STATIC_CONSTANT(
92 int ,
93 value = boost::serialization::implementation_level::type::value
97 template<class T>
98 struct tracking_level<optional<T> >
100 typedef mpl::integral_c_tag tag;
101 typedef mpl::int_<boost::serialization::track_never> type;
102 BOOST_STATIC_CONSTANT(
103 int ,
104 value = boost::serialization::tracking_level::type::value
108 #endif
110 } // serialization
111 } // namespace boost
113 #endif // BOOST_SERIALIZATION_OPTIONAL_HPP_