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 namespace proton {
00031
00032 namespace internal{
00033 class scalar_base;
00034 class value_base;
00035 }
00036
00037 namespace codec {
00038
00045 class encoder : public internal::data {
00046 public:
00048 explicit encoder(const data& d) : data(d) {}
00049
00051 PN_CPP_EXTERN explicit encoder(internal::value_base& v);
00052
00061 PN_CPP_EXTERN bool encode(char* buffer, size_t& size);
00062
00065 PN_CPP_EXTERN void encode(std::string&);
00066
00069 PN_CPP_EXTERN std::string encode();
00070
00073 PN_CPP_EXTERN encoder& operator<<(bool);
00074 PN_CPP_EXTERN encoder& operator<<(uint8_t);
00075 PN_CPP_EXTERN encoder& operator<<(int8_t);
00076 PN_CPP_EXTERN encoder& operator<<(uint16_t);
00077 PN_CPP_EXTERN encoder& operator<<(int16_t);
00078 PN_CPP_EXTERN encoder& operator<<(uint32_t);
00079 PN_CPP_EXTERN encoder& operator<<(int32_t);
00080 PN_CPP_EXTERN encoder& operator<<(wchar_t);
00081 PN_CPP_EXTERN encoder& operator<<(uint64_t);
00082 PN_CPP_EXTERN encoder& operator<<(int64_t);
00083 PN_CPP_EXTERN encoder& operator<<(timestamp);
00084 PN_CPP_EXTERN encoder& operator<<(float);
00085 PN_CPP_EXTERN encoder& operator<<(double);
00086 PN_CPP_EXTERN encoder& operator<<(decimal32);
00087 PN_CPP_EXTERN encoder& operator<<(decimal64);
00088 PN_CPP_EXTERN encoder& operator<<(decimal128);
00089 PN_CPP_EXTERN encoder& operator<<(const uuid&);
00090 PN_CPP_EXTERN encoder& operator<<(const std::string&);
00091 PN_CPP_EXTERN encoder& operator<<(const symbol&);
00092 PN_CPP_EXTERN encoder& operator<<(const binary&);
00093 PN_CPP_EXTERN encoder& operator<<(const internal::scalar_base&);
00094 PN_CPP_EXTERN encoder& operator<<(const null&);
00096
00101 PN_CPP_EXTERN encoder& operator<<(const internal::value_base&);
00102
00104 PN_CPP_EXTERN encoder& operator<<(const start&);
00105
00107 PN_CPP_EXTERN encoder& operator<<(const finish&);
00108
00110
00111
00112 template <class T> void* operator<<(const T*);
00113
00114 template <class T> struct list_cref { T& ref; list_cref(T& r) : ref(r) {} };
00115 template <class T> struct map_cref { T& ref; map_cref(T& r) : ref(r) {} };
00116
00117 template <class T> struct array_cref {
00118 start array_start;
00119 T& ref;
00120 array_cref(T& r, type_id el, bool described) : array_start(ARRAY, el, described), ref(r) {}
00121 };
00122
00123 template <class T> static list_cref<T> list(T& x) { return list_cref<T>(x); }
00124 template <class T> static map_cref<T> map(T& x) { return map_cref<T>(x); }
00125 template <class T> static array_cref<T> array(T& x, type_id element, bool described=false) {
00126 return array_cref<T>(x, element, described);
00127 }
00128
00129 template <class T> encoder& operator<<(const map_cref<T>& x) {
00130 internal::state_guard sg(*this);
00131 *this << start::map();
00132 for (typename T::const_iterator i = x.ref.begin(); i != x.ref.end(); ++i)
00133 *this << i->first << i->second;
00134 *this << finish();
00135 return *this;
00136 }
00137
00138 template <class T> encoder& operator<<(const list_cref<T>& x) {
00139 internal::state_guard sg(*this);
00140 *this << start::list();
00141 for (typename T::const_iterator i = x.ref.begin(); i != x.ref.end(); ++i)
00142 *this << *i;
00143 *this << finish();
00144 return *this;
00145 }
00146
00147 template <class T> encoder& operator<<(const array_cref<T>& x) {
00148 internal::state_guard sg(*this);
00149 *this << x.array_start;
00150 for (typename T::const_iterator i = x.ref.begin(); i != x.ref.end(); ++i)
00151 *this << *i;
00152 *this << finish();
00153 return *this;
00154 }
00156
00157 private:
00158 template<class T, class U> encoder& insert(const T& x, int (*put)(pn_data_t*, U));
00159 void check(long result);
00160 };
00161
00163 inline encoder& operator<<(encoder& e, const char* s) { return e << std::string(s); }
00164
00166 template <class T> typename internal::enable_if<internal::is_unknown_integer<T>::value, encoder&>::type
00167 operator<<(encoder& e, T i) {
00168 using namespace internal;
00169 return e << static_cast<typename integer_type<sizeof(T), is_signed<T>::value>::type>(i);
00170 }
00171
00173
00174 namespace is_encodable_impl {
00175
00176 using namespace internal;
00177
00178 sfinae::no operator<<(sfinae::wildcard, sfinae::wildcard);
00179
00180 template<typename T> struct is_encodable : public sfinae {
00181 static yes test(encoder);
00182 static no test(...);
00183 static encoder* e;
00184 static const T* t;
00185 static bool const value = sizeof(test(*e << *t)) == sizeof(yes);
00186 };
00187
00188
00189 template <> struct is_encodable<value> : public true_type {};
00190
00191 }
00192
00193 using is_encodable_impl::is_encodable;
00194
00196
00197 }
00198 }
00199
00200 #endif