00001 #ifndef PROTON_VALUE_HPP
00002 #define PROTON_VALUE_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 "./codec/encoder.hpp"
00026 #include "./codec/decoder.hpp"
00027 #include "./internal/type_traits.hpp"
00028 #include "./scalar.hpp"
00029 #include "./types_fwd.hpp"
00030
00031 #include <proton/type_compat.h>
00032
00033 #include <iosfwd>
00034
00037
00038 namespace proton {
00039
00040 namespace internal {
00041
00042
00043 class value_base {
00044 protected:
00045 internal::data& data();
00046 internal::data data_;
00047
00048 friend class codec::encoder;
00049 friend class codec::decoder;
00050 };
00051
00052 }
00053
00057 class value : public internal::value_base, private internal::comparable<value> {
00058 private:
00059
00060 template<class T, class U=void> struct assignable :
00061 public internal::enable_if<codec::is_encodable<T>::value, U> {};
00062 template<class U> struct assignable<value, U> {};
00063
00064 public:
00066 PN_CPP_EXTERN value();
00067
00070 PN_CPP_EXTERN value(const value&);
00071 PN_CPP_EXTERN value& operator=(const value&);
00072 #if PN_CPP_HAS_RVALUE_REFERENCES
00073 PN_CPP_EXTERN value(value&&);
00074 PN_CPP_EXTERN value& operator=(value&&);
00075 #endif
00077
00079 template <class T> value(const T& x, typename assignable<T>::type* = 0) { *this = x; }
00080
00082 template <class T> typename assignable<T, value&>::type operator=(const T& x) {
00083 codec::encoder e(*this);
00084 e << x;
00085 return *this;
00086 }
00087
00089 PN_CPP_EXTERN type_id type() const;
00090
00092 PN_CPP_EXTERN bool empty() const;
00093
00095 PN_CPP_EXTERN void clear();
00096
00098 template<class T> PN_CPP_DEPRECATED("Use 'proton::get'") void get(T &t) const;
00099 template<class T> PN_CPP_DEPRECATED("Use 'proton::get'") T get() const;
00101
00103 friend PN_CPP_EXTERN void swap(value&, value&);
00104
00107 friend PN_CPP_EXTERN bool operator==(const value& x, const value& y);
00108 friend PN_CPP_EXTERN bool operator<(const value& x, const value& y);
00110
00115 friend PN_CPP_EXTERN std::ostream& operator<<(std::ostream&, const value&);
00116
00119 value(pn_data_t* d);
00120 void reset(pn_data_t* d = 0);
00122 };
00123
00126 template<class T> T get(const value& v) { T x; get(v, x); return x; }
00127
00133 template<class T> void get(const value& v, T& x) { codec::decoder d(v, true); d >> x; }
00134
00136 template<class T, class U> inline void get(const U& u, T& x) { const value v(u); get(v, x); }
00137
00140 template<class T> T coerce(const value& v) { T x; coerce(v, x); return x; }
00141
00147 template<class T> void coerce(const value& v, T& x) {
00148 codec::decoder d(v, false);
00149 scalar s;
00150 if (type_id_is_scalar(v.type())) {
00151 d >> s;
00152 x = internal::coerce<T>(s);
00153 } else {
00154 d >> x;
00155 }
00156 }
00157
00159 template<> inline void get<null>(const value& v, null&) {
00160 assert_type_equal(NULL_TYPE, v.type());
00161 }
00162
00164 PN_CPP_EXTERN std::string to_string(const value& x);
00165
00167 template<class T> void value::get(T &x) const { x = proton::get<T>(*this); }
00168 template<class T> T value::get() const { return proton::get<T>(*this); }
00170
00171 }
00172
00173 #endif // PROTON_VALUE_HPP