Brian Silverman | 8867871 | 2018-08-04 23:56:48 -0700 | [diff] [blame^] | 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/collections_load_imp.hpp> |
| 33 | #include <boost/serialization/split_free.hpp> |
| 34 | #include <boost/serialization/array_wrapper.hpp> |
| 35 | #include <boost/mpl/bool_fwd.hpp> |
| 36 | #include <boost/mpl/if.hpp> |
| 37 | |
| 38 | // default is being compatible with version 1.34.1 files, not 1.35 files |
| 39 | #ifndef BOOST_SERIALIZATION_VECTOR_VERSIONED |
| 40 | #define BOOST_SERIALIZATION_VECTOR_VERSIONED(V) (V==4 || V==5) |
| 41 | #endif |
| 42 | |
| 43 | // function specializations must be defined in the appropriate |
| 44 | // namespace - boost::serialization |
| 45 | #if defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION) |
| 46 | #define STD _STLP_STD |
| 47 | #else |
| 48 | #define STD std |
| 49 | #endif |
| 50 | |
| 51 | namespace boost { |
| 52 | namespace serialization { |
| 53 | |
| 54 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
| 55 | // vector< T > |
| 56 | |
| 57 | // the default versions |
| 58 | |
| 59 | template<class Archive, class U, class Allocator> |
| 60 | inline void save( |
| 61 | Archive & ar, |
| 62 | const std::vector<U, Allocator> &t, |
| 63 | const unsigned int /* file_version */, |
| 64 | mpl::false_ |
| 65 | ){ |
| 66 | boost::serialization::stl::save_collection<Archive, STD::vector<U, Allocator> >( |
| 67 | ar, t |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | template<class Archive, class U, class Allocator> |
| 72 | inline void load( |
| 73 | Archive & ar, |
| 74 | std::vector<U, Allocator> &t, |
| 75 | const unsigned int /* file_version */, |
| 76 | mpl::false_ |
| 77 | ){ |
| 78 | const boost::archive::library_version_type library_version( |
| 79 | ar.get_library_version() |
| 80 | ); |
| 81 | // retrieve number of elements |
| 82 | item_version_type item_version(0); |
| 83 | collection_size_type count; |
| 84 | ar >> BOOST_SERIALIZATION_NVP(count); |
| 85 | if(boost::archive::library_version_type(3) < library_version){ |
| 86 | ar >> BOOST_SERIALIZATION_NVP(item_version); |
| 87 | } |
| 88 | t.reserve(count); |
| 89 | stl::collection_load_impl(ar, t, count, item_version); |
| 90 | } |
| 91 | |
| 92 | // the optimized versions |
| 93 | |
| 94 | template<class Archive, class U, class Allocator> |
| 95 | inline void save( |
| 96 | Archive & ar, |
| 97 | const std::vector<U, Allocator> &t, |
| 98 | const unsigned int /* file_version */, |
| 99 | mpl::true_ |
| 100 | ){ |
| 101 | const collection_size_type count(t.size()); |
| 102 | ar << BOOST_SERIALIZATION_NVP(count); |
| 103 | if (!t.empty()) |
| 104 | // explict template arguments to pass intel C++ compiler |
| 105 | ar << serialization::make_array<const U, collection_size_type>( |
| 106 | static_cast<const U *>(&t[0]), |
| 107 | count |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | template<class Archive, class U, class Allocator> |
| 112 | inline void load( |
| 113 | Archive & ar, |
| 114 | std::vector<U, Allocator> &t, |
| 115 | const unsigned int /* file_version */, |
| 116 | mpl::true_ |
| 117 | ){ |
| 118 | collection_size_type count(t.size()); |
| 119 | ar >> BOOST_SERIALIZATION_NVP(count); |
| 120 | t.resize(count); |
| 121 | unsigned int item_version=0; |
| 122 | if(BOOST_SERIALIZATION_VECTOR_VERSIONED(ar.get_library_version())) { |
| 123 | ar >> BOOST_SERIALIZATION_NVP(item_version); |
| 124 | } |
| 125 | if (!t.empty()) |
| 126 | // explict template arguments to pass intel C++ compiler |
| 127 | ar >> serialization::make_array<U, collection_size_type>( |
| 128 | static_cast<U *>(&t[0]), |
| 129 | count |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | // dispatch to either default or optimized versions |
| 134 | |
| 135 | template<class Archive, class U, class Allocator> |
| 136 | inline void save( |
| 137 | Archive & ar, |
| 138 | const std::vector<U, Allocator> &t, |
| 139 | const unsigned int file_version |
| 140 | ){ |
| 141 | typedef typename |
| 142 | boost::serialization::use_array_optimization<Archive>::template apply< |
| 143 | typename remove_const<U>::type |
| 144 | >::type use_optimized; |
| 145 | save(ar,t,file_version, use_optimized()); |
| 146 | } |
| 147 | |
| 148 | template<class Archive, class U, class Allocator> |
| 149 | inline void load( |
| 150 | Archive & ar, |
| 151 | std::vector<U, Allocator> &t, |
| 152 | const unsigned int file_version |
| 153 | ){ |
| 154 | #ifdef BOOST_SERIALIZATION_VECTOR_135_HPP |
| 155 | if (ar.get_library_version()==boost::archive::library_version_type(5)) |
| 156 | { |
| 157 | load(ar,t,file_version, boost::is_arithmetic<U>()); |
| 158 | return; |
| 159 | } |
| 160 | #endif |
| 161 | typedef typename |
| 162 | boost::serialization::use_array_optimization<Archive>::template apply< |
| 163 | typename remove_const<U>::type |
| 164 | >::type use_optimized; |
| 165 | load(ar,t,file_version, use_optimized()); |
| 166 | } |
| 167 | |
| 168 | // split non-intrusive serialization function member into separate |
| 169 | // non intrusive save/load member functions |
| 170 | template<class Archive, class U, class Allocator> |
| 171 | inline void serialize( |
| 172 | Archive & ar, |
| 173 | std::vector<U, Allocator> & t, |
| 174 | const unsigned int file_version |
| 175 | ){ |
| 176 | boost::serialization::split_free(ar, t, file_version); |
| 177 | } |
| 178 | |
| 179 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
| 180 | // vector<bool> |
| 181 | template<class Archive, class Allocator> |
| 182 | inline void save( |
| 183 | Archive & ar, |
| 184 | const std::vector<bool, Allocator> &t, |
| 185 | const unsigned int /* file_version */ |
| 186 | ){ |
| 187 | // record number of elements |
| 188 | collection_size_type count (t.size()); |
| 189 | ar << BOOST_SERIALIZATION_NVP(count); |
| 190 | std::vector<bool>::const_iterator it = t.begin(); |
| 191 | while(count-- > 0){ |
| 192 | bool tb = *it++; |
| 193 | ar << boost::serialization::make_nvp("item", tb); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | template<class Archive, class Allocator> |
| 198 | inline void load( |
| 199 | Archive & ar, |
| 200 | std::vector<bool, Allocator> &t, |
| 201 | const unsigned int /* file_version */ |
| 202 | ){ |
| 203 | // retrieve number of elements |
| 204 | collection_size_type count; |
| 205 | ar >> BOOST_SERIALIZATION_NVP(count); |
| 206 | t.resize(count); |
| 207 | for(collection_size_type i = collection_size_type(0); i < count; ++i){ |
| 208 | bool b; |
| 209 | ar >> boost::serialization::make_nvp("item", b); |
| 210 | t[i] = b; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // split non-intrusive serialization function member into separate |
| 215 | // non intrusive save/load member functions |
| 216 | template<class Archive, class Allocator> |
| 217 | inline void serialize( |
| 218 | Archive & ar, |
| 219 | std::vector<bool, Allocator> & t, |
| 220 | const unsigned int file_version |
| 221 | ){ |
| 222 | boost::serialization::split_free(ar, t, file_version); |
| 223 | } |
| 224 | |
| 225 | } // serialization |
| 226 | } // namespace boost |
| 227 | |
| 228 | #include <boost/serialization/collection_traits.hpp> |
| 229 | |
| 230 | BOOST_SERIALIZATION_COLLECTION_TRAITS(std::vector) |
| 231 | #undef STD |
| 232 | |
| 233 | #endif // BOOST_SERIALIZATION_VECTOR_HPP |