MessagePack for C++
carray.hpp
Go to the documentation of this file.
1 //
2 // MessagePack for C++ static resolution routine
3 //
4 // Copyright (C) 2016 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_HPP
11 #define MSGPACK_V1_TYPE_CARRAY_HPP
12 
13 #include "msgpack/versioning.hpp"
14 #include "msgpack/object_fwd.hpp"
17 
18 namespace msgpack {
19 
23 
24 namespace adaptor {
25 
26 template <typename T, std::size_t N>
27 struct convert<T[N]> {
28  msgpack::object const& operator()(msgpack::object const& o, T* v) const {
29  if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
30  if (o.via.array.size > N) { throw msgpack::type_error(); }
32  msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
33  do {
34  p->convert(*v);
35  ++p;
36  ++v;
37  } while(p < pend);
38  return o;
39  }
40 };
41 
42 template <std::size_t N>
43 struct convert<char[N]> {
44  msgpack::object const& operator()(msgpack::object const& o, char(&v)[N]) const {
45  switch (o.type) {
46  case msgpack::type::BIN:
47  if (o.via.bin.size > N) { throw msgpack::type_error(); }
48  std::memcpy(v, o.via.bin.ptr, o.via.bin.size);
49  break;
50  case msgpack::type::STR:
51  if (o.via.str.size > N) { throw msgpack::type_error(); }
52  std::memcpy(v, o.via.str.ptr, o.via.str.size);
53  if (o.via.str.size < N) v[o.via.str.size] = '\0';
54  break;
55  default:
56  throw msgpack::type_error();
57  break;
58  }
59  return o;
60  }
61 };
62 
63 template <std::size_t N>
64 struct convert<unsigned char[N]> {
65  msgpack::object const& operator()(msgpack::object const& o, unsigned char(&v)[N]) const {
66  switch (o.type) {
67  case msgpack::type::BIN:
68  if (o.via.bin.size > N) { throw msgpack::type_error(); }
69  std::memcpy(v, o.via.bin.ptr, o.via.bin.size);
70  break;
71  case msgpack::type::STR:
72  if (o.via.str.size > N) { throw msgpack::type_error(); }
73  std::memcpy(v, o.via.str.ptr, o.via.str.size);
74  if (o.via.str.size < N) v[o.via.str.size] = '\0';
75  break;
76  default:
77  throw msgpack::type_error();
78  break;
79  }
80  return o;
81  }
82 };
83 
84 
85 template <typename T, std::size_t N>
86 struct pack<T[N]> {
87  template <typename Stream>
89  uint32_t size = checked_get_container_size(N);
90  o.pack_array(size);
91  const T* ptr = v;
92  for (; ptr != &v[N]; ++ptr) o.pack(*ptr);
93  return o;
94  }
95 };
96 
97 template <std::size_t N>
98 struct pack<char[N]> {
99  template <typename Stream>
101  char const* p = v;
102  uint32_t size = checked_get_container_size(N);
103  char const* p2 = static_cast<char const*>(std::memchr(p, '\0', size));
104  uint32_t adjusted_size = p2 ? static_cast<uint32_t>(p2 - p) : size;
105  o.pack_str(adjusted_size);
106  o.pack_str_body(p, adjusted_size);
107  return o;
108  }
109 };
110 
111 template <std::size_t N>
112 struct pack<const char[N]> {
113  template <typename Stream>
115  uint32_t size = checked_get_container_size(N);
116  char const* p = v;
117  char const* p2 = static_cast<char const*>(std::memchr(p, '\0', size));
118  uint32_t adjusted_size = p2 ? static_cast<uint32_t>(p2 - p) : size;
119  o.pack_str(adjusted_size);
120  o.pack_str_body(p, adjusted_size);
121  return o;
122  }
123 };
124 
125 template <std::size_t N>
126 struct pack<unsigned char[N]> {
127  template <typename Stream>
128  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const unsigned char(&v)[N]) const {
129  unsigned char const* p = v;
130  uint32_t size = checked_get_container_size(N);
131  o.pack_bin(size);
132  o.pack_bin_body(reinterpret_cast<char const*>(p), size);
133  return o;
134  }
135 };
136 
137 template <std::size_t N>
138 struct pack<const unsigned char[N]> {
139  template <typename Stream>
140  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const unsigned char(&v)[N]) const {
141  unsigned char const* p = v;
142  uint32_t size = checked_get_container_size(N);
143  o.pack_bin(size);
144  o.pack_bin_body(reinterpret_cast<char const*>(p), size);
145  return o;
146  }
147 };
148 
149 template <typename T, std::size_t N>
150 struct object_with_zone<T[N]> {
151  void operator()(msgpack::object::with_zone& o, const T(&v)[N]) const {
152  uint32_t size = checked_get_container_size(N);
155  o.via.array.ptr = ptr;
156  o.via.array.size = size;
157  const T* pv = v;
158  for (; pv != &v[size]; ++pv) {
159  *ptr++ = msgpack::object(*pv, o.zone);
160  }
161  }
162 };
163 
164 template <std::size_t N>
165 struct object_with_zone<char[N]> {
166  void operator()(msgpack::object::with_zone& o, const char(&v)[N]) const {
167  char const* p = v;
168  uint32_t size = checked_get_container_size(N);
169  char const* p2 = static_cast<char const*>(std::memchr(p, '\0', size));
170  uint32_t adjusted_size = p2 ? static_cast<uint32_t>(p2 - p) : size;
172  char* ptr = static_cast<char*>(o.zone.allocate_align(adjusted_size, MSGPACK_ZONE_ALIGNOF(char)));
173  o.via.str.ptr = ptr;
174  o.via.str.size = adjusted_size;
175  std::memcpy(ptr, p, adjusted_size);
176  }
177 };
178 
179 template <std::size_t N>
180 struct object_with_zone<const char[N]> {
181  void operator()(msgpack::object::with_zone& o, const char(&v)[N]) const {
182  char const* p = v;
183  uint32_t size = checked_get_container_size(N);
184  char const* p2 = static_cast<char const*>(std::memchr(p, '\0', size));
185  uint32_t adjusted_size = p2 ? static_cast<uint32_t>(p2 - p) : size;
187  char* ptr = static_cast<char*>(o.zone.allocate_align(adjusted_size, MSGPACK_ZONE_ALIGNOF(char)));
188  o.via.str.ptr = ptr;
189  o.via.str.size = adjusted_size;
190  std::memcpy(ptr, p, adjusted_size);
191  }
192 };
193 
194 template <std::size_t N>
195 struct object_with_zone<unsigned char[N]> {
196  void operator()(msgpack::object::with_zone& o, const unsigned char(&v)[N]) const {
197  uint32_t size = checked_get_container_size(N);
199  char* ptr = static_cast<char*>(o.zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
200  o.via.bin.ptr = ptr;
201  o.via.bin.size = size;
202  std::memcpy(ptr, v, size);
203  }
204 };
205 
206 template <std::size_t N>
207 struct object_with_zone<const unsigned char[N]> {
208  void operator()(msgpack::object::with_zone& o, const unsigned char(&v)[N]) const {
209  uint32_t size = checked_get_container_size(N);
211  char* ptr = static_cast<char*>(o.zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
212  o.via.bin.ptr = ptr;
213  o.via.bin.size = size;
214  std::memcpy(ptr, v, size);
215  }
216 };
217 
218 template <std::size_t N>
219 struct object<char[N]> {
220  void operator()(msgpack::object& o, const char(&v)[N]) const {
221  char const* p = v;
222  uint32_t size = checked_get_container_size(N);
223  char const* p2 = static_cast<char const*>(std::memchr(p, '\0', size));
224  uint32_t adjusted_size = p2 ? static_cast<uint32_t>(p2 - p) : size;
226  o.via.str.ptr = p;
227  o.via.str.size = adjusted_size;
228  }
229 };
230 
231 template <std::size_t N>
232 struct object<const char[N]> {
233  void operator()(msgpack::object& o, const char(&v)[N]) const {
234  char const* p = v;
235  uint32_t size = checked_get_container_size(N);
236  char const* p2 = static_cast<char const*>(std::memchr(p, '\0', size));
237  uint32_t adjusted_size = p2 ? static_cast<uint32_t>(p2 - p) : size;
239  o.via.str.ptr = p;
240  o.via.str.size = adjusted_size;
241  }
242 };
243 
244 
245 } // namespace adaptor
246 
248 } // MSGPACK_API_VERSION_NAMESPACE(v1)
250 
251 } // namespace msgpack
252 
253 #endif // MSGPACK_V1_TYPE_CARRAY_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_str_body(const char *b, uint32_t l)
Packing str body.
Definition: pack.hpp:1255
packer< Stream > & pack_bin_body(const char *b, uint32_t l)
Packing bin body.
Definition: pack.hpp:1309
packer< Stream > & pack_str(uint32_t l)
Packing str header and length.
Definition: pack.hpp:1232
packer< Stream > & pack_array(uint32_t n)
Packing array header and size.
Definition: pack.hpp:1195
packer< Stream > & pack(const T &v)
Packing function template.
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
@ ARRAY
Definition: object_fwd_decl.hpp:40
@ BIN
Definition: object_fwd_decl.hpp:39
Definition: adaptor_base.hpp:15
uint32_t checked_get_container_size(T size)
Definition: check_container_size.hpp:55
msgpack::object const & operator()(msgpack::object const &o, T *v) const
Definition: carray.hpp:28
msgpack::object const & operator()(msgpack::object const &o, char(&v)[N]) const
Definition: carray.hpp:44
msgpack::object const & operator()(msgpack::object const &o, unsigned char(&v)[N]) const
Definition: carray.hpp:65
Definition: adaptor_base.hpp:27
void operator()(msgpack::object &o, const char(&v)[N]) const
Definition: carray.hpp:220
void operator()(msgpack::object &o, const char(&v)[N]) const
Definition: carray.hpp:233
void operator()(msgpack::object::with_zone &o, const T(&v)[N]) const
Definition: carray.hpp:151
void operator()(msgpack::object::with_zone &o, const char(&v)[N]) const
Definition: carray.hpp:166
void operator()(msgpack::object::with_zone &o, const char(&v)[N]) const
Definition: carray.hpp:181
void operator()(msgpack::object::with_zone &o, const unsigned char(&v)[N]) const
Definition: carray.hpp:208
void operator()(msgpack::object::with_zone &o, const unsigned char(&v)[N]) const
Definition: carray.hpp:196
Definition: adaptor_base.hpp:43
Definition: adaptor_base.hpp:38
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const T(&v)[N]) const
Definition: carray.hpp:88
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const char(&v)[N]) const
Definition: carray.hpp:100
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const char(&v)[N]) const
Definition: carray.hpp:114
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const unsigned char(&v)[N]) const
Definition: carray.hpp:140
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const unsigned char(&v)[N]) const
Definition: carray.hpp:128
Definition: adaptor_base.hpp:32
Definition: object.hpp:35
msgpack::zone & zone
Definition: object.hpp:37
uint32_t size
Definition: object_fwd.hpp:23
msgpack::object * ptr
Definition: object_fwd.hpp:24
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::enable_if< !msgpack::is_array< T >::value &&!msgpack::is_pointer< T >::value, T & >::type convert(T &v) const
Convert the object.
Definition: object.hpp:1071
msgpack::type::object_type type
Definition: object_fwd.hpp:92
msgpack::object_array array
Definition: object_fwd.hpp:85
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