| 1 |
#ifndef PROTON_VALUE_HPP |
| 2 |
#define PROTON_VALUE_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 "./codec/encoder.hpp" |
| 26 |
#include "./codec/decoder.hpp" |
| 27 |
#include "./internal/type_traits.hpp" |
| 28 |
#include "./scalar.hpp" |
| 29 |
#include "./types_fwd.hpp" |
| 30 |
|
| 31 |
#include <proton/type_compat.h> |
| 32 |
|
| 33 |
#include <iosfwd> |
| 34 |
|
| 35 |
/// @file |
| 36 |
/// @copybrief proton::value |
| 37 |
|
| 38 |
namespace proton { |
| 39 |
|
| 40 |
namespace internal { |
| 41 |
|
| 42 |
// Separate value data from implicit conversion constructors to avoid template recursion. |
| 43 |
class value_base { |
| 44 |
protected: |
| 45 |
internal::data& data(); |
| 46 |
internal::data data_; |
| 47 |
|
| 48 |
friend class codec::encoder; |
| 49 |
friend class codec::decoder; |
| 50 |
}; |
| 51 |
|
| 52 |
} // internal |
| 53 |
|
| 54 |
/// A holder for any AMQP value, simple or complex. |
| 55 |
/// |
| 56 |
/// @see @ref types_page |
| 57 |
class value : public internal::value_base, private internal::comparable<value> { |
| 58 |
private: |
| 59 |
// Enabler for encodable types excluding proton::value. |
| 60 |
template<class T, class U=void> struct assignable : |
| 61 |
public std::enable_if<codec::is_encodable<T>::value, U> {}; |
| 62 |
template<class U> struct assignable<value, U> {}; |
| 63 |
|
| 64 |
public: |
| 65 |
/// Create a null value |
| 66 |
PN_CPP_EXTERN value(); |
| 67 |
|
| 68 |
/// @name Copy a value |
| 69 |
/// @{ |
| 70 |
PN_CPP_EXTERN value(const value&); |
| 71 |
PN_CPP_EXTERN value& operator=(const value&); |
| 72 |
PN_CPP_EXTERN value(value&&); |
| 73 |
PN_CPP_EXTERN value& operator=(value&&); |
| 74 |
/// @} |
| 75 |
|
| 76 |
/// Copy from any allowed type T. |
| 77 |
template <class T> value(const T& x, typename assignable<T>::type* = 0) { *this = x; } |
| 78 |
|
| 79 |
/// Assign from any allowed type T. |
| 80 |
template <class T> typename assignable<T, value&>::type operator=(const T& x) { |
| 81 |
codec::encoder e(*this); |
| 82 |
e << x; |
| 83 |
return *this; |
| 84 |
} |
| 85 |
|
| 86 |
/// Get the type ID for the current value. |
| 87 |
PN_CPP_EXTERN type_id type() const; |
| 88 |
|
| 89 |
/// True if the value is null |
| 90 |
PN_CPP_EXTERN bool empty() const; |
| 91 |
|
| 92 |
/// Reset the value to null/empty |
| 93 |
PN_CPP_EXTERN void clear(); |
| 94 |
|
| 95 |
/// @cond INTERNAL |
| 96 |
template<class T> PN_CPP_DEPRECATED("Use 'proton::get'") void get(T &t) const; |
| 97 |
template<class T> PN_CPP_DEPRECATED("Use 'proton::get'") T get() const; |
| 98 |
/// @endcond |
| 99 |
|
| 100 |
/// swap values |
| 101 |
friend PN_CPP_EXTERN void swap(value&, value&); |
| 102 |
|
| 103 |
/// @name Comparison operators |
| 104 |
/// @{ |
| 105 |
friend PN_CPP_EXTERN bool operator==(const value& x, const value& y); |
| 106 |
friend PN_CPP_EXTERN bool operator<(const value& x, const value& y); |
| 107 |
///@} |
| 108 |
|
| 109 |
/// If contained value is a scalar type T, print using operator<<(T) |
| 110 |
/// |
| 111 |
/// Complex types are printed in a non-standard human-readable format but |
| 112 |
/// that may change in future so should not be parsed. |
| 113 |
friend PN_CPP_EXTERN std::ostream& operator<<(std::ostream&, const value&); |
| 114 |
|
| 115 |
///@cond INTERNAL |
| 116 |
/// Used to refer to existing pn_data_t* values as proton::value |
| 117 |
value(pn_data_t* d); // Refer to existing pn_data_t |
| 118 |
void reset(pn_data_t* d = 0); // Refer to a new pn_data_t |
| 119 |
///@endcond |
| 120 |
}; |
| 121 |
|
| 122 |
/// @copydoc scalar::get |
| 123 |
/// @relatedalso proton::value |
| 124 |
template<class T> T get(const value& v) { T x; get(v, x); return x; } |
| 125 |
|
| 126 |
/// Like get(const value&) but extracts the value to a reference @p x |
| 127 |
/// instead of returning it. May be more efficient for complex values |
| 128 |
/// (arrays, maps, etc.) |
| 129 |
/// |
| 130 |
/// @relatedalso proton::value |
| 131 |
template<class T> void get(const value& v, T& x) { codec::decoder d(v, true); d >> x; } |
| 132 |
|
| 133 |
/// @relatedalso proton::value |
| 134 |
template<class T, class U> inline void get(const U& u, T& x) { const value v(u); get(v, x); } |
| 135 |
|
| 136 |
/// @copydoc scalar::coerce |
| 137 |
/// @relatedalso proton::value |
| 138 |
template<class T> T coerce(const value& v) { T x; coerce(v, x); return x; } |
| 139 |
|
| 140 |
/// Like coerce(const value&) but assigns the value to a reference |
| 141 |
/// instead of returning it. May be more efficient for complex values |
| 142 |
/// (arrays, maps, etc.) |
| 143 |
/// |
| 144 |
/// @relatedalso proton::value |
| 145 |
template<class T> void coerce(const value& v, T& x) { |
| 146 |
codec::decoder d(v, false); |
| 147 |
scalar s; |
| 148 |
if (type_id_is_scalar(v.type())) { |
| 149 |
d >> s; |
| 150 |
x = internal::coerce<T>(s); |
| 151 |
} else { |
| 152 |
d >> x; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
/// Special case for null, just checks that value contains NULL. |
| 157 |
template<> inline void get<null>(const value& v, null&) { |
| 158 |
assert_type_equal(NULL_TYPE, v.type()); |
| 159 |
} |
| 160 |
|
| 161 |
/// @copybrief get<null>() |
| 162 |
template<> inline void get<decltype(nullptr)>(const value& v, decltype(nullptr)&) { |
| 163 |
assert_type_equal(NULL_TYPE, v.type()); |
| 164 |
} |
| 165 |
|
| 166 |
/// Return a readable string representation of x for display purposes. |
| 167 |
PN_CPP_EXTERN std::string to_string(const value& x); |
| 168 |
|
| 169 |
/// @cond INTERNAL |
| 170 |
template<class T> void value::get(T &x) const { x = proton::get<T>(*this); } |
| 171 |
template<class T> T value::get() const { return proton::get<T>(*this); } |
| 172 |
/// @endcond |
| 173 |
|
| 174 |
} // proton |
| 175 |
|
| 176 |
#endif // PROTON_VALUE_HPP |