Qpid Proton C++  0.13.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 "./data.hpp"
26 #include "../internal/type_traits.hpp"
27 #include "../types_fwd.hpp"
28 
29 #include <utility>
30 
31 namespace proton {
32 
33 class annotation_key;
34 class message_id;
35 class scalar;
36 class value;
37 
38 namespace internal {
39 class value_base;
40 }
41 
42 namespace codec {
43 
50 class decoder : public data {
51  public:
55  explicit decoder(const data& d, bool exact=false) : data(d), exact_(exact) {}
56 
59  PN_CPP_EXTERN explicit decoder(const internal::value_base&, bool exact=false);
60 
63  PN_CPP_EXTERN void decode(const char* buffer, size_t size);
64 
67  PN_CPP_EXTERN void decode(const std::string&);
68 
70  PN_CPP_EXTERN bool more();
71 
77  PN_CPP_EXTERN type_id next_type();
78 
85  PN_CPP_EXTERN decoder& operator>>(bool&);
86  PN_CPP_EXTERN decoder& operator>>(uint8_t&);
87  PN_CPP_EXTERN decoder& operator>>(int8_t&);
88  PN_CPP_EXTERN decoder& operator>>(uint16_t&);
89  PN_CPP_EXTERN decoder& operator>>(int16_t&);
90  PN_CPP_EXTERN decoder& operator>>(uint32_t&);
91  PN_CPP_EXTERN decoder& operator>>(int32_t&);
92  PN_CPP_EXTERN decoder& operator>>(wchar_t&);
93  PN_CPP_EXTERN decoder& operator>>(uint64_t&);
94  PN_CPP_EXTERN decoder& operator>>(int64_t&);
95  PN_CPP_EXTERN decoder& operator>>(timestamp&);
96  PN_CPP_EXTERN decoder& operator>>(float&);
97  PN_CPP_EXTERN decoder& operator>>(double&);
98  PN_CPP_EXTERN decoder& operator>>(decimal32&);
99  PN_CPP_EXTERN decoder& operator>>(decimal64&);
100  PN_CPP_EXTERN decoder& operator>>(decimal128&);
101  PN_CPP_EXTERN decoder& operator>>(uuid&);
102  PN_CPP_EXTERN decoder& operator>>(std::string&);
103  PN_CPP_EXTERN decoder& operator>>(symbol&);
104  PN_CPP_EXTERN decoder& operator>>(binary&);
105  PN_CPP_EXTERN decoder& operator>>(message_id&);
106  PN_CPP_EXTERN decoder& operator>>(annotation_key&);
107  PN_CPP_EXTERN decoder& operator>>(scalar&);
108  PN_CPP_EXTERN decoder& operator>>(internal::value_base&);
109  PN_CPP_EXTERN decoder& operator>>(null&);
111 
116  PN_CPP_EXTERN decoder& operator>>(start&);
117 
120  PN_CPP_EXTERN decoder& operator>>(const finish&);
121 
123  template <class T> struct sequence_ref { T& ref; sequence_ref(T& r) : ref(r) {} };
124  template <class T> struct associative_ref { T& ref; associative_ref(T& r) : ref(r) {} };
125  template <class T> struct pair_sequence_ref { T& ref; pair_sequence_ref(T& r) : ref(r) {} };
126 
127  template <class T> static sequence_ref<T> sequence(T& x) { return sequence_ref<T>(x); }
128  template <class T> static associative_ref<T> associative(T& x) { return associative_ref<T>(x); }
129  template <class T> static pair_sequence_ref<T> pair_sequence(T& x) { return pair_sequence_ref<T>(x); }
131 
135  template <class T> decoder& operator>>(sequence_ref<T> r) {
136  start s;
137  *this >> s;
138  if (s.is_described) next();
139  r.ref.resize(s.size);
140  for (typename T::iterator i = r.ref.begin(); i != r.ref.end(); ++i)
141  *this >> *i;
142  return *this;
143  }
144 
146  template <class T> decoder& operator>>(associative_ref<T> r) {
147  using namespace internal;
148  start s;
149  *this >> s;
150  assert_type_equal(MAP, s.type);
151  r.ref.clear();
152  for (size_t i = 0; i < s.size/2; ++i) {
153  typename remove_const<typename T::key_type>::type k;
154  typename remove_const<typename T::mapped_type>::type v;
155  *this >> k >> v;
156  r.ref[k] = v;
157  }
158  return *this;
159  }
160 
163  template <class T> decoder& operator>>(pair_sequence_ref<T> r) {
164  using namespace internal;
165  start s;
166  *this >> s;
167  assert_type_equal(MAP, s.type);
168  r.ref.clear();
169  for (size_t i = 0; i < s.size/2; ++i) {
170  typedef typename T::value_type value_type;
171  typename remove_const<typename value_type::first_type>::type k;
172  typename remove_const<typename value_type::second_type>::type v;
173  *this >> k >> v;
174  r.ref.push_back(value_type(k, v));
175  }
176  return *this;
177  }
178 
179  private:
180  type_id pre_get();
181  template <class T, class U> decoder& extract(T& x, U (*get)(pn_data_t*));
182  bool exact_;
183 
184  friend class message;
185 };
186 
189 template<class T> T get(decoder& d) {
190  assert_type_equal(internal::type_id_of<T>::value, d.next_type());
191  T x;
192  d >> x;
193  return x;
194 }
196 
199 template <class T> typename internal::enable_if<internal::is_unknown_integer<T>::value, decoder&>::type
200 operator>>(decoder& d, T& i) {
201  using namespace internal;
202  typename integer_type<sizeof(T), is_signed<T>::value>::type v;
203  d >> v; // Extract as a known integer type
204  i = v; // C++ conversion to the target type.
205  return d;
206 }
207 
208 } // codec
209 } // proton
210 
211 #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:49
Experimental - Start encoding a complex type.
Definition: data.hpp:105
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:135
decoder & operator>>(associative_ref< T > r)
Extract an AMQP MAP to a C++ associative container.
Definition: decoder.hpp:146
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:163
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: data.hpp:128
decoder(const data &d, bool exact=false)
Wrap a Proton C data object.
Definition: decoder.hpp:55
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:50
An AMQP message ID.
Definition: message_id.hpp:42
bool more()
Return true if there are more value to extract at the current level.