collections_load_imp.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 <boost/assert.hpp>
  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/archive/detail/basic_iarchive.hpp>
  28. #include <boost/serialization/access.hpp>
  29. #include <boost/serialization/nvp.hpp>
  30. #include <boost/serialization/detail/stack_constructor.hpp>
  31. #include <boost/serialization/collection_size_type.hpp>
  32. #include <boost/serialization/item_version_type.hpp>
  33. namespace boost{
  34. namespace serialization {
  35. namespace stl {
  36. //////////////////////////////////////////////////////////////////////
  37. // implementation of serialization for STL containers
  38. //
  39. // sequential container input
  40. template<class Archive, class Container>
  41. struct archive_input_seq
  42. {
  43. inline BOOST_DEDUCED_TYPENAME Container::iterator
  44. operator()(
  45. Archive &ar,
  46. Container &s,
  47. const unsigned int v,
  48. BOOST_DEDUCED_TYPENAME Container::iterator hint
  49. ){
  50. typedef BOOST_DEDUCED_TYPENAME Container::value_type type;
  51. detail::stack_construct<Archive, type> t(ar, v);
  52. // borland fails silently w/o full namespace
  53. ar >> boost::serialization::make_nvp("item", t.reference());
  54. s.push_back(t.reference());
  55. ar.reset_object_address(& s.back() , & t.reference());
  56. return hint;
  57. }
  58. };
  59. // map input
  60. template<class Archive, class Container>
  61. struct archive_input_map
  62. {
  63. inline BOOST_DEDUCED_TYPENAME Container::iterator
  64. operator()(
  65. Archive &ar,
  66. Container &s,
  67. const unsigned int v,
  68. BOOST_DEDUCED_TYPENAME Container::iterator hint
  69. ){
  70. typedef BOOST_DEDUCED_TYPENAME Container::value_type type;
  71. detail::stack_construct<Archive, type> t(ar, v);
  72. // borland fails silently w/o full namespace
  73. ar >> boost::serialization::make_nvp("item", t.reference());
  74. BOOST_DEDUCED_TYPENAME Container::iterator result =
  75. s.insert(hint, t.reference());
  76. // note: the following presumes that the map::value_type was NOT tracked
  77. // in the archive. This is the usual case, but here there is no way
  78. // to determine that.
  79. ar.reset_object_address(
  80. & (result->second),
  81. & t.reference().second
  82. );
  83. return result;
  84. }
  85. };
  86. // set input
  87. template<class Archive, class Container>
  88. struct archive_input_set
  89. {
  90. inline BOOST_DEDUCED_TYPENAME Container::iterator
  91. operator()(
  92. Archive &ar,
  93. Container &s,
  94. const unsigned int v,
  95. BOOST_DEDUCED_TYPENAME Container::iterator hint
  96. ){
  97. typedef BOOST_DEDUCED_TYPENAME Container::value_type type;
  98. detail::stack_construct<Archive, type> t(ar, v);
  99. // borland fails silently w/o full namespace
  100. ar >> boost::serialization::make_nvp("item", t.reference());
  101. BOOST_DEDUCED_TYPENAME Container::iterator result =
  102. s.insert(hint, t.reference());
  103. ar.reset_object_address(& (* result), & t.reference());
  104. return result;
  105. }
  106. };
  107. template<class Container>
  108. class reserve_imp
  109. {
  110. public:
  111. void operator()(Container &s, std::size_t count) const {
  112. s.reserve(count);
  113. }
  114. };
  115. template<class Container>
  116. class no_reserve_imp
  117. {
  118. public:
  119. void operator()(Container & /* s */, std::size_t /* count */) const{}
  120. };
  121. template<class Archive, class Container, class InputFunction, class R>
  122. inline void load_collection(Archive & ar, Container &s)
  123. {
  124. s.clear();
  125. collection_size_type count;
  126. const boost::archive::library_version_type library_version(
  127. ar.get_library_version()
  128. );
  129. // retrieve number of elements
  130. item_version_type item_version(0);
  131. ar >> BOOST_SERIALIZATION_NVP(count);
  132. if(boost::archive::library_version_type(3) < library_version){
  133. ar >> BOOST_SERIALIZATION_NVP(item_version);
  134. }
  135. R rx;
  136. rx(s, count);
  137. InputFunction ifunc;
  138. BOOST_DEDUCED_TYPENAME Container::iterator hint;
  139. hint = s.begin();
  140. while(count-- > 0){
  141. hint = ifunc(ar, s, item_version, hint);
  142. }
  143. }
  144. } // namespace stl
  145. } // namespace serialization
  146. } // namespace boost
  147. #endif //BOOST_SERIALIZATION_COLLECTIONS_LOAD_IMP_HPP