Debian Codegraph @

cpp/src/encoder.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/encoder.hpp"
21
22 #include "proton_bits.hpp"
23 #include "types_internal.hpp"
24 #include "msg.hpp"
25
26 #include "proton/annotation_key.hpp"
27 #include "proton/binary.hpp"
28 #include "proton/decimal.hpp"
29 #include "proton/message_id.hpp"
30 #include "proton/null.hpp"
31 #include "proton/scalar_base.hpp"
32 #include "proton/symbol.hpp"
33 #include "proton/timestamp.hpp"
34 #include "proton/value.hpp"
35
36 #include <proton/codec.h>
37
38 #include <algorithm>
39 #include <assert.h>
40
41 namespace proton {
42 namespace codec {
43
44 void encoder::check(long result) {
45 if (result < 0)
46 throw conversion_error(error_str(pn_data_error(pn_object()), result));
47 }
48
49
50 encoder::encoder(internal::value_base& v) : data(v.data()) {
51 clear();
52 }
53
54 bool encoder::encode(char* buffer, size_t& size) {
55 internal::state_guard sg(*this); // In case of error
56 ssize_t result = pn_data_encode(pn_object(), buffer, size);
57 if (result == PN_OVERFLOW) {
58 result = pn_data_encoded_size(pn_object());
59 if (result >= 0) {
60 size = size_t(result);
61 return false;
62 }
63 }
64 check(result);
65 size = size_t(result);
66 sg.cancel(); // Don't restore state, all is well.
67 pn_data_clear(pn_object());
68 return true;
69 }
70
71 void encoder::encode(std::string& s) {
72 s.resize(std::max(s.capacity(), size_t(1))); // Use full capacity, ensure not empty
73 size_t size = s.size();
74 assert(!s.empty());
75 if (!encode(&s[0], size)) {
76 s.resize(size);
77 assert(!s.empty());
78 encode(&s[0], size);
79 }
80 }
81
82 std::string encoder::encode() {
83 std::string s;
84 encode(s);
85 return s;
86 }
87
88 encoder& encoder::operator<<(const start& s) {
89 switch (s.type) {
90 case ARRAY: pn_data_put_array(pn_object(), s.is_described, pn_type_t(s.element)); break;
91 case MAP: pn_data_put_map(pn_object()); break;
92 case LIST: pn_data_put_list(pn_object()); break;
93 case DESCRIBED: pn_data_put_described(pn_object()); break;
94 default:
95 throw conversion_error(MSG("" << s.type << " is not a container type"));
96 }
97 pn_data_enter(pn_object());
98 return *this;
99 }
100
101 encoder& encoder::operator<<(const finish&) {
102 pn_data_exit(pn_object());
103 return *this;
104 }
105
106 namespace {
107
108 template <class T, class U> T coerce(const U &x) { return x; }
109 template <> pn_uuid_t coerce(const uuid& x) { pn_uuid_t y; byte_copy(y, x); return y; }
110 template <> pn_decimal32_t coerce(const decimal32 &x) { pn_decimal32_t y; byte_copy(y, x); return y; }
111 template <> pn_decimal64_t coerce(const decimal64 &x) { pn_decimal64_t y; byte_copy(y, x); return y; }
112 template <> pn_decimal128_t coerce(const decimal128 &x) { pn_decimal128_t y; byte_copy(y, x); return y; }
113
114 int pn_data_put_amqp_string(pn_data_t *d, const std::string& x) { return pn_data_put_string(d, pn_bytes(x)); }
115 int pn_data_put_amqp_binary(pn_data_t *d, const binary& x) { return pn_data_put_binary(d, pn_bytes(x)); }
116 int pn_data_put_amqp_symbol(pn_data_t *d, const symbol& x) { return pn_data_put_symbol(d, pn_bytes(x)); }
117 } // namespace
118
119 template <class T, class U>
120 encoder& encoder::insert(const T& x, int (*put)(pn_data_t*, U)) {
121 internal::state_guard sg(*this); // Save state in case of error.
122 check(put(pn_object(), coerce<U>(x)));
123 sg.cancel(); // Don't restore state, all is good.
124 return *this;
125 }
126
127 encoder& encoder::operator<<(bool x) { return insert(x, pn_data_put_bool); }
128 encoder& encoder::operator<<(uint8_t x) { return insert(x, pn_data_put_ubyte); }
129 encoder& encoder::operator<<(int8_t x) { return insert(x, pn_data_put_byte); }
130 encoder& encoder::operator<<(uint16_t x) { return insert(x, pn_data_put_ushort); }
131 encoder& encoder::operator<<(int16_t x) { return insert(x, pn_data_put_short); }
132 encoder& encoder::operator<<(uint32_t x) { return insert(x, pn_data_put_uint); }
133 encoder& encoder::operator<<(int32_t x) { return insert(x, pn_data_put_int); }
134 encoder& encoder::operator<<(wchar_t x) { return insert(x, pn_data_put_char); }
135 encoder& encoder::operator<<(uint64_t x) { return insert(x, pn_data_put_ulong); }
136 encoder& encoder::operator<<(int64_t x) { return insert(x, pn_data_put_long); }
137 encoder& encoder::operator<<(timestamp x) { return insert(x.milliseconds(), pn_data_put_timestamp); }
138 encoder& encoder::operator<<(float x) { return insert(x, pn_data_put_float); }
139 encoder& encoder::operator<<(double x) { return insert(x, pn_data_put_double); }
140 encoder& encoder::operator<<(decimal32 x) { return insert(x, pn_data_put_decimal32); }
141 encoder& encoder::operator<<(decimal64 x) { return insert(x, pn_data_put_decimal64); }
142 encoder& encoder::operator<<(decimal128 x) { return insert(x, pn_data_put_decimal128); }
143 encoder& encoder::operator<<(const uuid& x) { return insert(x, pn_data_put_uuid); }
144 encoder& encoder::operator<<(const std::string& x) { return insert(x, pn_data_put_amqp_string); }
145 encoder& encoder::operator<<(const symbol& x) { return insert(x, pn_data_put_amqp_symbol); }
146 encoder& encoder::operator<<(const binary& x) { return insert(x, pn_data_put_amqp_binary); }
147 encoder& encoder::operator<<(const null&) { pn_data_put_null(pn_object()); return *this; }
148 encoder& encoder::operator<<(decltype(nullptr)) { pn_data_put_null(pn_object()); return *this; }
149
150 encoder& encoder::operator<<(const scalar_base& x) { return insert(x.atom_, pn_data_put_atom); }
151
152 encoder& encoder::operator<<(const internal::value_base& x) {
153 data d = x.data_;
154 if (*this == d)
155 throw conversion_error("cannot insert into self");
156 if (!d || d.empty())
157 return *this << null();
158 d.rewind();
159 check(append(d));
160 return *this;
161 }
162
163 } // codec
164 } // proton