MessagePack for C++
array_char.hpp
Go to the documentation of this file.
1 //
2 // MessagePack for C++ static resolution routine
3 //
4 // Copyright (C) 2014-2015 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_CPP11_ARRAY_CHAR_HPP
11 #define MSGPACK_V1_TYPE_CPP11_ARRAY_CHAR_HPP
12 
13 #include "msgpack/versioning.hpp"
16 
17 #include <array>
18 #include <cstring>
19 
20 namespace msgpack {
21 
25 
26 namespace adaptor {
27 
28 template <std::size_t N>
29 struct convert<std::array<char, N>> {
30  msgpack::object const& operator()(msgpack::object const& o, std::array<char, N>& v) const {
31  switch (o.type) {
32  case msgpack::type::BIN:
33  if(o.via.bin.size > N) { throw msgpack::type_error(); }
34  std::memcpy(v.data(), o.via.bin.ptr, o.via.bin.size);
35  break;
36  case msgpack::type::STR:
37  if(o.via.str.size > N) { throw msgpack::type_error(); }
38  std::memcpy(v.data(), o.via.str.ptr, N);
39  break;
40  default:
41  throw msgpack::type_error();
42  break;
43  }
44  return o;
45  }
46 };
47 
48 template <>
49 struct convert<std::array<char, 0>> {
50  msgpack::object const& operator()(msgpack::object const& o, std::array<char, 0>&) const {
51  return o;
52  }
53 };
54 
55 template <std::size_t N>
56 struct pack<std::array<char, N>> {
57  template <typename Stream>
58  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::array<char, N>& v) const {
59  uint32_t size = checked_get_container_size(v.size());
60  o.pack_bin(size);
61  o.pack_bin_body(v.data(), size);
62 
63  return o;
64  }
65 };
66 
67 template <std::size_t N>
68 struct object<std::array<char, N>> {
69  void operator()(msgpack::object& o, const std::array<char, N>& v) const {
70  uint32_t size = checked_get_container_size(v.size());
72  o.via.bin.ptr = v.data();
73  o.via.bin.size = size;
74  }
75 };
76 
77 template <std::size_t N>
78 struct object_with_zone<std::array<char, N>> {
79  void operator()(msgpack::object::with_zone& o, const std::array<char, N>& v) const {
80  uint32_t size = checked_get_container_size(v.size());
82  char* ptr = static_cast<char*>(o.zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
83  o.via.bin.ptr = ptr;
84  o.via.bin.size = size;
85  std::memcpy(ptr, v.data(), size);
86  }
87 };
88 
89 } // namespace adaptor
90 
92 } // MSGPACK_API_VERSION_NAMESPACE(v1)
94 
95 } // namespace msgpack
96 
97 #endif // MSGPACK_V1_TYPE_CPP11_ARRAY_CHAR_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
uint32_t checked_get_container_size(T size)
Definition: check_container_size.hpp:55
msgpack::object const & operator()(msgpack::object const &o, std::array< char, 0 > &) const
Definition: array_char.hpp:50
msgpack::object const & operator()(msgpack::object const &o, std::array< char, N > &v) const
Definition: array_char.hpp:30
Definition: adaptor_base.hpp:27
void operator()(msgpack::object &o, const std::array< char, N > &v) const
Definition: array_char.hpp:69
void operator()(msgpack::object::with_zone &o, const std::array< char, N > &v) const
Definition: array_char.hpp:79
Definition: adaptor_base.hpp:43
Definition: adaptor_base.hpp:38
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::array< char, N > &v) const
Definition: array_char.hpp:58
Definition: adaptor_base.hpp:32
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