MessagePack for C++
array_byte.hpp
Go to the documentation of this file.
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 #include "msgpack/cpp_version.hpp"
14 
15 #if MSGPACK_CPP_VERSION >= 201703
16 
17 #include "msgpack/versioning.hpp"
19 #include "msgpack/object.hpp"
21 
22 #include <array>
23 #include <cstring>
24 #include <cstddef>
25 
26 namespace msgpack {
27 
31 
32 namespace adaptor {
33 
34 template <std::size_t N>
35 struct convert<std::array<std::byte, N> > {
36  msgpack::object const& operator()(msgpack::object const& o, std::array<std::byte, N>& v) const {
37  switch (o.type) {
38  case msgpack::type::BIN:
39  if (o.via.bin.size != N)
40  throw msgpack::type_error();
41  if (N != 0) {
42 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
43 #pragma GCC diagnostic push
44 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
45 #endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
46  std::memcpy(&v.front(), o.via.bin.ptr, o.via.bin.size);
47 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
48 #pragma GCC diagnostic pop
49 #endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
50  }
51  break;
52  case msgpack::type::STR:
53  if (o.via.bin.size != N)
54  throw msgpack::type_error();
55  if (N != 0) {
56 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
57 #pragma GCC diagnostic push
58 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
59 #endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
60  std::memcpy(&v.front(), o.via.str.ptr, o.via.str.size);
61 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
62 #pragma GCC diagnostic pop
63 #endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
64  }
65  break;
66  default:
67  throw msgpack::type_error();
68  break;
69  }
70  return o;
71  }
72 };
73 
74 template <std::size_t N>
75 struct pack<std::array<std::byte, N> > {
76  template <typename Stream>
77  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::array<std::byte, N>& v) const {
78  uint32_t size = checked_get_container_size(v.size());
79  o.pack_bin(size);
80  if (size != 0) {
81  o.pack_bin_body(reinterpret_cast<char const*>(&v.front()), size);
82  }
83 
84  return o;
85  }
86 };
87 
88 template <std::size_t N>
89 struct object<std::array<std::byte, N> > {
90  void operator()(msgpack::object& o, const std::array<std::byte, N>& v) const {
91  uint32_t size = checked_get_container_size(v.size());
93  if (size != 0) {
94  o.via.bin.ptr = reinterpret_cast<char const*>(&v.front());
95  }
96  o.via.bin.size = size;
97  }
98 };
99 
100 template <std::size_t N>
101 struct object_with_zone<std::array<std::byte, N> > {
102  void operator()(msgpack::object::with_zone& o, const std::array<std::byte, N>& v) const {
103  uint32_t size = checked_get_container_size(v.size());
105  o.via.bin.size = size;
106  if (size != 0) {
107  char* ptr = static_cast<char*>(o.zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
108  o.via.bin.ptr = ptr;
109  std::memcpy(ptr, &v.front(), size);
110  }
111  }
112 };
113 
114 } // namespace adaptor
115 
117 } // MSGPACK_API_VERSION_NAMESPACE(v1)
119 
120 } // namespace msgpack
121 
122 #endif // MSGPACK_CPP_VERSION >= 201703
123 
124 #endif // MSGPACK_V1_TYPE_ARRAY_BYTE_HPP
The class template that supports continuous packing.
Definition: pack.hpp:33
packer< Stream > & pack_bin(uint32_t l)
Packing bin header and length.
Definition: pack.hpp:1312
packer< Stream > & pack_bin_body(const char *b, uint32_t l)
Packing bin body.
Definition: pack.hpp:1331
Definition: object_fwd.hpp:231
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition: cpp03_zone.hpp:255
std::size_t size(T const &t)
Definition: size_equal_only.hpp:24
@ STR
Definition: object_fwd_decl.hpp:38
@ BIN
Definition: object_fwd_decl.hpp:39
Definition: adaptor_base.hpp:15
void pack(msgpack::packer< Stream > &o, const T &v)
Definition: object.hpp:1185
uint32_t checked_get_container_size(T size)
Definition: check_container_size.hpp:55
void convert(T &v, msgpack::object const &o)
Definition: object.hpp:1178
msgpack::object const & operator()(msgpack::object const &o, T &v) const
Definition: object.hpp:646
void operator()(msgpack::object::with_zone &o, T const &v) const
Definition: object.hpp:662
void operator()(msgpack::object &o, T const &v) const
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, T const &v) const
Definition: object.hpp:655
Definition: object.hpp:35
msgpack::zone & zone
Definition: object.hpp:37
uint32_t size
Definition: object_fwd.hpp:38
const char * ptr
Definition: object_fwd.hpp:39
const char * ptr
Definition: object_fwd.hpp:34
uint32_t size
Definition: object_fwd.hpp:33
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:75
union_type via
Definition: object_fwd.hpp:93
msgpack::type::object_type type
Definition: object_fwd.hpp:92
msgpack::object_str str
Definition: object_fwd.hpp:87
msgpack::object_bin bin
Definition: object_fwd.hpp:88
#define MSGPACK_ZONE_ALIGNOF(type)
Definition: cpp03_zone_decl.hpp:30
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:66