Debian Codegraph @

cpp/include/proton/codec/decoder.hpp

raw
1 #ifndef PROTON_CODEC_DECODER_HPP
2 #define PROTON_CODEC_DECODER_HPP
3
4 /*
5 *
6 * Licensed to the Apache Software Foundation (ASF) under one
7 * or more contributor license agreements. See the NOTICE file
8 * distributed with this work for additional information
9 * regarding copyright ownership. The ASF licenses this file
10 * to you under the Apache License, Version 2.0 (the
11 * "License"); you may not use this file except in compliance
12 * with the License. You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing,
17 * software distributed under the License is distributed on an
18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 * KIND, either express or implied. See the License for the
20 * specific language governing permissions and limitations
21 * under the License.
22 *
23 */
24
25 #include "../internal/data.hpp"
26 #include "../internal/type_traits.hpp"
27 #include "../types_fwd.hpp"
28 #include "./common.hpp"
29
30 #include <proton/type_compat.h>
31
32 #include <utility>
33
34 /// @file
35 /// @copybrief proton::codec::decoder
36
37 namespace proton {
38
39 class annotation_key;
40 class message_id;
41 class scalar;
42 class value;
43
44 namespace internal {
45 class value_base;
46 }
47
48 namespace codec {
49
50 /// **Unsettled API** - A stream-like decoder from AMQP bytes to C++
51 /// values.
52 ///
53 /// For internal use only.
54 ///
55 /// @see @ref types_page for the recommended ways to manage AMQP data
56 class decoder : public internal::data {
57 public:
58 /// Wrap a Proton C data object. The exact flag if set means
59 /// decode only when there is an exact match between the AMQP and
60 /// C++ type. If not set then perform automatic conversions.
61 explicit decoder(const data& d, bool exact=false) : data(d), exact_(exact) {}
62
63 /// Attach decoder to a proton::value. The decoder is rewound to
64 /// the start of the data.
65 PN_CPP_EXTERN explicit decoder(const internal::value_base&, bool exact=false);
66
67 /// Decode AMQP data from a buffer and add it to the end of the
68 /// decoders stream.
69 PN_CPP_EXTERN void decode(const char* buffer, size_t size);
70
71 /// Decode AMQP data from a std::string and add it to the end of
72 /// the decoders stream.
73 PN_CPP_EXTERN void decode(const std::string&);
74
75 /// Return true if there are more value to extract at the current level.
76 PN_CPP_EXTERN bool more();
77
78 /// Get the type of the next value that will be read by
79 /// operator>>.
80 ///
81 /// @throw conversion_error if no more values. @see
82 /// decoder::more().
83 PN_CPP_EXTERN type_id next_type();
84
85 /// @name Extract built-in types
86 ///
87 /// @throw conversion_error if the decoder is empty or has an
88 /// incompatible type.
89 ///
90 /// @{
91 PN_CPP_EXTERN decoder& operator>>(bool&);
92 PN_CPP_EXTERN decoder& operator>>(uint8_t&);
93 PN_CPP_EXTERN decoder& operator>>(int8_t&);
94 PN_CPP_EXTERN decoder& operator>>(uint16_t&);
95 PN_CPP_EXTERN decoder& operator>>(int16_t&);
96 PN_CPP_EXTERN decoder& operator>>(uint32_t&);
97 PN_CPP_EXTERN decoder& operator>>(int32_t&);
98 PN_CPP_EXTERN decoder& operator>>(wchar_t&);
99 PN_CPP_EXTERN decoder& operator>>(uint64_t&);
100 PN_CPP_EXTERN decoder& operator>>(int64_t&);
101 PN_CPP_EXTERN decoder& operator>>(timestamp&);
102 PN_CPP_EXTERN decoder& operator>>(float&);
103 PN_CPP_EXTERN decoder& operator>>(double&);
104 PN_CPP_EXTERN decoder& operator>>(decimal32&);
105 PN_CPP_EXTERN decoder& operator>>(decimal64&);
106 PN_CPP_EXTERN decoder& operator>>(decimal128&);
107 PN_CPP_EXTERN decoder& operator>>(uuid&);
108 PN_CPP_EXTERN decoder& operator>>(std::string&);
109 PN_CPP_EXTERN decoder& operator>>(symbol&);
110 PN_CPP_EXTERN decoder& operator>>(binary&);
111 PN_CPP_EXTERN decoder& operator>>(message_id&);
112 PN_CPP_EXTERN decoder& operator>>(annotation_key&);
113 PN_CPP_EXTERN decoder& operator>>(scalar&);
114 PN_CPP_EXTERN decoder& operator>>(internal::value_base&);
115 PN_CPP_EXTERN decoder& operator>>(null&);
116 PN_CPP_EXTERN decoder& operator>>(decltype(nullptr)&);
117 ///@}
118
119 /// Start decoding a container type, such as an ARRAY, LIST or
120 /// MAP. This "enters" the container, more() will return false at
121 /// the end of the container. Call finish() to "exit" the
122 /// container and move on to the next value.
123 PN_CPP_EXTERN decoder& operator>>(start&);
124
125 /// Finish decoding a container type, and move on to the next
126 /// value in the stream.
127 PN_CPP_EXTERN decoder& operator>>(const finish&);
128
129 /// @cond INTERNAL
130 template <class T> struct sequence_ref { T& ref; sequence_ref(T& r) : ref(r) {} };
131 template <class T> struct associative_ref { T& ref; associative_ref(T& r) : ref(r) {} };
132 template <class T> struct pair_sequence_ref { T& ref; pair_sequence_ref(T& r) : ref(r) {} };
133
134 template <class T> static sequence_ref<T> sequence(T& x) { return sequence_ref<T>(x); }
135 template <class T> static associative_ref<T> associative(T& x) { return associative_ref<T>(x); }
136 template <class T> static pair_sequence_ref<T> pair_sequence(T& x) { return pair_sequence_ref<T>(x); }
137 /// @endcond
138
139 /// Extract any AMQP sequence (ARRAY, LIST or MAP) to a C++
140 /// sequence container of T if the elements types are convertible
141 /// to T. A MAP is extracted as `[key1, value1, key2, value2...]`.
142 template <class T> decoder& operator>>(sequence_ref<T> r) {
143 start s;
144 *this >> s;
145 if (s.is_described) next();
146 r.ref.resize(s.size);
147 for (typename T::iterator i = r.ref.begin(); i != r.ref.end(); ++i)
148 *this >> *i;
149 return *this;
150 }
151
152 /// Extract an AMQP MAP to a C++ associative container
153 template <class T> decoder& operator>>(associative_ref<T> r) {
154 using namespace internal;
155 start s;
156 *this >> s;
157 assert_type_equal(MAP, s.type);
158 r.ref.clear();
159 for (size_t i = 0; i < s.size/2; ++i) {
160 typename std::remove_const<typename T::key_type>::type k;
161 typename std::remove_const<typename T::mapped_type>::type v;
162 *this >> k >> v;
163 r.ref[k] = v;
164 }
165 return *this;
166 }
167
168 /// Extract an AMQP MAP to a C++ push_back sequence of pairs
169 /// preserving encoded order.
170 template <class T> decoder& operator>>(pair_sequence_ref<T> r) {
171 using namespace internal;
172 start s;
173 *this >> s;
174 assert_type_equal(MAP, s.type);
175 r.ref.clear();
176 for (size_t i = 0; i < s.size/2; ++i) {
177 typedef typename T::value_type value_type;
178 typename std::remove_const<typename value_type::first_type>::type k;
179 typename std::remove_const<typename value_type::second_type>::type v;
180 *this >> k >> v;
181 r.ref.push_back(value_type(k, v));
182 }
183 return *this;
184 }
185
186 private:
187 type_id pre_get();
188 template <class T, class U> decoder& extract(T& x, U (*get)(pn_data_t*));
189 bool exact_;
190
191 friend class message;
192 };
193
194 /// @cond INTERNAL
195 /// XXX Document this
196 template<class T> T get(decoder& d) {
197 assert_type_equal(internal::type_id_of<T>::value, d.next_type());
198 T x;
199 d >> x;
200 return x;
201 }
202 /// @endcond
203
204 /// operator>> for integer types that are not covered by the standard
205 /// overrides.
206 template <class T> typename std::enable_if<internal::is_unknown_integer<T>::value, decoder&>::type
207 operator>>(decoder& d, T& i) {
208 using namespace internal;
209 typename integer_type<sizeof(T), std::is_signed<T>::value>::type v;
210 d >> v; // Extract as a known integer type
211 i = v; // C++ conversion to the target type.
212 return d;
213 }
214
215 } // codec
216 } // proton
217
218 #endif // PROTON_CODEC_DECODER_HPP