00001 #ifndef PROTON_CODEC_ENCODER_HPP
00002 #define PROTON_CODEC_ENCODER_HPP
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "../internal/data.hpp"
00026 #include "../internal/type_traits.hpp"
00027 #include "../types_fwd.hpp"
00028 #include "./common.hpp"
00029
00030 #include <proton/type_compat.h>
00031
00034
00035 namespace proton {
00036 class scalar_base;
00037
00038 namespace internal{
00039 class value_base;
00040 }
00041
00042 namespace codec {
00043
00050 class encoder : public internal::data {
00051 public:
00053 explicit encoder(const data& d) : data(d) {}
00054
00056 PN_CPP_EXTERN explicit encoder(internal::value_base& v);
00057
00066 PN_CPP_EXTERN bool encode(char* buffer, size_t& size);
00067
00070 PN_CPP_EXTERN void encode(std::string&);
00071
00074 PN_CPP_EXTERN std::string encode();
00075
00078 PN_CPP_EXTERN encoder& operator<<(bool);
00079 PN_CPP_EXTERN encoder& operator<<(uint8_t);
00080 PN_CPP_EXTERN encoder& operator<<(int8_t);
00081 PN_CPP_EXTERN encoder& operator<<(uint16_t);
00082 PN_CPP_EXTERN encoder& operator<<(int16_t);
00083 PN_CPP_EXTERN encoder& operator<<(uint32_t);
00084 PN_CPP_EXTERN encoder& operator<<(int32_t);
00085 PN_CPP_EXTERN encoder& operator<<(wchar_t);
00086 PN_CPP_EXTERN encoder& operator<<(uint64_t);
00087 PN_CPP_EXTERN encoder& operator<<(int64_t);
00088 PN_CPP_EXTERN encoder& operator<<(timestamp);
00089 PN_CPP_EXTERN encoder& operator<<(float);
00090 PN_CPP_EXTERN encoder& operator<<(double);
00091 PN_CPP_EXTERN encoder& operator<<(decimal32);
00092 PN_CPP_EXTERN encoder& operator<<(decimal64);
00093 PN_CPP_EXTERN encoder& operator<<(decimal128);
00094 PN_CPP_EXTERN encoder& operator<<(const uuid&);
00095 PN_CPP_EXTERN encoder& operator<<(const std::string&);
00096 PN_CPP_EXTERN encoder& operator<<(const symbol&);
00097 PN_CPP_EXTERN encoder& operator<<(const binary&);
00098 PN_CPP_EXTERN encoder& operator<<(const scalar_base&);
00099 PN_CPP_EXTERN encoder& operator<<(const null&);
00101
00106 PN_CPP_EXTERN encoder& operator<<(const internal::value_base&);
00107
00109 PN_CPP_EXTERN encoder& operator<<(const start&);
00110
00112 PN_CPP_EXTERN encoder& operator<<(const finish&);
00113
00115
00116
00117 template <class T> void* operator<<(const T*);
00118
00119 template <class T> struct list_cref { T& ref; list_cref(T& r) : ref(r) {} };
00120 template <class T> struct map_cref { T& ref; map_cref(T& r) : ref(r) {} };
00121
00122 template <class T> struct array_cref {
00123 start array_start;
00124 T& ref;
00125 array_cref(T& r, type_id el, bool described) : array_start(ARRAY, el, described), ref(r) {}
00126 };
00127
00128 template <class T> static list_cref<T> list(T& x) { return list_cref<T>(x); }
00129 template <class T> static map_cref<T> map(T& x) { return map_cref<T>(x); }
00130 template <class T> static array_cref<T> array(T& x, type_id element, bool described=false) {
00131 return array_cref<T>(x, element, described);
00132 }
00133
00134 template <class T> encoder& operator<<(const map_cref<T>& x) {
00135 internal::state_guard sg(*this);
00136 *this << start::map();
00137 for (typename T::const_iterator i = x.ref.begin(); i != x.ref.end(); ++i)
00138 *this << i->first << i->second;
00139 *this << finish();
00140 return *this;
00141 }
00142
00143 template <class T> encoder& operator<<(const list_cref<T>& x) {
00144 internal::state_guard sg(*this);
00145 *this << start::list();
00146 for (typename T::const_iterator i = x.ref.begin(); i != x.ref.end(); ++i)
00147 *this << *i;
00148 *this << finish();
00149 return *this;
00150 }
00151
00152 template <class T> encoder& operator<<(const array_cref<T>& x) {
00153 internal::state_guard sg(*this);
00154 *this << x.array_start;
00155 for (typename T::const_iterator i = x.ref.begin(); i != x.ref.end(); ++i)
00156 *this << *i;
00157 *this << finish();
00158 return *this;
00159 }
00161
00162 private:
00163 template<class T, class U> encoder& insert(const T& x, int (*put)(pn_data_t*, U));
00164 void check(long result);
00165 };
00166
00168 inline encoder& operator<<(encoder& e, const char* s) { return e << std::string(s); }
00169
00171 template <class T> typename internal::enable_if<internal::is_unknown_integer<T>::value, encoder&>::type
00172 operator<<(encoder& e, T i) {
00173 using namespace internal;
00174 return e << static_cast<typename integer_type<sizeof(T), is_signed<T>::value>::type>(i);
00175 }
00176
00178
00179 namespace is_encodable_impl {
00180
00181 using namespace internal;
00182
00183 sfinae::no operator<<(encoder const&, const sfinae::any_t &);
00184
00185 template<typename T> struct is_encodable : public sfinae {
00186 static yes test(encoder&);
00187 static no test(...);
00188 static encoder* e;
00189 static const T* t;
00190 static bool const value = sizeof(test(*e << *t)) == sizeof(yes);
00191 };
00192
00193
00194 template <> struct is_encodable<value> : public true_type {};
00195
00196 }
00197
00198 using is_encodable_impl::is_encodable;
00199
00200
00201 template <class M, class K, class T, class Enable = void>
00202 struct is_encodable_map : public internal::false_type {};
00203
00204 template <class M, class K, class T> struct is_encodable_map<
00205 M, K, T, typename internal::enable_if<
00206 internal::is_same<K, typename M::key_type>::value &&
00207 internal::is_same<T, typename M::mapped_type>::value &&
00208 is_encodable<M>::value
00209 >::type
00210 > : public internal::true_type {};
00211
00212
00214
00215 }
00216 }
00217
00218 #endif