fix doc example typo
[boost.git] / boost / serialization / vector.hpp
blobe678cf33e2404e68397932e253a5e020ffccc29d
1 #ifndef BOOST_SERIALIZATION_VECTOR_HPP
2 #define BOOST_SERIALIZATION_VECTOR_HPP
4 // MS compatible compilers support #pragma once
5 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
6 # pragma once
7 #endif
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10 // vector.hpp: serialization for stl vector templates
12 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
13 // fast array serialization (C) Copyright 2005 Matthias Troyer
14 // Use, modification and distribution is subject to the Boost Software
15 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
16 // http://www.boost.org/LICENSE_1_0.txt)
18 // See http://www.boost.org for updates, documentation, and revision history.
20 #include <vector>
22 #include <boost/config.hpp>
23 #include <boost/detail/workaround.hpp>
25 #include <boost/serialization/collections_save_imp.hpp>
26 #include <boost/serialization/collections_load_imp.hpp>
27 #include <boost/serialization/split_free.hpp>
28 #include <boost/serialization/array.hpp>
29 #include <boost/serialization/detail/get_data.hpp>
30 #include <boost/mpl/bool.hpp>
32 // default is being compatible with version 1.34.1 files, not 1.35 files
33 #ifndef BOOST_SERIALIZATION_VECTOR_VERSION
34 #define BOOST_SERIALIZATION_VECTOR_VERSION 3
35 #endif
38 namespace boost {
39 namespace serialization {
41 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
42 // vector<T>
44 // the default versions
46 template<class Archive, class U, class Allocator>
47 inline void save(
48 Archive & ar,
49 const std::vector<U, Allocator> &t,
50 const unsigned int /* file_version */,
51 mpl::false_
53 boost::serialization::stl::save_collection<Archive, STD::vector<U, Allocator> >(
54 ar, t
58 template<class Archive, class U, class Allocator>
59 inline void load(
60 Archive & ar,
61 std::vector<U, Allocator> &t,
62 const unsigned int /* file_version */,
63 mpl::false_
65 boost::serialization::stl::load_collection<
66 Archive,
67 std::vector<U, Allocator>,
68 boost::serialization::stl::archive_input_seq<
69 Archive, STD::vector<U, Allocator>
71 boost::serialization::stl::reserve_imp<STD::vector<U, Allocator> >
72 >(ar, t);
75 // the optimized versions
77 template<class Archive, class U, class Allocator>
78 inline void save(
79 Archive & ar,
80 const std::vector<U, Allocator> &t,
81 const unsigned int /* file_version */,
82 mpl::true_
84 const collection_size_type count(t.size());
85 ar << BOOST_SERIALIZATION_NVP(count);
86 if(BOOST_SERIALIZATION_VECTOR_VERSION < ar.get_library_version()) {
87 const unsigned int item_version = version<U>::value;
88 ar << BOOST_SERIALIZATION_NVP(item_version);
90 if (!t.empty())
91 ar << make_array(detail::get_data(t),t.size());
94 template<class Archive, class U, class Allocator>
95 inline void load(
96 Archive & ar,
97 std::vector<U, Allocator> &t,
98 const unsigned int /* file_version */,
99 mpl::true_
101 collection_size_type count(t.size());
102 ar >> BOOST_SERIALIZATION_NVP(count);
103 t.resize(count);
104 unsigned int item_version=0;
105 if(BOOST_SERIALIZATION_VECTOR_VERSION < ar.get_library_version())
106 ar >> BOOST_SERIALIZATION_NVP(item_version);
107 if (!t.empty())
108 ar >> make_array(detail::get_data(t),t.size());
111 // dispatch to either default or optimized versions
113 template<class Archive, class U, class Allocator>
114 inline void save(
115 Archive & ar,
116 const std::vector<U, Allocator> &t,
117 const unsigned int file_version
119 typedef BOOST_DEDUCED_TYPENAME
120 boost::serialization::use_array_optimization<Archive>::template apply<
121 BOOST_DEDUCED_TYPENAME remove_const<U>::type
122 >::type use_optimized;
123 save(ar,t,file_version, use_optimized());
126 template<class Archive, class U, class Allocator>
127 inline void load(
128 Archive & ar,
129 std::vector<U, Allocator> &t,
130 const unsigned int file_version
132 typedef BOOST_DEDUCED_TYPENAME
133 boost::serialization::use_array_optimization<Archive>::template apply<
134 BOOST_DEDUCED_TYPENAME remove_const<U>::type
135 >::type use_optimized;
136 load(ar,t,file_version, use_optimized());
139 // split non-intrusive serialization function member into separate
140 // non intrusive save/load member functions
141 template<class Archive, class U, class Allocator>
142 inline void serialize(
143 Archive & ar,
144 std::vector<U, Allocator> & t,
145 const unsigned int file_version
147 boost::serialization::split_free(ar, t, file_version);
150 #if ! BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
152 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
153 // vector<bool>
154 template<class Archive, class Allocator>
155 inline void save(
156 Archive & ar,
157 const std::vector<bool, Allocator> &t,
158 const unsigned int /* file_version */
160 // record number of elements
161 collection_size_type count (t.size());
162 ar << BOOST_SERIALIZATION_NVP(count);
163 std::vector<bool>::const_iterator it = t.begin();
164 while(count-- > 0){
165 bool tb = *it++;
166 ar << boost::serialization::make_nvp("item", tb);
170 template<class Archive, class Allocator>
171 inline void load(
172 Archive & ar,
173 std::vector<bool, Allocator> &t,
174 const unsigned int /* file_version */
176 // retrieve number of elements
177 collection_size_type count;
178 ar >> BOOST_SERIALIZATION_NVP(count);
179 t.clear();
180 while(count-- > 0){
181 bool i;
182 ar >> boost::serialization::make_nvp("item", i);
183 t.push_back(i);
187 // split non-intrusive serialization function member into separate
188 // non intrusive save/load member functions
189 template<class Archive, class Allocator>
190 inline void serialize(
191 Archive & ar,
192 std::vector<bool, Allocator> & t,
193 const unsigned int file_version
195 boost::serialization::split_free(ar, t, file_version);
198 #endif // BOOST_WORKAROUND
200 } // serialization
201 } // namespace boost
203 #include <boost/serialization/collection_traits.hpp>
205 BOOST_SERIALIZATION_COLLECTION_TRAITS(std::vector)
207 #endif // BOOST_SERIALIZATION_VECTOR_HPP