MessagePack for C++
Loading...
Searching...
No Matches
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
18
19#include <cstring>
20#include <cstddef>
21
22namespace msgpack {
23
27
28namespace adaptor {
29
30template <std::size_t N>
31struct convert<std::byte[N]> {
32 msgpack::object const& operator()(msgpack::object const& o, std::byte(&v)[N]) const {
33 switch (o.type) {
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;
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
51template <std::size_t N>
52struct 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
63template <std::size_t N>
64struct 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
75template <std::size_t N>
76struct 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
87template <std::size_t N>
88struct 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
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:236
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition cpp03_zone.hpp:246
std::size_t size(T const &t)
Definition size_equal_only.hpp:24
@ STR
Definition object_fwd_decl.hpp:39
@ BIN
Definition object_fwd_decl.hpp:40
Definition adaptor_base.hpp:15
void pack(msgpack::packer< Stream > &o, const T &v)
Definition object.hpp:1201
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:1194
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:58