MessagePack for C++
carray_byte.hpp
Go to the documentation of this file.
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"
18 
19 #include <cstring>
20 #include <cstddef>
21 
22 namespace msgpack {
23 
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);
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);
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 
102 } // MSGPACK_API_VERSION_NAMESPACE(v1)
104 
105 } // namespace msgpack
106 
107 #endif // __cplusplus >= 201703
108 
109 #endif // MSGPACK_V1_TYPE_CARRAY_BYTE_HPP
msgpack::type::STR
@ STR
Definition: object_fwd_decl.hpp:39
msgpack::object_bin::ptr
const char * ptr
Definition: object_fwd.hpp:39
msgpack::packer
The class template that supports continuous packing.
Definition: adaptor_base_decl.hpp:24
msgpack::zone::allocate_align
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition: cpp03_zone.hpp:246
msgpack
Definition: adaptor_base.hpp:15
MSGPACK_ZONE_ALIGNOF
#define MSGPACK_ZONE_ALIGNOF(type)
Definition: cpp03_zone_decl.hpp:30
msgpack::object_str::ptr
const char * ptr
Definition: object_fwd.hpp:34
msgpack::type::BIN
@ BIN
Definition: object_fwd_decl.hpp:40
msgpack::object_bin::size
uint32_t size
Definition: object_fwd.hpp:38
msgpack::checked_get_container_size
uint32_t checked_get_container_size(T size)
Definition: check_container_size.hpp:55
MSGPACK_API_VERSION_NAMESPACE
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:58
msgpack::object::via
union_type via
Definition: object_fwd.hpp:93
msgpack::convert
void convert(T &v, msgpack::object const &o)
Definition: object.hpp:1194
adaptor_base.hpp
msgpack::pack
void pack(msgpack::packer< Stream > &o, const T &v)
Definition: object.hpp:1201
versioning.hpp
msgpack::type::size
std::size_t size(T const &t)
Definition: size_equal_only.hpp:24
msgpack::object_str::size
uint32_t size
Definition: object_fwd.hpp:33
msgpack::object::union_type::bin
msgpack::object_bin bin
Definition: object_fwd.hpp:88
check_container_size.hpp
msgpack::type_error
Definition: object_fwd.hpp:236
msgpack::adaptor::pack::operator()
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, T const &v) const
Definition: object.hpp:650
msgpack::adaptor::convert::operator()
msgpack::object const & operator()(msgpack::object const &o, T &v) const
Definition: object.hpp:641
msgpack::object
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:75
v2::object::with_zone::zone
msgpack::zone & zone
Definition: object.hpp:37
msgpack::packer::pack_bin
packer< Stream > & pack_bin(uint32_t l)
Packing bin header and length.
Definition: pack.hpp:1290
msgpack::packer::pack_bin_body
packer< Stream > & pack_bin_body(const char *b, uint32_t l)
Packing bin body.
Definition: pack.hpp:1309
v2::object::with_zone
Definition: object.hpp:35
msgpack::object::type
msgpack::type::object_type type
Definition: object_fwd.hpp:92
msgpack::object::union_type::str
msgpack::object_str str
Definition: object_fwd.hpp:87
msgpack::adaptor::object_with_zone::operator()
void operator()(msgpack::object::with_zone &o, T const &v) const
Definition: object.hpp:657