Qpid Proton C++ API  0.32.0
encoder.hpp
Go to the documentation of this file.
1 #ifndef PROTON_CODEC_ENCODER_HPP
2 #define PROTON_CODEC_ENCODER_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 
34 
35 namespace proton {
36 class scalar_base;
37 
38 namespace internal{
39 class value_base;
40 }
41 
42 namespace codec {
43 
50 class encoder : public internal::data {
51  public:
53  explicit encoder(const data& d) : data(d) {}
54 
56  PN_CPP_EXTERN explicit encoder(internal::value_base& v);
57 
66  PN_CPP_EXTERN bool encode(char* buffer, size_t& size);
67 
70  PN_CPP_EXTERN void encode(std::string&);
71 
74  PN_CPP_EXTERN std::string encode();
75 
78  PN_CPP_EXTERN encoder& operator<<(bool);
79  PN_CPP_EXTERN encoder& operator<<(uint8_t);
80  PN_CPP_EXTERN encoder& operator<<(int8_t);
81  PN_CPP_EXTERN encoder& operator<<(uint16_t);
82  PN_CPP_EXTERN encoder& operator<<(int16_t);
83  PN_CPP_EXTERN encoder& operator<<(uint32_t);
84  PN_CPP_EXTERN encoder& operator<<(int32_t);
85  PN_CPP_EXTERN encoder& operator<<(wchar_t);
86  PN_CPP_EXTERN encoder& operator<<(uint64_t);
87  PN_CPP_EXTERN encoder& operator<<(int64_t);
88  PN_CPP_EXTERN encoder& operator<<(timestamp);
89  PN_CPP_EXTERN encoder& operator<<(float);
90  PN_CPP_EXTERN encoder& operator<<(double);
91  PN_CPP_EXTERN encoder& operator<<(decimal32);
92  PN_CPP_EXTERN encoder& operator<<(decimal64);
93  PN_CPP_EXTERN encoder& operator<<(decimal128);
94  PN_CPP_EXTERN encoder& operator<<(const uuid&);
95  PN_CPP_EXTERN encoder& operator<<(const std::string&);
96  PN_CPP_EXTERN encoder& operator<<(const symbol&);
97  PN_CPP_EXTERN encoder& operator<<(const binary&);
98  PN_CPP_EXTERN encoder& operator<<(const scalar_base&);
99  PN_CPP_EXTERN encoder& operator<<(const null&);
100 #if PN_CPP_HAS_NULLPTR
101  PN_CPP_EXTERN encoder& operator<<(decltype(nullptr));
102 #endif
103 
109  PN_CPP_EXTERN encoder& operator<<(const internal::value_base&);
110 
112  PN_CPP_EXTERN encoder& operator<<(const start&);
113 
115  PN_CPP_EXTERN encoder& operator<<(const finish&);
116 
118 
119  // Undefined template to prevent pointers being implicitly converted to bool.
120  template <class T> void* operator<<(const T*);
121 
122  template <class T> struct list_cref { T& ref; list_cref(T& r) : ref(r) {} };
123  template <class T> struct map_cref { T& ref; map_cref(T& r) : ref(r) {} };
124 
125  template <class T> struct array_cref {
126  start array_start;
127  T& ref;
128  array_cref(T& r, type_id el, bool described) : array_start(ARRAY, el, described), ref(r) {}
129  };
130 
131  template <class T> static list_cref<T> list(T& x) { return list_cref<T>(x); }
132  template <class T> static map_cref<T> map(T& x) { return map_cref<T>(x); }
133  template <class T> static array_cref<T> array(T& x, type_id element, bool described=false) {
134  return array_cref<T>(x, element, described);
135  }
136 
137  template <class T> encoder& operator<<(const map_cref<T>& x) {
138  internal::state_guard sg(*this);
139  *this << start::map();
140  for (typename T::const_iterator i = x.ref.begin(); i != x.ref.end(); ++i)
141  *this << i->first << i->second;
142  *this << finish();
143  return *this;
144  }
145 
146  template <class T> encoder& operator<<(const list_cref<T>& x) {
147  internal::state_guard sg(*this);
148  *this << start::list();
149  for (typename T::const_iterator i = x.ref.begin(); i != x.ref.end(); ++i)
150  *this << *i;
151  *this << finish();
152  return *this;
153  }
154 
155  template <class T> encoder& operator<<(const array_cref<T>& x) {
156  internal::state_guard sg(*this);
157  *this << x.array_start;
158  for (typename T::const_iterator i = x.ref.begin(); i != x.ref.end(); ++i)
159  *this << *i;
160  *this << finish();
161  return *this;
162  }
164 
165  private:
166  template<class T, class U> encoder& insert(const T& x, int (*put)(pn_data_t*, U));
167  void check(long result);
168 };
169 
171 inline encoder& operator<<(encoder& e, const char* s) { return e << std::string(s); }
172 
174 template <class T> typename internal::enable_if<internal::is_unknown_integer<T>::value, encoder&>::type
175 operator<<(encoder& e, T i) {
176  using namespace internal;
177  return e << static_cast<typename integer_type<sizeof(T), is_signed<T>::value>::type>(i);
178 }
179 
181 
182 namespace is_encodable_impl { // Protect the world from fallback operator<<
183 
184 using namespace internal;
185 
186 sfinae::no operator<<(encoder const&, const sfinae::any_t &); // Fallback
187 
188 template<typename T> struct is_encodable : public sfinae {
189  static yes test(encoder&);
190  static no test(...); // Failed test, no match.
191  static encoder* e;
192  static const T* t;
193  static bool const value = sizeof(test(*e << *t)) == sizeof(yes);
194 };
195 
196 // Avoid recursion
197 template <> struct is_encodable<value> : public true_type {};
198 
199 } // is_encodable_impl
200 
201 using is_encodable_impl::is_encodable;
202 
203 // Metafunction to test if a class looks like an encodable map from K to T.
204 template <class M, class K, class T, class Enable = void>
205 struct is_encodable_map : public internal::false_type {};
206 
207 template <class M, class K, class T> struct is_encodable_map<
208  M, K, T, typename internal::enable_if<
209  internal::is_same<K, typename M::key_type>::value &&
210  internal::is_same<T, typename M::mapped_type>::value &&
211  is_encodable<M>::value
212  >::type
213  > : public internal::true_type {};
214 
215 
217 
218 } // codec
219 } // proton
220 
221 #endif
proton::decimal32
A 32-bit decimal floating-point value.
Definition: decimal.hpp:46
proton::scalar_base
The base class for scalar types.
Definition: scalar_base.hpp:60
proton::value
A holder for any AMQP value, simple or complex.
Definition: value.hpp:57
common.hpp
Unsettled API - Shared codec functions.
proton::decimal64
A 64-bit decimal floating-point value.
Definition: decimal.hpp:49
proton::codec::encoder::operator<<
encoder & operator<<(const start &)
Start a complex type.
proton::codec::encoder::encode
void encode(std::string &)
Encode the current values into a std::string and resize the string if necessary.
proton::codec::encoder::encoder
encoder(internal::value_base &v)
Encoder into v. Clears any current value in v.
proton::codec::operator<<
encoder & operator<<(encoder &e, const std::deque< T, A > &x)
std::deque<T> for most T is encoded as an amqp::ARRAY (same type elements)
Definition: deque.hpp:37
proton::codec::encoder::operator<<
encoder & operator<<(const internal::value_base &)
Insert a proton::value.
proton::codec::encoder::encode
std::string encode()
Encode the current values into a std::string.
proton::type_id
type_id
An identifier for AMQP types.
Definition: type_id.hpp:37
proton::symbol
A string that represents the AMQP symbol type.
Definition: symbol.hpp:35
proton::codec::encoder::operator<<
encoder & operator<<(const finish &)
Finish a complex type.
proton::codec::encoder::encoder
encoder(const data &d)
Wrap Proton-C data object.
Definition: encoder.hpp:53
proton::codec::start
Unsettled API - Start encoding a complex type.
Definition: common.hpp:34
proton::ARRAY
@ ARRAY
A sequence of values of the same type.
Definition: type_id.hpp:60
proton::codec::encoder::encode
bool encode(char *buffer, size_t &size)
Encode the current values into buffer and update size to reflect the number of bytes encoded.
proton::uuid
A 16-byte universally unique identifier.
Definition: uuid.hpp:37
proton::codec::encoder
Unsettled API - A stream-like encoder from C++ values to AMQP bytes.
Definition: encoder.hpp:50
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::decimal128
A 128-bit decimal floating-point value.
Definition: decimal.hpp:52
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