MessagePack for C++
unordered_set.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_SET_HPP
11 #define MSGPACK_V1_TYPE_CPP11_UNORDERED_SET_HPP
12 
13 #include "msgpack/versioning.hpp"
15 #include "msgpack/object.hpp"
17 
18 #include <unordered_set>
19 
20 namespace msgpack {
21 
25 
26 namespace adaptor {
27 
28 template <typename Key, typename Hash, typename Compare, typename Alloc>
29 struct as<std::unordered_set<Key, Hash, Compare, Alloc>, typename std::enable_if<msgpack::has_as<Key>::value>::type> {
30  std::unordered_set<Key, Hash, Compare, Alloc> operator()(msgpack::object const& o) const {
31  if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
33  msgpack::object* const pbegin = o.via.array.ptr;
34  std::unordered_set<Key, Hash, Compare, Alloc> v;
35  while (p > pbegin) {
36  --p;
37  v.insert(p->as<Key>());
38  }
39  return v;
40  }
41 };
42 
43 template <typename Key, typename Hash, typename Compare, typename Alloc>
44 struct convert<std::unordered_set<Key, Hash, Compare, Alloc>> {
45  msgpack::object const& operator()(msgpack::object const& o, std::unordered_set<Key, Hash, Compare, Alloc>& v) const {
46  if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
48  msgpack::object* const pbegin = o.via.array.ptr;
49  std::unordered_set<Key, Hash, Compare, Alloc> tmp;
50  while(p > pbegin) {
51  --p;
52  tmp.insert(p->as<Key>());
53  }
54  v = std::move(tmp);
55  return o;
56  }
57 };
58 
59 template <typename Key, typename Hash, typename Compare, typename Alloc>
60 struct pack<std::unordered_set<Key, Hash, Compare, Alloc>> {
61  template <typename Stream>
62  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::unordered_set<Key, Hash, Compare, Alloc>& v) const {
63  uint32_t size = checked_get_container_size(v.size());
64  o.pack_array(size);
65  for(typename std::unordered_set<Key, Hash, Compare, Alloc>::const_iterator it(v.begin()), it_end(v.end());
66  it != it_end; ++it) {
67  o.pack(*it);
68  }
69  return o;
70  }
71 };
72 
73 template <typename Key, typename Hash, typename Compare, typename Alloc>
74 struct object_with_zone<std::unordered_set<Key, Hash, Compare, Alloc>> {
75  void operator()(msgpack::object::with_zone& o, const std::unordered_set<Key, Hash, Compare, Alloc>& v) const {
77  if(v.empty()) {
79  o.via.array.size = 0;
80  } else {
81  uint32_t size = checked_get_container_size(v.size());
83  msgpack::object* const pend = p + size;
84  o.via.array.ptr = p;
85  o.via.array.size = size;
86  typename std::unordered_set<Key, Hash, Compare, Alloc>::const_iterator it(v.begin());
87  do {
88  *p = msgpack::object(*it, o.zone);
89  ++p;
90  ++it;
91  } while(p < pend);
92  }
93  }
94 };
95 
96 
97 template <typename Key, typename Hash, typename Compare, typename Alloc>
98 struct as<std::unordered_multiset<Key, Hash, Compare, Alloc>, typename std::enable_if<msgpack::has_as<Key>::value>::type> {
99  std::unordered_multiset<Key, Hash, Compare, Alloc> operator()(msgpack::object const& o) const {
100  if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
102  msgpack::object* const pbegin = o.via.array.ptr;
103  std::unordered_multiset<Key, Hash, Compare, Alloc> v;
104  while (p > pbegin) {
105  --p;
106  v.insert(p->as<Key>());
107  }
108  return v;
109  }
110 };
111 
112 template <typename Key, typename Hash, typename Compare, typename Alloc>
113 struct convert<std::unordered_multiset<Key, Hash, Compare, Alloc>> {
114  msgpack::object const& operator()(msgpack::object const& o, std::unordered_multiset<Key, Hash, Compare, Alloc>& v) const {
115  if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
117  msgpack::object* const pbegin = o.via.array.ptr;
118  std::unordered_multiset<Key, Hash, Compare, Alloc> tmp;
119  while(p > pbegin) {
120  --p;
121  tmp.insert(p->as<Key>());
122  }
123  v = std::move(tmp);
124  return o;
125  }
126 };
127 
128 template <typename Key, typename Hash, typename Compare, typename Alloc>
129 struct pack<std::unordered_multiset<Key, Hash, Compare, Alloc>> {
130  template <typename Stream>
131  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::unordered_multiset<Key, Hash, Compare, Alloc>& v) const {
132  uint32_t size = checked_get_container_size(v.size());
133  o.pack_array(size);
134  for(typename std::unordered_multiset<Key, Hash, Compare, Alloc>::const_iterator it(v.begin()), it_end(v.end());
135  it != it_end; ++it) {
136  o.pack(*it);
137  }
138  return o;
139  }
140 };
141 
142 template <typename Key, typename Hash, typename Compare, typename Alloc>
143 struct object_with_zone<std::unordered_multiset<Key, Hash, Compare, Alloc>> {
144  void operator()(msgpack::object::with_zone& o, const std::unordered_multiset<Key, Hash, Compare, Alloc>& v) const {
146  if(v.empty()) {
148  o.via.array.size = 0;
149  } else {
150  uint32_t size = checked_get_container_size(v.size());
152  msgpack::object* const pend = p + size;
153  o.via.array.ptr = p;
154  o.via.array.size = size;
155  typename std::unordered_multiset<Key, Hash, Compare, Alloc>::const_iterator it(v.begin());
156  do {
157  *p = msgpack::object(*it, o.zone);
158  ++p;
159  ++it;
160  } while(p < pend);
161  }
162  }
163 };
164 
165 } // namespace adaptor
166 
168 } // MSGPACK_API_VERSION_NAMESPACE(v1)
170 
171 } // namespace msgpack
172 
173 #endif // MSGPACK_V1_TYPE_CPP11_UNORDERED_SET_HPP
The class template that supports continuous packing.
Definition: pack.hpp:33
packer< Stream > & pack_array(uint32_t n)
Packing array header and size.
Definition: pack.hpp:1217
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
@ ARRAY
Definition: object_fwd_decl.hpp:40
Definition: adaptor_base.hpp:15
uint32_t checked_get_container_size(T size)
Definition: check_container_size.hpp:55
std::unordered_multiset< Key, Hash, Compare, Alloc > operator()(msgpack::object const &o) const
Definition: unordered_set.hpp:99
std::unordered_set< Key, Hash, Compare, Alloc > operator()(msgpack::object const &o) const
Definition: unordered_set.hpp:30
Definition: object_fwd_decl.hpp:61
msgpack::object const & operator()(msgpack::object const &o, std::unordered_multiset< Key, Hash, Compare, Alloc > &v) const
Definition: unordered_set.hpp:114
msgpack::object const & operator()(msgpack::object const &o, std::unordered_set< Key, Hash, Compare, Alloc > &v) const
Definition: unordered_set.hpp:45
Definition: adaptor_base.hpp:27
void operator()(msgpack::object::with_zone &o, const std::unordered_multiset< Key, Hash, Compare, Alloc > &v) const
Definition: unordered_set.hpp:144
void operator()(msgpack::object::with_zone &o, const std::unordered_set< Key, Hash, Compare, Alloc > &v) const
Definition: unordered_set.hpp:75
Definition: adaptor_base.hpp:43
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::unordered_multiset< Key, Hash, Compare, Alloc > &v) const
Definition: unordered_set.hpp:131
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::unordered_set< Key, Hash, Compare, Alloc > &v) const
Definition: unordered_set.hpp:62
Definition: adaptor_base.hpp:32
Definition: object.hpp:35
msgpack::zone & zone
Definition: object.hpp:37
uint32_t size
Definition: object_fwd.hpp:23
msgpack::object * ptr
Definition: object_fwd.hpp:24
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:1126
union_type via
Definition: object_fwd.hpp:93
msgpack::type::object_type type
Definition: object_fwd.hpp:92
msgpack::object_array array
Definition: object_fwd.hpp:85
#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