vector.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #ifndef BOOST_SERIALIZATION_VECTOR_HPP
  2. #define BOOST_SERIALIZATION_VECTOR_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  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/type_traits/is_arithmetic.hpp>
  19. #include <boost/serialization/collections_save_imp.hpp>
  20. #include <boost/serialization/collections_load_imp.hpp>
  21. #include <boost/serialization/split_free.hpp>
  22. #include <boost/serialization/array.hpp>
  23. #include <boost/serialization/detail/get_data.hpp>
  24. #include <boost/mpl/bool.hpp>
  25. // default is being compatible with version 1.34.1 files, not 1.35 files
  26. #ifndef BOOST_SERIALIZATION_VECTOR_VERSIONED
  27. #define BOOST_SERIALIZATION_VECTOR_VERSIONED(V) (V==4 || V==5)
  28. #endif
  29. // function specializations must be defined in the appropriate
  30. // namespace - boost::serialization
  31. #if defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION)
  32. #define STD _STLP_STD
  33. #else
  34. #define STD std
  35. #endif
  36. namespace boost {
  37. namespace serialization {
  38. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  39. // vector< T >
  40. // the default versions
  41. template<class Archive, class U, class Allocator>
  42. inline void save(
  43. Archive & ar,
  44. const std::vector<U, Allocator> &t,
  45. const unsigned int /* file_version */,
  46. mpl::false_
  47. ){
  48. boost::serialization::stl::save_collection<Archive, STD::vector<U, Allocator> >(
  49. ar, t
  50. );
  51. }
  52. template<class Archive, class U, class Allocator>
  53. inline void load(
  54. Archive & ar,
  55. std::vector<U, Allocator> &t,
  56. const unsigned int /* file_version */,
  57. mpl::false_
  58. ){
  59. boost::serialization::stl::load_collection<
  60. Archive,
  61. std::vector<U, Allocator>,
  62. boost::serialization::stl::archive_input_seq<
  63. Archive, STD::vector<U, Allocator>
  64. >,
  65. boost::serialization::stl::reserve_imp<STD::vector<U, Allocator> >
  66. >(ar, t);
  67. }
  68. // the optimized versions
  69. template<class Archive, class U, class Allocator>
  70. inline void save(
  71. Archive & ar,
  72. const std::vector<U, Allocator> &t,
  73. const unsigned int /* file_version */,
  74. mpl::true_
  75. ){
  76. const collection_size_type count(t.size());
  77. ar << BOOST_SERIALIZATION_NVP(count);
  78. if (!t.empty())
  79. ar << make_array(detail::get_data(t),t.size());
  80. }
  81. template<class Archive, class U, class Allocator>
  82. inline void load(
  83. Archive & ar,
  84. std::vector<U, Allocator> &t,
  85. const unsigned int /* file_version */,
  86. mpl::true_
  87. ){
  88. collection_size_type count(t.size());
  89. ar >> BOOST_SERIALIZATION_NVP(count);
  90. t.resize(count);
  91. unsigned int item_version=0;
  92. if(BOOST_SERIALIZATION_VECTOR_VERSIONED(ar.get_library_version())) {
  93. ar >> BOOST_SERIALIZATION_NVP(item_version);
  94. }
  95. if (!t.empty())
  96. ar >> make_array(detail::get_data(t),t.size());
  97. }
  98. // dispatch to either default or optimized versions
  99. template<class Archive, class U, class Allocator>
  100. inline void save(
  101. Archive & ar,
  102. const std::vector<U, Allocator> &t,
  103. const unsigned int file_version
  104. ){
  105. typedef BOOST_DEDUCED_TYPENAME
  106. boost::serialization::use_array_optimization<Archive>::template apply<
  107. BOOST_DEDUCED_TYPENAME remove_const<U>::type
  108. >::type use_optimized;
  109. save(ar,t,file_version, use_optimized());
  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. ){
  117. #ifdef BOOST_SERIALIZATION_VECTOR_135_HPP
  118. if (ar.get_library_version()==boost::archive::library_version_type(5))
  119. {
  120. load(ar,t,file_version, boost::is_arithmetic<U>());
  121. return;
  122. }
  123. #endif
  124. typedef BOOST_DEDUCED_TYPENAME
  125. boost::serialization::use_array_optimization<Archive>::template apply<
  126. BOOST_DEDUCED_TYPENAME remove_const<U>::type
  127. >::type use_optimized;
  128. load(ar,t,file_version, use_optimized());
  129. }
  130. // split non-intrusive serialization function member into separate
  131. // non intrusive save/load member functions
  132. template<class Archive, class U, class Allocator>
  133. inline void serialize(
  134. Archive & ar,
  135. std::vector<U, Allocator> & t,
  136. const unsigned int file_version
  137. ){
  138. boost::serialization::split_free(ar, t, file_version);
  139. }
  140. #if ! BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
  141. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  142. // vector<bool>
  143. template<class Archive, class Allocator>
  144. inline void save(
  145. Archive & ar,
  146. const std::vector<bool, Allocator> &t,
  147. const unsigned int /* file_version */
  148. ){
  149. // record number of elements
  150. collection_size_type count (t.size());
  151. ar << BOOST_SERIALIZATION_NVP(count);
  152. std::vector<bool>::const_iterator it = t.begin();
  153. while(count-- > 0){
  154. bool tb = *it++;
  155. ar << boost::serialization::make_nvp("item", tb);
  156. }
  157. }
  158. template<class Archive, class Allocator>
  159. inline void load(
  160. Archive & ar,
  161. std::vector<bool, Allocator> &t,
  162. const unsigned int /* file_version */
  163. ){
  164. // retrieve number of elements
  165. collection_size_type count;
  166. ar >> BOOST_SERIALIZATION_NVP(count);
  167. t.clear();
  168. while(count-- > 0){
  169. bool i;
  170. ar >> boost::serialization::make_nvp("item", i);
  171. t.push_back(i);
  172. }
  173. }
  174. // split non-intrusive serialization function member into separate
  175. // non intrusive save/load member functions
  176. template<class Archive, class Allocator>
  177. inline void serialize(
  178. Archive & ar,
  179. std::vector<bool, Allocator> & t,
  180. const unsigned int file_version
  181. ){
  182. boost::serialization::split_free(ar, t, file_version);
  183. }
  184. #endif // BOOST_WORKAROUND
  185. } // serialization
  186. } // namespace boost
  187. #include <boost/serialization/collection_traits.hpp>
  188. BOOST_SERIALIZATION_COLLECTION_TRAITS(std::vector)
  189. #undef STD
  190. #endif // BOOST_SERIALIZATION_VECTOR_HPP