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"
16 
17 #include <unordered_set>
18 
19 namespace msgpack {
20 
24 
25 namespace adaptor {
26 
27 template <typename Key, typename Hash, typename Compare, typename Alloc>
28 struct as<std::unordered_set<Key, Hash, Compare, Alloc>, typename std::enable_if<msgpack::has_as<Key>::value>::type> {
29  std::unordered_set<Key, Hash, Compare, Alloc> operator()(msgpack::object const& o) const {
30  if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
32  msgpack::object* const pbegin = o.via.array.ptr;
33  std::unordered_set<Key, Hash, Compare, Alloc> v;
34  while (p > pbegin) {
35  --p;
36  v.insert(p->as<Key>());
37  }
38  return v;
39  }
40 };
41 
42 template <typename Key, typename Hash, typename Compare, typename Alloc>
43 struct convert<std::unordered_set<Key, Hash, Compare, Alloc>> {
44  msgpack::object const& operator()(msgpack::object const& o, std::unordered_set<Key, Hash, Compare, Alloc>& v) const {
45  if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
47  msgpack::object* const pbegin = o.via.array.ptr;
48  std::unordered_set<Key, Hash, Compare, Alloc> tmp;
49  while(p > pbegin) {
50  --p;
51  tmp.insert(p->as<Key>());
52  }
53  v = std::move(tmp);
54  return o;
55  }
56 };
57 
58 template <typename Key, typename Hash, typename Compare, typename Alloc>
59 struct pack<std::unordered_set<Key, Hash, Compare, Alloc>> {
60  template <typename Stream>
61  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::unordered_set<Key, Hash, Compare, Alloc>& v) const {
62  uint32_t size = checked_get_container_size(v.size());
63  o.pack_array(size);
64  for(typename std::unordered_set<Key, Hash, Compare, Alloc>::const_iterator it(v.begin()), it_end(v.end());
65  it != it_end; ++it) {
66  o.pack(*it);
67  }
68  return o;
69  }
70 };
71 
72 template <typename Key, typename Hash, typename Compare, typename Alloc>
73 struct object_with_zone<std::unordered_set<Key, Hash, Compare, Alloc>> {
74  void operator()(msgpack::object::with_zone& o, const std::unordered_set<Key, Hash, Compare, Alloc>& v) const {
76  if(v.empty()) {
78  o.via.array.size = 0;
79  } else {
80  uint32_t size = checked_get_container_size(v.size());
82  msgpack::object* const pend = p + size;
83  o.via.array.ptr = p;
84  o.via.array.size = size;
85  typename std::unordered_set<Key, Hash, Compare, Alloc>::const_iterator it(v.begin());
86  do {
87  *p = msgpack::object(*it, o.zone);
88  ++p;
89  ++it;
90  } while(p < pend);
91  }
92  }
93 };
94 
95 
96 template <typename Key, typename Hash, typename Compare, typename Alloc>
97 struct as<std::unordered_multiset<Key, Hash, Compare, Alloc>, typename std::enable_if<msgpack::has_as<Key>::value>::type> {
98  std::unordered_multiset<Key, Hash, Compare, Alloc> operator()(msgpack::object const& o) const {
99  if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
101  msgpack::object* const pbegin = o.via.array.ptr;
102  std::unordered_multiset<Key, Hash, Compare, Alloc> v;
103  while (p > pbegin) {
104  --p;
105  v.insert(p->as<Key>());
106  }
107  return v;
108  }
109 };
110 
111 template <typename Key, typename Hash, typename Compare, typename Alloc>
112 struct convert<std::unordered_multiset<Key, Hash, Compare, Alloc>> {
113  msgpack::object const& operator()(msgpack::object const& o, std::unordered_multiset<Key, Hash, Compare, Alloc>& v) const {
114  if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
116  msgpack::object* const pbegin = o.via.array.ptr;
117  std::unordered_multiset<Key, Hash, Compare, Alloc> tmp;
118  while(p > pbegin) {
119  --p;
120  tmp.insert(p->as<Key>());
121  }
122  v = std::move(tmp);
123  return o;
124  }
125 };
126 
127 template <typename Key, typename Hash, typename Compare, typename Alloc>
128 struct pack<std::unordered_multiset<Key, Hash, Compare, Alloc>> {
129  template <typename Stream>
130  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::unordered_multiset<Key, Hash, Compare, Alloc>& v) const {
131  uint32_t size = checked_get_container_size(v.size());
132  o.pack_array(size);
133  for(typename std::unordered_multiset<Key, Hash, Compare, Alloc>::const_iterator it(v.begin()), it_end(v.end());
134  it != it_end; ++it) {
135  o.pack(*it);
136  }
137  return o;
138  }
139 };
140 
141 template <typename Key, typename Hash, typename Compare, typename Alloc>
142 struct object_with_zone<std::unordered_multiset<Key, Hash, Compare, Alloc>> {
143  void operator()(msgpack::object::with_zone& o, const std::unordered_multiset<Key, Hash, Compare, Alloc>& v) const {
145  if(v.empty()) {
147  o.via.array.size = 0;
148  } else {
149  uint32_t size = checked_get_container_size(v.size());
151  msgpack::object* const pend = p + size;
152  o.via.array.ptr = p;
153  o.via.array.size = size;
154  typename std::unordered_multiset<Key, Hash, Compare, Alloc>::const_iterator it(v.begin());
155  do {
156  *p = msgpack::object(*it, o.zone);
157  ++p;
158  ++it;
159  } while(p < pend);
160  }
161  }
162 };
163 
164 } // namespace adaptor
165 
167 } // MSGPACK_API_VERSION_NAMESPACE(v1)
169 
170 } // namespace msgpack
171 
172 #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:1195
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
@ 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:98
std::unordered_set< Key, Hash, Compare, Alloc > operator()(msgpack::object const &o) const
Definition: unordered_set.hpp:29
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:113
msgpack::object const & operator()(msgpack::object const &o, std::unordered_set< Key, Hash, Compare, Alloc > &v) const
Definition: unordered_set.hpp:44
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:143
void operator()(msgpack::object::with_zone &o, const std::unordered_set< Key, Hash, Compare, Alloc > &v) const
Definition: unordered_set.hpp:74
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:130
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::unordered_set< Key, Hash, Compare, Alloc > &v) const
Definition: unordered_set.hpp:61
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:1121
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