serialize_ptr_map_adapter.hpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/core/serialization.hpp>
  10. namespace boost
  11. {
  12. namespace serialization
  13. {
  14. template<class Archive, class T, class VoidPtrMap, class CloneAllocator, bool Ordered>
  15. void save(Archive& ar, const ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap, CloneAllocator,Ordered>& c, unsigned int /*version*/)
  16. {
  17. typedef ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap, CloneAllocator,Ordered> container;
  18. typedef BOOST_DEDUCED_TYPENAME container::const_iterator const_iterator;
  19. ar << boost::serialization::make_nvp( ptr_container_detail::count(),
  20. ptr_container_detail::serialize_as_const(c.size()) );
  21. const_iterator i = c.begin(), e = c.end();
  22. for(; i != e; ++i)
  23. {
  24. ar << boost::serialization::make_nvp( ptr_container_detail::first(), i->first );
  25. ar << boost::serialization::make_nvp( ptr_container_detail::second(),
  26. ptr_container_detail::serialize_as_const(i->second) );
  27. }
  28. }
  29. template<class Archive, class T, class VoidPtrMap, class CloneAllocator, bool Ordered>
  30. void load(Archive& ar, ptr_map_adapter<T, VoidPtrMap, CloneAllocator,Ordered>& c, unsigned int /*version*/)
  31. {
  32. typedef ptr_map_adapter<T, VoidPtrMap, CloneAllocator,Ordered> container;
  33. typedef BOOST_DEDUCED_TYPENAME container::key_type key_type;
  34. typedef BOOST_DEDUCED_TYPENAME container::size_type size_type;
  35. typedef BOOST_DEDUCED_TYPENAME container::iterator iterator;
  36. c.clear();
  37. size_type n;
  38. ar >> boost::serialization::make_nvp( ptr_container_detail::count(), n );
  39. for(size_type i = 0u; i != n; ++i)
  40. {
  41. key_type key;
  42. T* value;
  43. ar >> boost::serialization::make_nvp( ptr_container_detail::first(), key );
  44. ar >> boost::serialization::make_nvp( ptr_container_detail::second(), value );
  45. std::pair<iterator, bool> p = c.insert(key, value);
  46. ar.reset_object_address(&p.first->first, &key);
  47. }
  48. }
  49. template<class Archive, class T, class VoidPtrMap, class CloneAllocator, bool Ordered>
  50. void load(Archive& ar, ptr_multimap_adapter<T, VoidPtrMap, CloneAllocator,Ordered>& c, unsigned int /*version*/)
  51. {
  52. typedef ptr_multimap_adapter<T, VoidPtrMap, CloneAllocator,Ordered> container;
  53. typedef BOOST_DEDUCED_TYPENAME container::key_type key_type;
  54. typedef BOOST_DEDUCED_TYPENAME container::size_type size_type;
  55. typedef BOOST_DEDUCED_TYPENAME container::iterator iterator;
  56. c.clear();
  57. size_type n;
  58. ar >> boost::serialization::make_nvp( ptr_container_detail::count(), n );
  59. for(size_type i = 0u; i != n; ++i)
  60. {
  61. key_type key;
  62. T* value;
  63. ar >> boost::serialization::make_nvp( ptr_container_detail::first(), key );
  64. ar >> boost::serialization::make_nvp( ptr_container_detail::second(), value );
  65. iterator p = c.insert(key, value);
  66. ar.reset_object_address(&p->first, &key);
  67. }
  68. }
  69. } // namespace serialization
  70. } // namespace boost
  71. #endif