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_V1_TYPE_VECTOR_UNSIGNED_CHAR_HPP
11 #define MSGPACK_V1_TYPE_VECTOR_UNSIGNED_CHAR_HPP
12 
13 #include "msgpack/versioning.hpp"
15 #include "msgpack/object.hpp"
17 
18 #include <vector>
19 #include <cstring>
20 
21 namespace msgpack {
22 
26 
27 namespace adaptor {
28 
29 template <typename Alloc>
30 struct convert<std::vector<unsigned char, Alloc> > {
31  msgpack::object const& operator()(msgpack::object const& o, std::vector<unsigned char, Alloc>& v) const {
32  switch (o.type) {
33  case msgpack::type::BIN:
34  v.resize(o.via.bin.size);
35  if (o.via.bin.size != 0) {
36 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
37 #pragma GCC diagnostic push
38 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
39 #endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
40  std::memcpy(&v.front(), o.via.bin.ptr, o.via.bin.size);
41 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
42 #pragma GCC diagnostic pop
43 #endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
44  }
45  break;
46  case msgpack::type::STR:
47  v.resize(o.via.str.size);
48  if (o.via.str.size != 0) {
49 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
50 #pragma GCC diagnostic push
51 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
52 #endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
53  std::memcpy(&v.front(), o.via.str.ptr, o.via.str.size);
54 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
55 #pragma GCC diagnostic pop
56 #endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
57  }
58  break;
59  default:
60  throw msgpack::type_error();
61  break;
62  }
63  return o;
64  }
65 };
66 
67 template <typename Alloc>
68 struct pack<std::vector<unsigned char, Alloc> > {
69  template <typename Stream>
70  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::vector<unsigned char, Alloc>& v) const {
71  uint32_t size = checked_get_container_size(v.size());
72  o.pack_bin(size);
73  if (size != 0) {
74  o.pack_bin_body(reinterpret_cast<char const*>(&v.front()), size);
75  }
76 
77  return o;
78  }
79 };
80 
81 template <typename Alloc>
82 struct object<std::vector<unsigned char, Alloc> > {
83  void operator()(msgpack::object& o, const std::vector<unsigned char, Alloc>& v) const {
84  uint32_t size = checked_get_container_size(v.size());
86  if (size != 0) {
87  o.via.bin.ptr = reinterpret_cast<char const*>(&v.front());
88  }
89  o.via.bin.size = size;
90  }
91 };
92 
93 template <typename Alloc>
94 struct object_with_zone<std::vector<unsigned char, Alloc> > {
95  void operator()(msgpack::object::with_zone& o, const std::vector<unsigned char, Alloc>& v) const {
96  uint32_t size = checked_get_container_size(v.size());
98  o.via.bin.size = size;
99  if (size != 0) {
100  char* ptr = static_cast<char*>(o.zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
101  o.via.bin.ptr = ptr;
102  std::memcpy(ptr, &v.front(), size);
103  }
104  }
105 };
106 
107 } // namespace adaptor
108 
110 } // MSGPACK_API_VERSION_NAMESPACE(v1)
112 
113 } // namespace msgpack
114 
115 #endif // MSGPACK_V1_TYPE_VECTOR_UNSIGNED_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:1312
packer< Stream > & pack_bin_body(const char *b, uint32_t l)
Packing bin body.
Definition: pack.hpp:1331
Definition: object_fwd.hpp:231
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition: cpp03_zone.hpp:255
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::vector< unsigned char, Alloc > &v) const
Definition: vector_unsigned_char.hpp:31
Definition: adaptor_base.hpp:27
void operator()(msgpack::object &o, const std::vector< unsigned char, Alloc > &v) const
Definition: vector_unsigned_char.hpp:83
void operator()(msgpack::object::with_zone &o, const std::vector< unsigned char, Alloc > &v) const
Definition: vector_unsigned_char.hpp:95
Definition: adaptor_base.hpp:43
Definition: adaptor_base.hpp:38
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::vector< unsigned char, Alloc > &v) const
Definition: vector_unsigned_char.hpp:70
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