Debian Codegraph @

cpp/src/decoder.cpp

raw
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 #include "proton/codec/decoder.hpp"
21
22 #include "proton/annotation_key.hpp"
23 #include "proton/binary.hpp"
24 #include "proton/decimal.hpp"
25 #include "proton/message_id.hpp"
26 #include "proton/scalar.hpp"
27 #include "proton/symbol.hpp"
28 #include "proton/timestamp.hpp"
29 #include "proton/value.hpp"
30
31 #include "proton_bits.hpp"
32 #include "types_internal.hpp"
33 #include "msg.hpp"
34
35 #include <proton/codec.h>
36
37 namespace proton {
38 namespace codec {
39
40 /**@file
41 *
42 * Note the pn_data_t "current" node is always pointing *before* the next value
43 * to be returned by the decoder.
44 */
45 decoder::decoder(const internal::value_base& v, bool exact)
46 : data(const_cast<internal::value_base&>(v).data()), exact_(exact)
47 {
48 rewind();
49 }
50
51 namespace {
52 template <class T> T check(T result) {
53 if (result < 0)
54 throw conversion_error(error_str(result));
55 return result;
56 }
57 }
58
59 void decoder::decode(const char* i, size_t size) {
60 internal::state_guard sg(*this);
61 const char* end = i + size;
62 while (i < end)
63 i += check(pn_data_decode(pn_object(), i, size_t(end - i)));
64 }
65
66 void decoder::decode(const std::string& s) { decode(s.data(), s.size()); }
67
68 bool decoder::more() {
69 internal::state_guard sg(*this);
70 return next();
71 }
72
73 type_id decoder::pre_get() {
74 if (!next()) throw conversion_error("no more data");
75 type_id t = type_id(pn_data_type(pn_object()));
76 if (t < 0) throw conversion_error("invalid data");
77 return t;
78 }
79
80 namespace {
81
82 template <class T, class U> void assign(T& x, const U& y) { x = y; }
83 void assign(uuid& x, const pn_uuid_t y) { byte_copy(x, y); }
84 void assign(decimal32& x, const pn_decimal32_t y) { byte_copy(x, y); }
85 void assign(decimal64& x, const pn_decimal64_t y) { byte_copy(x, y); }
86 void assign(decimal128& x, const pn_decimal128_t y) { byte_copy(x, y); }
87 void assign(symbol& x, const pn_bytes_t y) { x = str(y); }
88 void assign(binary& x, const pn_bytes_t y) { x = bin(y); }
89
90 } // namespace
91
92
93 // Simple extract with no type conversion.
94 template <class T, class U> decoder& decoder::extract(T& x, U (*get)(pn_data_t*)) {
95 internal::state_guard sg(*this);
96 assert_type_equal(internal::type_id_of<T>::value, pre_get());
97 assign(x, get(pn_object()));
98 sg.cancel(); // No error, cancel the reset.
99 return *this;
100 }
101
102 type_id decoder::next_type() {
103 internal::state_guard sg(*this);
104 return pre_get();
105 }
106
107 decoder& decoder::operator>>(start& s) {
108 internal::state_guard sg(*this);
109 s.type = pre_get();
110 switch (s.type) {
111 case ARRAY:
112 s.size = pn_data_get_array(pn_object());
113 s.element = type_id(pn_data_get_array_type(pn_object())); s.is_described = pn_data_is_array_described(pn_object());
114 break;
115 case LIST:
116 s.size = pn_data_get_list(pn_object());
117 break;
118 case MAP:
119 s.size = pn_data_get_map(pn_object());
120 break;
121 case DESCRIBED:
122 s.is_described = true;
123 s.size = 1;
124 break;
125 default:
126 throw conversion_error(MSG("" << s.type << " is not a container type"));
127 }
128 pn_data_enter(pn_object());
129 sg.cancel();
130 return *this;
131 }
132
133 decoder& decoder::operator>>(const finish&) {
134 pn_data_exit(pn_object());
135 return *this;
136 }
137
138 decoder& decoder::operator>>(null&) {
139 internal::state_guard sg(*this);
140 assert_type_equal(NULL_TYPE, pre_get());
141 return *this;
142 }
143
144 decoder& decoder::operator>>(decltype(nullptr)&) {
145 internal::state_guard sg(*this);
146 assert_type_equal(NULL_TYPE, pre_get());
147 return *this;
148 }
149
150 decoder& decoder::operator>>(internal::value_base& x) {
151 if (*this == x.data_)
152 throw conversion_error("extract into self");
153 data d = x.data();
154 d.clear();
155 narrow();
156 try {
157 check(d.appendn(*this, 1));
158 widen();
159 } catch(...) {
160 widen();
161 throw;
162 }
163 next();
164 return *this;
165 }
166
167 decoder& decoder::operator>>(message_id& x) {
168 internal::state_guard sg(*this);
169 type_id got = pre_get();
170 if (got != ULONG && got != UUID && got != BINARY && got != STRING)
171 throw conversion_error(
172 msg() << "expected one of ulong, uuid, binary or string but found " << got);
173 x.set(pn_data_get_atom(pn_object()));
174 sg.cancel();
175 return *this;
176 }
177
178 decoder& decoder::operator>>(annotation_key& x) {
179 internal::state_guard sg(*this);
180 type_id got = pre_get();
181 if (got != ULONG && got != SYMBOL)
182 throw conversion_error(msg() << "expected one of ulong or symbol but found " << got);
183 x.set(pn_data_get_atom(pn_object()));
184 sg.cancel();
185 return *this;
186 }
187
188 decoder& decoder::operator>>(scalar& x) {
189 internal::state_guard sg(*this);
190 type_id got = pre_get();
191 if (!type_id_is_scalar(got))
192 throw conversion_error("expected scalar, found "+type_name(got));
193 x.set(pn_data_get_atom(pn_object()));
194 sg.cancel(); // No error, no rewind
195 return *this;
196 }
197
198 decoder& decoder::operator>>(bool &x) { return extract(x, pn_data_get_bool); }
199
200 decoder& decoder::operator>>(uint8_t &x) { return extract(x, pn_data_get_ubyte); }
201
202 decoder& decoder::operator>>(int8_t &x) { return extract(x, pn_data_get_byte); }
203
204 decoder& decoder::operator>>(uint16_t &x) {
205 internal::state_guard sg(*this);
206 type_id tid = pre_get();
207 if (exact_) assert_type_equal(USHORT, tid);
208 switch (tid) {
209 case UBYTE: x = pn_data_get_ubyte(pn_object()); break;
210 case USHORT: x = pn_data_get_ushort(pn_object()); break;
211 default: assert_type_equal(USHORT, tid);
212 }
213 sg.cancel();
214 return *this;
215 }
216
217 decoder& decoder::operator>>(int16_t &x) {
218 internal::state_guard sg(*this);
219 type_id tid = pre_get();
220 if (exact_) assert_type_equal(SHORT, tid);
221 switch (tid) {
222 case BYTE: x = pn_data_get_byte(pn_object()); break;
223 case SHORT: x = pn_data_get_short(pn_object()); break;
224 default: assert_type_equal(SHORT, tid);
225 }
226 sg.cancel();
227 return *this;
228 }
229
230 decoder& decoder::operator>>(uint32_t &x) {
231 internal::state_guard sg(*this);
232 type_id tid = pre_get();
233 if (exact_) assert_type_equal(UINT, tid);
234 switch (tid) {
235 case UBYTE: x = pn_data_get_ubyte(pn_object()); break;
236 case USHORT: x = pn_data_get_ushort(pn_object()); break;
237 case UINT: x = pn_data_get_uint(pn_object()); break;
238 default: assert_type_equal(UINT, tid);
239 }
240 sg.cancel();
241 return *this;
242 }
243
244 decoder& decoder::operator>>(int32_t &x) {
245 internal::state_guard sg(*this);
246 type_id tid = pre_get();
247 if (exact_) assert_type_equal(INT, tid);
248 switch (tid) {
249 case BYTE: x = pn_data_get_byte(pn_object()); break;
250 case SHORT: x = pn_data_get_short(pn_object()); break;
251 case INT: x = pn_data_get_int(pn_object()); break;
252 default: assert_type_equal(INT, tid);
253 }
254 sg.cancel();
255 return *this;
256 }
257
258 decoder& decoder::operator>>(uint64_t &x) {
259 internal::state_guard sg(*this);
260 type_id tid = pre_get();
261 if (exact_) assert_type_equal(ULONG, tid);
262 switch (tid) {
263 case UBYTE: x = pn_data_get_ubyte(pn_object()); break;
264 case USHORT: x = pn_data_get_ushort(pn_object()); break;
265 case UINT: x = pn_data_get_uint(pn_object()); break;
266 case ULONG: x = pn_data_get_ulong(pn_object()); break;
267 default: assert_type_equal(ULONG, tid);
268 }
269 sg.cancel();
270 return *this;
271 }
272
273 decoder& decoder::operator>>(int64_t &x) {
274 internal::state_guard sg(*this);
275 type_id tid = pre_get();
276 if (exact_) assert_type_equal(LONG, tid);
277 switch (tid) {
278 case BYTE: x = pn_data_get_byte(pn_object()); break;
279 case SHORT: x = pn_data_get_short(pn_object()); break;
280 case INT: x = pn_data_get_int(pn_object()); break;
281 case LONG: x = pn_data_get_long(pn_object()); break;
282 default: assert_type_equal(LONG, tid);
283 }
284 sg.cancel();
285 return *this;
286 }
287
288 decoder& decoder::operator>>(wchar_t &x) { return extract(x, pn_data_get_char); }
289
290 decoder& decoder::operator>>(timestamp &x) { return extract(x, pn_data_get_timestamp); }
291
292 decoder& decoder::operator>>(float &x) {
293 internal::state_guard sg(*this);
294 type_id tid = pre_get();
295 if (exact_) assert_type_equal(FLOAT, tid);
296 switch (tid) {
297 case FLOAT: x = pn_data_get_float(pn_object()); break;
298 case DOUBLE: x = float(pn_data_get_double(pn_object())); break;
299 default: assert_type_equal(FLOAT, tid);
300 }
301 sg.cancel();
302 return *this;
303 }
304
305 decoder& decoder::operator>>(double &x) {
306 internal::state_guard sg(*this);
307 type_id tid = pre_get();
308 if (exact_) assert_type_equal(DOUBLE, tid);
309 switch (tid) {
310 case FLOAT: x = static_cast<double>(pn_data_get_float(pn_object())); break;
311 case DOUBLE: x = pn_data_get_double(pn_object()); break;
312 default: assert_type_equal(DOUBLE, tid);
313 }
314 sg.cancel();
315 return *this;
316 }
317
318 decoder& decoder::operator>>(decimal32 &x) { return extract(x, pn_data_get_decimal32); }
319 decoder& decoder::operator>>(decimal64 &x) { return extract(x, pn_data_get_decimal64); }
320 decoder& decoder::operator>>(decimal128 &x) { return extract(x, pn_data_get_decimal128); }
321
322 decoder& decoder::operator>>(uuid &x) { return extract(x, pn_data_get_uuid); }
323 decoder& decoder::operator>>(binary &x) { return extract(x, pn_data_get_binary); }
324 decoder& decoder::operator>>(symbol &x) { return extract(x, pn_data_get_symbol); }
325
326 decoder& decoder::operator>>(std::string &x) {
327 internal::state_guard sg(*this);
328 type_id tid = pre_get();
329 if (exact_) assert_type_equal(STRING, tid);
330 switch (tid) {
331 case STRING: x = str(pn_data_get_string(pn_object())); break;
332 case SYMBOL: x = str(pn_data_get_symbol(pn_object())); break;
333 default: assert_type_equal(STRING, tid);
334 }
335 sg.cancel();
336 return *this;
337 }
338
339 } // codec
340 } // proton