cereal
A C++11 library for serialization
pair_associative_container.hpp
Go to the documentation of this file.
1 
5 /*
6  Copyright (c) 2014, Randolph Voorhies, Shane Grant
7  All rights reserved.
8 
9  Redistribution and use in source and binary forms, with or without
10  modification, are permitted provided that the following conditions are met:
11  * Redistributions of source code must retain the above copyright
12  notice, this list of conditions and the following disclaimer.
13  * Redistributions in binary form must reproduce the above copyright
14  notice, this list of conditions and the following disclaimer in the
15  documentation and/or other materials provided with the distribution.
16  * Neither the name of the copyright holder nor the
17  names of its contributors may be used to endorse or promote products
18  derived from this software without specific prior written permission.
19 
20  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
24  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #ifndef CEREAL_CONCEPTS_PAIR_ASSOCIATIVE_CONTAINER_HPP_
32 #define CEREAL_CONCEPTS_PAIR_ASSOCIATIVE_CONTAINER_HPP_
33 
34 #include "cereal/cereal.hpp"
35 
36 namespace cereal
37 {
39  template <class Archive, template <typename...> class Map, typename... Args, typename = typename Map<Args...>::mapped_type> inline
40  void CEREAL_SAVE_FUNCTION_NAME( Archive & ar, Map<Args...> const & map )
41  {
42  ar( make_size_tag( static_cast<size_type>(map.size()) ) );
43 
44  for( const auto & i : map )
45  ar( make_map_item(i.first, i.second) );
46  }
47 
49  template <class Archive, template <typename...> class Map, typename... Args, typename = typename Map<Args...>::mapped_type> inline
50  void CEREAL_LOAD_FUNCTION_NAME( Archive & ar, Map<Args...> & map )
51  {
52  size_type size;
53  ar( make_size_tag( size ) );
54 
55  map.clear();
56 
57  auto hint = map.begin();
58  for( size_t i = 0; i < size; ++i )
59  {
60  typename Map<Args...>::key_type key;
61  typename Map<Args...>::mapped_type value;
62 
63  ar( make_map_item(key, value) );
64  #ifdef CEREAL_OLDER_GCC
65  hint = map.insert( hint, std::make_pair(std::move(key), std::move(value)) );
66  #else // NOT CEREAL_OLDER_GCC
67  hint = map.emplace_hint( hint, std::move( key ), std::move( value ) );
68  #endif // NOT CEREAL_OLDER_GCC
69  }
70  }
71 } // namespace cereal
72 
73 #endif // CEREAL_CONCEPTS_PAIR_ASSOCIATIVE_CONTAINER_HPP_
CEREAL_LOAD_FUNCTION_NAME
#define CEREAL_LOAD_FUNCTION_NAME
The deserialization (load) function name to search for.
Definition: macros.hpp:85
CEREAL_SAVE_FUNCTION_NAME
#define CEREAL_SAVE_FUNCTION_NAME
The serialization (save) function name to search for.
Definition: macros.hpp:92
cereal.hpp
Main cereal functionality.
cereal::make_map_item
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:385
cereal::size_type
CEREAL_SIZE_TYPE size_type
The size type used by cereal.
Definition: helpers.hpp:61