scalar_base.hpp
Go to the documentation of this file.
#ifndef PROTON_SCALAR_BASE_HPP
#define PROTON_SCALAR_BASE_HPP
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
#include "./binary.hpp"
#include "./decimal.hpp"
#include "./error.hpp"
#include "./internal/comparable.hpp"
#include "./internal/export.hpp"
#include "./internal/type_traits.hpp"
#include "./symbol.hpp"
#include "./timestamp.hpp"
#include "./type_id.hpp"
#include "./types_fwd.hpp"
#include "./uuid.hpp"
#include <proton/type_compat.h>
#include <iosfwd>
#include <string>
#include <typeinfo>
namespace proton {
class scalar_base;
namespace codec {
class decoder;
class encoder;
}
namespace internal {
template<class T> T get(const scalar_base& s);
}
class scalar_base : private internal::comparable<scalar_base> {
public:
PN_CPP_EXTERN type_id type() const;
PN_CPP_EXTERN bool empty() const;
friend PN_CPP_EXTERN bool operator<(const scalar_base& x, const scalar_base& y);
friend PN_CPP_EXTERN bool operator==(const scalar_base& x, const scalar_base& y);
friend PN_CPP_EXTERN std::ostream& operator<<(std::ostream& o, const scalar_base& x);
protected:
PN_CPP_EXTERN scalar_base(const pn_atom_t& a);
PN_CPP_EXTERN scalar_base();
PN_CPP_EXTERN scalar_base(const scalar_base&);
PN_CPP_EXTERN scalar_base& operator=(const scalar_base&);
PN_CPP_EXTERN void put_(bool);
PN_CPP_EXTERN void put_(uint8_t);
PN_CPP_EXTERN void put_(int8_t);
PN_CPP_EXTERN void put_(uint16_t);
PN_CPP_EXTERN void put_(int16_t);
PN_CPP_EXTERN void put_(uint32_t);
PN_CPP_EXTERN void put_(int32_t);
PN_CPP_EXTERN void put_(uint64_t);
PN_CPP_EXTERN void put_(int64_t);
PN_CPP_EXTERN void put_(wchar_t);
PN_CPP_EXTERN void put_(float);
PN_CPP_EXTERN void put_(double);
PN_CPP_EXTERN void put_(timestamp);
PN_CPP_EXTERN void put_(const decimal32&);
PN_CPP_EXTERN void put_(const decimal64&);
PN_CPP_EXTERN void put_(const decimal128&);
PN_CPP_EXTERN void put_(const uuid&);
PN_CPP_EXTERN void put_(const std::string&);
PN_CPP_EXTERN void put_(const symbol&);
PN_CPP_EXTERN void put_(const binary&);
PN_CPP_EXTERN void put_(const char* s);
PN_CPP_EXTERN void put_(const null&);
template<class T> void put(const T& x) { putter<T>::put(*this, x); }
private:
PN_CPP_EXTERN void get_(bool&) const;
PN_CPP_EXTERN void get_(uint8_t&) const;
PN_CPP_EXTERN void get_(int8_t&) const;
PN_CPP_EXTERN void get_(uint16_t&) const;
PN_CPP_EXTERN void get_(int16_t&) const;
PN_CPP_EXTERN void get_(uint32_t&) const;
PN_CPP_EXTERN void get_(int32_t&) const;
PN_CPP_EXTERN void get_(uint64_t&) const;
PN_CPP_EXTERN void get_(int64_t&) const;
PN_CPP_EXTERN void get_(wchar_t&) const;
PN_CPP_EXTERN void get_(float&) const;
PN_CPP_EXTERN void get_(double&) const;
PN_CPP_EXTERN void get_(timestamp&) const;
PN_CPP_EXTERN void get_(decimal32&) const;
PN_CPP_EXTERN void get_(decimal64&) const;
PN_CPP_EXTERN void get_(decimal128&) const;
PN_CPP_EXTERN void get_(uuid&) const;
PN_CPP_EXTERN void get_(std::string&) const;
PN_CPP_EXTERN void get_(symbol&) const;
PN_CPP_EXTERN void get_(binary&) const;
PN_CPP_EXTERN void get_(null&) const;
// use template structs, functions cannot be partially specialized.
template <class T, class Enable=void> struct putter {
static void put(scalar_base& s, const T& x) { s.put_(x); }
};
template <class T>
struct putter<T, typename internal::enable_if<internal::is_unknown_integer<T>::value>::type> {
static void put(scalar_base& s, const T& x) {
s.put_(static_cast<typename internal::known_integer<T>::type>(x));
}
};
template <class T, class Enable=void>
struct getter {
static T get(const scalar_base& s) { T x; s.get_(x); return x; }
};
template <class T>
struct getter<T, typename internal::enable_if<internal::is_unknown_integer<T>::value>::type> {
static T get(const scalar_base& s) {
typename internal::known_integer<T>::type x; s.get_(x); return x;
}
};
void ok(pn_type_t) const;
void set(const pn_atom_t&);
void set(const binary& x, pn_type_t t);
pn_atom_t atom_;
binary bytes_; // Hold binary data.
friend class message;
friend class codec::encoder;
friend class codec::decoder;
template<class T> friend T internal::get(const scalar_base& s);
};
namespace internal {
template<class T> T get(const scalar_base& s) {
return scalar_base::getter<T>::get(s);
}
template <class R, class F> R visit(const scalar_base& s, F f) {
switch(s.type()) {
case BOOLEAN: return f(internal::get<bool>(s));
case UBYTE: return f(internal::get<uint8_t>(s));
case BYTE: return f(internal::get<int8_t>(s));
case USHORT: return f(internal::get<uint16_t>(s));
case SHORT: return f(internal::get<int16_t>(s));
case UINT: return f(internal::get<uint32_t>(s));
case INT: return f(internal::get<int32_t>(s));
case CHAR: return f(internal::get<wchar_t>(s));
case ULONG: return f(internal::get<uint64_t>(s));
case LONG: return f(internal::get<int64_t>(s));
case TIMESTAMP: return f(internal::get<timestamp>(s));
case FLOAT: return f(internal::get<float>(s));
case DOUBLE: return f(internal::get<double>(s));
case DECIMAL32: return f(internal::get<decimal32>(s));
case DECIMAL64: return f(internal::get<decimal64>(s));
case DECIMAL128: return f(internal::get<decimal128>(s));
case UUID: return f(internal::get<uuid>(s));
case BINARY: return f(internal::get<binary>(s));
case STRING: return f(internal::get<std::string>(s));
case SYMBOL: return f(internal::get<symbol>(s));
default: throw conversion_error("invalid scalar type "+type_name(s.type()));
}
}
PN_CPP_EXTERN conversion_error make_coercion_error(const char* cpp_type, type_id amqp_type);
template<class T> struct coerce_op {
template <class U>
typename enable_if<is_convertible<U, T>::value, T>::type operator()(const U& x) {
return static_cast<T>(x);
}
template <class U>
typename enable_if<!is_convertible<U, T>::value, T>::type operator()(const U&) {
throw make_coercion_error(typeid(T).name(), type_id_of<U>::value);
}
};
template <class T> T coerce(const scalar_base& s) { return visit<T>(s, coerce_op<T>()); }
} // namespace internal
PN_CPP_EXTERN std::string to_string(const scalar_base& x);
} // proton
#endif // PROTON_SCALAR_BASE_HPP
proton::get< symbol >
symbol get< symbol >(const annotation_key &x)
Get the symbol value or throw conversion_error.
Definition: annotation_key.hpp:77
proton::decimal32
A 32-bit decimal floating-point value.
Definition: decimal.hpp:46
proton::TIMESTAMP
Signed 64-bit milliseconds since the epoch.
Definition: type_id.hpp:49
proton::scalar_base::operator<
friend bool operator<(const scalar_base &x, const scalar_base &y)
Compare.
symbol.hpp
proton::scalar_base
The base class for scalar types.
Definition: scalar_base.hpp:60
proton::UBYTE
Unsigned 8-bit integer.
Definition: type_id.hpp:40
proton::DECIMAL128
128-bit decimal floating point.
Definition: type_id.hpp:54
type_id.hpp
proton::BOOLEAN
Boolean true or false.
Definition: type_id.hpp:39
proton::decimal64
A 64-bit decimal floating-point value.
Definition: decimal.hpp:49
proton::INT
Signed 32-bit integer.
Definition: type_id.hpp:45
uuid.hpp
proton::get< binary >
binary get< binary >(const message_id &x)
Get the binary value or throw conversion_error.
Definition: message_id.hpp:85
proton::UINT
Unsigned 32-bit integer.
Definition: type_id.hpp:44
proton::ULONG
Unsigned 64-bit integer.
Definition: type_id.hpp:47
proton::FLOAT
32-bit binary floating point.
Definition: type_id.hpp:50
proton::scalar_base::type
type_id type() const
AMQP type of data stored in the scalar.
proton::UUID
16-byte UUID.
Definition: type_id.hpp:55
proton::DECIMAL32
32-bit decimal floating point.
Definition: type_id.hpp:52
proton::USHORT
Unsigned 16-bit integer.
Definition: type_id.hpp:42
proton::BYTE
Signed 8-bit integer.
Definition: type_id.hpp:41
proton::scalar_base::operator==
friend bool operator==(const scalar_base &x, const scalar_base &y)
Compare.
proton::type_id
type_id
An identifier for AMQP types.
Definition: type_id.hpp:37
proton::coerce
T coerce(const annotation_key &x)
Get the binary value or throw conversion_error.
Definition: annotation_key.hpp:83
proton::symbol
A string that represents the AMQP symbol type.
Definition: symbol.hpp:35
proton::LONG
Signed 64-bit integer.
Definition: type_id.hpp:48
proton::get
T get(const scalar &s)
Get a contained value of type T.
Definition: scalar.hpp:58
proton::to_string
std::string to_string(const scalar_base &x)
Return a readable string representation of x for display purposes.
proton::scalar_base::operator<<
friend std::ostream & operator<<(std::ostream &o, const scalar_base &x)
Print the contained value.
proton::get< uint64_t >
uint64_t get< uint64_t >(const annotation_key &x)
Get the uint64_t value or throw conversion_error.
Definition: annotation_key.hpp:72
proton::BINARY
Variable-length sequence of bytes.
Definition: type_id.hpp:56
proton::codec::decoder
Unsettled API - A stream-like decoder from AMQP bytes to C++ values.
Definition: decoder.hpp:56
proton::SHORT
Signed 16-bit integer.
Definition: type_id.hpp:43
proton::scalar_base::empty
bool empty() const
True if there is no value, i.e. type() == NULL_TYPE.
proton::CHAR
32-bit unicode character.
Definition: type_id.hpp:46
proton::get< uuid >
uuid get< uuid >(const message_id &x)
Get the uuid value or throw conversion_error.
Definition: message_id.hpp:83
proton::uuid
A 16-byte universally unique identifier.
Definition: uuid.hpp:37
proton::DOUBLE
64-bit binary floating point.
Definition: type_id.hpp:51
proton::codec::encoder
Unsettled API - A stream-like encoder from C++ values to AMQP bytes.
Definition: encoder.hpp:50
proton
The main Proton namespace.
Definition: annotation_key.hpp:33
timestamp.hpp
decimal.hpp
proton::type_name
std::string type_name(type_id)
Get the name of the AMQP type.
proton::STRING
Variable-length utf8-encoded string.
Definition: type_id.hpp:57
proton::decimal128
A 128-bit decimal floating-point value.
Definition: decimal.hpp:52
binary.hpp
proton::SYMBOL
Variable-length encoded string.
Definition: type_id.hpp:58
proton::DECIMAL64
64-bit decimal floating point.
Definition: type_id.hpp:53
proton::binary
Arbitrary binary data.
Definition: binary.hpp:40
proton::timestamp
A 64-bit timestamp in milliseconds since the Unix epoch.
Definition: timestamp.hpp:35
proton::message
An AMQP message.
Definition: message.hpp:50
types_fwd.hpp
error.hpp