| 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 |
|
| 32 |
/// @file |
| 33 |
/// @copybrief proton::codec::encoder |
| 34 |
|
| 35 |
namespace proton { |
| 36 |
class scalar_base; |
| 37 |
|
| 38 |
namespace internal{ |
| 39 |
class value_base; |
| 40 |
} |
| 41 |
|
| 42 |
namespace codec { |
| 43 |
|
| 44 |
/// **Unsettled API** - A stream-like encoder from C++ values to AMQP |
| 45 |
/// bytes. |
| 46 |
/// |
| 47 |
/// For internal use only. |
| 48 |
/// |
| 49 |
/// @see @ref types_page for the recommended ways to manage AMQP data |
| 50 |
class encoder : public internal::data { |
| 51 |
public: |
| 52 |
/// Wrap Proton-C data object. |
| 53 |
explicit encoder(const data& d) : data(d) {} |
| 54 |
|
| 55 |
/// Encoder into v. Clears any current value in v. |
| 56 |
PN_CPP_EXTERN explicit encoder(internal::value_base& v); |
| 57 |
|
| 58 |
/// Encode the current values into buffer and update size to reflect the |
| 59 |
/// number of bytes encoded. |
| 60 |
/// |
| 61 |
/// Clears the encoder. |
| 62 |
/// |
| 63 |
/// @return if buffer == 0 or size is too small, then return false |
| 64 |
/// and size to the required size. Otherwise, return true and set |
| 65 |
/// size to the number of bytes encoded. |
| 66 |
PN_CPP_EXTERN bool encode(char* buffer, size_t& size); |
| 67 |
|
| 68 |
/// Encode the current values into a std::string and resize the |
| 69 |
/// string if necessary. Clears the encoder. |
| 70 |
PN_CPP_EXTERN void encode(std::string&); |
| 71 |
|
| 72 |
/// Encode the current values into a std::string. Clears the |
| 73 |
/// encoder. |
| 74 |
PN_CPP_EXTERN std::string encode(); |
| 75 |
|
| 76 |
/// @name Insert built-in types |
| 77 |
/// @{ |
| 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 |
PN_CPP_EXTERN encoder& operator<<(decltype(nullptr)); |
| 101 |
/// @} |
| 102 |
|
| 103 |
/// Insert a proton::value. |
| 104 |
/// |
| 105 |
/// @internal NOTE insert value_base, not value to avoid recursive |
| 106 |
/// implicit conversions. |
| 107 |
PN_CPP_EXTERN encoder& operator<<(const internal::value_base&); |
| 108 |
|
| 109 |
/// Start a complex type |
| 110 |
PN_CPP_EXTERN encoder& operator<<(const start&); |
| 111 |
|
| 112 |
/// Finish a complex type |
| 113 |
PN_CPP_EXTERN encoder& operator<<(const finish&); |
| 114 |
|
| 115 |
/// @cond INTERNAL |
| 116 |
|
| 117 |
// Undefined template to prevent pointers being implicitly converted to bool. |
| 118 |
template <class T> void* operator<<(const T*); |
| 119 |
|
| 120 |
template <class T> struct list_cref { T& ref; list_cref(T& r) : ref(r) {} }; |
| 121 |
template <class T> struct map_cref { T& ref; map_cref(T& r) : ref(r) {} }; |
| 122 |
|
| 123 |
template <class T> struct array_cref { |
| 124 |
start array_start; |
| 125 |
T& ref; |
| 126 |
array_cref(T& r, type_id el, bool described) : array_start(ARRAY, el, described), ref(r) {} |
| 127 |
}; |
| 128 |
|
| 129 |
template <class T> static list_cref<T> list(T& x) { return list_cref<T>(x); } |
| 130 |
template <class T> static map_cref<T> map(T& x) { return map_cref<T>(x); } |
| 131 |
template <class T> static array_cref<T> array(T& x, type_id element, bool described=false) { |
| 132 |
return array_cref<T>(x, element, described); |
| 133 |
} |
| 134 |
|
| 135 |
template <class T> encoder& operator<<(const map_cref<T>& x) { |
| 136 |
internal::state_guard sg(*this); |
| 137 |
*this << start::map(); |
| 138 |
for (typename T::const_iterator i = x.ref.begin(); i != x.ref.end(); ++i) |
| 139 |
*this << i->first << i->second; |
| 140 |
*this << finish(); |
| 141 |
return *this; |
| 142 |
} |
| 143 |
|
| 144 |
template <class T> encoder& operator<<(const list_cref<T>& x) { |
| 145 |
internal::state_guard sg(*this); |
| 146 |
*this << start::list(); |
| 147 |
for (typename T::const_iterator i = x.ref.begin(); i != x.ref.end(); ++i) |
| 148 |
*this << *i; |
| 149 |
*this << finish(); |
| 150 |
return *this; |
| 151 |
} |
| 152 |
|
| 153 |
template <class T> encoder& operator<<(const array_cref<T>& x) { |
| 154 |
internal::state_guard sg(*this); |
| 155 |
*this << x.array_start; |
| 156 |
for (typename T::const_iterator i = x.ref.begin(); i != x.ref.end(); ++i) |
| 157 |
*this << *i; |
| 158 |
*this << finish(); |
| 159 |
return *this; |
| 160 |
} |
| 161 |
/// @endcond |
| 162 |
|
| 163 |
private: |
| 164 |
template<class T, class U> encoder& insert(const T& x, int (*put)(pn_data_t*, U)); |
| 165 |
void check(long result); |
| 166 |
}; |
| 167 |
|
| 168 |
/// Treat char* as string |
| 169 |
inline encoder& operator<<(encoder& e, const char* s) { return e << std::string(s); } |
| 170 |
|
| 171 |
/// operator << for integer types that are not covered by the standard overrides. |
| 172 |
template <class T> typename std::enable_if<internal::is_unknown_integer<T>::value, encoder&>::type |
| 173 |
operator<<(encoder& e, T i) { |
| 174 |
using namespace internal; |
| 175 |
return e << static_cast<typename integer_type<sizeof(T), std::is_signed<T>::value>::type>(i); |
| 176 |
} |
| 177 |
|
| 178 |
/// @cond INTERNAL |
| 179 |
|
| 180 |
namespace is_encodable_impl { // Protect the world from fallback operator<< |
| 181 |
|
| 182 |
using namespace internal; |
| 183 |
|
| 184 |
sfinae::no operator<<(encoder const&, const sfinae::any_t &); // Fallback |
| 185 |
|
| 186 |
template<typename T> struct is_encodable : public sfinae { |
| 187 |
static yes test(encoder&); |
| 188 |
static no test(...); // Failed test, no match. |
| 189 |
static encoder* e; |
| 190 |
static const T* t; |
| 191 |
static bool const value = sizeof(test(*e << *t)) == sizeof(yes); |
| 192 |
}; |
| 193 |
|
| 194 |
// Avoid recursion |
| 195 |
template <> struct is_encodable<value> : public std::true_type {}; |
| 196 |
|
| 197 |
} // is_encodable_impl |
| 198 |
|
| 199 |
using is_encodable_impl::is_encodable; |
| 200 |
|
| 201 |
// Metafunction to test if a class looks like an encodable map from K to T. |
| 202 |
template <class M, class K, class T, class Enable = void> |
| 203 |
struct is_encodable_map : public std::false_type {}; |
| 204 |
|
| 205 |
template <class M, class K, class T> struct is_encodable_map< |
| 206 |
M, K, T, typename std::enable_if< |
| 207 |
std::is_same<K, typename M::key_type>::value && |
| 208 |
std::is_same<T, typename M::mapped_type>::value && |
| 209 |
is_encodable<M>::value |
| 210 |
>::type |
| 211 |
> : public std::true_type {}; |
| 212 |
|
| 213 |
|
| 214 |
/// @endcond |
| 215 |
|
| 216 |
} // codec |
| 217 |
} // proton |
| 218 |
|
| 219 |
#endif /// PROTON_CODEC_ENCODER_HPP |