array_ref.hpp
Go to the documentation of this file.
//
// 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/v1/adaptor/array_ref.hpp"
#include "msgpack/adaptor/check_container_size.hpp"
#include "msgpack/cpp_config.hpp"
#include <cstring>
#include <string>
namespace msgpack {
MSGPACK_API_VERSION_NAMESPACE(v1) {
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 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
*p = msgpack::object(*it, o.zone);
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif // (__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
} // MSGPACK_API_VERSION_NAMESPACE(v1)
} // namespace msgpack
#endif // MSGPACK_V1_TYPE_ARRAY_REF_HPP
msgpack::type::array_ref< T[N]>::data
T * data
Definition: array_ref.hpp:73
msgpack::is_array
Definition: cpp_config_decl.hpp:71
msgpack::object_array::size
uint32_t size
Definition: object_fwd.hpp:23
msgpack::zone::allocate_align
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition: cpp03_zone.hpp:246
msgpack::type::array_ref< T[N]>::size
std::size_t size() const
Definition: array_ref.hpp:75
msgpack::type::array_ref< T[N]>::operator==
bool operator==(array_ref< U > const &t) const
Definition: array_ref.hpp:80
msgpack::checked_get_container_size
uint32_t checked_get_container_size(T size)
Definition: check_container_size.hpp:55
msgpack::adaptor::pack< msgpack::type::array_ref< T > >::operator()
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const msgpack::type::array_ref< T > &v) const
Definition: array_ref.hpp:218
msgpack::object::via
union_type via
Definition: object_fwd.hpp:93
msgpack::type::array_ref::data
T * data
Definition: array_ref.hpp:32
msgpack::adaptor::convert< msgpack::type::array_ref< std::vector< T > > >::operator()
msgpack::object const & operator()(msgpack::object const &o, msgpack::type::array_ref< std::vector< T > > &v) const
Definition: array_ref.hpp:197
msgpack::object::with_zone::zone
msgpack::zone & zone
Definition: object.hpp:36
msgpack::object_array::ptr
msgpack::object * ptr
Definition: object_fwd.hpp:24
msgpack::packer::pack_array
packer< Stream > & pack_array(uint32_t n)
Packing array header and size.
Definition: pack.hpp:1160
msgpack::object_bin::size
uint32_t size
Definition: object_fwd.hpp:38
msgpack
Definition: adaptor_base.hpp:15
msgpack::adaptor::pack< msgpack::type::array_ref< T[N]> >::operator()
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const msgpack::type::array_ref< T[N]> &v) const
Definition: array_ref.hpp:233
msgpack::type::array_ref::operator!=
bool operator!=(array_ref< U > const &t) const
Definition: array_ref.hpp:43
msgpack::object::with_zone
Definition: object.hpp:34
msgpack::packer::pack
packer< Stream > & pack(const T &v)
Packing function template.
msgpack::type::array_ref::operator==
bool operator==(array_ref< U > const &t) const
Definition: array_ref.hpp:39
cpp_config.hpp
msgpack::type::array_ref
Definition: array_ref.hpp:28
msgpack::adaptor::object_with_zone< msgpack::type::array_ref< T[N]> >::operator()
void operator()(msgpack::object::with_zone &o, const msgpack::type::array_ref< T[N]> &v) const
Definition: array_ref.hpp:279
msgpack::type::array_ref< T[N]>::array_ref
array_ref(T(&t)[N])
Definition: array_ref.hpp:71
msgpack::adaptor::object_with_zone
Definition: adaptor_base.hpp:43
msgpack::type_error
Definition: object_fwd.hpp:236
msgpack::type::array_ref::array_ref
array_ref()
Definition: array_ref.hpp:29
msgpack::type::array_ref< T[N]>::array_ref
array_ref()
Definition: array_ref.hpp:70
msgpack::adaptor::pack
Definition: adaptor_base.hpp:32
msgpack::object::union_type::array
msgpack::object_array array
Definition: object_fwd.hpp:85
msgpack::type::array_ref< T[N]>
Definition: array_ref.hpp:69
msgpack::enable_if
Definition: cpp_config_decl.hpp:56
msgpack::object
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:75
msgpack::adaptor::convert< msgpack::type::array_ref< T > >::operator()
msgpack::object const & operator()(msgpack::object const &o, msgpack::type::array_ref< T > &v) const
Definition: array_ref.hpp:157
msgpack::adaptor::object_with_zone< msgpack::type::array_ref< T > >::operator()
void operator()(msgpack::object::with_zone &o, const msgpack::type::array_ref< T > &v) const
Definition: array_ref.hpp:247
msgpack::object::type
msgpack::type::object_type type
Definition: object_fwd.hpp:92
MSGPACK_API_VERSION_NAMESPACE
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:58
msgpack::type::ARRAY
Definition: object_fwd_decl.hpp:41
msgpack::type::array_ref::size
std::size_t size() const
Definition: array_ref.hpp:34
msgpack::type::make_array_ref
msgpack::enable_if< !msgpack::is_array< T const >::value, array_ref< T const >>::type make_array_ref(const T &t)
Definition: array_ref.hpp:127
MSGPACK_ZONE_ALIGNOF
#define MSGPACK_ZONE_ALIGNOF(type)
Definition: cpp03_zone_decl.hpp:30
check_container_size.hpp
msgpack::object::convert
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:543
msgpack::packer
The class template that supports continuous packing.
Definition: adaptor_base_decl.hpp:24
msgpack::type::array_ref::operator>
bool operator>(array_ref< U > const &t) const
Definition: array_ref.hpp:52
MSGPACK_NULLPTR
#define MSGPACK_NULLPTR
Definition: cpp_config_decl.hpp:35
msgpack::type::array_ref< T[N]>::operator!=
bool operator!=(array_ref< U > const &t) const
Definition: array_ref.hpp:90
msgpack::type::array_ref::array_ref
array_ref(T &t)
Definition: array_ref.hpp:30
msgpack::adaptor::convert< msgpack::type::array_ref< T[N]> >::operator()
msgpack::object const & operator()(msgpack::object const &o, msgpack::type::array_ref< T[N]> &v) const
Definition: array_ref.hpp:177
msgpack::adaptor::convert
Definition: adaptor_base.hpp:27
msgpack::type::array_ref::operator>=
bool operator>=(array_ref< U > const &t) const
Definition: array_ref.hpp:62
msgpack::object::union_type::bin
msgpack::object_bin bin
Definition: object_fwd.hpp:88
array_ref.hpp