Qpid Proton C++  0.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
encoder.hpp
1 #ifndef ENCODER_H
2 #define ENCODER_H
3 
4 /*
5  * Licensed to the Apache Software Foundation (ASF) under one
6  * or more contributor license agreements. See the NOTICE file
7  * distributed with this work for additional information
8  * regarding copyright ownership. The ASF licenses this file
9  * to you under the Apache License, Version 2.0 (the
10  * "License"); you may not use this file except in compliance
11  * with the License. You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing,
16  * software distributed under the License is distributed on an
17  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18  * KIND, either express or implied. See the License for the
19  * specific language governing permissions and limitations
20  * under the License.
21  */
22 
25 
26 #include "proton/error.hpp"
27 #include "proton/types.hpp"
28 #include "proton/type_traits.hpp"
29 #include "proton/object.hpp"
30 #include <iosfwd>
31 
32 #ifndef PN_NO_CONTAINER_CONVERT
33 
34 #include <vector>
35 #include <deque>
36 #include <list>
37 #include <map>
38 
39 #if PN_HAS_CPP11
40 #include <array>
41 #include <forward_list>
42 #include <unordered_map>
43 #endif // PN_HAS_CPP11
44 
45 #endif // PN_NO_CONTAINER_CONVERT
46 
49 
50 struct pn_data_t;
51 
52 namespace proton {
53 
54 class scalar;
55 class data;
56 class message_id;
57 class annotation_key;
58 class value;
59 
60 template<class T, type_id A> struct cref {
61  typedef T cpp_type;
62  static const type_id type;
63 
64  cref(const T& v) : value(v) {}
65  const T& value;
66 };
67 
68 template <class T, type_id A> const type_id cref<T, A>::type = A;
69 
77 template <type_id A, class T> cref<T, A> as(const T& value) { return cref<T, A>(value); }
78 
118 class encoder : public object<pn_data_t> {
119  public:
120  encoder(pn_data_t* e) : object<pn_data_t>(e) {}
121 
130  PN_CPP_EXTERN bool encode(char* buffer, size_t& size);
131 
136  PN_CPP_EXTERN void encode(std::string&);
137 
139  PN_CPP_EXTERN std::string encode();
140 
141  PN_CPP_EXTERN class data data();
142 
146  friend PN_CPP_EXTERN encoder operator<<(encoder, amqp_null);
147  friend PN_CPP_EXTERN encoder operator<<(encoder, amqp_boolean);
148  friend PN_CPP_EXTERN encoder operator<<(encoder, amqp_ubyte);
149  friend PN_CPP_EXTERN encoder operator<<(encoder, amqp_byte);
150  friend PN_CPP_EXTERN encoder operator<<(encoder, amqp_ushort);
151  friend PN_CPP_EXTERN encoder operator<<(encoder, amqp_short);
152  friend PN_CPP_EXTERN encoder operator<<(encoder, amqp_uint);
153  friend PN_CPP_EXTERN encoder operator<<(encoder, amqp_int);
154  friend PN_CPP_EXTERN encoder operator<<(encoder, amqp_char);
155  friend PN_CPP_EXTERN encoder operator<<(encoder, amqp_ulong);
156  friend PN_CPP_EXTERN encoder operator<<(encoder, amqp_long);
157  friend PN_CPP_EXTERN encoder operator<<(encoder, amqp_timestamp);
158  friend PN_CPP_EXTERN encoder operator<<(encoder, amqp_float);
159  friend PN_CPP_EXTERN encoder operator<<(encoder, amqp_double);
160  friend PN_CPP_EXTERN encoder operator<<(encoder, amqp_decimal32);
161  friend PN_CPP_EXTERN encoder operator<<(encoder, amqp_decimal64);
162  friend PN_CPP_EXTERN encoder operator<<(encoder, amqp_decimal128);
163  friend PN_CPP_EXTERN encoder operator<<(encoder, amqp_uuid);
164  friend PN_CPP_EXTERN encoder operator<<(encoder, amqp_string);
165  friend PN_CPP_EXTERN encoder operator<<(encoder, amqp_symbol);
166  friend PN_CPP_EXTERN encoder operator<<(encoder, amqp_binary);
167  friend PN_CPP_EXTERN encoder operator<<(encoder, const message_id&);
168  friend PN_CPP_EXTERN encoder operator<<(encoder, const annotation_key&);
169  friend PN_CPP_EXTERN encoder operator<<(encoder, const value&);
170  friend PN_CPP_EXTERN encoder operator<<(encoder, const scalar&);
172 
183  friend PN_CPP_EXTERN encoder operator<<(encoder, const start&);
184 
186  friend PN_CPP_EXTERN encoder operator<<(encoder e, finish);
187 
188 
192  template <class T, type_id A> friend PN_CPP_EXTERN encoder operator<<(encoder, cref<T, A>);
193  template <class T> friend encoder operator<<(encoder, cref<T, ARRAY>);
194  template <class T> friend encoder operator<<(encoder, cref<T, LIST>);
195  template <class T> friend encoder operator<<(encoder, cref<T, MAP>);
196  // TODO aconway 2015-06-16: described values.
198 };
199 
200 // Need to disambiguate char* conversion to bool and std::string as amqp_string.
201 inline encoder operator<<(encoder e, char* s) { return e << amqp_string(s); }
202 inline encoder operator<<(encoder e, const char* s) { return e << amqp_string(s); }
203 inline encoder operator<<(encoder e, const std::string& s) { return e << amqp_string(s); }
204 
205 // operator << for integer types that are not covered by the standard overrides.
206 template <class T>
207 typename enable_if<is_unknown_integer<T>::value, encoder>::type operator<<(encoder e, T i) {
208  typename integer_type<sizeof(T), is_signed<T>::value>::type v = i;
209  return e << v; // Insert as a known integer type
210 }
211 
212 // TODO aconway 2015-06-16: described array insertion.
213 
214 template <class T> encoder operator<<(encoder e, cref<T, ARRAY> a) {
215  e << start::array(type_id_of<typename T::value_type>::value);
216  for (typename T::const_iterator i = a.value.begin(); i != a.value.end(); ++i)
217  e << *i;
218  e << finish();
219  return e;
220 }
221 
222 template <class T> encoder operator<<(encoder e, cref<T, LIST> l) {
223  e << start::list();
224  for (typename T::const_iterator i = l.value.begin(); i != l.value.end(); ++i)
225  e << *i;
226  e << finish();
227  return e;
228 }
229 
230 template <class T> encoder operator<<(encoder e, cref<T, MAP> m){
231  e << start::map();
232  for (typename T::const_iterator i = m.value.begin(); i != m.value.end(); ++i) {
233  e << i->first;
234  e << i->second;
235  }
236  e << finish();
237  return e;
238 }
239 
240 #ifndef PN_NO_CONTAINER_CONVERT
241 // Encode as ARRAY
242 template <class T, class A> encoder operator<<(encoder e, const std::vector<T, A>& v) { return e << as<ARRAY>(v); }
243 template <class T, class A> encoder operator<<(encoder e, const std::deque<T, A>& v) { return e << as<ARRAY>(v); }
244 template <class T, class A> encoder operator<<(encoder e, const std::list<T, A>& v) { return e << as<ARRAY>(v); }
245 
246 // Encode as LIST
247 template <class A> encoder operator<<(encoder e, const std::vector<value, A>& v) { return e << as<LIST>(v); }
248 template <class A> encoder operator<<(encoder e, const std::deque<value, A>& v) { return e << as<LIST>(v); }
249 template <class A> encoder operator<<(encoder e, const std::list<value, A>& v) { return e << as<LIST>(v); }
250 
251 // Encode as MAP
252 template <class K, class T, class C, class A> encoder operator<<(encoder e, const std::map<K, T, C, A>& v) { return e << as<MAP>(v); }
253 
254 #if PN_HAS_CPP11
255 
256 // Encode as ARRAY.
257 template <class T, class A> encoder operator<<(encoder e, const std::forward_list<T, A>& v) { return e << as<ARRAY>(v); }
258 template <class T, std::size_t N> encoder operator<<(encoder e, const std::array<T, N>& v) { return e << as<ARRAY>(v); }
259 
260 // Encode as LIST.
261 template <class A> encoder operator<<(encoder e, const std::forward_list<value, A>& v) { return e << as<LIST>(v); }
262 template <std::size_t N> encoder operator<<(encoder e, const std::array<value, N>& v) { return e << as<LIST>(v); }
263 
264 // Encode as map.
265 template <class K, class T, class C, class A> encoder operator<<(encoder e, const std::unordered_map<K, T, C, A>& v) { return e << as<MAP>(v); }
266 
267 #endif // PN_HAS_CPP11
268 
269 #endif // PN_NO_CONTAINER_CONVERT
270 
271 }
272 
274 
275 #endif // ENCODER_H
Defines C++ types representing AMQP types.