vector.hpp 5.8 KB

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