serialize_ptr_map_adapter.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright Sebastian Ramacher, 2007.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_PTR_CONTAINER_DETAIL_SERIALIZE_PTR_MAP_ADAPTER_HPP
  6. #define BOOST_PTR_CONTAINER_DETAIL_SERIALIZE_PTR_MAP_ADAPTER_HPP
  7. #include <boost/ptr_container/ptr_map_adapter.hpp>
  8. #include <boost/ptr_container/detail/serialize_xml_names.hpp>
  9. #include <boost/serialization/split_free.hpp>
  10. #include <boost/serialization/nvp.hpp>
  11. namespace boost
  12. {
  13. namespace serialization
  14. {
  15. template<class Archive, class T, class VoidPtrMap, class CloneAllocator, bool Ordered>
  16. void save(Archive& ar, const ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap, CloneAllocator,Ordered>& c, unsigned int /*version*/)
  17. {
  18. typedef ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap, CloneAllocator,Ordered> container;
  19. typedef BOOST_DEDUCED_TYPENAME container::const_iterator const_iterator;
  20. ar << boost::serialization::make_nvp( ptr_container_detail::count(),
  21. ptr_container_detail::serialize_as_const(c.size()) );
  22. const_iterator i = c.begin(), e = c.end();
  23. for(; i != e; ++i)
  24. {
  25. ar << boost::serialization::make_nvp( ptr_container_detail::first(), i->first );
  26. ar << boost::serialization::make_nvp( ptr_container_detail::second(),
  27. ptr_container_detail::serialize_as_const(i->second) );
  28. }
  29. }
  30. template<class Archive, class T, class VoidPtrMap, class CloneAllocator, bool Ordered>
  31. void load(Archive& ar, ptr_map_adapter<T, VoidPtrMap, CloneAllocator,Ordered>& c, unsigned int /*version*/)
  32. {
  33. typedef ptr_map_adapter<T, VoidPtrMap, CloneAllocator,Ordered> container;
  34. typedef BOOST_DEDUCED_TYPENAME container::key_type key_type;
  35. typedef BOOST_DEDUCED_TYPENAME container::size_type size_type;
  36. typedef BOOST_DEDUCED_TYPENAME container::iterator iterator;
  37. c.clear();
  38. size_type n;
  39. ar >> boost::serialization::make_nvp( ptr_container_detail::count(), n );
  40. for(size_type i = 0u; i != n; ++i)
  41. {
  42. key_type key;
  43. T* value;
  44. ar >> boost::serialization::make_nvp( ptr_container_detail::first(), key );
  45. ar >> boost::serialization::make_nvp( ptr_container_detail::second(), value );
  46. std::pair<iterator, bool> p = c.insert(key, value);
  47. ar.reset_object_address(&p.first->first, &key);
  48. }
  49. }
  50. template<class Archive, class T, class VoidPtrMap, class CloneAllocator, bool Ordered>
  51. void load(Archive& ar, ptr_multimap_adapter<T, VoidPtrMap, CloneAllocator,Ordered>& c, unsigned int /*version*/)
  52. {
  53. typedef ptr_multimap_adapter<T, VoidPtrMap, CloneAllocator,Ordered> container;
  54. typedef BOOST_DEDUCED_TYPENAME container::key_type key_type;
  55. typedef BOOST_DEDUCED_TYPENAME container::size_type size_type;
  56. typedef BOOST_DEDUCED_TYPENAME container::iterator iterator;
  57. c.clear();
  58. size_type n;
  59. ar >> boost::serialization::make_nvp( ptr_container_detail::count(), n );
  60. for(size_type i = 0u; i != n; ++i)
  61. {
  62. key_type key;
  63. T* value;
  64. ar >> boost::serialization::make_nvp( ptr_container_detail::first(), key );
  65. ar >> boost::serialization::make_nvp( ptr_container_detail::second(), value );
  66. iterator p = c.insert(key, value);
  67. ar.reset_object_address(&p->first, &key);
  68. }
  69. }
  70. } // namespace serialization
  71. } // namespace boost
  72. #endif