MessagePack for C++
unordered_map.hpp
Go to the documentation of this file.
1 //
2 // MessagePack for C++ static resolution routine
3 //
4 // Copyright (C) 2014-2015 KONDO Takatoshi
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //
10 #ifndef MSGPACK_V1_TYPE_CPP11_UNORDERED_MAP_HPP
11 #define MSGPACK_V1_TYPE_CPP11_UNORDERED_MAP_HPP
12 
13 #include "msgpack/versioning.hpp"
16 
17 #include <unordered_map>
18 
19 namespace msgpack {
20 
24 
25 namespace adaptor {
26 
27 template <typename K, typename V, typename Hash, typename Compare, typename Alloc>
28 struct as<
29  std::unordered_map<K, V, Hash, Compare, Alloc>,
30  typename std::enable_if<msgpack::has_as<K>::value || msgpack::has_as<V>::value>::type> {
31  std::unordered_map<K, V, Hash, Compare, Alloc> operator()(msgpack::object const& o) const {
32  if (o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
34  msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size);
35  std::unordered_map<K, V, Hash, Compare, Alloc> v;
36  for (; p != pend; ++p) {
37  v.emplace(p->key.as<K>(), p->val.as<V>());
38  }
39  return v;
40  }
41 };
42 
43 template <typename K, typename V, typename Hash, typename Compare, typename Alloc>
44 struct convert<std::unordered_map<K, V, Hash, Compare, Alloc>> {
45  msgpack::object const& operator()(msgpack::object const& o, std::unordered_map<K, V, Hash, Compare, Alloc>& v) const {
46  if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
48  msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size);
49  std::unordered_map<K, V, Hash, Compare, Alloc> tmp;
50  for(; p != pend; ++p) {
51  K key;
52  p->key.convert(key);
53  p->val.convert(tmp[std::move(key)]);
54  }
55  v = std::move(tmp);
56  return o;
57  }
58 };
59 
60 template <typename K, typename V, typename Hash, typename Compare, typename Alloc>
61 struct pack<std::unordered_map<K, V, Hash, Compare, Alloc>> {
62  template <typename Stream>
63  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::unordered_map<K, V, Hash, Compare, Alloc>& v) const {
64  uint32_t size = checked_get_container_size(v.size());
65  o.pack_map(size);
66  for(typename std::unordered_map<K, V, Hash, Compare, Alloc>::const_iterator it(v.begin()), it_end(v.end());
67  it != it_end; ++it) {
68  o.pack(it->first);
69  o.pack(it->second);
70  }
71  return o;
72  }
73 };
74 
75 template <typename K, typename V, typename Hash, typename Compare, typename Alloc>
76 struct object_with_zone<std::unordered_map<K, V, Hash, Compare, Alloc>> {
77  void operator()(msgpack::object::with_zone& o, const std::unordered_map<K, V, Hash, Compare, Alloc>& v) const {
79  if(v.empty()) {
81  o.via.map.size = 0;
82  } else {
83  uint32_t size = checked_get_container_size(v.size());
85  msgpack::object_kv* const pend = p + size;
86  o.via.map.ptr = p;
87  o.via.map.size = size;
88  typename std::unordered_map<K, V, Hash, Compare, Alloc>::const_iterator it(v.begin());
89  do {
90  p->key = msgpack::object(it->first, o.zone);
91  p->val = msgpack::object(it->second, o.zone);
92  ++p;
93  ++it;
94  } while(p < pend);
95  }
96  }
97 };
98 
99 
100 template <typename K, typename V, typename Hash, typename Compare, typename Alloc>
101 struct as<
102  std::unordered_multimap<K, V, Hash, Compare, Alloc>,
103  typename std::enable_if<msgpack::has_as<K>::value || msgpack::has_as<V>::value>::type> {
104  std::unordered_multimap<K, V, Hash, Compare, Alloc> operator()(msgpack::object const& o) const {
105  if (o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
107  msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size);
108  std::unordered_multimap<K, V, Hash, Compare, Alloc> v;
109  for (; p != pend; ++p) {
110  v.emplace(p->key.as<K>(), p->val.as<V>());
111  }
112  return v;
113  }
114 };
115 
116 template <typename K, typename V, typename Hash, typename Compare, typename Alloc>
117 struct convert<std::unordered_multimap<K, V, Hash, Compare, Alloc>> {
118  msgpack::object const& operator()(msgpack::object const& o, std::unordered_multimap<K, V, Hash, Compare, Alloc>& v) const {
119  if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
121  msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size);
122  std::unordered_multimap<K, V, Hash, Compare, Alloc> tmp;
123  for(; p != pend; ++p) {
124  std::pair<K, V> value;
125  p->key.convert(value.first);
126  p->val.convert(value.second);
127  tmp.insert(std::move(value));
128  }
129  v = std::move(tmp);
130  return o;
131  }
132 };
133 
134 template <typename K, typename V, typename Hash, typename Compare, typename Alloc>
135 struct pack<std::unordered_multimap<K, V, Hash, Compare, Alloc>> {
136  template <typename Stream>
137  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::unordered_multimap<K, V, Hash, Compare, Alloc>& v) const {
138  uint32_t size = checked_get_container_size(v.size());
139  o.pack_map(size);
140  for(typename std::unordered_multimap<K, V, Hash, Compare, Alloc>::const_iterator it(v.begin()), it_end(v.end());
141  it != it_end; ++it) {
142  o.pack(it->first);
143  o.pack(it->second);
144  }
145  return o;
146  }
147 };
148 
149 template <typename K, typename V, typename Hash, typename Compare, typename Alloc>
150 struct object_with_zone<std::unordered_multimap<K, V, Hash, Compare, Alloc>> {
151  void operator()(msgpack::object::with_zone& o, const std::unordered_multimap<K, V, Hash, Compare, Alloc>& v) const {
153  if(v.empty()) {
155  o.via.map.size = 0;
156  } else {
157  uint32_t size = checked_get_container_size(v.size());
159  msgpack::object_kv* const pend = p + size;
160  o.via.map.ptr = p;
161  o.via.map.size = size;
162  typename std::unordered_multimap<K, V, Hash, Compare, Alloc>::const_iterator it(v.begin());
163  do {
164  p->key = msgpack::object(it->first, o.zone);
165  p->val = msgpack::object(it->second, o.zone);
166  ++p;
167  ++it;
168  } while(p < pend);
169  }
170  }
171 };
172 
173 } // namespace adaptor
174 
176 } // MSGPACK_API_VERSION_NAMESPACE(v1)
178 
179 } // namespace msgpack
180 
181 
182 #endif // MSGPACK_V1_TYPE_CPP11_UNORDERED_MAP_HPP
The class template that supports continuous packing.
Definition: pack.hpp:33
packer< Stream > & pack_map(uint32_t n)
Packing map header and size.
Definition: pack.hpp:1213
packer< Stream > & pack(const T &v)
Packing function template.
Definition: object_fwd.hpp:231
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition: cpp03_zone.hpp:256
std::size_t size(T const &t)
Definition: size_equal_only.hpp:24
@ MAP
Definition: object_fwd_decl.hpp:41
Definition: adaptor_base.hpp:15
uint32_t checked_get_container_size(T size)
Definition: check_container_size.hpp:55
std::unordered_map< K, V, Hash, Compare, Alloc > operator()(msgpack::object const &o) const
Definition: unordered_map.hpp:31
std::unordered_multimap< K, V, Hash, Compare, Alloc > operator()(msgpack::object const &o) const
Definition: unordered_map.hpp:104
Definition: object_fwd_decl.hpp:61
msgpack::object const & operator()(msgpack::object const &o, std::unordered_map< K, V, Hash, Compare, Alloc > &v) const
Definition: unordered_map.hpp:45
msgpack::object const & operator()(msgpack::object const &o, std::unordered_multimap< K, V, Hash, Compare, Alloc > &v) const
Definition: unordered_map.hpp:118
Definition: adaptor_base.hpp:27
void operator()(msgpack::object::with_zone &o, const std::unordered_map< K, V, Hash, Compare, Alloc > &v) const
Definition: unordered_map.hpp:77
void operator()(msgpack::object::with_zone &o, const std::unordered_multimap< K, V, Hash, Compare, Alloc > &v) const
Definition: unordered_map.hpp:151
Definition: adaptor_base.hpp:43
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::unordered_map< K, V, Hash, Compare, Alloc > &v) const
Definition: unordered_map.hpp:63
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::unordered_multimap< K, V, Hash, Compare, Alloc > &v) const
Definition: unordered_map.hpp:137
Definition: adaptor_base.hpp:32
Definition: object.hpp:35
msgpack::zone & zone
Definition: object.hpp:37
Definition: object.hpp:30
msgpack::object val
Definition: object.hpp:32
msgpack::object key
Definition: object.hpp:31
uint32_t size
Definition: object_fwd.hpp:28
msgpack::object_kv * ptr
Definition: object_fwd.hpp:29
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:75
std::enable_if< msgpack::has_as< T >::value, T >::type as() const
Get value as T.
Definition: object.hpp:1121
union_type via
Definition: object_fwd.hpp:93
msgpack::enable_if< !msgpack::is_array< T >::value &&!msgpack::is_pointer< T >::value, T & >::type convert(T &v) const
Convert the object.
Definition: object.hpp:1071
msgpack::type::object_type type
Definition: object_fwd.hpp:92
msgpack::object_map map
Definition: object_fwd.hpp:86
#define MSGPACK_NULLPTR
Definition: cpp_config_decl.hpp:85
#define MSGPACK_ZONE_ALIGNOF(type)
Definition: cpp03_zone_decl.hpp:30
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:66