proton/value.hpp

Go to the documentation of this file.
00001 #ifndef PROTON_VALUE_HPP
00002 #define PROTON_VALUE_HPP
00003 
00004 /*
00005  *
00006  * Licensed to the Apache Software Foundation (ASF) under one
00007  * or more contributor license agreements.  See the NOTICE file
00008  * distributed with this work for additional information
00009  * regarding copyright ownership.  The ASF licenses this file
00010  * to you under the Apache License, Version 2.0 (the
00011  * "License"); you may not use this file except in compliance
00012  * with the License.  You may obtain a copy of the License at
00013  *
00014  *   http://www.apache.org/licenses/LICENSE-2.0
00015  *
00016  * Unless required by applicable law or agreed to in writing,
00017  * software distributed under the License is distributed on an
00018  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00019  * KIND, either express or implied.  See the License for the
00020  * specific language governing permissions and limitations
00021  * under the License.
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 // Separate value data from implicit conversion constructors to avoid template recursion.
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 } // internal
00053 
00057 class value : public internal::value_base, private internal::comparable<value> {
00058   private:
00059     // Enabler for encodable types excluding proton::value.
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);          // Refer to existing pn_data_t
00120     void reset(pn_data_t* d = 0); // Refer to a new pn_data_t
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 #if PN_CPP_HAS_NULLPTR
00164 template<> inline void get<decltype(nullptr)>(const value& v, decltype(nullptr)&) {
00165     assert_type_equal(NULL_TYPE, v.type());
00166 }
00167 #endif
00168 
00170 PN_CPP_EXTERN std::string to_string(const value& x);
00171 
00173 template<class T> void value::get(T &x) const { x = proton::get<T>(*this); }
00174 template<class T> T value::get() const { return proton::get<T>(*this); }
00176 
00177 } // proton
00178 
00179 #endif // PROTON_VALUE_HPP

Generated on 30 Jul 2020 for Qpid Proton C++ by  doxygen 1.6.1