MessagePack for C++
Loading...
Searching...
No Matches
ext.hpp
Go to the documentation of this file.
1//
2// MessagePack for C++ static resolution routine
3//
4// Copyright (C) 2015-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_EXT_HPP
11#define MSGPACK_V1_TYPE_EXT_HPP
12
15#include <cstring>
16#include <string>
17#include <cassert>
18
19namespace msgpack {
20
24
25namespace type {
26
27class ext {
28public:
29 ext() : m_data(1, 0) {}
30 ext(int8_t t, const char* p, uint32_t s) {
31 msgpack::detail::check_container_size_for_ext<sizeof(std::size_t)>(s);
32 m_data.reserve(static_cast<std::size_t>(s) + 1);
33 m_data.push_back(static_cast<char>(t));
34 m_data.insert(m_data.end(), p, p + s);
35 }
36 ext(int8_t t, uint32_t s) {
37 msgpack::detail::check_container_size_for_ext<sizeof(std::size_t)>(s);
38 m_data.resize(static_cast<std::size_t>(s) + 1);
39 m_data[0] = static_cast<char>(t);
40 }
41 ext(ext_ref const&);
42 int8_t type() const {
43 return static_cast<int8_t>(m_data[0]);
44 }
45 const char* data() const {
46 return &m_data[0] + 1;
47 }
48 char* data() {
49 return &m_data[0] + 1;
50 }
51 uint32_t size() const {
52 return static_cast<uint32_t>(m_data.size()) - 1;
53 }
54 bool operator== (const ext& x) const {
55 return m_data == x.m_data;
56 }
57
58 bool operator!= (const ext& x) const {
59 return !(*this == x);
60 }
61
62 bool operator< (const ext& x) const {
63 return m_data < x.m_data;
64 }
65
66 bool operator> (const ext& x) const {
67 return m_data > x.m_data;
68 }
69private:
70 std::vector<char> m_data;
71 friend class ext_ref;
72};
73
74} // namespace type
75
76namespace adaptor {
77
78template <>
81 if(o.type != msgpack::type::EXT) {
82 throw msgpack::type_error();
83 }
85 return o;
86 }
87};
88
89template <>
91 template <typename Stream>
93 // size limit has already been checked at ext's constructor
94 uint32_t size = v.size();
95 o.pack_ext(size, v.type());
96 o.pack_ext_body(v.data(), size);
97 return o;
98 }
99};
100
101template <>
104 // size limit has already been checked at ext's constructor
105 uint32_t size = v.size();
106 o.type = msgpack::type::EXT;
107 char* ptr = static_cast<char*>(o.zone.allocate_align(size + 1, MSGPACK_ZONE_ALIGNOF(char)));
108 o.via.ext.ptr = ptr;
109 o.via.ext.size = size;
110 ptr[0] = static_cast<char>(v.type());
111 std::memcpy(ptr + 1, v.data(), size);
112 }
113};
114
115} // namespace adaptor
116
117namespace type {
118
119class ext_ref {
120public:
121 // ext_ref should be default constructible to support 'convert'.
122 // A default constructed ext_ref object::m_ptr doesn't have the buffer to point to.
123 // In order to avoid nullptr checking branches, m_ptr points to m_size.
124 // So type() returns unspecified but valid value. It might be a zero because m_size
125 // is initialized as zero, but shouldn't assume that.
126 ext_ref() : m_ptr(static_cast<char*>(static_cast<void*>(&m_size))), m_size(0) {}
127 ext_ref(const char* p, uint32_t s) :
128 m_ptr(s == 0 ? static_cast<char*>(static_cast<void*>(&m_size)) : p),
129 m_size(s == 0 ? 0 : s - 1) {
130 msgpack::detail::check_container_size_for_ext<sizeof(std::size_t)>(s);
131 }
132
133 // size limit has already been checked at ext's constructor
134 ext_ref(ext const& x) : m_ptr(&x.m_data[0]), m_size(x.size()) {}
135
136 const char* data() const {
137 return m_ptr + 1;
138 }
139
140 uint32_t size() const {
141 return m_size;
142 }
143
144 int8_t type() const {
145 return static_cast<int8_t>(m_ptr[0]);
146 }
147
148 std::string str() const {
149 return std::string(m_ptr + 1, m_size);
150 }
151
152 bool operator== (const ext_ref& x) const {
153 return m_size == x.m_size && std::memcmp(m_ptr, x.m_ptr, m_size) == 0;
154 }
155
156 bool operator!= (const ext_ref& x) const {
157 return !(*this == x);
158 }
159
160 bool operator< (const ext_ref& x) const {
161 if (m_size < x.m_size) return true;
162 if (m_size > x.m_size) return false;
163 return std::memcmp(m_ptr, x.m_ptr, m_size) < 0;
164 }
165
166 bool operator> (const ext_ref& x) const {
167 if (m_size > x.m_size) return true;
168 if (m_size < x.m_size) return false;
169 return std::memcmp(m_ptr, x.m_ptr, m_size) > 0;
170 }
171private:
172 const char* m_ptr;
173 uint32_t m_size;
174 friend struct adaptor::object<msgpack::type::ext_ref>;
175};
176
177inline ext::ext(ext_ref const& x) {
178 // size limit has already been checked at ext_ref's constructor
179 m_data.reserve(x.size() + 1);
180
181 m_data.push_back(static_cast<char>(x.type()));
182 m_data.insert(m_data.end(), x.data(), x.data() + x.size());
183}
184
185} // namespace type
186
187namespace adaptor {
188
189template <>
192 if(o.type != msgpack::type::EXT) { throw msgpack::type_error(); }
194 return o;
195 }
196};
197
198template <>
200 template <typename Stream>
202 // size limit has already been checked at ext_ref's constructor
203 uint32_t size = v.size();
204 o.pack_ext(size, v.type());
205 o.pack_ext_body(v.data(), size);
206 return o;
207 }
208};
209
210template <>
213 // size limit has already been checked at ext_ref's constructor
214 uint32_t size = v.size();
215 o.type = msgpack::type::EXT;
216 o.via.ext.ptr = v.m_ptr;
217 o.via.ext.size = size;
218 }
219};
220
221template <>
224 static_cast<msgpack::object&>(o) << v;
225 }
226};
227
228} // namespace adaptor
229
231} // MSGPACK_API_VERSION_NAMESPACE(v1)
233
234} // namespace msgpack
235
236#endif // MSGPACK_V1_TYPE_EXT_HPP
The class template that supports continuous packing.
Definition pack.hpp:33
packer< Stream > & pack_ext(size_t l, int8_t type)
Packing ext header, type, and length.
Definition pack.hpp:1316
packer< Stream > & pack_ext_body(const char *b, uint32_t l)
Packing ext body.
Definition pack.hpp:1375
Definition ext.hpp:119
const char * data() const
Definition ext.hpp:136
ext_ref(ext const &x)
Definition ext.hpp:134
int8_t type() const
Definition ext.hpp:144
std::string str() const
Definition ext.hpp:148
uint32_t size() const
Definition ext.hpp:140
bool operator>(const ext_ref &x) const
Definition ext.hpp:166
ext_ref(const char *p, uint32_t s)
Definition ext.hpp:127
ext_ref()
Definition ext.hpp:126
Definition ext.hpp:27
ext(int8_t t, uint32_t s)
Definition ext.hpp:36
bool operator>(const ext &x) const
Definition ext.hpp:66
const char * data() const
Definition ext.hpp:45
uint32_t size() const
Definition ext.hpp:51
ext()
Definition ext.hpp:29
int8_t type() const
Definition ext.hpp:42
char * data()
Definition ext.hpp:48
ext(int8_t t, const char *p, uint32_t s)
Definition ext.hpp:30
Definition object_fwd.hpp:236
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition cpp03_zone.hpp:246
Definition adaptor_base.hpp:15
msgpack::object const & operator()(msgpack::object const &o, msgpack::type::ext &v) const
Definition ext.hpp:80
msgpack::object const & operator()(msgpack::object const &o, msgpack::type::ext_ref &v) const
Definition ext.hpp:191
Definition adaptor_base.hpp:27
void operator()(msgpack::object &o, const msgpack::type::ext_ref &v) const
Definition ext.hpp:212
void operator()(msgpack::object::with_zone &o, const msgpack::type::ext &v) const
Definition ext.hpp:103
void operator()(msgpack::object::with_zone &o, const msgpack::type::ext_ref &v) const
Definition ext.hpp:223
Definition adaptor_base.hpp:43
Definition adaptor_base.hpp:38
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const msgpack::type::ext &v) const
Definition ext.hpp:92
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const msgpack::type::ext_ref &v) const
Definition ext.hpp:201
Definition adaptor_base.hpp:32
Definition object.hpp:35
msgpack::zone & zone
Definition object.hpp:37
int8_t type() const
Definition object_fwd.hpp:43
const char * ptr
Definition object_fwd.hpp:46
const char * data() const
Definition object_fwd.hpp:44
uint32_t size
Definition object_fwd.hpp:45
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_ext ext
Definition object_fwd.hpp:89
#define MSGPACK_ZONE_ALIGNOF(type)
Definition cpp03_zone_decl.hpp:30
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition versioning.hpp:58