Ticket #11612: vector.hpp

File vector.hpp, 7.0 KB (added by thetetrismaster@…, 7 years ago)
Line 
1#ifndef BOOST_SERIALIZATION_VECTOR_HPP
2#define BOOST_SERIALIZATION_VECTOR_HPP
3
4// MS compatible compilers support #pragma once
5#if defined(_MSC_VER)
6# pragma once
7#endif
8
9/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10// vector.hpp: serialization for stl vector templates
11
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)
17
18// See http://www.boost.org for updates, documentation, and revision history.
19
20#include <vector>
21
22#include <boost/config.hpp>
23#include <boost/detail/workaround.hpp>
24
25#include <boost/archive/detail/basic_iarchive.hpp>
26#include <boost/serialization/access.hpp>
27#include <boost/serialization/nvp.hpp>
28#include <boost/serialization/collection_size_type.hpp>
29#include <boost/serialization/item_version_type.hpp>
30
31#include <boost/serialization/collections_save_imp.hpp>
32#include <boost/serialization/split_free.hpp>
33#include <boost/serialization/array.hpp>
34#include <boost/serialization/detail/get_data.hpp>
35#include <boost/serialization/detail/stack_constructor.hpp>
36#include <boost/serialization/detail/is_default_constructible.hpp>
37#include <boost/mpl/bool.hpp>
38
39// default is being compatible with version 1.34.1 files, not 1.35 files
40#ifndef BOOST_SERIALIZATION_VECTOR_VERSIONED
41#define BOOST_SERIALIZATION_VECTOR_VERSIONED(V) (V==4 || V==5)
42#endif
43
44// function specializations must be defined in the appropriate
45// namespace - boost::serialization
46#if defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION)
47#define STD _STLP_STD
48#else
49#define STD std
50#endif
51
52namespace boost {
53namespace serialization {
54
55/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
56// vector< T >
57
58// the default versions
59
60template<class Archive, class U, class Allocator>
61inline void save(
62 Archive & ar,
63 const std::vector<U, Allocator> &t,
64 const unsigned int /* file_version */,
65 mpl::false_
66){
67 boost::serialization::stl::save_collection<Archive, STD::vector<U, Allocator> >(
68 ar, t
69 );
70}
71
72template<class Archive, class U, class Allocator>
73inline void load(
74 Archive & ar,
75 std::vector<U, Allocator> &t,
76 const unsigned int /* file_version */,
77 mpl::false_
78){
79 const boost::archive::library_version_type library_version(
80 ar.get_library_version()
81 );
82 // retrieve number of elements
83 item_version_type item_version(0);
84 collection_size_type count;
85 ar >> BOOST_SERIALIZATION_NVP(count);
86 if(boost::archive::library_version_type(3) < library_version){
87 ar >> BOOST_SERIALIZATION_NVP(item_version);
88 }
89 if(detail::is_default_constructible<U>()){
90 t.resize(count);
91 typename std::vector<U, Allocator>::iterator hint;
92 hint = t.begin();
93 while(count-- > 0){
94 ar >> boost::serialization::make_nvp("item", *hint++);
95 }
96 }
97 else{
98 t.reserve(count);
99 while(count-- > 0){
100 detail::stack_construct<Archive, U> u(ar, item_version);
101 ar >> boost::serialization::make_nvp("item", u.reference());
102 t.push_back(u.reference());
103 ar.reset_object_address(& t.back() , & u.reference());
104 }
105 }
106}
107
108// the optimized versions
109
110template<class Archive, class U, class Allocator>
111inline void save(
112 Archive & ar,
113 const std::vector<U, Allocator> &t,
114 const unsigned int /* file_version */,
115 mpl::true_
116){
117 const collection_size_type count(t.size());
118 ar << BOOST_SERIALIZATION_NVP(count);
119 if (!t.empty())
120 ar << make_array(detail::get_data(t),t.size());
121}
122
123template<class Archive, class U, class Allocator>
124inline void load(
125 Archive & ar,
126 std::vector<U, Allocator> &t,
127 const unsigned int /* file_version */,
128 mpl::true_
129){
130 collection_size_type count(t.size());
131 ar >> BOOST_SERIALIZATION_NVP(count);
132 t.resize(count);
133 unsigned int item_version=0;
134 if(BOOST_SERIALIZATION_VECTOR_VERSIONED(ar.get_library_version())) {
135 ar >> BOOST_SERIALIZATION_NVP(item_version);
136 }
137 if (!t.empty())
138 ar >> make_array(detail::get_data(t),t.size());
139 }
140
141// dispatch to either default or optimized versions
142
143template<class Archive, class U, class Allocator>
144inline void save(
145 Archive & ar,
146 const std::vector<U, Allocator> &t,
147 const unsigned int file_version
148){
149 typedef typename
150 boost::serialization::use_array_optimization<Archive>::template apply<
151 typename remove_const<U>::type
152 >::type use_optimized;
153 save(ar,t,file_version, use_optimized());
154}
155
156template<class Archive, class U, class Allocator>
157inline void load(
158 Archive & ar,
159 std::vector<U, Allocator> &t,
160 const unsigned int file_version
161){
162#ifdef BOOST_SERIALIZATION_VECTOR_135_HPP
163 if (ar.get_library_version()==boost::archive::library_version_type(5))
164 {
165 load(ar,t,file_version, boost::is_arithmetic<U>());
166 return;
167 }
168#endif
169 typedef typename
170 boost::serialization::use_array_optimization<Archive>::template apply<
171 typename remove_const<U>::type
172 >::type use_optimized;
173 load(ar,t,file_version, use_optimized());
174}
175
176// split non-intrusive serialization function member into separate
177// non intrusive save/load member functions
178template<class Archive, class U, class Allocator>
179inline void serialize(
180 Archive & ar,
181 std::vector<U, Allocator> & t,
182 const unsigned int file_version
183){
184 boost::serialization::split_free(ar, t, file_version);
185}
186
187/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
188// vector<bool>
189template<class Archive, class Allocator>
190inline void save(
191 Archive & ar,
192 const std::vector<bool, Allocator> &t,
193 const unsigned int /* file_version */
194){
195 // record number of elements
196 collection_size_type count (t.size());
197 ar << BOOST_SERIALIZATION_NVP(count);
198 std::vector<bool>::const_iterator it = t.begin();
199 while(count-- > 0){
200 bool tb = *it++;
201 ar << boost::serialization::make_nvp("item", tb);
202 }
203}
204
205template<class Archive, class Allocator>
206inline void load(
207 Archive & ar,
208 std::vector<bool, Allocator> &t,
209 const unsigned int /* file_version */
210){
211 // retrieve number of elements
212 collection_size_type count;
213 ar >> BOOST_SERIALIZATION_NVP(count);
214 t.resize(count);
215 int i;
216 for(i = 0; i < count; ++i){
217 bool b;
218 ar >> boost::serialization::make_nvp("item", b);
219 t[i] = b;
220 }
221}
222
223// split non-intrusive serialization function member into separate
224// non intrusive save/load member functions
225template<class Archive, class Allocator>
226inline void serialize(
227 Archive & ar,
228 std::vector<bool, Allocator> & t,
229 const unsigned int file_version
230){
231 boost::serialization::split_free(ar, t, file_version);
232}
233
234} // serialization
235} // namespace boost
236
237#include <boost/serialization/collection_traits.hpp>
238
239BOOST_SERIALIZATION_COLLECTION_TRAITS(std::vector)
240#undef STD
241
242#endif // BOOST_SERIALIZATION_VECTOR_HPP