MessagePack for C++
set.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_V1_TYPE_SET_HPP
11 #define MSGPACK_V1_TYPE_SET_HPP
12 
13 #include "msgpack/versioning.hpp"
16 
17 #include <set>
18 
19 namespace msgpack {
20 
24 
25 namespace adaptor {
26 
27 #if !defined(MSGPACK_USE_CPP03)
28 
29 template <typename T, typename Compare, typename Alloc>
30 struct as<std::set<T, Compare, Alloc>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
31  std::set<T, Compare, Alloc> operator()(msgpack::object const& o) const {
32  if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
34  msgpack::object* const pbegin = o.via.array.ptr;
35  std::set<T, Compare, Alloc> v;
36  while (p > pbegin) {
37  --p;
38  v.insert(p->as<T>());
39  }
40  return v;
41  }
42 };
43 
44 #endif // !defined(MSGPACK_USE_CPP03)
45 
46 template <typename T, typename Compare, typename Alloc>
47 struct convert<std::set<T, Compare, Alloc> > {
48  msgpack::object const& operator()(msgpack::object const& o, std::set<T, Compare, Alloc>& v) const {
49  if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
51  msgpack::object* const pbegin = o.via.array.ptr;
52  std::set<T, Compare, Alloc> tmp;
53  while (p > pbegin) {
54  --p;
55  tmp.insert(p->as<T>());
56  }
57 #if __cplusplus >= 201103L
58  v = std::move(tmp);
59 #else
60  tmp.swap(v);
61 #endif
62  return o;
63  }
64 };
65 
66 template <typename T, typename Compare, typename Alloc>
67 struct pack<std::set<T, Compare, Alloc> > {
68  template <typename Stream>
69  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::set<T, Compare, Alloc>& v) const {
70  uint32_t size = checked_get_container_size(v.size());
71  o.pack_array(size);
72  for (typename std::set<T, Compare, Alloc>::const_iterator it(v.begin()), it_end(v.end());
73  it != it_end; ++it) {
74  o.pack(*it);
75  }
76  return o;
77  }
78 };
79 
80 template <typename T, typename Compare, typename Alloc>
81 struct object_with_zone<std::set<T, Compare, Alloc> > {
82  void operator()(msgpack::object::with_zone& o, const std::set<T, Compare, Alloc>& v) const {
84  if (v.empty()) {
85  o.via.array.ptr = nullptr;
86  o.via.array.size = 0;
87  }
88  else {
89  uint32_t size = checked_get_container_size(v.size());
90  msgpack::object* p = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*size));
91  msgpack::object* const pend = p + size;
92  o.via.array.ptr = p;
93  o.via.array.size = size;
94  typename std::set<T, Compare, Alloc>::const_iterator it(v.begin());
95  do {
96  *p = msgpack::object(*it, o.zone);
97  ++p;
98  ++it;
99  } while(p < pend);
100  }
101  }
102 };
103 
104 #if !defined(MSGPACK_USE_CPP03)
105 
106 template <typename T, typename Compare, typename Alloc>
107 struct as<std::multiset<T, Compare, Alloc>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
108  std::multiset<T, Compare, Alloc> operator()(msgpack::object const& o) const {
109  if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
111  msgpack::object* const pbegin = o.via.array.ptr;
112  std::multiset<T, Compare, Alloc> v;
113  while (p > pbegin) {
114  --p;
115  v.insert(p->as<T>());
116  }
117  return v;
118  }
119 };
120 
121 #endif // !defined(MSGPACK_USE_CPP03)
122 
123 template <typename T, typename Compare, typename Alloc>
124 struct convert<std::multiset<T, Compare, Alloc> > {
125  msgpack::object const& operator()(msgpack::object const& o, std::multiset<T, Compare, Alloc>& v) const {
126  if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
128  msgpack::object* const pbegin = o.via.array.ptr;
129  std::multiset<T, Compare, Alloc> tmp;
130  while (p > pbegin) {
131  --p;
132  tmp.insert(p->as<T>());
133  }
134 #if __cplusplus >= 201103L
135  v = std::move(tmp);
136 #else
137  tmp.swap(v);
138 #endif
139  return o;
140  }
141 };
142 
143 template <typename T, typename Compare, typename Alloc>
144 struct pack<std::multiset<T, Compare, Alloc> > {
145  template <typename Stream>
146  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::multiset<T, Compare, Alloc>& v) const {
147  uint32_t size = checked_get_container_size(v.size());
148  o.pack_array(size);
149  for (typename std::multiset<T, Compare, Alloc>::const_iterator it(v.begin()), it_end(v.end());
150  it != it_end; ++it) {
151  o.pack(*it);
152  }
153  return o;
154  }
155 };
156 
157 template <typename T, typename Compare, typename Alloc>
158 struct object_with_zone<std::multiset<T, Compare, Alloc> > {
159  void operator()(msgpack::object::with_zone& o, const std::multiset<T, Compare, Alloc>& v) const {
161  if (v.empty()) {
162  o.via.array.ptr = nullptr;
163  o.via.array.size = 0;
164  } else {
165  uint32_t size = checked_get_container_size(v.size());
166  msgpack::object* p = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*size));
167  msgpack::object* const pend = p + size;
168  o.via.array.ptr = p;
169  o.via.array.size = size;
170  typename std::multiset<T, Compare, Alloc>::const_iterator it(v.begin());
171  do {
172  *p = msgpack::object(*it, o.zone);
173  ++p;
174  ++it;
175  } while(p < pend);
176  }
177  }
178 };
179 
180 } // namespace adaptor
181 
183 } // MSGPACK_API_VERSION_NAMESPACE(v1)
185 
186 } // namespace msgpack
187 
188 #endif // MSGPACK_V1_TYPE_SET_HPP
uint32_t size
Definition: object_fwd.hpp:23
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition: cpp03_zone.hpp:236
uint32_t checked_get_container_size(T size)
Definition: check_container_size.hpp:55
Definition: object_fwd_decl.hpp:60
union_type via
Definition: object_fwd.hpp:92
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::set< T, Compare, Alloc > &v) const
Definition: set.hpp:69
msgpack::zone & zone
Definition: object.hpp:36
void operator()(msgpack::object::with_zone &o, const std::set< T, Compare, Alloc > &v) const
Definition: set.hpp:82
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
Definition: object.hpp:34
packer< Stream > & pack(const T &v)
Packing function template.
msgpack::object const & operator()(msgpack::object const &o, std::set< T, Compare, Alloc > &v) const
Definition: set.hpp:48
std::enable_if< msgpack::has_as< T >::value, T >::type as() const
Get value as T.
Definition: object.hpp:567
Definition: adaptor_base.hpp:43
Definition: object_fwd.hpp:222
Definition: adaptor_base.hpp:32
msgpack::object_array array
Definition: object_fwd.hpp:84
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:75
std::multiset< T, Compare, Alloc > operator()(msgpack::object const &o) const
Definition: set.hpp:108
msgpack::type::object_type type
Definition: object_fwd.hpp:91
msgpack::object const & operator()(msgpack::object const &o, std::multiset< T, Compare, Alloc > &v) const
Definition: set.hpp:125
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:58
Definition: object_fwd_decl.hpp:39
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::multiset< T, Compare, Alloc > &v) const
Definition: set.hpp:146
The class template that supports continuous packing.
Definition: adaptor_base_decl.hpp:23
std::set< T, Compare, Alloc > operator()(msgpack::object const &o) const
Definition: set.hpp:31
T & move(T &t)
void operator()(msgpack::object::with_zone &o, const std::multiset< T, Compare, Alloc > &v) const
Definition: set.hpp:159
Definition: adaptor_base.hpp:27