1/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2
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)
7
8// Provides non-intrusive serialization for boost::optional.
9
10#ifndef BOOST_SERIALIZATION_OPTIONAL_HPP_
11#define BOOST_SERIALIZATION_OPTIONAL_HPP_
12
13#if defined(_MSC_VER)
14# pragma once
15#endif
16
17#include <boost/config.hpp>
18
19#include <boost/archive/detail/basic_iarchive.hpp>
20
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>
28
29// function specializations must be defined in the appropriate
30// namespace - boost::serialization
31namespace boost {
32namespace serialization {
33
34template<class Archive, class T>
35void save(
36 Archive & ar,
37 const boost::optional< T > & t,
38 const unsigned int /*version*/
39){
40 const bool tflag = t.is_initialized();
41 ar << boost::serialization::make_nvp(name: "initialized", t: tflag);
42 if (tflag){
43 const boost::serialization::item_version_type item_version(version< T >::value);
44 #if 0
45 const boost::archive::library_version_type library_version(
46 ar.get_library_version()
47 };
48 if(boost::archive::library_version_type(3) < library_version){
49 ar << BOOST_SERIALIZATION_NVP(item_version);
50 }
51 #else
52 ar << BOOST_SERIALIZATION_NVP(item_version);
53 #endif
54 ar << boost::serialization::make_nvp("value", *t);
55 }
56}
57
58template<class Archive, class T>
59void load(
60 Archive & ar,
61 boost::optional< T > & t,
62 const unsigned int /*version*/
63){
64 bool tflag;
65 ar >> boost::serialization::make_nvp(name: "initialized", t&: tflag);
66 if (tflag){
67 boost::serialization::item_version_type item_version(0);
68 boost::archive::library_version_type library_version(
69 ar.get_library_version()
70 );
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);
74 }
75 detail::stack_construct<Archive, T> aux(ar, item_version);
76 ar >> boost::serialization::make_nvp("value", aux.reference());
77 t.reset(aux.reference());
78 }
79 else {
80 t.reset();
81 }
82}
83
84template<class Archive, class T>
85void serialize(
86 Archive & ar,
87 boost::optional< T > & t,
88 const unsigned int version
89){
90 boost::serialization::split_free(ar, t, version);
91}
92
93} // serialization
94} // namespace boost
95
96#endif // BOOST_SERIALIZATION_OPTIONAL_HPP_
97

source code of boost/boost/serialization/optional.hpp