vector.hpp 6.8 KB

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