carray.hpp
Go to the documentation of this file.
//
// MessagePack for C++ static resolution routine
//
// Copyright (C) 2016 KONDO Takatoshi
//
// 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_CARRAY_HPP
#define MSGPACK_V1_TYPE_CARRAY_HPP
#include "msgpack/versioning.hpp"
#include "msgpack/object_fwd.hpp"
#include "msgpack/adaptor/adaptor_base.hpp"
#include "msgpack/adaptor/check_container_size.hpp"
namespace msgpack {
MSGPACK_API_VERSION_NAMESPACE(v1) {
namespace adaptor {
template <typename T, std::size_t N>
struct convert<T[N]> {
msgpack::object const& operator()(msgpack::object const& o, T* v) const {
if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
if (o.via.array.size > N) { throw msgpack::type_error(); }
msgpack::object* p = o.via.array.ptr;
msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
do {
p->convert(*v);
++p;
++v;
} while(p < pend);
return o;
}
};
template <std::size_t N>
struct convert<char[N]> {
msgpack::object const& operator()(msgpack::object const& o, char(&v)[N]) const {
switch (o.type) {
case msgpack::type::BIN:
if (o.via.bin.size > N) { throw msgpack::type_error(); }
std::memcpy(v, o.via.bin.ptr, o.via.bin.size);
break;
case msgpack::type::STR:
if (o.via.str.size > N) { throw msgpack::type_error(); }
std::memcpy(v, o.via.str.ptr, o.via.str.size);
if (o.via.str.size < N) v[o.via.str.size] = '\0';
break;
default:
throw msgpack::type_error();
break;
}
return o;
}
};
template <std::size_t N>
struct convert<unsigned char[N]> {
msgpack::object const& operator()(msgpack::object const& o, unsigned char(&v)[N]) const {
switch (o.type) {
case msgpack::type::BIN:
if (o.via.bin.size > N) { throw msgpack::type_error(); }
std::memcpy(v, o.via.bin.ptr, o.via.bin.size);
break;
case msgpack::type::STR:
if (o.via.str.size > N) { throw msgpack::type_error(); }
std::memcpy(v, o.via.str.ptr, o.via.str.size);
if (o.via.str.size < N) v[o.via.str.size] = '\0';
break;
default:
throw msgpack::type_error();
break;
}
return o;
}
};
template <typename T, std::size_t N>
struct pack<T[N]> {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const T(&v)[N]) const {
uint32_t size = checked_get_container_size(N);
o.pack_array(size);
const T* ptr = v;
for (; ptr != &v[N]; ++ptr) o.pack(*ptr);
return o;
}
};
template <std::size_t N>
struct pack<char[N]> {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const char(&v)[N]) const {
char const* p = v;
uint32_t size = checked_get_container_size(N);
char const* p2 = static_cast<char const*>(std::memchr(p, '\0', size));
uint32_t adjusted_size = p2 ? static_cast<uint32_t>(p2 - p) : size;
o.pack_str(adjusted_size);
o.pack_str_body(p, adjusted_size);
return o;
}
};
template <std::size_t N>
struct pack<const char[N]> {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const char(&v)[N]) const {
uint32_t size = checked_get_container_size(N);
char const* p = v;
char const* p2 = static_cast<char const*>(std::memchr(p, '\0', size));
uint32_t adjusted_size = p2 ? static_cast<uint32_t>(p2 - p) : size;
o.pack_str(adjusted_size);
o.pack_str_body(p, adjusted_size);
return o;
}
};
template <std::size_t N>
struct pack<unsigned char[N]> {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const unsigned char(&v)[N]) const {
unsigned char const* p = v;
uint32_t size = checked_get_container_size(N);
o.pack_bin(size);
o.pack_bin_body(reinterpret_cast<char const*>(p), size);
return o;
}
};
template <std::size_t N>
struct pack<const unsigned char[N]> {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const unsigned char(&v)[N]) const {
unsigned char const* p = v;
uint32_t size = checked_get_container_size(N);
o.pack_bin(size);
o.pack_bin_body(reinterpret_cast<char const*>(p), size);
return o;
}
};
template <typename T, std::size_t N>
struct object_with_zone<T[N]> {
void operator()(msgpack::object::with_zone& o, const T(&v)[N]) const {
uint32_t size = checked_get_container_size(N);
o.type = msgpack::type::ARRAY;
msgpack::object* ptr = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object) * size, MSGPACK_ZONE_ALIGNOF(msgpack::object)));
o.via.array.ptr = ptr;
o.via.array.size = size;
const T* pv = v;
for (; pv != &v[size]; ++pv) {
*ptr++ = msgpack::object(*pv, o.zone);
}
}
};
template <std::size_t N>
struct object_with_zone<char[N]> {
void operator()(msgpack::object::with_zone& o, const char(&v)[N]) const {
char const* p = v;
uint32_t size = checked_get_container_size(N);
char const* p2 = static_cast<char const*>(std::memchr(p, '\0', size));
uint32_t adjusted_size = p2 ? static_cast<uint32_t>(p2 - p) : size;
o.type = msgpack::type::STR;
char* ptr = static_cast<char*>(o.zone.allocate_align(adjusted_size, MSGPACK_ZONE_ALIGNOF(char)));
o.via.str.ptr = ptr;
o.via.str.size = adjusted_size;
std::memcpy(ptr, p, adjusted_size);
}
};
template <std::size_t N>
struct object_with_zone<const char[N]> {
void operator()(msgpack::object::with_zone& o, const char(&v)[N]) const {
char const* p = v;
uint32_t size = checked_get_container_size(N);
char const* p2 = static_cast<char const*>(std::memchr(p, '\0', size));
uint32_t adjusted_size = p2 ? static_cast<uint32_t>(p2 - p) : size;
o.type = msgpack::type::STR;
char* ptr = static_cast<char*>(o.zone.allocate_align(adjusted_size, MSGPACK_ZONE_ALIGNOF(char)));
o.via.str.ptr = ptr;
o.via.str.size = adjusted_size;
std::memcpy(ptr, p, adjusted_size);
}
};
template <std::size_t N>
struct object_with_zone<unsigned char[N]> {
void operator()(msgpack::object::with_zone& o, const unsigned char(&v)[N]) const {
uint32_t size = checked_get_container_size(N);
o.type = msgpack::type::BIN;
char* ptr = static_cast<char*>(o.zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
o.via.bin.ptr = ptr;
o.via.bin.size = size;
std::memcpy(ptr, v, size);
}
};
template <std::size_t N>
struct object_with_zone<const unsigned char[N]> {
void operator()(msgpack::object::with_zone& o, const unsigned char(&v)[N]) const {
uint32_t size = checked_get_container_size(N);
o.type = msgpack::type::BIN;
char* ptr = static_cast<char*>(o.zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
o.via.bin.ptr = ptr;
o.via.bin.size = size;
std::memcpy(ptr, v, size);
}
};
template <std::size_t N>
struct object<char[N]> {
void operator()(msgpack::object& o, const char(&v)[N]) const {
char const* p = v;
uint32_t size = checked_get_container_size(N);
char const* p2 = static_cast<char const*>(std::memchr(p, '\0', size));
uint32_t adjusted_size = p2 ? static_cast<uint32_t>(p2 - p) : size;
o.type = msgpack::type::STR;
o.via.str.ptr = p;
o.via.str.size = adjusted_size;
}
};
template <std::size_t N>
struct object<const char[N]> {
void operator()(msgpack::object& o, const char(&v)[N]) const {
char const* p = v;
uint32_t size = checked_get_container_size(N);
char const* p2 = static_cast<char const*>(std::memchr(p, '\0', size));
uint32_t adjusted_size = p2 ? static_cast<uint32_t>(p2 - p) : size;
o.type = msgpack::type::STR;
o.via.str.ptr = p;
o.via.str.size = adjusted_size;
}
};
} // namespace adaptor
} // MSGPACK_API_VERSION_NAMESPACE(v1)
} // namespace msgpack
#endif // MSGPACK_V1_TYPE_CARRAY_HPP
msgpack::packer::pack_bin
packer< Stream > & pack_bin(uint32_t l)
Packing bin header and length.
Definition: pack.hpp:1255
msgpack::type::STR
Definition: object_fwd_decl.hpp:39
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::adaptor::object< char[N]>::operator()
void operator()(msgpack::object &o, const char(&v)[N]) const
Definition: carray.hpp:220
msgpack::object_bin::ptr
const char * ptr
Definition: object_fwd.hpp:39
object_fwd.hpp
msgpack::adaptor::convert< T[N]>::operator()
msgpack::object const & operator()(msgpack::object const &o, T *v) const
Definition: carray.hpp:28
msgpack::checked_get_container_size
uint32_t checked_get_container_size(T size)
Definition: check_container_size.hpp:55
msgpack::object::via
union_type via
Definition: object_fwd.hpp:93
msgpack::type::BIN
Definition: object_fwd_decl.hpp:40
adaptor_base.hpp
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
versioning.hpp
msgpack::object_str::ptr
const char * ptr
Definition: object_fwd.hpp:34
msgpack::adaptor::pack< const char[N]>::operator()
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const char(&v)[N]) const
Definition: carray.hpp:114
msgpack::adaptor::convert< char[N]>::operator()
msgpack::object const & operator()(msgpack::object const &o, char(&v)[N]) const
Definition: carray.hpp:44
msgpack::packer::pack_bin_body
packer< Stream > & pack_bin_body(const char *b, uint32_t l)
Packing bin body.
Definition: pack.hpp:1274
v2::object::with_zone
Definition: object.hpp:35
msgpack::packer::pack
packer< Stream > & pack(const T &v)
Packing function template.
msgpack::packer::pack_str_body
packer< Stream > & pack_str_body(const char *b, uint32_t l)
Packing str body.
Definition: pack.hpp:1220
msgpack::adaptor::object_with_zone< T[N]>::operator()
void operator()(msgpack::object::with_zone &o, const T(&v)[N]) const
Definition: carray.hpp:151
msgpack::adaptor::pack< char[N]>::operator()
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const char(&v)[N]) const
Definition: carray.hpp:100
msgpack::adaptor::pack< unsigned char[N]>::operator()
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const unsigned char(&v)[N]) const
Definition: carray.hpp:128
msgpack::adaptor::object_with_zone
Definition: adaptor_base.hpp:43
msgpack::type_error
Definition: object_fwd.hpp:236
msgpack::adaptor::pack
Definition: adaptor_base.hpp:32
msgpack::type::size
std::size_t size(T const &t)
Definition: size_equal_only.hpp:24
msgpack::object::union_type::array
msgpack::object_array array
Definition: object_fwd.hpp:85
msgpack::object::union_type::str
msgpack::object_str str
Definition: object_fwd.hpp:87
msgpack::object
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:75
msgpack::adaptor::object_with_zone< const char[N]>::operator()
void operator()(msgpack::object::with_zone &o, const char(&v)[N]) const
Definition: carray.hpp:181
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::adaptor::pack< const unsigned char[N]>::operator()
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const unsigned char(&v)[N]) const
Definition: carray.hpp:140
msgpack::adaptor::object_with_zone< char[N]>::operator()
void operator()(msgpack::object::with_zone &o, const char(&v)[N]) const
Definition: carray.hpp:166
msgpack::object_str::size
uint32_t size
Definition: object_fwd.hpp:33
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:1071
msgpack::adaptor::pack< T[N]>::operator()
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const T(&v)[N]) const
Definition: carray.hpp:88
msgpack::adaptor::object
Definition: adaptor_base.hpp:38
msgpack::packer
The class template that supports continuous packing.
Definition: adaptor_base_decl.hpp:24
msgpack::adaptor::convert< unsigned char[N]>::operator()
msgpack::object const & operator()(msgpack::object const &o, unsigned char(&v)[N]) const
Definition: carray.hpp:65
v2::object::with_zone::zone
msgpack::zone & zone
Definition: object.hpp:37
msgpack::adaptor::object_with_zone< const unsigned char[N]>::operator()
void operator()(msgpack::object::with_zone &o, const unsigned char(&v)[N]) const
Definition: carray.hpp:208
msgpack::adaptor::convert
Definition: adaptor_base.hpp:27
msgpack::packer::pack_str
packer< Stream > & pack_str(uint32_t l)
Packing str header and length.
Definition: pack.hpp:1197
msgpack::adaptor::object_with_zone< unsigned char[N]>::operator()
void operator()(msgpack::object::with_zone &o, const unsigned char(&v)[N]) const
Definition: carray.hpp:196
msgpack::adaptor::object< const char[N]>::operator()
void operator()(msgpack::object &o, const char(&v)[N]) const
Definition: carray.hpp:233
msgpack::object::union_type::bin
msgpack::object_bin bin
Definition: object_fwd.hpp:88