collections_load_imp.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #ifndef BOOST_SERIALIZATION_COLLECTIONS_LOAD_IMP_HPP
  2. #define BOOST_SERIALIZATION_COLLECTIONS_LOAD_IMP_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. #if defined(_MSC_VER) && (_MSC_VER <= 1020)
  8. # pragma warning (disable : 4786) // too long name, harmless warning
  9. #endif
  10. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  11. // collections_load_imp.hpp: serialization for loading stl collections
  12. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  13. // Use, modification and distribution is subject to the Boost Software
  14. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt)
  16. // See http://www.boost.org for updates, documentation, and revision history.
  17. // helper function templates for serialization of collections
  18. #include <cassert>
  19. #include <cstddef> // size_t
  20. #include <boost/config.hpp> // msvc 6.0 needs this for warning suppression
  21. #if defined(BOOST_NO_STDC_NAMESPACE)
  22. namespace std{
  23. using ::size_t;
  24. } // namespace std
  25. #endif
  26. #include <boost/detail/workaround.hpp>
  27. #include <boost/serialization/access.hpp>
  28. #include <boost/serialization/nvp.hpp>
  29. #include <boost/serialization/detail/stack_constructor.hpp>
  30. #include <boost/serialization/collection_size_type.hpp>
  31. namespace boost{
  32. namespace serialization {
  33. namespace stl {
  34. //////////////////////////////////////////////////////////////////////
  35. // implementation of serialization for STL containers
  36. //
  37. // sequential container input
  38. template<class Archive, class Container>
  39. struct archive_input_seq
  40. {
  41. inline void operator()(
  42. Archive &ar,
  43. Container &s,
  44. const unsigned int v
  45. ){
  46. typedef BOOST_DEDUCED_TYPENAME Container::value_type type;
  47. detail::stack_construct<Archive, type> t(ar, v);
  48. // borland fails silently w/o full namespace
  49. ar >> boost::serialization::make_nvp("item", t.reference());
  50. s.push_back(t.reference());
  51. ar.reset_object_address(& s.back() , & t.reference());
  52. }
  53. };
  54. // map input
  55. template<class Archive, class Container>
  56. struct archive_input_map
  57. {
  58. inline void operator()(
  59. Archive &ar,
  60. Container &s,
  61. const unsigned int v
  62. ){
  63. typedef BOOST_DEDUCED_TYPENAME Container::value_type type;
  64. detail::stack_construct<Archive, type> t(ar, v);
  65. // borland fails silently w/o full namespace
  66. ar >> boost::serialization::make_nvp("item", t.reference());
  67. std::pair<BOOST_DEDUCED_TYPENAME Container::const_iterator, bool> result =
  68. s.insert(t.reference());
  69. // note: the following presumes that the map::value_type was NOT tracked
  70. // in the archive. This is the usual case, but here there is no way
  71. // to determine that.
  72. if(result.second){
  73. ar.reset_object_address(
  74. & (result.first->second),
  75. & t.reference().second
  76. );
  77. }
  78. }
  79. };
  80. // multimap input
  81. template<class Archive, class Container>
  82. struct archive_input_multimap
  83. {
  84. inline void operator()(
  85. Archive &ar,
  86. Container &s,
  87. const unsigned int v
  88. ){
  89. typedef BOOST_DEDUCED_TYPENAME Container::value_type type;
  90. detail::stack_construct<Archive, type> t(ar, v);
  91. // borland fails silently w/o full namespace
  92. ar >> boost::serialization::make_nvp("item", t.reference());
  93. BOOST_DEDUCED_TYPENAME Container::const_iterator result
  94. = s.insert(t.reference());
  95. // note: the following presumes that the map::value_type was NOT tracked
  96. // in the archive. This is the usual case, but here there is no way
  97. // to determine that.
  98. ar.reset_object_address(
  99. & result->second,
  100. & t.reference()
  101. );
  102. }
  103. };
  104. // set input
  105. template<class Archive, class Container>
  106. struct archive_input_set
  107. {
  108. inline void operator()(
  109. Archive &ar,
  110. Container &s,
  111. const unsigned int v
  112. ){
  113. typedef BOOST_DEDUCED_TYPENAME Container::value_type type;
  114. detail::stack_construct<Archive, type> t(ar, v);
  115. // borland fails silently w/o full namespace
  116. ar >> boost::serialization::make_nvp("item", t.reference());
  117. std::pair<BOOST_DEDUCED_TYPENAME Container::const_iterator, bool> result =
  118. s.insert(t.reference());
  119. if(result.second)
  120. ar.reset_object_address(& (* result.first), & t.reference());
  121. }
  122. };
  123. // multiset input
  124. template<class Archive, class Container>
  125. struct archive_input_multiset
  126. {
  127. inline void operator()(
  128. Archive &ar,
  129. Container &s,
  130. const unsigned int v
  131. ){
  132. typedef BOOST_DEDUCED_TYPENAME Container::value_type type;
  133. detail::stack_construct<Archive, type> t(ar, v);
  134. // borland fails silently w/o full namespace
  135. ar >> boost::serialization::make_nvp("item", t.reference());
  136. BOOST_DEDUCED_TYPENAME Container::const_iterator result
  137. = s.insert(t.reference());
  138. ar.reset_object_address(& (* result), & t.reference());
  139. }
  140. };
  141. template<class Container>
  142. class reserve_imp
  143. {
  144. public:
  145. void operator()(Container &s, std::size_t count) const {
  146. s.reserve(count);
  147. }
  148. };
  149. template<class Container>
  150. class no_reserve_imp
  151. {
  152. public:
  153. void operator()(Container & /* s */, std::size_t /* count */) const{}
  154. };
  155. template<class Archive, class Container, class InputFunction, class R>
  156. inline void load_collection(Archive & ar, Container &s)
  157. {
  158. s.clear();
  159. // retrieve number of elements
  160. collection_size_type count;
  161. unsigned int item_version;
  162. ar >> BOOST_SERIALIZATION_NVP(count);
  163. if(3 < ar.get_library_version())
  164. ar >> BOOST_SERIALIZATION_NVP(item_version);
  165. else
  166. item_version = 0;
  167. R rx;
  168. rx(s, count);
  169. std::size_t c = count;
  170. InputFunction ifunc;
  171. while(c-- > 0){
  172. ifunc(ar, s, item_version);
  173. }
  174. }
  175. } // namespace stl
  176. } // namespace serialization
  177. } // namespace boost
  178. #endif //BOOST_SERIALIZATION_COLLECTIONS_LOAD_IMP_HPP