MessagePack for C++
Loading...
Searching...
No Matches
array_ref.hpp
Go to the documentation of this file.
1//
2// MessagePack for C++ static resolution routine
3//
4// Copyright (C) 2008-2016 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_ARRAY_REF_HPP
11#define MSGPACK_V1_TYPE_ARRAY_REF_HPP
12
15#include <cstring>
16#include <string>
17
18namespace msgpack {
19
23
24namespace type {
25
26template <typename T>
27struct array_ref {
29 array_ref(T& t) : data(&t) {}
30
31 T* data;
32
33 std::size_t size() const {
34 return data->size();
35 }
36
37 template <typename U>
38 bool operator==(array_ref<U> const& t) const {
39 return *data == *t.data;
40 }
41 template <typename U>
42 bool operator!=(array_ref<U> const& t) const {
43 return !(*data == *t.data);
44 }
45 template <typename U>
46 bool operator< (array_ref<U> const& t) const
47 {
48 return *data < *t.data;
49 }
50 template <typename U>
51 bool operator> (array_ref<U> const& t) const
52 {
53 return *t.data < *data;
54 }
55 template <typename U>
56 bool operator<= (array_ref<U> const& t) const
57 {
58 return !(*t.data < *data);
59 }
60 template <typename U>
61 bool operator>= (array_ref<U> const& t) const
62 {
63 return !(*data < *t.data);
64 }
65};
66
67template <typename T, std::size_t N>
68struct array_ref<T[N]> {
70 array_ref(T(&t)[N]) : data(t) {}
71
72 T* data;
73
74 std::size_t size() const {
75 return N;
76 }
77
78 template <typename U>
79 bool operator==(array_ref<U> const& t) const {
80 if (N != t.size()) return false;
81 for (std::size_t i = 0; i < N; ++i) {
82 if (!(data[i] == t.data[i])) return false;
83 }
84 return true;
85 }
86 template <typename U>
87 bool operator!=(array_ref<U> const& t) const {
88 return !(*this == t);
89 }
90 template <typename U>
91 bool operator< (array_ref<U> const& t) const
92 {
93 std::size_t n = (N < t.size()) ? N : t.size();
94 for (std::size_t i = 0; i < n; ++i) {
95 if (data[i] < t.data[i]) return true;
96 if (t.data[i] < data[i]) return false;
97 }
98 return N < t.size();
99 }
100 template <typename U>
101 bool operator> (array_ref<U> const& t) const
102 {
103 std::size_t n = (N < t.size()) ? N : t.size();
104 for (std::size_t i = 0; i < n; ++i) {
105 if (t.data[i] < data[i]) return true;
106 if (data[i] < t.data[i]) return false;
107 }
108 return t.size() < N;
109 }
110 template <typename U>
111 bool operator<= (array_ref<U> const& t) const
112 {
113 return !(*this > t);
114 }
115 template <typename U>
116 bool operator>= (array_ref<U> const& t) const
117 {
118 return !(*this < t);
119 }
120};
121
122template <typename T>
123inline
124typename msgpack::enable_if<
125 !msgpack::is_array<T const>::value,
126 array_ref<T const>
127>::type
128make_array_ref(const T& t) {
129 return array_ref<T const>(t);
130}
131
132template <typename T>
133inline
134typename msgpack::enable_if<
135 !msgpack::is_array<T>::value,
136 array_ref<T>
137>::type
139 return array_ref<T>(t);
140}
141
142template <typename T, std::size_t N>
143inline array_ref<const T[N]> make_array_ref(const T(&t)[N]) {
144 return array_ref<const T[N]>(t);
145}
146
147template <typename T, std::size_t N>
149 return array_ref<T[N]>(t);
150}
151
152} // namespace type
153
154namespace adaptor {
155
156template <typename T>
159 if (!v.data) { throw msgpack::type_error(); }
160 if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
161 if (v.size() < o.via.bin.size) { throw msgpack::type_error(); }
162 if (o.via.array.size > 0) {
164 msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
165 typename T::iterator it = v.data->begin();
166 do {
167 p->convert(*it);
168 ++p;
169 ++it;
170 } while(p < pend);
171 }
172 return o;
173 }
174};
175
176template <typename T, std::size_t N>
179 if (!v.data) { throw msgpack::type_error(); }
180 if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
181 if (v.size() < o.via.bin.size) { throw msgpack::type_error(); }
182 if (o.via.array.size > 0) {
184 msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
185 T* it = v.data;
186 do {
187 p->convert(*it);
188 ++p;
189 ++it;
190 } while(p < pend);
191 }
192 return o;
193 }
194};
195
196template <typename T>
197struct convert<msgpack::type::array_ref<std::vector<T> > > {
198 msgpack::object const& operator()(msgpack::object const& o, msgpack::type::array_ref<std::vector<T> >& v) const {
199 if (!v.data) { throw msgpack::type_error(); }
200 if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
201 v.data->resize(o.via.bin.size);
202 if (o.via.array.size > 0) {
204 msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
205 typename std::vector<T>::iterator it = v.data->begin();
206 do {
207 p->convert(*it);
208 ++p;
209 ++it;
210 } while(p < pend);
211 }
212 return o;
213 }
214};
215
216template <typename T>
218 template <typename Stream>
220 if (!v.data) { throw msgpack::type_error(); }
221 uint32_t size = checked_get_container_size(v.size());
222 o.pack_array(size);
223 for (typename T::const_iterator it(v.data->begin()), it_end(v.data->end());
224 it != it_end; ++it) {
225 o.pack(*it);
226 }
227 return o;
228 }
229};
230
231template <typename T, std::size_t N>
232struct pack<msgpack::type::array_ref<T[N]> > {
233 template <typename Stream>
235 if (!v.data) { throw msgpack::type_error(); }
236 uint32_t size = checked_get_container_size(v.size());
237 o.pack_array(size);
238 for (T const* it = v.data;
239 it != &v.data[v.size()]; ++it) {
240 o.pack(*it);
241 }
242 return o;
243 }
244};
245
246template <typename T>
249 if (!v.data) { throw msgpack::type_error(); }
251 if (v.data->empty()) {
253 o.via.array.size = 0;
254 }
255 else {
256 uint32_t size = checked_get_container_size(v.size());
258 msgpack::object* const pend = p + size;
259 o.via.array.ptr = p;
260 o.via.array.size = size;
261 typename T::const_iterator it(v.data->begin());
262 do {
263#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
264#pragma GCC diagnostic push
265#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
266#endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
267
268 *p = msgpack::object(*it, o.zone);
269#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
270#pragma GCC diagnostic pop
271#endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
272 ++p;
273 ++it;
274 } while(p < pend);
275 }
276 }
277};
278
279template <typename T, std::size_t N>
282 if (!v.data) { throw msgpack::type_error(); }
284 uint32_t size = checked_get_container_size(v.size());
286 msgpack::object* const pend = p + size;
287 o.via.array.ptr = p;
288 o.via.array.size = size;
289 T const* it = v.data;
290 do {
291 *p = msgpack::object(*it, o.zone);
292 ++p;
293 ++it;
294 } while(p < pend);
295 }
296};
297
298
299} // namespace adaptor
300
302} // MSGPACK_API_VERSION_NAMESPACE(v1)
304
305} // namespace msgpack
306
307#endif // MSGPACK_V1_TYPE_ARRAY_REF_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:234
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition cpp03_zone.hpp:255
Definition array_ref.hpp:24
msgpack::enable_if<!msgpack::is_array< Tconst >::value, array_ref< Tconst > >::type make_array_ref(const T &t)
Definition array_ref.hpp:128
@ 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
msgpack::object const & operator()(msgpack::object const &o, msgpack::type::array_ref< T > &v) const
Definition array_ref.hpp:158
msgpack::object const & operator()(msgpack::object const &o, msgpack::type::array_ref< T[N]> &v) const
Definition array_ref.hpp:178
msgpack::object const & operator()(msgpack::object const &o, msgpack::type::array_ref< std::vector< T > > &v) const
Definition array_ref.hpp:198
Definition adaptor_base.hpp:27
void operator()(msgpack::object::with_zone &o, const msgpack::type::array_ref< T > &v) const
Definition array_ref.hpp:248
void operator()(msgpack::object::with_zone &o, const msgpack::type::array_ref< T[N]> &v) const
Definition array_ref.hpp:281
Definition adaptor_base.hpp:43
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const msgpack::type::array_ref< T > &v) const
Definition array_ref.hpp:219
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const msgpack::type::array_ref< T[N]> &v) const
Definition array_ref.hpp:234
Definition adaptor_base.hpp:32
uint32_t size
Definition object_fwd.hpp:23
msgpack::object * ptr
Definition object_fwd.hpp:24
uint32_t size
Definition object_fwd.hpp:38
msgpack::zone & zone
Definition object.hpp:37
Object class that corresponding to MessagePack format object.
Definition object_fwd.hpp:77
msgpack::enable_if<!msgpack::is_array< T >::value &&!msgpack::is_pointer< T >::value, T & >::type convert(T &v) const
Convert the object.
Definition object.hpp:1076
union_type via
Definition object_fwd.hpp:95
msgpack::type::object_type type
Definition object_fwd.hpp:94
object_with_zone_type with_zone
Definition object_fwd.hpp:225
bool operator==(array_ref< U > const &t) const
Definition array_ref.hpp:79
array_ref(T(&t)[N])
Definition array_ref.hpp:70
T * data
Definition array_ref.hpp:72
bool operator!=(array_ref< U > const &t) const
Definition array_ref.hpp:87
array_ref()
Definition array_ref.hpp:69
std::size_t size() const
Definition array_ref.hpp:74
Definition array_ref.hpp:27
std::size_t size() const
Definition array_ref.hpp:33
bool operator<(array_ref< U > const &t) const
Definition array_ref.hpp:46
array_ref()
Definition array_ref.hpp:28
T * data
Definition array_ref.hpp:31
bool operator>(array_ref< U > const &t) const
Definition array_ref.hpp:51
array_ref(T &t)
Definition array_ref.hpp:29
bool operator!=(array_ref< U > const &t) const
Definition array_ref.hpp:42
bool operator==(array_ref< U > const &t) const
Definition array_ref.hpp:38
bool operator>=(array_ref< U > const &t) const
Definition array_ref.hpp:61
bool operator<=(array_ref< U > const &t) const
Definition array_ref.hpp:56
msgpack::object_array array
Definition object_fwd.hpp:87
msgpack::object_bin bin
Definition object_fwd.hpp:90
#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