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) 2008-2015 FURUHASHI Sadayuki
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_TYPE_TR1_UNORDERED_MAP_HPP
11 #define MSGPACK_TYPE_TR1_UNORDERED_MAP_HPP
12 
13 #include "msgpack/versioning.hpp"
15 #include "msgpack/object.hpp"
17 
18 #if defined(_LIBCPP_VERSION) || (_MSC_VER >= 1700)
19 
20 #define MSGPACK_HAS_STD_UNORDERED_MAP
21 #include <unordered_map>
22 #define MSGPACK_STD_TR1 std
23 
24 #else // defined(_LIBCPP_VERSION) || (_MSC_VER >= 1700)
25 
26 #if __GNUC__ >= 4
27 
28 #define MSGPACK_HAS_STD_TR1_UNORDERED_MAP
29 
30 #include <tr1/unordered_map>
31 #define MSGPACK_STD_TR1 std::tr1
32 
33 #endif // __GNUC__ >= 4
34 
35 #endif // defined(_LIBCPP_VERSION) || (_MSC_VER >= 1700)
36 
37 #if defined(MSGPACK_STD_TR1)
38 
39 namespace msgpack {
40 
44 
45 namespace adaptor {
46 
47 template <typename K, typename V, typename Hash, typename Pred, typename Alloc>
48 struct convert<MSGPACK_STD_TR1::unordered_map<K, V, Hash, Pred, Alloc> > {
49  msgpack::object const& operator()(msgpack::object const& o, MSGPACK_STD_TR1::unordered_map<K, V, Hash, Pred, Alloc>& v) const {
50  if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
52  msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size);
53  MSGPACK_STD_TR1::unordered_map<K, V, Hash, Pred, Alloc> tmp;
54  for(; p != pend; ++p) {
55  K key;
56  p->key.convert(key);
57  p->val.convert(tmp[key]);
58  }
59  tmp.swap(v);
60  return o;
61  }
62 };
63 
64 template <typename K, typename V, typename Hash, typename Pred, typename Alloc>
65 struct pack<MSGPACK_STD_TR1::unordered_map<K, V, Hash, Pred, Alloc> > {
66  template <typename Stream>
67  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const MSGPACK_STD_TR1::unordered_map<K, V, Hash, Pred, Alloc>& v) const {
68  uint32_t size = checked_get_container_size(v.size());
69  o.pack_map(size);
70  for(typename MSGPACK_STD_TR1::unordered_map<K, V, Hash, Pred, Alloc>::const_iterator it(v.begin()), it_end(v.end());
71  it != it_end; ++it) {
72  o.pack(it->first);
73  o.pack(it->second);
74  }
75  return o;
76  }
77 };
78 
79 template <typename K, typename V, typename Hash, typename Pred, typename Alloc>
80 struct object_with_zone<MSGPACK_STD_TR1::unordered_map<K, V, Hash, Pred, Alloc> > {
81  void operator()(msgpack::object::with_zone& o, const MSGPACK_STD_TR1::unordered_map<K, V, Hash, Pred, Alloc>& v) const {
83  if(v.empty()) {
85  o.via.map.size = 0;
86  } else {
87  uint32_t size = checked_get_container_size(v.size());
89  msgpack::object_kv* const pend = p + size;
90  o.via.map.ptr = p;
91  o.via.map.size = size;
92  typename MSGPACK_STD_TR1::unordered_map<K, V, Hash, Pred, Alloc>::const_iterator it(v.begin());
93  do {
94  p->key = msgpack::object(it->first, o.zone);
95  p->val = msgpack::object(it->second, o.zone);
96  ++p;
97  ++it;
98  } while(p < pend);
99  }
100  }
101 };
102 
103 template <typename K, typename V, typename Hash, typename Pred, typename Alloc>
104 struct convert<MSGPACK_STD_TR1::unordered_multimap<K, V, Hash, Pred, Alloc> > {
105  msgpack::object const& operator()(msgpack::object const& o, MSGPACK_STD_TR1::unordered_multimap<K, V, Hash, Pred, Alloc>& v) const {
106  if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
108  msgpack::object_kv* const pend(o.via.map.ptr + o.via.map.size);
109  MSGPACK_STD_TR1::unordered_multimap<K, V, Hash, Pred, Alloc> tmp;
110  for(; p != pend; ++p) {
111  std::pair<K, V> value;
112  p->key.convert(value.first);
113  p->val.convert(value.second);
114  tmp.insert(value);
115  }
116  tmp.swap(v);
117  return o;
118  }
119 };
120 
121 template <typename K, typename V, typename Hash, typename Pred, typename Alloc>
122 struct pack<MSGPACK_STD_TR1::unordered_multimap<K, V, Hash, Pred, Alloc> > {
123  template <typename Stream>
124  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const MSGPACK_STD_TR1::unordered_multimap<K, V, Hash, Pred, Alloc>& v) const {
125  uint32_t size = checked_get_container_size(v.size());
126  o.pack_map(size);
127  for(typename MSGPACK_STD_TR1::unordered_multimap<K, V, Hash, Pred, Alloc>::const_iterator it(v.begin()), it_end(v.end());
128  it != it_end; ++it) {
129  o.pack(it->first);
130  o.pack(it->second);
131  }
132  return o;
133  }
134 };
135 
136 template <typename K, typename V, typename Hash, typename Pred, typename Alloc>
137 struct object_with_zone<MSGPACK_STD_TR1::unordered_multimap<K, V, Hash, Pred, Alloc> > {
138  void operator()(msgpack::object::with_zone& o, const MSGPACK_STD_TR1::unordered_multimap<K, V, Hash, Pred, Alloc>& v) const {
140  if(v.empty()) {
142  o.via.map.size = 0;
143  } else {
144  uint32_t size = checked_get_container_size(v.size());
146  msgpack::object_kv* const pend = p + size;
147  o.via.map.ptr = p;
148  o.via.map.size = size;
149  typename MSGPACK_STD_TR1::unordered_multimap<K, V, Hash, Pred, Alloc>::const_iterator it(v.begin());
150  do {
151  p->key = msgpack::object(it->first, o.zone);
152  p->val = msgpack::object(it->second, o.zone);
153  ++p;
154  ++it;
155  } while(p < pend);
156  }
157  }
158 };
159 
160 } // namespace adaptor
161 
163 } // MSGPACK_API_VERSION_NAMESPACE(v1)
165 
166 } // namespace msgpack
167 
168 #undef MSGPACK_STD_TR1
169 
170 #endif // MSGPACK_STD_TR1
171 
172 #endif // MSGPACK_TYPE_TR1_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:1235
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:255
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
void pack(msgpack::packer< Stream > &o, const T &v)
Definition: object.hpp:1185
uint32_t checked_get_container_size(T size)
Definition: check_container_size.hpp:55
void convert(T &v, msgpack::object const &o)
Definition: object.hpp:1178
msgpack::object const & operator()(msgpack::object const &o, T &v) const
Definition: object.hpp:646
void operator()(msgpack::object::with_zone &o, T const &v) const
Definition: object.hpp:662
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, T const &v) const
Definition: object.hpp:655
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
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:1076
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