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