| 1 |
#ifndef PROTON_SCALAR_BASE_HPP |
| 2 |
#define PROTON_SCALAR_BASE_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 "./binary.hpp" |
| 26 |
#include "./decimal.hpp" |
| 27 |
#include "./error.hpp" |
| 28 |
#include "./internal/comparable.hpp" |
| 29 |
#include "./internal/export.hpp" |
| 30 |
#include "./internal/type_traits.hpp" |
| 31 |
#include "./symbol.hpp" |
| 32 |
#include "./timestamp.hpp" |
| 33 |
#include "./type_id.hpp" |
| 34 |
#include "./types_fwd.hpp" |
| 35 |
#include "./uuid.hpp" |
| 36 |
|
| 37 |
#include <proton/type_compat.h> |
| 38 |
|
| 39 |
#include <iosfwd> |
| 40 |
#include <string> |
| 41 |
#include <typeinfo> |
| 42 |
|
| 43 |
/// @file |
| 44 |
/// @copybrief proton::scalar_base |
| 45 |
|
| 46 |
namespace proton { |
| 47 |
|
| 48 |
class scalar_base; |
| 49 |
|
| 50 |
namespace codec { |
| 51 |
class decoder; |
| 52 |
class encoder; |
| 53 |
} |
| 54 |
|
| 55 |
namespace internal { |
| 56 |
template<class T> T get(const scalar_base& s); |
| 57 |
} |
| 58 |
|
| 59 |
/// The base class for scalar types. |
| 60 |
class scalar_base : private internal::comparable<scalar_base> { |
| 61 |
public: |
| 62 |
/// AMQP type of data stored in the scalar |
| 63 |
PN_CPP_EXTERN type_id type() const; |
| 64 |
|
| 65 |
/// True if there is no value, i.e. type() == NULL_TYPE. |
| 66 |
PN_CPP_EXTERN bool empty() const; |
| 67 |
|
| 68 |
/// Compare |
| 69 |
friend PN_CPP_EXTERN bool operator<(const scalar_base& x, const scalar_base& y); |
| 70 |
/// Compare |
| 71 |
friend PN_CPP_EXTERN bool operator==(const scalar_base& x, const scalar_base& y); |
| 72 |
/// Print the contained value |
| 73 |
friend PN_CPP_EXTERN std::ostream& operator<<(std::ostream& o, const scalar_base& x); |
| 74 |
|
| 75 |
protected: |
| 76 |
PN_CPP_EXTERN scalar_base(const pn_atom_t& a); |
| 77 |
PN_CPP_EXTERN scalar_base(); |
| 78 |
PN_CPP_EXTERN scalar_base(const scalar_base&); |
| 79 |
PN_CPP_EXTERN scalar_base& operator=(const scalar_base&); |
| 80 |
|
| 81 |
PN_CPP_EXTERN void put_(bool); |
| 82 |
PN_CPP_EXTERN void put_(uint8_t); |
| 83 |
PN_CPP_EXTERN void put_(int8_t); |
| 84 |
PN_CPP_EXTERN void put_(uint16_t); |
| 85 |
PN_CPP_EXTERN void put_(int16_t); |
| 86 |
PN_CPP_EXTERN void put_(uint32_t); |
| 87 |
PN_CPP_EXTERN void put_(int32_t); |
| 88 |
PN_CPP_EXTERN void put_(uint64_t); |
| 89 |
PN_CPP_EXTERN void put_(int64_t); |
| 90 |
PN_CPP_EXTERN void put_(wchar_t); |
| 91 |
PN_CPP_EXTERN void put_(float); |
| 92 |
PN_CPP_EXTERN void put_(double); |
| 93 |
PN_CPP_EXTERN void put_(timestamp); |
| 94 |
PN_CPP_EXTERN void put_(const decimal32&); |
| 95 |
PN_CPP_EXTERN void put_(const decimal64&); |
| 96 |
PN_CPP_EXTERN void put_(const decimal128&); |
| 97 |
PN_CPP_EXTERN void put_(const uuid&); |
| 98 |
PN_CPP_EXTERN void put_(const std::string&); |
| 99 |
PN_CPP_EXTERN void put_(const symbol&); |
| 100 |
PN_CPP_EXTERN void put_(const binary&); |
| 101 |
PN_CPP_EXTERN void put_(const char* s); ///< Treated as an AMQP string |
| 102 |
PN_CPP_EXTERN void put_(const null&); |
| 103 |
PN_CPP_EXTERN void put_(decltype(nullptr)); |
| 104 |
|
| 105 |
template<class T> void put(const T& x) { putter<T>::put(*this, x); } |
| 106 |
|
| 107 |
private: |
| 108 |
PN_CPP_EXTERN void get_(bool&) const; |
| 109 |
PN_CPP_EXTERN void get_(uint8_t&) const; |
| 110 |
PN_CPP_EXTERN void get_(int8_t&) const; |
| 111 |
PN_CPP_EXTERN void get_(uint16_t&) const; |
| 112 |
PN_CPP_EXTERN void get_(int16_t&) const; |
| 113 |
PN_CPP_EXTERN void get_(uint32_t&) const; |
| 114 |
PN_CPP_EXTERN void get_(int32_t&) const; |
| 115 |
PN_CPP_EXTERN void get_(uint64_t&) const; |
| 116 |
PN_CPP_EXTERN void get_(int64_t&) const; |
| 117 |
PN_CPP_EXTERN void get_(wchar_t&) const; |
| 118 |
PN_CPP_EXTERN void get_(float&) const; |
| 119 |
PN_CPP_EXTERN void get_(double&) const; |
| 120 |
PN_CPP_EXTERN void get_(timestamp&) const; |
| 121 |
PN_CPP_EXTERN void get_(decimal32&) const; |
| 122 |
PN_CPP_EXTERN void get_(decimal64&) const; |
| 123 |
PN_CPP_EXTERN void get_(decimal128&) const; |
| 124 |
PN_CPP_EXTERN void get_(uuid&) const; |
| 125 |
PN_CPP_EXTERN void get_(std::string&) const; |
| 126 |
PN_CPP_EXTERN void get_(symbol&) const; |
| 127 |
PN_CPP_EXTERN void get_(binary&) const; |
| 128 |
PN_CPP_EXTERN void get_(null&) const; |
| 129 |
PN_CPP_EXTERN void get_(decltype(nullptr)&) const; |
| 130 |
|
| 131 |
|
| 132 |
// use template structs, functions cannot be partially specialized. |
| 133 |
template <class T, class Enable=void> struct putter { |
| 134 |
static void put(scalar_base& s, const T& x) { s.put_(x); } |
| 135 |
}; |
| 136 |
template <class T> |
| 137 |
struct putter<T, typename std::enable_if<internal::is_unknown_integer<T>::value>::type> { |
| 138 |
static void put(scalar_base& s, const T& x) { |
| 139 |
s.put_(static_cast<typename internal::known_integer<T>::type>(x)); |
| 140 |
} |
| 141 |
}; |
| 142 |
template <class T, class Enable=void> |
| 143 |
struct getter { |
| 144 |
static T get(const scalar_base& s) { T x; s.get_(x); return x; } |
| 145 |
}; |
| 146 |
template <class T> |
| 147 |
struct getter<T, typename std::enable_if<internal::is_unknown_integer<T>::value>::type> { |
| 148 |
static T get(const scalar_base& s) { |
| 149 |
typename internal::known_integer<T>::type x; s.get_(x); return x; |
| 150 |
} |
| 151 |
}; |
| 152 |
|
| 153 |
void ok(pn_type_t) const; |
| 154 |
void set(const pn_atom_t&); |
| 155 |
void set(const binary& x, pn_type_t t); |
| 156 |
|
| 157 |
pn_atom_t atom_; |
| 158 |
binary bytes_; // Hold binary data. |
| 159 |
|
| 160 |
/// @cond INTERNAL |
| 161 |
friend class message; |
| 162 |
friend class codec::encoder; |
| 163 |
friend class codec::decoder; |
| 164 |
template<class T> friend T internal::get(const scalar_base& s); |
| 165 |
/// @endcond |
| 166 |
}; |
| 167 |
|
| 168 |
namespace internal { |
| 169 |
|
| 170 |
template<class T> T get(const scalar_base& s) { |
| 171 |
return scalar_base::getter<T>::get(s); |
| 172 |
} |
| 173 |
|
| 174 |
template <class R, class F> R visit(const scalar_base& s, F f) { |
| 175 |
switch(s.type()) { |
| 176 |
case BOOLEAN: return f(internal::get<bool>(s)); |
| 177 |
case UBYTE: return f(internal::get<uint8_t>(s)); |
| 178 |
case BYTE: return f(internal::get<int8_t>(s)); |
| 179 |
case USHORT: return f(internal::get<uint16_t>(s)); |
| 180 |
case SHORT: return f(internal::get<int16_t>(s)); |
| 181 |
case UINT: return f(internal::get<uint32_t>(s)); |
| 182 |
case INT: return f(internal::get<int32_t>(s)); |
| 183 |
case CHAR: return f(internal::get<wchar_t>(s)); |
| 184 |
case ULONG: return f(internal::get<uint64_t>(s)); |
| 185 |
case LONG: return f(internal::get<int64_t>(s)); |
| 186 |
case TIMESTAMP: return f(internal::get<timestamp>(s)); |
| 187 |
case FLOAT: return f(internal::get<float>(s)); |
| 188 |
case DOUBLE: return f(internal::get<double>(s)); |
| 189 |
case DECIMAL32: return f(internal::get<decimal32>(s)); |
| 190 |
case DECIMAL64: return f(internal::get<decimal64>(s)); |
| 191 |
case DECIMAL128: return f(internal::get<decimal128>(s)); |
| 192 |
case UUID: return f(internal::get<uuid>(s)); |
| 193 |
case BINARY: return f(internal::get<binary>(s)); |
| 194 |
case STRING: return f(internal::get<std::string>(s)); |
| 195 |
case SYMBOL: return f(internal::get<symbol>(s)); |
| 196 |
default: throw conversion_error("invalid scalar type "+type_name(s.type())); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
PN_CPP_EXTERN conversion_error make_coercion_error(const char* cpp_type, type_id amqp_type); |
| 201 |
|
| 202 |
template<class T> struct coerce_op { |
| 203 |
template <class U> |
| 204 |
typename std::enable_if<std::is_convertible<U, T>::value, T>::type operator()(const U& x) { |
| 205 |
return static_cast<T>(x); |
| 206 |
} |
| 207 |
template <class U> |
| 208 |
typename std::enable_if<!std::is_convertible<U, T>::value, T>::type operator()(const U&) { |
| 209 |
throw make_coercion_error(typeid(T).name(), type_id_of<U>::value); |
| 210 |
} |
| 211 |
}; |
| 212 |
|
| 213 |
template <class T> T coerce(const scalar_base& s) { return visit<T>(s, coerce_op<T>()); } |
| 214 |
} // namespace internal |
| 215 |
|
| 216 |
/// Return a readable string representation of x for display purposes. |
| 217 |
PN_CPP_EXTERN std::string to_string(const scalar_base& x); |
| 218 |
|
| 219 |
} // proton |
| 220 |
|
| 221 |
#endif // PROTON_SCALAR_BASE_HPP |