Qpid Proton C++ API  0.33.0
decoder.hpp
Go to the documentation of this file.
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 
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 
56 class decoder : public internal::data {
57  public:
61  explicit decoder(const data& d, bool exact=false) : data(d), exact_(exact) {}
62 
65  PN_CPP_EXTERN explicit decoder(const internal::value_base&, bool exact=false);
66 
69  PN_CPP_EXTERN void decode(const char* buffer, size_t size);
70 
73  PN_CPP_EXTERN void decode(const std::string&);
74 
76  PN_CPP_EXTERN bool more();
77 
83  PN_CPP_EXTERN type_id next_type();
84 
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 #if PN_CPP_HAS_NULLPTR
117  PN_CPP_EXTERN decoder& operator>>(decltype(nullptr)&);
118 #endif
119 
125  PN_CPP_EXTERN decoder& operator>>(start&);
126 
129  PN_CPP_EXTERN decoder& operator>>(const finish&);
130 
132  template <class T> struct sequence_ref { T& ref; sequence_ref(T& r) : ref(r) {} };
133  template <class T> struct associative_ref { T& ref; associative_ref(T& r) : ref(r) {} };
134  template <class T> struct pair_sequence_ref { T& ref; pair_sequence_ref(T& r) : ref(r) {} };
135 
136  template <class T> static sequence_ref<T> sequence(T& x) { return sequence_ref<T>(x); }
137  template <class T> static associative_ref<T> associative(T& x) { return associative_ref<T>(x); }
138  template <class T> static pair_sequence_ref<T> pair_sequence(T& x) { return pair_sequence_ref<T>(x); }
140 
144  template <class T> decoder& operator>>(sequence_ref<T> r) {
145  start s;
146  *this >> s;
147  if (s.is_described) next();
148  r.ref.resize(s.size);
149  for (typename T::iterator i = r.ref.begin(); i != r.ref.end(); ++i)
150  *this >> *i;
151  return *this;
152  }
153 
155  template <class T> decoder& operator>>(associative_ref<T> r) {
156  using namespace internal;
157  start s;
158  *this >> s;
159  assert_type_equal(MAP, s.type);
160  r.ref.clear();
161  for (size_t i = 0; i < s.size/2; ++i) {
162  typename remove_const<typename T::key_type>::type k;
163  typename remove_const<typename T::mapped_type>::type v;
164  *this >> k >> v;
165  r.ref[k] = v;
166  }
167  return *this;
168  }
169 
172  template <class T> decoder& operator>>(pair_sequence_ref<T> r) {
173  using namespace internal;
174  start s;
175  *this >> s;
176  assert_type_equal(MAP, s.type);
177  r.ref.clear();
178  for (size_t i = 0; i < s.size/2; ++i) {
179  typedef typename T::value_type value_type;
180  typename remove_const<typename value_type::first_type>::type k;
181  typename remove_const<typename value_type::second_type>::type v;
182  *this >> k >> v;
183  r.ref.push_back(value_type(k, v));
184  }
185  return *this;
186  }
187 
188  private:
189  type_id pre_get();
190  template <class T, class U> decoder& extract(T& x, U (*get)(pn_data_t*));
191  bool exact_;
192 
193  friend class message;
194 };
195 
198 template<class T> T get(decoder& d) {
199  assert_type_equal(internal::type_id_of<T>::value, d.next_type());
200  T x;
201  d >> x;
202  return x;
203 }
205 
208 template <class T> typename internal::enable_if<internal::is_unknown_integer<T>::value, decoder&>::type
209 operator>>(decoder& d, T& i) {
210  using namespace internal;
211  typename integer_type<sizeof(T), is_signed<T>::value>::type v;
212  d >> v; // Extract as a known integer type
213  i = v; // C++ conversion to the target type.
214  return d;
215 }
216 
217 } // codec
218 } // proton
219 
220 #endif // PROTON_CODEC_DECODER_HPP
proton::codec::decoder::decoder
decoder(const internal::value_base &, bool exact=false)
Attach decoder to a proton::value.
proton::scalar
A holder for an instance of any scalar AMQP type.
Definition: scalar.hpp:37
proton::decimal32
A 32-bit decimal floating-point value.
Definition: decimal.hpp:46
proton::codec::decoder::operator>>
decoder & operator>>(sequence_ref< T > r)
Extract any AMQP sequence (ARRAY, LIST or MAP) to a C++ sequence container of T if the elements types...
Definition: decoder.hpp:144
proton::codec::decoder::more
bool more()
Return true if there are more value to extract at the current level.
common.hpp
Unsettled API - Shared codec functions.
proton::decimal64
A 64-bit decimal floating-point value.
Definition: decimal.hpp:49
proton::MAP
@ MAP
A sequence of key-value pairs.
Definition: type_id.hpp:62
proton::message_id
An AMQP message ID.
Definition: message_id.hpp:47
proton::codec::decoder::operator>>
decoder & operator>>(pair_sequence_ref< T > r)
Extract an AMQP MAP to a C++ push_back sequence of pairs preserving encoded order.
Definition: decoder.hpp:172
proton::type_id
type_id
An identifier for AMQP types.
Definition: type_id.hpp:37
proton::codec::decoder::operator>>
decoder & operator>>(const finish &)
Finish decoding a container type, and move on to the next value in the stream.
proton::symbol
A string that represents the AMQP symbol type.
Definition: symbol.hpp:35
proton::codec::decoder::next_type
type_id next_type()
Get the type of the next value that will be read by operator>>.
proton::get
T get(const scalar &s)
Get a contained value of type T.
Definition: scalar.hpp:60
proton::codec::decoder
Unsettled API - A stream-like decoder from AMQP bytes to C++ values.
Definition: decoder.hpp:56
proton::codec::decoder::operator>>
decoder & operator>>(start &)
Start decoding a container type, such as an ARRAY, LIST or MAP.
proton::codec::start
Unsettled API - Start encoding a complex type.
Definition: common.hpp:34
proton::codec::operator>>
internal::enable_if< internal::is_unknown_integer< T >::value, decoder & >::type operator>>(decoder &d, T &i)
operator>> for integer types that are not covered by the standard overrides.
Definition: decoder.hpp:209
proton::codec::decoder::decode
void decode(const char *buffer, size_t size)
Decode AMQP data from a buffer and add it to the end of the decoders stream.
proton::uuid
A 16-byte universally unique identifier.
Definition: uuid.hpp:37
proton
The main Proton namespace.
Definition: annotation_key.hpp:33
proton::codec::finish
Unsettled API - Finish inserting or extracting a complex type.
Definition: common.hpp:57
proton::codec::decoder::operator>>
decoder & operator>>(associative_ref< T > r)
Extract an AMQP MAP to a C++ associative container.
Definition: decoder.hpp:155
proton::decimal128
A 128-bit decimal floating-point value.
Definition: decimal.hpp:52
proton::codec::decoder::decode
void decode(const std::string &)
Decode AMQP data from a std::string and add it to the end of the decoders stream.
proton::binary
Arbitrary binary data.
Definition: binary.hpp:40
proton::timestamp
A 64-bit timestamp in milliseconds since the Unix epoch.
Definition: timestamp.hpp:35
proton::message
An AMQP message.
Definition: message.hpp:50
proton::assert_type_equal
void assert_type_equal(type_id want, type_id got)
Throw a conversion_error if want != got with a message including the names of the types.
proton::codec::decoder::decoder
decoder(const data &d, bool exact=false)
Wrap a Proton C data object.
Definition: decoder.hpp:61
proton::annotation_key
A key for use with AMQP annotation maps.
Definition: annotation_key.hpp:38