array_unsigned_char.hpp
Go to the documentation of this file.
//
// MessagePack for C++ static resolution routine
//
// Copyright (C) 2014-2015 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_CPP11_ARRAY_UNSIGNED_CHAR_HPP
#define MSGPACK_V1_TYPE_CPP11_ARRAY_UNSIGNED_CHAR_HPP
#include "msgpack/versioning.hpp"
#include "msgpack/adaptor/adaptor_base.hpp"
#include "msgpack/adaptor/check_container_size.hpp"
#include <array>
#include <cstring>
namespace msgpack {
MSGPACK_API_VERSION_NAMESPACE(v1) {
namespace adaptor {
template <std::size_t N>
struct convert<std::array<unsigned char, N>> {
msgpack::object const& operator()(msgpack::object const& o, std::array<unsigned char, N>& v) const {
switch (o.type) {
case msgpack::type::BIN:
if(o.via.bin.size > N) { throw msgpack::type_error(); }
std::memcpy(v.data(), 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.data(), o.via.str.ptr, N);
break;
default:
throw msgpack::type_error();
break;
}
return o;
}
};
template <>
struct convert<std::array<unsigned char, 0>> {
msgpack::object const& operator()(msgpack::object const& o, std::array<unsigned char, 0>&) const {
return o;
}
};
template <std::size_t N>
struct pack<std::array<unsigned char, N>> {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::array<unsigned char, N>& v) const {
uint32_t size = checked_get_container_size(v.size());
o.pack_bin(size);
o.pack_bin_body(reinterpret_cast<char const*>(v.data()), size);
return o;
}
};
template <std::size_t N>
struct object<std::array<unsigned char, N>> {
void operator()(msgpack::object& o, const std::array<unsigned char, N>& v) const {
uint32_t size = checked_get_container_size(v.size());
o.type = msgpack::type::BIN;
o.via.bin.ptr = reinterpret_cast<char const*>(v.data());
o.via.bin.size = size;
}
};
template <std::size_t N>
struct object_with_zone<std::array<unsigned char, N>> {
void operator()(msgpack::object::with_zone& o, const std::array<unsigned char, N>& v) const {
uint32_t size = checked_get_container_size(v.size());
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.data(), size);
}
};
} // namespace adaptor
} // MSGPACK_API_VERSION_NAMESPACE(v1)
} // namespace msgpack
#endif // MSGPACK_V1_TYPE_CPP11_ARRAY_UNSIGNED_CHAR_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::zone::allocate_align
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition: cpp03_zone.hpp:246
msgpack::object_bin::ptr
const char * ptr
Definition: object_fwd.hpp:39
msgpack::adaptor::pack< std::array< unsigned char, N > >::operator()
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::array< unsigned char, N > &v) const
Definition: array_unsigned_char.hpp:58
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_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::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::adaptor::object< std::array< unsigned char, N > >::operator()
void operator()(msgpack::object &o, const std::array< unsigned char, N > &v) const
Definition: array_unsigned_char.hpp:69
msgpack::adaptor::object_with_zone
Definition: adaptor_base.hpp:43
msgpack::adaptor::object_with_zone< std::array< unsigned char, N > >::operator()
void operator()(msgpack::object::with_zone &o, const std::array< unsigned char, N > &v) const
Definition: array_unsigned_char.hpp:79
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::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::convert< std::array< unsigned char, 0 > >::operator()
msgpack::object const & operator()(msgpack::object const &o, std::array< unsigned char, 0 > &) const
Definition: array_unsigned_char.hpp:50
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::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::adaptor::object
Definition: adaptor_base.hpp:38
msgpack::packer
The class template that supports continuous packing.
Definition: adaptor_base_decl.hpp:24
v2::object::with_zone::zone
msgpack::zone & zone
Definition: object.hpp:37
msgpack::adaptor::convert
Definition: adaptor_base.hpp:27
msgpack::object::union_type::bin
msgpack::object_bin bin
Definition: object_fwd.hpp:88
msgpack::adaptor::convert< std::array< unsigned char, N > >::operator()
msgpack::object const & operator()(msgpack::object const &o, std::array< unsigned char, N > &v) const
Definition: array_unsigned_char.hpp:30