MessagePack for C++
Loading...
Searching...
No Matches
vector_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_VECTOR_BYTE_HPP
11#define MSGPACK_V1_TYPE_VECTOR_BYTE_HPP
12
13#if __cplusplus >= 201703
14
18
19#include <vector>
20#include <cstring>
21#include <cstddef>
22
23namespace msgpack {
24
28
29namespace adaptor {
30
31template <typename Alloc>
32struct convert<std::vector<std::byte, Alloc> > {
33 msgpack::object const& operator()(msgpack::object const& o, std::vector<std::byte, Alloc>& v) const {
34 switch (o.type) {
36 v.resize(o.via.bin.size);
37 if (o.via.bin.size != 0) {
38#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
39#pragma GCC diagnostic push
40#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
41#endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
42 std::memcpy(&v.front(), o.via.bin.ptr, o.via.bin.size);
43#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
44#pragma GCC diagnostic pop
45#endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
46 }
47 break;
49 v.resize(o.via.str.size);
50 if (o.via.str.size != 0) {
51#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
52#pragma GCC diagnostic push
53#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
54#endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
55 std::memcpy(&v.front(), o.via.str.ptr, o.via.str.size);
56#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
57#pragma GCC diagnostic pop
58#endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
59 }
60 break;
61 default:
62 throw msgpack::type_error();
63 break;
64 }
65 return o;
66 }
67};
68
69template <typename Alloc>
70struct pack<std::vector<std::byte, Alloc> > {
71 template <typename Stream>
72 msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::vector<std::byte, Alloc>& v) const {
73 uint32_t size = checked_get_container_size(v.size());
74 o.pack_bin(size);
75 if (size != 0) {
76 o.pack_bin_body(reinterpret_cast<char const*>(&v.front()), size);
77 }
78
79 return o;
80 }
81};
82
83template <typename Alloc>
84struct object<std::vector<std::byte, Alloc> > {
85 void operator()(msgpack::object& o, const std::vector<std::byte, Alloc>& v) const {
86 uint32_t size = checked_get_container_size(v.size());
88 if (size != 0) {
89 o.via.bin.ptr = reinterpret_cast<char const*>(&v.front());
90 }
91 o.via.bin.size = size;
92 }
93};
94
95template <typename Alloc>
96struct object_with_zone<std::vector<std::byte, Alloc> > {
97 void operator()(msgpack::object::with_zone& o, const std::vector<std::byte, Alloc>& v) const {
98 uint32_t size = checked_get_container_size(v.size());
100 o.via.bin.size = size;
101 if (size != 0) {
102 char* ptr = static_cast<char*>(o.zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
103 o.via.bin.ptr = ptr;
104 std::memcpy(ptr, &v.front(), size);
105 }
106 }
107};
108
109} // namespace adaptor
110
112} // MSGPACK_API_VERSION_NAMESPACE(v1)
114
115} // namespace msgpack
116
117#endif // __cplusplus >= 201703
118
119#endif // MSGPACK_V1_TYPE_VECTOR_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
void operator()(msgpack::object &o, T const &v) const
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