map.hpp 4.3 KB

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