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"
14 #include "msgpack/cpp_version.hpp"
17 
18 #include <set>
19 
20 namespace msgpack {
21 
25 
26 namespace adaptor {
27 
28 #if !defined(MSGPACK_USE_CPP03)
29 
30 template <typename T, typename Compare, typename Alloc>
31 struct as<std::set<T, Compare, Alloc>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
32  std::set<T, Compare, Alloc> operator()(msgpack::object const& o) const {
33  if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
35  msgpack::object* const pbegin = o.via.array.ptr;
36  std::set<T, Compare, Alloc> v;
37  while (p > pbegin) {
38  --p;
39  v.insert(p->as<T>());
40  }
41  return v;
42  }
43 };
44 
45 #endif // !defined(MSGPACK_USE_CPP03)
46 
47 template <typename T, typename Compare, typename Alloc>
48 struct convert<std::set<T, Compare, Alloc> > {
49  msgpack::object const& operator()(msgpack::object const& o, std::set<T, Compare, Alloc>& v) const {
50  if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
52  msgpack::object* const pbegin = o.via.array.ptr;
53  std::set<T, Compare, Alloc> tmp;
54  while (p > pbegin) {
55  --p;
56  tmp.insert(p->as<T>());
57  }
58 #if MSGPACK_CPP_VERSION >= 201103L
59  v = std::move(tmp);
60 #else
61  tmp.swap(v);
62 #endif
63  return o;
64  }
65 };
66 
67 template <typename T, typename Compare, typename Alloc>
68 struct pack<std::set<T, Compare, Alloc> > {
69  template <typename Stream>
70  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::set<T, Compare, Alloc>& v) const {
71  uint32_t size = checked_get_container_size(v.size());
72  o.pack_array(size);
73  for (typename std::set<T, Compare, Alloc>::const_iterator it(v.begin()), it_end(v.end());
74  it != it_end; ++it) {
75  o.pack(*it);
76  }
77  return o;
78  }
79 };
80 
81 template <typename T, typename Compare, typename Alloc>
82 struct object_with_zone<std::set<T, Compare, Alloc> > {
83  void operator()(msgpack::object::with_zone& o, const std::set<T, Compare, Alloc>& v) const {
85  if (v.empty()) {
87  o.via.array.size = 0;
88  }
89  else {
90  uint32_t size = checked_get_container_size(v.size());
92  msgpack::object* const pend = p + size;
93  o.via.array.ptr = p;
94  o.via.array.size = size;
95  typename std::set<T, Compare, Alloc>::const_iterator it(v.begin());
96  do {
97  *p = msgpack::object(*it, o.zone);
98  ++p;
99  ++it;
100  } while(p < pend);
101  }
102  }
103 };
104 
105 #if !defined(MSGPACK_USE_CPP03)
106 
107 template <typename T, typename Compare, typename Alloc>
108 struct as<std::multiset<T, Compare, Alloc>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
109  std::multiset<T, Compare, Alloc> operator()(msgpack::object const& o) const {
110  if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
112  msgpack::object* const pbegin = o.via.array.ptr;
113  std::multiset<T, Compare, Alloc> v;
114  while (p > pbegin) {
115  --p;
116  v.insert(p->as<T>());
117  }
118  return v;
119  }
120 };
121 
122 #endif // !defined(MSGPACK_USE_CPP03)
123 
124 template <typename T, typename Compare, typename Alloc>
125 struct convert<std::multiset<T, Compare, Alloc> > {
126  msgpack::object const& operator()(msgpack::object const& o, std::multiset<T, Compare, Alloc>& v) const {
127  if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
129  msgpack::object* const pbegin = o.via.array.ptr;
130  std::multiset<T, Compare, Alloc> tmp;
131  while (p > pbegin) {
132  --p;
133  tmp.insert(p->as<T>());
134  }
135 #if MSGPACK_CPP_VERSION >= 201103L
136  v = std::move(tmp);
137 #else
138  tmp.swap(v);
139 #endif
140  return o;
141  }
142 };
143 
144 template <typename T, typename Compare, typename Alloc>
145 struct pack<std::multiset<T, Compare, Alloc> > {
146  template <typename Stream>
147  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::multiset<T, Compare, Alloc>& v) const {
148  uint32_t size = checked_get_container_size(v.size());
149  o.pack_array(size);
150  for (typename std::multiset<T, Compare, Alloc>::const_iterator it(v.begin()), it_end(v.end());
151  it != it_end; ++it) {
152  o.pack(*it);
153  }
154  return o;
155  }
156 };
157 
158 template <typename T, typename Compare, typename Alloc>
159 struct object_with_zone<std::multiset<T, Compare, Alloc> > {
160  void operator()(msgpack::object::with_zone& o, const std::multiset<T, Compare, Alloc>& v) const {
162  if (v.empty()) {
164  o.via.array.size = 0;
165  } else {
166  uint32_t size = checked_get_container_size(v.size());
168  msgpack::object* const pend = p + size;
169  o.via.array.ptr = p;
170  o.via.array.size = size;
171  typename std::multiset<T, Compare, Alloc>::const_iterator it(v.begin());
172  do {
173  *p = msgpack::object(*it, o.zone);
174  ++p;
175  ++it;
176  } while(p < pend);
177  }
178  }
179 };
180 
181 } // namespace adaptor
182 
184 } // MSGPACK_API_VERSION_NAMESPACE(v1)
186 
187 } // namespace msgpack
188 
189 #endif // MSGPACK_V1_TYPE_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::multiset< T, Compare, Alloc > operator()(msgpack::object const &o) const
Definition: set.hpp:109
std::set< T, Compare, Alloc > operator()(msgpack::object const &o) const
Definition: set.hpp:32
Definition: object_fwd_decl.hpp:61
msgpack::object const & operator()(msgpack::object const &o, std::multiset< T, Compare, Alloc > &v) const
Definition: set.hpp:126
msgpack::object const & operator()(msgpack::object const &o, std::set< T, Compare, Alloc > &v) const
Definition: set.hpp:49
Definition: adaptor_base.hpp:27
void operator()(msgpack::object::with_zone &o, const std::multiset< T, Compare, Alloc > &v) const
Definition: set.hpp:160
void operator()(msgpack::object::with_zone &o, const std::set< T, Compare, Alloc > &v) const
Definition: set.hpp:83
Definition: adaptor_base.hpp:43
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::multiset< T, Compare, Alloc > &v) const
Definition: set.hpp:147
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::set< T, Compare, Alloc > &v) const
Definition: set.hpp:70
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