1#ifndef PROTON_SCALAR_BASE_HPP
2#define PROTON_SCALAR_BASE_HPP
28#include "./internal/comparable.hpp"
29#include "./internal/export.hpp"
30#include "./internal/type_traits.hpp"
37#include <proton/type_compat.h>
56template<
class T> T
get(
const scalar_base& s);
60class scalar_base :
private internal::comparable<scalar_base> {
66 PN_CPP_EXTERN
bool empty()
const;
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);
94 PN_CPP_EXTERN
void put_(
const decimal32&);
95 PN_CPP_EXTERN
void put_(
const decimal64&);
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);
102 PN_CPP_EXTERN
void put_(
const null&);
103 PN_CPP_EXTERN
void put_(
decltype(
nullptr));
105 template<
class T>
void put(
const T& x) { putter<T>::put(*
this, x); }
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;
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;
133 template <
class T,
class Enable=
void>
struct putter {
134 static void put(
scalar_base& s,
const T& x) { s.put_(x); }
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));
142 template <
class T,
class Enable=
void>
144 static T
get(
const scalar_base& s) { T x; s.get_(x);
return x; }
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;
153 void ok(pn_type_t)
const;
154 void set(
const pn_atom_t&);
155 void set(
const binary& x, pn_type_t t);
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);
170template<
class T> T
get(
const scalar_base& s) {
171 return scalar_base::getter<T>::get(s);
174template <
class R,
class F> R visit(
const scalar_base& s, F f) {
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()));
200PN_CPP_EXTERN conversion_error make_coercion_error(
const char* cpp_type,
type_id amqp_type);
202template<
class T>
struct coerce_op {
204 typename std::enable_if<std::is_convertible<U, T>::value, T>::type operator()(
const U& x) {
205 return static_cast<T
>(x);
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);
213template <
class T> T
coerce(
const scalar_base& s) {
return visit<T>(s, coerce_op<T>()); }
Arbitrary binary data.
Definition binary.hpp:40
A 128-bit decimal floating-point value.
Definition decimal.hpp:52
A 32-bit decimal floating-point value.
Definition decimal.hpp:46
A 64-bit decimal floating-point value.
Definition decimal.hpp:49
The type of the AMQP null value.
Definition null.hpp:38
The base class for scalar types.
Definition scalar_base.hpp:60
friend bool operator==(const scalar_base &x, const scalar_base &y)
Compare.
friend bool operator<(const scalar_base &x, const scalar_base &y)
Compare.
friend std::ostream & operator<<(std::ostream &o, const scalar_base &x)
Print the contained value.
bool empty() const
True if there is no value, i.e. type() == NULL_TYPE.
type_id type() const
AMQP type of data stored in the scalar.
A string that represents the AMQP symbol type.
Definition symbol.hpp:35
A 64-bit timestamp in milliseconds since the Unix epoch.
Definition timestamp.hpp:35
A 16-byte universally unique identifier.
Definition uuid.hpp:37
The main Proton namespace.
Definition annotation_key.hpp:33
std::string to_string(const message &)
Human readable string representation.
std::string type_name(type_id)
Get the name of the AMQP type.
T get(const scalar &s)
Get a contained value of type T.
Definition scalar.hpp:60
type_id
An identifier for AMQP types.
Definition type_id.hpp:37
@ UBYTE
Unsigned 8-bit integer.
Definition type_id.hpp:40
@ DOUBLE
64-bit binary floating point.
Definition type_id.hpp:51
@ TIMESTAMP
Signed 64-bit milliseconds since the epoch.
Definition type_id.hpp:49
@ CHAR
32-bit unicode character.
Definition type_id.hpp:46
@ UINT
Unsigned 32-bit integer.
Definition type_id.hpp:44
@ ULONG
Unsigned 64-bit integer.
Definition type_id.hpp:47
@ SHORT
Signed 16-bit integer.
Definition type_id.hpp:43
@ BOOLEAN
Boolean true or false.
Definition type_id.hpp:39
@ DECIMAL32
32-bit decimal floating point.
Definition type_id.hpp:52
@ DECIMAL64
64-bit decimal floating point.
Definition type_id.hpp:53
@ USHORT
Unsigned 16-bit integer.
Definition type_id.hpp:42
@ FLOAT
32-bit binary floating point.
Definition type_id.hpp:50
@ BYTE
Signed 8-bit integer.
Definition type_id.hpp:41
@ LONG
Signed 64-bit integer.
Definition type_id.hpp:48
@ UUID
16-byte UUID.
Definition type_id.hpp:55
@ DECIMAL128
128-bit decimal floating point.
Definition type_id.hpp:54
@ SYMBOL
Variable-length encoded string.
Definition type_id.hpp:58
@ BINARY
Variable-length sequence of bytes.
Definition type_id.hpp:56
@ STRING
Variable-length utf8-encoded string.
Definition type_id.hpp:57
@ INT
Signed 32-bit integer.
Definition type_id.hpp:45
T coerce(const annotation_key &x)
Get the binary value or throw conversion_error.
Definition annotation_key.hpp:83
A string that represents the AMQP symbol type.
A 64-bit timestamp in milliseconds since the Unix epoch.
Type IDs for AMQP data types.
Forward declarations for Proton types used to represent AMQP types.
A 16-byte universally unique identifier.