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