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