MessagePack for C++
span.hpp
Go to the documentation of this file.
1 //
2 // MessagePack for C++ static resolution routine
3 //
4 // Copyright (C) 2021 KONDO Takatoshi and Daniil Kovalev
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 
11 #ifndef MSGPACK_V1_TYPE_CPP20_SPAN_HPP
12 #define MSGPACK_V1_TYPE_CPP20_SPAN_HPP
13 
14 #include "msgpack/cpp_version.hpp"
15 
16 // Some compilers still do not set the corresponding macro to 202002
17 #if MSGPACK_CPP_VERSION > 201703
18 
19 #include "msgpack/versioning.hpp"
22 #include "msgpack/meta.hpp"
23 
24 #include <span>
25 #include <cstring>
26 #include <cstddef>
27 
28 namespace msgpack {
29 
33 
34 namespace adaptor {
35 
36 #define MSGPACK_ADAPTOR_CONVERT_SPAN_BINARY(byte_t) \
37  template <> \
38  struct convert<std::span<const byte_t> > { \
39  msgpack::object const& operator()(msgpack::object const& o, std::span<const byte_t>& v) const { \
40  switch (o.type) { \
41  case msgpack::type::BIN: \
42  v = std::span(reinterpret_cast<const byte_t*>(o.via.bin.ptr), o.via.bin.size); \
43  break; \
44  default: \
45  throw msgpack::type_error(); \
46  break; \
47  } \
48  return o; \
49  } \
50  };
51 
52 #define MSGPACK_ADAPTOR_PACK_SPAN_BINARY(byte_t) \
53  template <> \
54  struct pack<std::span<byte_t> > { \
55  template <typename Stream> \
56  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::span<byte_t>& v) const { \
57  uint32_t size = checked_get_container_size(v.size()); \
58  o.pack_bin(size); \
59  if (size != 0) { \
60  o.pack_bin_body(reinterpret_cast<char const*>(v.data()), size); \
61  } \
62  return o; \
63  } \
64  };
65 
66 #define MSGPACK_ADAPTOR_OBJECT_SPAN_BINARY(byte_t) \
67  template <> \
68  struct object<std::span<byte_t> > { \
69  void operator()(msgpack::object& o, const std::span<byte_t>& v) const { \
70  uint32_t size = checked_get_container_size(v.size()); \
71  o.type = msgpack::type::BIN; \
72  if (size != 0) { \
73  o.via.bin.ptr = reinterpret_cast<char const*>(v.data()); \
74  } \
75  o.via.bin.size = size; \
76  } \
77  };
78 
79 #define MSGPACK_ADAPTOR_OBJECT_WITH_ZONE_SPAN_BINARY(byte_t) \
80  template <> \
81  struct object_with_zone<std::span<byte_t> > { \
82  void operator()(msgpack::object::with_zone& o, const std::span<byte_t>& v) const { \
83  uint32_t size = checked_get_container_size(v.size()); \
84  o.type = msgpack::type::BIN; \
85  o.via.bin.size = size; \
86  if (size != 0) { \
87  char* ptr = static_cast<char*>(o.zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char))); \
88  o.via.bin.ptr = ptr; \
89  std::memcpy(ptr, v.data(), size); \
90  } \
91  } \
92  };
93 
94 #define MSGPACK_ADAPTOR_SPAN_BINARY(byte_t) \
95  MSGPACK_ADAPTOR_CONVERT_SPAN_BINARY(byte_t) \
96  MSGPACK_ADAPTOR_PACK_SPAN_BINARY(byte_t) \
97  MSGPACK_ADAPTOR_PACK_SPAN_BINARY(const byte_t) \
98  MSGPACK_ADAPTOR_OBJECT_SPAN_BINARY(byte_t) \
99  MSGPACK_ADAPTOR_OBJECT_SPAN_BINARY(const byte_t) \
100  MSGPACK_ADAPTOR_OBJECT_WITH_ZONE_SPAN_BINARY(byte_t) \
101  MSGPACK_ADAPTOR_OBJECT_WITH_ZONE_SPAN_BINARY(const byte_t)
102 
103 MSGPACK_ADAPTOR_SPAN_BINARY(char)
104 MSGPACK_ADAPTOR_SPAN_BINARY(unsigned char)
105 MSGPACK_ADAPTOR_SPAN_BINARY(std::byte)
106 
107 #undef MSGPACK_ADAPTOR_SPAN_BINARY
108 #undef MSGPACK_ADAPTOR_OBJECT_WITH_ZONE_SPAN_BINARY
109 #undef MSGPACK_ADAPTOR_OBJECT_SPAN_BINARY
110 #undef MSGPACK_ADAPTOR_PACK_SPAN_BINARY
111 #undef MSGPACK_ADAPTOR_CONVERT_SPAN_BINARY
112 
113 } // namespace adaptor
114 
116 } // MSGPACK_API_VERSION_NAMESPACE(v1)
118 
119 } // namespace msgpack
120 
121 #endif // MSGPACK_CPP_VERSION > 201703
122 
123 #endif // MSGPACK_V1_TYPE_CPP11_ARRAY_HPP
Definition: adaptor_base.hpp:15
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:66