|
| 1 | +// |
| 2 | +// MessagePack for C++ static resolution routine |
| 3 | +// |
| 4 | +// Copyright (C) 2018 KONDO Takatoshi |
| 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_CARRAY_BYTE_HPP |
| 11 | +#define MSGPACK_V1_TYPE_CARRAY_BYTE_HPP |
| 12 | + |
| 13 | +#if __cplusplus >= 201703 |
| 14 | + |
| 15 | +#include "msgpack/versioning.hpp" |
| 16 | +#include "msgpack/adaptor/adaptor_base.hpp" |
| 17 | +#include "msgpack/adaptor/check_container_size.hpp" |
| 18 | + |
| 19 | +#include <cstring> |
| 20 | +#include <cstddef> |
| 21 | + |
| 22 | +namespace msgpack { |
| 23 | + |
| 24 | +/// @cond |
| 25 | +MSGPACK_API_VERSION_NAMESPACE(v1) { |
| 26 | +/// @endcond |
| 27 | + |
| 28 | +namespace adaptor { |
| 29 | + |
| 30 | +template <std::size_t N> |
| 31 | +struct convert<std::byte[N]> { |
| 32 | + msgpack::object const& operator()(msgpack::object const& o, std::byte(&v)[N]) const { |
| 33 | + switch (o.type) { |
| 34 | + case msgpack::type::BIN: |
| 35 | + if (o.via.bin.size > N) { throw msgpack::type_error(); } |
| 36 | + std::memcpy(v, o.via.bin.ptr, o.via.bin.size); |
| 37 | + break; |
| 38 | + case msgpack::type::STR: |
| 39 | + if (o.via.str.size > N) { throw msgpack::type_error(); } |
| 40 | + std::memcpy(v, o.via.str.ptr, o.via.str.size); |
| 41 | + if (o.via.str.size < N) v[o.via.str.size] = std::byte{'\0'}; |
| 42 | + break; |
| 43 | + default: |
| 44 | + throw msgpack::type_error(); |
| 45 | + break; |
| 46 | + } |
| 47 | + return o; |
| 48 | + } |
| 49 | +}; |
| 50 | + |
| 51 | +template <std::size_t N> |
| 52 | +struct pack<std::byte[N]> { |
| 53 | + template <typename Stream> |
| 54 | + msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::byte(&v)[N]) const { |
| 55 | + std::byte const* p = v; |
| 56 | + uint32_t size = checked_get_container_size(N); |
| 57 | + o.pack_bin(size); |
| 58 | + o.pack_bin_body(reinterpret_cast<char const*>(p), size); |
| 59 | + return o; |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +template <std::size_t N> |
| 64 | +struct pack<const std::byte[N]> { |
| 65 | + template <typename Stream> |
| 66 | + msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::byte(&v)[N]) const { |
| 67 | + std::byte const* p = v; |
| 68 | + uint32_t size = checked_get_container_size(N); |
| 69 | + o.pack_bin(size); |
| 70 | + o.pack_bin_body(reinterpret_cast<char const*>(p), size); |
| 71 | + return o; |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +template <std::size_t N> |
| 76 | +struct object_with_zone<std::byte[N]> { |
| 77 | + void operator()(msgpack::object::with_zone& o, const std::byte(&v)[N]) const { |
| 78 | + uint32_t size = checked_get_container_size(N); |
| 79 | + o.type = msgpack::type::BIN; |
| 80 | + char* ptr = static_cast<char*>(o.zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char))); |
| 81 | + o.via.bin.ptr = ptr; |
| 82 | + o.via.bin.size = size; |
| 83 | + std::memcpy(ptr, v, size); |
| 84 | + } |
| 85 | +}; |
| 86 | + |
| 87 | +template <std::size_t N> |
| 88 | +struct object_with_zone<const std::byte[N]> { |
| 89 | + void operator()(msgpack::object::with_zone& o, const std::byte(&v)[N]) const { |
| 90 | + uint32_t size = checked_get_container_size(N); |
| 91 | + o.type = msgpack::type::BIN; |
| 92 | + char* ptr = static_cast<char*>(o.zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char))); |
| 93 | + o.via.bin.ptr = ptr; |
| 94 | + o.via.bin.size = size; |
| 95 | + std::memcpy(ptr, v, size); |
| 96 | + } |
| 97 | +}; |
| 98 | + |
| 99 | +} // namespace adaptor |
| 100 | + |
| 101 | +/// @cond |
| 102 | +} // MSGPACK_API_VERSION_NAMESPACE(v1) |
| 103 | +/// @endcond |
| 104 | + |
| 105 | +} // namespace msgpack |
| 106 | + |
| 107 | +#endif // __cplusplus >= 201703 |
| 108 | + |
| 109 | +#endif // MSGPACK_V1_TYPE_CARRAY_BYTE_HPP |
0 commit comments