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