MessagePack for C++
vector_unsigned_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_TYPE_VECTOR_UNSIGNED_CHAR_HPP
11 #define MSGPACK_TYPE_VECTOR_UNSIGNED_CHAR_HPP
12 
13 #include "msgpack/versioning.hpp"
16 
17 #include <vector>
18 
19 namespace msgpack {
20 
24 
25 namespace adaptor {
26 
27 template <typename Alloc>
28 struct convert<std::vector<unsigned char, Alloc> > {
29  msgpack::object const& operator()(msgpack::object const& o, std::vector<unsigned char, Alloc>& v) const {
30  switch (o.type) {
31  case msgpack::type::BIN:
32  v.resize(o.via.bin.size);
33  if (o.via.bin.size != 0) {
34 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
35 #pragma GCC diagnostic push
36 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
37 #endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
38  std::memcpy(&v.front(), o.via.bin.ptr, o.via.bin.size);
39 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
40 #pragma GCC diagnostic pop
41 #endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
42  }
43  break;
44  case msgpack::type::STR:
45  v.resize(o.via.str.size);
46  if (o.via.str.size != 0) {
47 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
48 #pragma GCC diagnostic push
49 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
50 #endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
51  std::memcpy(&v.front(), o.via.str.ptr, o.via.str.size);
52 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
53 #pragma GCC diagnostic pop
54 #endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
55  }
56  break;
57  default:
58  throw msgpack::type_error();
59  break;
60  }
61  return o;
62  }
63 };
64 
65 template <typename Alloc>
66 struct pack<std::vector<unsigned char, Alloc> > {
67  template <typename Stream>
68  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::vector<unsigned char, Alloc>& v) const {
69  uint32_t size = checked_get_container_size(v.size());
70  o.pack_bin(size);
71  if (size != 0) {
72  o.pack_bin_body(reinterpret_cast<char const*>(&v.front()), size);
73  }
74 
75  return o;
76  }
77 };
78 
79 template <typename Alloc>
80 struct object<std::vector<unsigned char, Alloc> > {
81  void operator()(msgpack::object& o, const std::vector<unsigned char, Alloc>& v) const {
82  uint32_t size = checked_get_container_size(v.size());
84  if (size != 0) {
85  o.via.bin.ptr = reinterpret_cast<char const*>(&v.front());
86  }
87  o.via.bin.size = size;
88  }
89 };
90 
91 template <typename Alloc>
92 struct object_with_zone<std::vector<unsigned char, Alloc> > {
93  void operator()(msgpack::object::with_zone& o, const std::vector<unsigned char, Alloc>& v) const {
94  uint32_t size = checked_get_container_size(v.size());
96  o.via.bin.size = size;
97  if (size != 0) {
98  char* ptr = static_cast<char*>(o.zone.allocate_align(size));
99  o.via.bin.ptr = ptr;
100  std::memcpy(ptr, &v.front(), size);
101  }
102  }
103 };
104 
105 } // namespace adaptor
106 
108 } // MSGPACK_API_VERSION_NAMESPACE(v1)
110 
111 } // namespace msgpack
112 
113 #endif // MSGPACK_TYPE_VECTOR_UNSIGNED_CHAR_HPP
packer< Stream > & pack_bin(uint32_t l)
Packing bin header and length.
Definition: pack.hpp:1258
msgpack::object const & operator()(msgpack::object const &o, std::vector< unsigned char, Alloc > &v) const
Definition: vector_unsigned_char.hpp:29
Definition: object_fwd.hpp:37
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:58
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition: cpp03_zone.hpp:248
const char * ptr
Definition: object_fwd.hpp:66
uint32_t checked_get_container_size(T size)
Definition: check_container_size.hpp:55
void operator()(msgpack::object &o, const std::vector< unsigned char, Alloc > &v) const
Definition: vector_unsigned_char.hpp:81
union_type via
Definition: object_fwd.hpp:123
Definition: object_fwd.hpp:38
msgpack::zone & zone
Definition: object_fwd.hpp:262
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::vector< unsigned char, Alloc > &v) const
Definition: vector_unsigned_char.hpp:68
uint32_t size
Definition: object_fwd.hpp:65
void operator()(msgpack::object::with_zone &o, const std::vector< unsigned char, Alloc > &v) const
Definition: vector_unsigned_char.hpp:93
Definition: adaptor_base.hpp:15
const char * ptr
Definition: object_fwd.hpp:61
packer< Stream > & pack_bin_body(const char *b, uint32_t l)
Packing bin body.
Definition: pack.hpp:1277
Definition: object_fwd.hpp:260
Definition: adaptor_base.hpp:45
Definition: object_fwd.hpp:253
Definition: adaptor_base.hpp:34
msgpack::object_str str
Definition: object_fwd.hpp:117
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:106
msgpack::type::object_type type
Definition: object_fwd.hpp:122
uint32_t size
Definition: object_fwd.hpp:60
Definition: adaptor_base.hpp:40
The class template that supports continuous packing.
Definition: adaptor_base.hpp:22
Definition: adaptor_base.hpp:29
msgpack::object_bin bin
Definition: object_fwd.hpp:118