vector.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #ifndef BOOST_SERIALIZATION_VECTOR_HPP
  2. #define BOOST_SERIALIZATION_VECTOR_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER)
  5. # pragma once
  6. #endif
  7. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  8. // vector.hpp: serialization for stl vector templates
  9. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  10. // fast array serialization (C) Copyright 2005 Matthias Troyer
  11. // Use, modification and distribution is subject to the Boost Software
  12. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. // See http://www.boost.org for updates, documentation, and revision history.
  15. #include <vector>
  16. #include <boost/config.hpp>
  17. #include <boost/detail/workaround.hpp>
  18. #include <boost/archive/detail/basic_iarchive.hpp>
  19. #include <boost/serialization/access.hpp>
  20. #include <boost/serialization/nvp.hpp>
  21. #include <boost/serialization/collection_size_type.hpp>
  22. #include <boost/serialization/item_version_type.hpp>
  23. #include <boost/serialization/collections_save_imp.hpp>
  24. #include <boost/serialization/collections_load_imp.hpp>
  25. #include <boost/serialization/split_free.hpp>
  26. #include <boost/serialization/array_wrapper.hpp>
  27. #include <boost/mpl/bool_fwd.hpp>
  28. #include <boost/mpl/if.hpp>
  29. // default is being compatible with version 1.34.1 files, not 1.35 files
  30. #ifndef BOOST_SERIALIZATION_VECTOR_VERSIONED
  31. #define BOOST_SERIALIZATION_VECTOR_VERSIONED(V) (V==4 || V==5)
  32. #endif
  33. // function specializations must be defined in the appropriate
  34. // namespace - boost::serialization
  35. #if defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION)
  36. #define STD _STLP_STD
  37. #else
  38. #define STD std
  39. #endif
  40. namespace boost {
  41. namespace serialization {
  42. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  43. // vector< T >
  44. // the default versions
  45. template<class Archive, class U, class Allocator>
  46. inline void save(
  47. Archive & ar,
  48. const std::vector<U, Allocator> &t,
  49. const unsigned int /* file_version */,
  50. mpl::false_
  51. ){
  52. boost::serialization::stl::save_collection<Archive, STD::vector<U, Allocator> >(
  53. ar, t
  54. );
  55. }
  56. template<class Archive, class U, class Allocator>
  57. inline void load(
  58. Archive & ar,
  59. std::vector<U, Allocator> &t,
  60. const unsigned int /* file_version */,
  61. mpl::false_
  62. ){
  63. const boost::archive::library_version_type library_version(
  64. ar.get_library_version()
  65. );
  66. // retrieve number of elements
  67. item_version_type item_version(0);
  68. collection_size_type count;
  69. ar >> BOOST_SERIALIZATION_NVP(count);
  70. if(boost::archive::library_version_type(3) < library_version){
  71. ar >> BOOST_SERIALIZATION_NVP(item_version);
  72. }
  73. t.reserve(count);
  74. stl::collection_load_impl(ar, t, count, item_version);
  75. }
  76. // 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_
  83. ){
  84. const collection_size_type count(t.size());
  85. ar << BOOST_SERIALIZATION_NVP(count);
  86. if (!t.empty())
  87. // explict template arguments to pass intel C++ compiler
  88. ar << serialization::make_array<const U, collection_size_type>(
  89. static_cast<const U *>(&t[0]),
  90. count
  91. );
  92. }
  93. template<class Archive, class U, class Allocator>
  94. inline void load(
  95. Archive & ar,
  96. std::vector<U, Allocator> &t,
  97. const unsigned int /* file_version */,
  98. mpl::true_
  99. ){
  100. collection_size_type count(t.size());
  101. ar >> BOOST_SERIALIZATION_NVP(count);
  102. t.resize(count);
  103. unsigned int item_version=0;
  104. if(BOOST_SERIALIZATION_VECTOR_VERSIONED(ar.get_library_version())) {
  105. ar >> BOOST_SERIALIZATION_NVP(item_version);
  106. }
  107. if (!t.empty())
  108. // explict template arguments to pass intel C++ compiler
  109. ar >> serialization::make_array<U, collection_size_type>(
  110. static_cast<U *>(&t[0]),
  111. count
  112. );
  113. }
  114. // dispatch to either default or optimized versions
  115. template<class Archive, class U, class Allocator>
  116. inline void save(
  117. Archive & ar,
  118. const std::vector<U, Allocator> &t,
  119. const unsigned int file_version
  120. ){
  121. typedef typename
  122. boost::serialization::use_array_optimization<Archive>::template apply<
  123. typename remove_const<U>::type
  124. >::type use_optimized;
  125. save(ar,t,file_version, use_optimized());
  126. }
  127. template<class Archive, class U, class Allocator>
  128. inline void load(
  129. Archive & ar,
  130. std::vector<U, Allocator> &t,
  131. const unsigned int file_version
  132. ){
  133. #ifdef BOOST_SERIALIZATION_VECTOR_135_HPP
  134. if (ar.get_library_version()==boost::archive::library_version_type(5))
  135. {
  136. load(ar,t,file_version, boost::is_arithmetic<U>());
  137. return;
  138. }
  139. #endif
  140. typedef typename
  141. boost::serialization::use_array_optimization<Archive>::template apply<
  142. typename remove_const<U>::type
  143. >::type use_optimized;
  144. load(ar,t,file_version, use_optimized());
  145. }
  146. // split non-intrusive serialization function member into separate
  147. // non intrusive save/load member functions
  148. template<class Archive, class U, class Allocator>
  149. inline void serialize(
  150. Archive & ar,
  151. std::vector<U, Allocator> & t,
  152. const unsigned int file_version
  153. ){
  154. boost::serialization::split_free(ar, t, file_version);
  155. }
  156. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  157. // vector<bool>
  158. template<class Archive, class Allocator>
  159. inline void save(
  160. Archive & ar,
  161. const std::vector<bool, Allocator> &t,
  162. const unsigned int /* file_version */
  163. ){
  164. // record number of elements
  165. collection_size_type count (t.size());
  166. ar << BOOST_SERIALIZATION_NVP(count);
  167. std::vector<bool>::const_iterator it = t.begin();
  168. while(count-- > 0){
  169. bool tb = *it++;
  170. ar << boost::serialization::make_nvp("item", tb);
  171. }
  172. }
  173. template<class Archive, class Allocator>
  174. inline void load(
  175. Archive & ar,
  176. std::vector<bool, Allocator> &t,
  177. const unsigned int /* file_version */
  178. ){
  179. // retrieve number of elements
  180. collection_size_type count;
  181. ar >> BOOST_SERIALIZATION_NVP(count);
  182. t.resize(count);
  183. for(collection_size_type i = collection_size_type(0); i < count; ++i){
  184. bool b;
  185. ar >> boost::serialization::make_nvp("item", b);
  186. t[i] = b;
  187. }
  188. }
  189. // split non-intrusive serialization function member into separate
  190. // non intrusive save/load member functions
  191. template<class Archive, class Allocator>
  192. inline void serialize(
  193. Archive & ar,
  194. std::vector<bool, Allocator> & t,
  195. const unsigned int file_version
  196. ){
  197. boost::serialization::split_free(ar, t, file_version);
  198. }
  199. } // serialization
  200. } // namespace boost
  201. #include <boost/serialization/collection_traits.hpp>
  202. BOOST_SERIALIZATION_COLLECTION_TRAITS(std::vector)
  203. #undef STD
  204. #endif // BOOST_SERIALIZATION_VECTOR_HPP