cereal
A C++11 library for serialization
map.hpp
Go to the documentation of this file.
1 
4 /*
5  Copyright (c) 2014, Randolph Voorhies, Shane Grant
6  All rights reserved.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10  * Redistributions of source code must retain the above copyright
11  notice, this list of conditions and the following disclaimer.
12  * Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions and the following disclaimer in the
14  documentation and/or other materials provided with the distribution.
15  * Neither the name of cereal nor the
16  names of its contributors may be used to endorse or promote products
17  derived from this software without specific prior written permission.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES OR SHANE GRANT BE LIABLE FOR ANY
23  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 #ifndef CEREAL_TYPES_MAP_HPP_
31 #define CEREAL_TYPES_MAP_HPP_
32 
33 #include <cereal/cereal.hpp>
34 #include <map>
35 
36 namespace cereal
37 {
38  namespace map_detail
39  {
41  template <class Archive, class MapT> inline
42  void save( Archive & ar, MapT const & map )
43  {
44  ar( make_size_tag( static_cast<size_type>(map.size()) ) );
45 
46  for( const auto & i : map )
47  {
48  ar( make_map_item(i.first, i.second) );
49  }
50  }
51 
53  template <class Archive, class MapT> inline
54  void load( Archive & ar, MapT & map )
55  {
56  size_type size;
57  ar( make_size_tag( size ) );
58 
59  map.clear();
60 
61  auto hint = map.begin();
62  for( size_t i = 0; i < size; ++i )
63  {
64  typename MapT::key_type key;
65  typename MapT::mapped_type value;
66 
67  ar( make_map_item(key, value) );
68  #ifdef CEREAL_OLDER_GCC
69  hint = map.insert( hint, std::make_pair(std::move(key), std::move(value)) );
70  #else // NOT CEREAL_OLDER_GCC
71  hint = map.emplace_hint( hint, std::move( key ), std::move( value ) );
72  #endif // NOT CEREAL_OLDER_GCC
73  }
74  }
75  }
76 
78  template <class Archive, class K, class T, class C, class A> inline
79  void CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::map<K, T, C, A> const & map )
80  {
81  map_detail::save( ar, map );
82  }
83 
85  template <class Archive, class K, class T, class C, class A> inline
86  void CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::map<K, T, C, A> & map )
87  {
88  map_detail::load( ar, map );
89  }
90 
92 
93  template <class Archive, class K, class T, class C, class A> inline
94  void CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::multimap<K, T, C, A> const & multimap )
95  {
96  map_detail::save( ar, multimap );
97  }
98 
100 
101  template <class Archive, class K, class T, class C, class A> inline
102  void CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::multimap<K, T, C, A> & multimap )
103  {
104  map_detail::load( ar, multimap );
105  }
106 } // namespace cereal
107 
108 #endif // CEREAL_TYPES_MAP_HPP_
MapItem< KeyType, ValueType > make_map_item(KeyType &&key, ValueType &&value)
Create a MapItem so that human readable archives will group keys and values together.
Definition: helpers.hpp:323
uint64_t size_type
The size type used by cereal.
Definition: helpers.hpp:59
Definition: access.hpp:39
Main cereal functionality.
#define CEREAL_LOAD_FUNCTION_NAME
The deserialization (load) function name to search for.
Definition: macros.hpp:58
#define CEREAL_SAVE_FUNCTION_NAME
The serialization (save) function name to search for.
Definition: macros.hpp:65