Qpid Proton C++  0.14.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
encoder.hpp
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 namespace proton {
31 
32 namespace internal{
33 class scalar_base;
34 class value_base;
35 }
36 
37 namespace codec {
38 
45 class encoder : public internal::data {
46  public:
48  explicit encoder(const data& d) : data(d) {}
49 
51  PN_CPP_EXTERN explicit encoder(internal::value_base& v);
52 
61  PN_CPP_EXTERN bool encode(char* buffer, size_t& size);
62 
65  PN_CPP_EXTERN void encode(std::string&);
66 
69  PN_CPP_EXTERN std::string encode();
70 
73  PN_CPP_EXTERN encoder& operator<<(bool);
74  PN_CPP_EXTERN encoder& operator<<(uint8_t);
75  PN_CPP_EXTERN encoder& operator<<(int8_t);
76  PN_CPP_EXTERN encoder& operator<<(uint16_t);
77  PN_CPP_EXTERN encoder& operator<<(int16_t);
78  PN_CPP_EXTERN encoder& operator<<(uint32_t);
79  PN_CPP_EXTERN encoder& operator<<(int32_t);
80  PN_CPP_EXTERN encoder& operator<<(wchar_t);
81  PN_CPP_EXTERN encoder& operator<<(uint64_t);
82  PN_CPP_EXTERN encoder& operator<<(int64_t);
83  PN_CPP_EXTERN encoder& operator<<(timestamp);
84  PN_CPP_EXTERN encoder& operator<<(float);
85  PN_CPP_EXTERN encoder& operator<<(double);
86  PN_CPP_EXTERN encoder& operator<<(decimal32);
87  PN_CPP_EXTERN encoder& operator<<(decimal64);
88  PN_CPP_EXTERN encoder& operator<<(decimal128);
89  PN_CPP_EXTERN encoder& operator<<(const uuid&);
90  PN_CPP_EXTERN encoder& operator<<(const std::string&);
91  PN_CPP_EXTERN encoder& operator<<(const symbol&);
92  PN_CPP_EXTERN encoder& operator<<(const binary&);
93  PN_CPP_EXTERN encoder& operator<<(const internal::scalar_base&);
94  PN_CPP_EXTERN encoder& operator<<(const null&);
96 
101  PN_CPP_EXTERN encoder& operator<<(const internal::value_base&);
102 
104  PN_CPP_EXTERN encoder& operator<<(const start&);
105 
107  PN_CPP_EXTERN encoder& operator<<(const finish&);
108 
110 
111  // Undefined template to prevent pointers being implicitly converted to bool.
112  template <class T> void* operator<<(const T*);
113 
114  template <class T> struct list_cref { T& ref; list_cref(T& r) : ref(r) {} };
115  template <class T> struct map_cref { T& ref; map_cref(T& r) : ref(r) {} };
116 
117  template <class T> struct array_cref {
118  start array_start;
119  T& ref;
120  array_cref(T& r, type_id el, bool described) : array_start(ARRAY, el, described), ref(r) {}
121  };
122 
123  template <class T> static list_cref<T> list(T& x) { return list_cref<T>(x); }
124  template <class T> static map_cref<T> map(T& x) { return map_cref<T>(x); }
125  template <class T> static array_cref<T> array(T& x, type_id element, bool described=false) {
126  return array_cref<T>(x, element, described);
127  }
128 
129  template <class T> encoder& operator<<(const map_cref<T>& x) {
130  internal::state_guard sg(*this);
131  *this << start::map();
132  for (typename T::const_iterator i = x.ref.begin(); i != x.ref.end(); ++i)
133  *this << i->first << i->second;
134  *this << finish();
135  return *this;
136  }
137 
138  template <class T> encoder& operator<<(const list_cref<T>& x) {
139  internal::state_guard sg(*this);
140  *this << start::list();
141  for (typename T::const_iterator i = x.ref.begin(); i != x.ref.end(); ++i)
142  *this << *i;
143  *this << finish();
144  return *this;
145  }
146 
147  template <class T> encoder& operator<<(const array_cref<T>& x) {
148  internal::state_guard sg(*this);
149  *this << x.array_start;
150  for (typename T::const_iterator i = x.ref.begin(); i != x.ref.end(); ++i)
151  *this << *i;
152  *this << finish();
153  return *this;
154  }
156 
157  private:
158  template<class T, class U> encoder& insert(const T& x, int (*put)(pn_data_t*, U));
159  void check(long result);
160 };
161 
163 inline encoder& operator<<(encoder& e, const char* s) { return e << std::string(s); }
164 
166 template <class T> typename internal::enable_if<internal::is_unknown_integer<T>::value, encoder&>::type
167 operator<<(encoder& e, T i) {
168  using namespace internal;
169  return e << static_cast<typename integer_type<sizeof(T), is_signed<T>::value>::type>(i);
170 }
171 
173 
174 namespace is_encodable_impl { // Protected the world from wildcard operator<<
175 
176 using namespace internal;
177 
178 sfinae::no operator<<(sfinae::wildcard, sfinae::wildcard); // Fallback
179 
180 template<typename T> struct is_encodable : public sfinae {
181  static yes test(encoder);
182  static no test(...); // Failed test, no match.
183  static encoder* e;
184  static const T* t;
185  static bool const value = sizeof(test(*e << *t)) == sizeof(yes);
186 };
187 
188 // Avoid recursion
189 template <> struct is_encodable<value> : public true_type {};
190 
191 } // is_encodable_impl
192 
193 using is_encodable_impl::is_encodable;
194 
196 
197 } // codec
198 } // proton
199 
200 #endif
Experimental - Start encoding a complex type.
Definition: common.hpp:31
Experimental - Stream-like encoder from AMQP bytes to C++ values.
Definition: encoder.hpp:45
A sequence of values of the same type.
Definition: type_id.hpp:61
encoder & operator<<(encoder &e, const std::deque< T, A > &x)
std::deque&lt;T&gt; for most T is encoded as an amqp::ARRAY (same type elements)
Definition: deque.hpp:34
A 16-byte universally unique identifier.
Definition: uuid.hpp:34
64-bit decimal floating point.
Definition: decimal.hpp:51
encoder(const data &d)
Wrap Proton-C data object.
Definition: encoder.hpp:48
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
A holder for any AMQP value, simple or complex.
Definition: value.hpp:60
std::string encode()
Encode the current values into a std::string.
Experimental - Finish inserting or extracting a complex type.
Definition: common.hpp:54