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