map.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #ifndef BOOST_SERIALIZATION_MAP_HPP
  2. #define BOOST_SERIALIZATION_MAP_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. // serialization/map.hpp:
  9. // serialization for stl map templates
  10. // (C) Copyright 2002-2014 Robert Ramey - http://www.rrsd.com .
  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 <map>
  16. #include <boost/config.hpp>
  17. #include <boost/archive/detail/basic_iarchive.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/item_version_type.hpp>
  22. #include <boost/serialization/detail/stack_constructor.hpp>
  23. #include <boost/serialization/utility.hpp>
  24. #include <boost/serialization/collections_save_imp.hpp>
  25. #include <boost/serialization/split_free.hpp>
  26. namespace boost {
  27. namespace serialization {
  28. ////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  29. // implementation of serialization for map and mult-map STL containers
  30. template<class Archive, class Container>
  31. inline void load_map_collection(Archive & ar, Container &s)
  32. {
  33. s.clear();
  34. const boost::archive::library_version_type library_version(
  35. ar.get_library_version()
  36. );
  37. // retrieve number of elements
  38. item_version_type item_version(0);
  39. collection_size_type count;
  40. ar >> BOOST_SERIALIZATION_NVP(count);
  41. if(boost::archive::library_version_type(3) < library_version){
  42. ar >> BOOST_SERIALIZATION_NVP(item_version);
  43. }
  44. typename Container::iterator hint;
  45. hint = s.begin();
  46. while(count-- > 0){
  47. typedef typename Container::value_type type;
  48. detail::stack_construct<Archive, type> t(ar, item_version);
  49. ar >> boost::serialization::make_nvp("item", t.reference());
  50. typename Container::iterator result =
  51. #ifdef BOOST_NO_CXX11_HDR_UNORDERED_MAP
  52. s.insert(hint, t.reference());
  53. #else
  54. s.emplace_hint(hint, t.reference());
  55. #endif
  56. ar.reset_object_address(& (result->second), & t.reference().second);
  57. hint = result;
  58. ++hint;
  59. }
  60. }
  61. // map
  62. template<class Archive, class Type, class Key, class Compare, class Allocator >
  63. inline void save(
  64. Archive & ar,
  65. const std::map<Key, Type, Compare, Allocator> &t,
  66. const unsigned int /* file_version */
  67. ){
  68. boost::serialization::stl::save_collection<
  69. Archive,
  70. std::map<Key, Type, Compare, Allocator>
  71. >(ar, t);
  72. }
  73. template<class Archive, class Type, class Key, class Compare, class Allocator >
  74. inline void load(
  75. Archive & ar,
  76. std::map<Key, Type, Compare, Allocator> &t,
  77. const unsigned int /* file_version */
  78. ){
  79. load_map_collection(ar, t);
  80. }
  81. // split non-intrusive serialization function member into separate
  82. // non intrusive save/load member functions
  83. template<class Archive, class Type, class Key, class Compare, class Allocator >
  84. inline void serialize(
  85. Archive & ar,
  86. std::map<Key, Type, Compare, Allocator> &t,
  87. const unsigned int file_version
  88. ){
  89. boost::serialization::split_free(ar, t, file_version);
  90. }
  91. // multimap
  92. template<class Archive, class Type, class Key, class Compare, class Allocator >
  93. inline void save(
  94. Archive & ar,
  95. const std::multimap<Key, Type, Compare, Allocator> &t,
  96. const unsigned int /* file_version */
  97. ){
  98. boost::serialization::stl::save_collection<
  99. Archive,
  100. std::multimap<Key, Type, Compare, Allocator>
  101. >(ar, t);
  102. }
  103. template<class Archive, class Type, class Key, class Compare, class Allocator >
  104. inline void load(
  105. Archive & ar,
  106. std::multimap<Key, Type, Compare, Allocator> &t,
  107. const unsigned int /* file_version */
  108. ){
  109. load_map_collection(ar, t);
  110. }
  111. // split non-intrusive serialization function member into separate
  112. // non intrusive save/load member functions
  113. template<class Archive, class Type, class Key, class Compare, class Allocator >
  114. inline void serialize(
  115. Archive & ar,
  116. std::multimap<Key, Type, Compare, Allocator> &t,
  117. const unsigned int file_version
  118. ){
  119. boost::serialization::split_free(ar, t, file_version);
  120. }
  121. } // serialization
  122. } // namespace boost
  123. #endif // BOOST_SERIALIZATION_MAP_HPP