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
void operator()(msgpack::object::with_zone &o, const std::unordered_multiset< Key, Hash, Compare, Alloc > &v) const
Definition: unordered_set.hpp:143
uint32_t size
Definition: object_fwd.hpp:23
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition: cpp03_zone.hpp:246
msgpack::object const & operator()(msgpack::object const &o, std::unordered_set< Key, Hash, Compare, Alloc > &v) const
Definition: unordered_set.hpp:44
uint32_t checked_get_container_size(T size)
Definition: check_container_size.hpp:55
Definition: object_fwd_decl.hpp:62
union_type via
Definition: object_fwd.hpp:93
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::unordered_set< Key, Hash, Compare, Alloc > &v) const
Definition: unordered_set.hpp:61
msgpack::zone & zone
Definition: object.hpp:36
msgpack::object * ptr
Definition: object_fwd.hpp:24
packer< Stream > & pack_array(uint32_t n)
Packing array header and size.
Definition: pack.hpp:1160
Definition: adaptor_base.hpp:15
void operator()(msgpack::object::with_zone &o, const std::unordered_set< Key, Hash, Compare, Alloc > &v) const
Definition: unordered_set.hpp:74
Definition: object.hpp:34
packer< Stream > & pack(const T &v)
Packing function template.
msgpack::object const & operator()(msgpack::object const &o, std::unordered_multiset< Key, Hash, Compare, Alloc > &v) const
Definition: unordered_set.hpp:113
std::enable_if< msgpack::has_as< T >::value, T >::type as() const
Get value as T.
Definition: object.hpp:593
Definition: adaptor_base.hpp:43
Definition: object_fwd.hpp:236
Definition: adaptor_base.hpp:32
std::size_t size(T const &t)
Definition: size_equal_only.hpp:24
std::unordered_set< Key, Hash, Compare, Alloc > operator()(msgpack::object const &o) const
Definition: unordered_set.hpp:29
msgpack::object_array array
Definition: object_fwd.hpp:85
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:75
msgpack::type::object_type type
Definition: object_fwd.hpp:92
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:58
Definition: object_fwd_decl.hpp:41
std::unordered_multiset< Key, Hash, Compare, Alloc > operator()(msgpack::object const &o) const
Definition: unordered_set.hpp:98
#define MSGPACK_ZONE_ALIGNOF(type)
Definition: cpp03_zone_decl.hpp:30
The class template that supports continuous packing.
Definition: adaptor_base_decl.hpp:24
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::unordered_multiset< Key, Hash, Compare, Alloc > &v) const
Definition: unordered_set.hpp:130
#define MSGPACK_NULLPTR
Definition: cpp_config_decl.hpp:35
T & move(T &t)
Definition: adaptor_base.hpp:27