Qpid Proton C++  0.14.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
decoder.hpp
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 <utility>
31 
32 namespace proton {
33 
34 class annotation_key;
35 class message_id;
36 class scalar;
37 class value;
38 
39 namespace internal {
40 class value_base;
41 }
42 
43 namespace codec {
44 
51 class decoder : public internal::data {
52  public:
56  explicit decoder(const data& d, bool exact=false) : data(d), exact_(exact) {}
57 
60  PN_CPP_EXTERN explicit decoder(const internal::value_base&, bool exact=false);
61 
64  PN_CPP_EXTERN void decode(const char* buffer, size_t size);
65 
68  PN_CPP_EXTERN void decode(const std::string&);
69 
71  PN_CPP_EXTERN bool more();
72 
78  PN_CPP_EXTERN type_id next_type();
79 
86  PN_CPP_EXTERN decoder& operator>>(bool&);
87  PN_CPP_EXTERN decoder& operator>>(uint8_t&);
88  PN_CPP_EXTERN decoder& operator>>(int8_t&);
89  PN_CPP_EXTERN decoder& operator>>(uint16_t&);
90  PN_CPP_EXTERN decoder& operator>>(int16_t&);
91  PN_CPP_EXTERN decoder& operator>>(uint32_t&);
92  PN_CPP_EXTERN decoder& operator>>(int32_t&);
93  PN_CPP_EXTERN decoder& operator>>(wchar_t&);
94  PN_CPP_EXTERN decoder& operator>>(uint64_t&);
95  PN_CPP_EXTERN decoder& operator>>(int64_t&);
96  PN_CPP_EXTERN decoder& operator>>(timestamp&);
97  PN_CPP_EXTERN decoder& operator>>(float&);
98  PN_CPP_EXTERN decoder& operator>>(double&);
99  PN_CPP_EXTERN decoder& operator>>(decimal32&);
100  PN_CPP_EXTERN decoder& operator>>(decimal64&);
101  PN_CPP_EXTERN decoder& operator>>(decimal128&);
102  PN_CPP_EXTERN decoder& operator>>(uuid&);
103  PN_CPP_EXTERN decoder& operator>>(std::string&);
104  PN_CPP_EXTERN decoder& operator>>(symbol&);
105  PN_CPP_EXTERN decoder& operator>>(binary&);
106  PN_CPP_EXTERN decoder& operator>>(message_id&);
107  PN_CPP_EXTERN decoder& operator>>(annotation_key&);
108  PN_CPP_EXTERN decoder& operator>>(scalar&);
109  PN_CPP_EXTERN decoder& operator>>(internal::value_base&);
110  PN_CPP_EXTERN decoder& operator>>(null&);
112 
117  PN_CPP_EXTERN decoder& operator>>(start&);
118 
121  PN_CPP_EXTERN decoder& operator>>(const finish&);
122 
124  template <class T> struct sequence_ref { T& ref; sequence_ref(T& r) : ref(r) {} };
125  template <class T> struct associative_ref { T& ref; associative_ref(T& r) : ref(r) {} };
126  template <class T> struct pair_sequence_ref { T& ref; pair_sequence_ref(T& r) : ref(r) {} };
127 
128  template <class T> static sequence_ref<T> sequence(T& x) { return sequence_ref<T>(x); }
129  template <class T> static associative_ref<T> associative(T& x) { return associative_ref<T>(x); }
130  template <class T> static pair_sequence_ref<T> pair_sequence(T& x) { return pair_sequence_ref<T>(x); }
132 
136  template <class T> decoder& operator>>(sequence_ref<T> r) {
137  start s;
138  *this >> s;
139  if (s.is_described) next();
140  r.ref.resize(s.size);
141  for (typename T::iterator i = r.ref.begin(); i != r.ref.end(); ++i)
142  *this >> *i;
143  return *this;
144  }
145 
147  template <class T> decoder& operator>>(associative_ref<T> r) {
148  using namespace internal;
149  start s;
150  *this >> s;
151  assert_type_equal(MAP, s.type);
152  r.ref.clear();
153  for (size_t i = 0; i < s.size/2; ++i) {
154  typename remove_const<typename T::key_type>::type k;
155  typename remove_const<typename T::mapped_type>::type v;
156  *this >> k >> v;
157  r.ref[k] = v;
158  }
159  return *this;
160  }
161 
164  template <class T> decoder& operator>>(pair_sequence_ref<T> r) {
165  using namespace internal;
166  start s;
167  *this >> s;
168  assert_type_equal(MAP, s.type);
169  r.ref.clear();
170  for (size_t i = 0; i < s.size/2; ++i) {
171  typedef typename T::value_type value_type;
172  typename remove_const<typename value_type::first_type>::type k;
173  typename remove_const<typename value_type::second_type>::type v;
174  *this >> k >> v;
175  r.ref.push_back(value_type(k, v));
176  }
177  return *this;
178  }
179 
180  private:
181  type_id pre_get();
182  template <class T, class U> decoder& extract(T& x, U (*get)(pn_data_t*));
183  bool exact_;
184 
185  friend class message;
186 };
187 
190 template<class T> T get(decoder& d) {
191  assert_type_equal(internal::type_id_of<T>::value, d.next_type());
192  T x;
193  d >> x;
194  return x;
195 }
197 
200 template <class T> typename internal::enable_if<internal::is_unknown_integer<T>::value, decoder&>::type
201 operator>>(decoder& d, T& i) {
202  using namespace internal;
203  typename integer_type<sizeof(T), is_signed<T>::value>::type v;
204  d >> v; // Extract as a known integer type
205  i = v; // C++ conversion to the target type.
206  return d;
207 }
208 
209 } // codec
210 } // proton
211 
212 #endif // PROTON_CODEC_DECODER_HPP
A holder for an instance of any scalar AMQP type.
Definition: scalar.hpp:35
An AMQP message.
Definition: message.hpp:51
Experimental - Start encoding a complex type.
Definition: common.hpp:31
A key for use with AMQP annotation maps.
Definition: annotation_key.hpp:33
A sequence of key-value pairs.
Definition: type_id.hpp:63
A 16-byte universally unique identifier.
Definition: uuid.hpp:34
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:136
decoder & operator>>(associative_ref< T > r)
Extract an AMQP MAP to a C++ associative container.
Definition: decoder.hpp:147
64-bit decimal floating point.
Definition: decimal.hpp:51
A std::string that represents the AMQP symbol type.
Definition: symbol.hpp:30
Arbitrary binary data.
Definition: binary.hpp:34
128-bit decimal floating point.
Definition: decimal.hpp:54
type_id
An identifier for AMQP types.
Definition: type_id.hpp:38
32-bit decimal floating point.
Definition: decimal.hpp:48
A 64-bit timestamp in milliseconds since the Unix epoch.
Definition: timestamp.hpp:30
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:164
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...
Experimental - Finish inserting or extracting a complex type.
Definition: common.hpp:54
decoder(const data &d, bool exact=false)
Wrap a Proton C data object.
Definition: decoder.hpp:56
type_id next_type()
Get the type of the next value that will be read by operator&gt;&gt;.
void decode(const char *buffer, size_t size)
Decode AMQP data from a buffer and add it to the end of the decoders stream.
Experimental - Stream-like decoder from AMQP bytes to C++ values.
Definition: decoder.hpp:51
An AMQP message ID.
Definition: message_id.hpp:42
bool more()
Return true if there are more value to extract at the current level.