|
| 1 | +// |
| 2 | +// MessagePack for C++ static resolution routine |
| 3 | +// |
| 4 | +// Copyright (C) 2021 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_ARRAY_BYTE_HPP |
| 11 | +#define MSGPACK_V1_TYPE_ARRAY_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 <array> |
| 20 | +#include <cstring> |
| 21 | +#include <cstddef> |
| 22 | + |
| 23 | +namespace msgpack { |
| 24 | + |
| 25 | +/// @cond |
| 26 | +MSGPACK_API_VERSION_NAMESPACE(v1) { |
| 27 | +/// @endcond |
| 28 | + |
| 29 | +namespace adaptor { |
| 30 | + |
| 31 | +template <std::size_t N> |
| 32 | +struct convert<std::array<std::byte, N> > { |
| 33 | + msgpack::object const& operator()(msgpack::object const& o, std::array<std::byte, N>& v) const { |
| 34 | + switch (o.type) { |
| 35 | + case msgpack::type::BIN: |
| 36 | + if (o.via.bin.size != N) |
| 37 | + throw msgpack::type_error(); |
| 38 | + if (N != 0) { |
| 39 | +#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__) |
| 40 | +#pragma GCC diagnostic push |
| 41 | +#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" |
| 42 | +#endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__) |
| 43 | + std::memcpy(&v.front(), o.via.bin.ptr, o.via.bin.size); |
| 44 | +#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__) |
| 45 | +#pragma GCC diagnostic pop |
| 46 | +#endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__) |
| 47 | + } |
| 48 | + break; |
| 49 | + case msgpack::type::STR: |
| 50 | + if (o.via.bin.size != N) |
| 51 | + throw msgpack::type_error(); |
| 52 | + if (N != 0) { |
| 53 | +#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__) |
| 54 | +#pragma GCC diagnostic push |
| 55 | +#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" |
| 56 | +#endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__) |
| 57 | + std::memcpy(&v.front(), o.via.str.ptr, o.via.str.size); |
| 58 | +#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__) |
| 59 | +#pragma GCC diagnostic pop |
| 60 | +#endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__) |
| 61 | + } |
| 62 | + break; |
| 63 | + default: |
| 64 | + throw msgpack::type_error(); |
| 65 | + break; |
| 66 | + } |
| 67 | + return o; |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | +template <std::size_t N> |
| 72 | +struct pack<std::array<std::byte, N> > { |
| 73 | + template <typename Stream> |
| 74 | + msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::array<std::byte, N>& v) const { |
| 75 | + uint32_t size = checked_get_container_size(v.size()); |
| 76 | + o.pack_bin(size); |
| 77 | + if (size != 0) { |
| 78 | + o.pack_bin_body(reinterpret_cast<char const*>(&v.front()), size); |
| 79 | + } |
| 80 | + |
| 81 | + return o; |
| 82 | + } |
| 83 | +}; |
| 84 | + |
| 85 | +template <std::size_t N> |
| 86 | +struct object<std::array<std::byte, N> > { |
| 87 | + void operator()(msgpack::object& o, const std::array<std::byte, N>& v) const { |
| 88 | + uint32_t size = checked_get_container_size(v.size()); |
| 89 | + o.type = msgpack::type::BIN; |
| 90 | + if (size != 0) { |
| 91 | + o.via.bin.ptr = reinterpret_cast<char const*>(&v.front()); |
| 92 | + } |
| 93 | + o.via.bin.size = size; |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +template <std::size_t N> |
| 98 | +struct object_with_zone<std::array<std::byte, N> > { |
| 99 | + void operator()(msgpack::object::with_zone& o, const std::array<std::byte, N>& v) const { |
| 100 | + uint32_t size = checked_get_container_size(v.size()); |
| 101 | + o.type = msgpack::type::BIN; |
| 102 | + o.via.bin.size = size; |
| 103 | + if (size != 0) { |
| 104 | + char* ptr = static_cast<char*>(o.zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char))); |
| 105 | + o.via.bin.ptr = ptr; |
| 106 | + std::memcpy(ptr, &v.front(), size); |
| 107 | + } |
| 108 | + } |
| 109 | +}; |
| 110 | + |
| 111 | +} // namespace adaptor |
| 112 | + |
| 113 | +/// @cond |
| 114 | +} // MSGPACK_API_VERSION_NAMESPACE(v1) |
| 115 | +/// @endcond |
| 116 | + |
| 117 | +} // namespace msgpack |
| 118 | + |
| 119 | +#endif // __cplusplus >= 201703 |
| 120 | + |
| 121 | +#endif // MSGPACK_V1_TYPE_ARRAY_BYTE_HPP |
0 commit comments