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 #include "msgpack/cpp_version.hpp"
14 
15 #if MSGPACK_CPP_VERSION >= 201703
16 
17 #include "msgpack/versioning.hpp"
20 
21 #include <cstring>
22 #include <cstddef>
23 
24 namespace msgpack {
25 
29 
30 namespace adaptor {
31 
32 template <std::size_t N>
33 struct convert<std::byte[N]> {
34  msgpack::object const& operator()(msgpack::object const& o, std::byte(&v)[N]) const {
35  switch (o.type) {
36  case msgpack::type::BIN:
37  if (o.via.bin.size > N) { throw msgpack::type_error(); }
38  std::memcpy(v, o.via.bin.ptr, o.via.bin.size);
39  break;
40  case msgpack::type::STR:
41  if (o.via.str.size > N) { throw msgpack::type_error(); }
42  std::memcpy(v, o.via.str.ptr, o.via.str.size);
43  if (o.via.str.size < N) v[o.via.str.size] = std::byte{'\0'};
44  break;
45  default:
46  throw msgpack::type_error();
47  break;
48  }
49  return o;
50  }
51 };
52 
53 template <std::size_t N>
54 struct pack<std::byte[N]> {
55  template <typename Stream>
56  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::byte(&v)[N]) const {
57  std::byte const* p = v;
58  uint32_t size = checked_get_container_size(N);
59  o.pack_bin(size);
60  o.pack_bin_body(reinterpret_cast<char const*>(p), size);
61  return o;
62  }
63 };
64 
65 template <std::size_t N>
66 struct pack<const std::byte[N]> {
67  template <typename Stream>
68  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::byte(&v)[N]) const {
69  std::byte const* p = v;
70  uint32_t size = checked_get_container_size(N);
71  o.pack_bin(size);
72  o.pack_bin_body(reinterpret_cast<char const*>(p), size);
73  return o;
74  }
75 };
76 
77 template <std::size_t N>
78 struct object_with_zone<std::byte[N]> {
79  void operator()(msgpack::object::with_zone& o, const std::byte(&v)[N]) const {
80  uint32_t size = checked_get_container_size(N);
82  char* ptr = static_cast<char*>(o.zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
83  o.via.bin.ptr = ptr;
84  o.via.bin.size = size;
85  std::memcpy(ptr, v, size);
86  }
87 };
88 
89 template <std::size_t N>
90 struct object_with_zone<const std::byte[N]> {
91  void operator()(msgpack::object::with_zone& o, const std::byte(&v)[N]) const {
92  uint32_t size = checked_get_container_size(N);
94  char* ptr = static_cast<char*>(o.zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
95  o.via.bin.ptr = ptr;
96  o.via.bin.size = size;
97  std::memcpy(ptr, v, size);
98  }
99 };
100 
101 } // namespace adaptor
102 
104 } // MSGPACK_API_VERSION_NAMESPACE(v1)
106 
107 } // namespace msgpack
108 
109 #endif // MSGPACK_CPP_VERSION >= 201703
110 
111 #endif // MSGPACK_V1_TYPE_CARRAY_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:1290
packer< Stream > & pack_bin_body(const char *b, uint32_t l)
Packing bin body.
Definition: pack.hpp:1309
Definition: object_fwd.hpp:231
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition: cpp03_zone.hpp:256
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:1180
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:1173
msgpack::object const & operator()(msgpack::object const &o, T &v) const
Definition: object.hpp:641
void operator()(msgpack::object::with_zone &o, T const &v) const
Definition: object.hpp:657
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, T const &v) const
Definition: object.hpp:650
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