MessagePack for C++
Loading...
Searching...
No Matches
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
16
17#include <set>
18
19namespace msgpack {
20
24
25namespace adaptor {
26
27#if !defined(MSGPACK_USE_CPP03)
28
29template <typename T, typename Compare, typename Alloc>
30struct 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
46template <typename T, typename Compare, typename Alloc>
47struct 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
66template <typename T, typename Compare, typename Alloc>
67struct 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
80template <typename T, typename Compare, typename Alloc>
81struct object_with_zone<std::set<T, Compare, Alloc> > {
82 void operator()(msgpack::object::with_zone& o, const std::set<T, Compare, Alloc>& v) const {
83 o.type = msgpack::type::ARRAY;
84 if (v.empty()) {
86 o.via.array.size = 0;
87 }
88 else {
89 uint32_t size = checked_get_container_size(v.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
106template <typename T, typename Compare, typename Alloc>
107struct 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
123template <typename T, typename Compare, typename Alloc>
124struct 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
143template <typename T, typename Compare, typename Alloc>
144struct 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
157template <typename T, typename Compare, typename Alloc>
158struct object_with_zone<std::multiset<T, Compare, Alloc> > {
159 void operator()(msgpack::object::with_zone& o, const std::multiset<T, Compare, Alloc>& v) const {
160 o.type = msgpack::type::ARRAY;
161 if (v.empty()) {
163 o.via.array.size = 0;
164 } else {
165 uint32_t size = checked_get_container_size(v.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
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:236
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition cpp03_zone.hpp:246
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:108
std::set< T, Compare, Alloc > operator()(msgpack::object const &o) const
Definition set.hpp:31
Definition object_fwd_decl.hpp:62
msgpack::object const & operator()(msgpack::object const &o, std::multiset< T, Compare, Alloc > &v) const
Definition set.hpp:125
msgpack::object const & operator()(msgpack::object const &o, std::set< T, Compare, Alloc > &v) const
Definition set.hpp:48
Definition adaptor_base.hpp:27
void operator()(msgpack::object::with_zone &o, const std::multiset< T, Compare, Alloc > &v) const
Definition set.hpp:159
void operator()(msgpack::object::with_zone &o, const std::set< T, Compare, Alloc > &v) const
Definition set.hpp:82
Definition adaptor_base.hpp:43
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::multiset< T, Compare, Alloc > &v) const
Definition set.hpp:146
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::set< T, Compare, Alloc > &v) const
Definition set.hpp:69
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:35
#define MSGPACK_ZONE_ALIGNOF(type)
Definition cpp03_zone_decl.hpp:30
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition versioning.hpp:58