| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "msg.hpp" |
| 21 |
#include "types_internal.hpp" |
| 22 |
|
| 23 |
#include "proton/binary.hpp" |
| 24 |
#include "proton/decimal.hpp" |
| 25 |
#include "proton/internal/type_traits.hpp" |
| 26 |
#include "proton/scalar_base.hpp" |
| 27 |
#include "proton/symbol.hpp" |
| 28 |
#include "proton/timestamp.hpp" |
| 29 |
#include "proton/uuid.hpp" |
| 30 |
|
| 31 |
#include <ostream> |
| 32 |
#include <sstream> |
| 33 |
|
| 34 |
namespace proton { |
| 35 |
|
| 36 |
scalar_base::scalar_base() { atom_.type = PN_NULL; } |
| 37 |
scalar_base::scalar_base(const pn_atom_t& a) { set(a); } |
| 38 |
scalar_base::scalar_base(const scalar_base& x) { set(x.atom_); } |
| 39 |
|
| 40 |
scalar_base& scalar_base::operator=(const scalar_base& x) { |
| 41 |
if (this != &x) |
| 42 |
set(x.atom_); |
| 43 |
return *this; |
| 44 |
} |
| 45 |
|
| 46 |
type_id scalar_base::type() const { return type_id(atom_.type); } |
| 47 |
|
| 48 |
bool scalar_base::empty() const { return type() == NULL_TYPE; } |
| 49 |
|
| 50 |
void scalar_base::set(const binary& x, pn_type_t t) { |
| 51 |
atom_.type = t; |
| 52 |
bytes_ = x; |
| 53 |
atom_.u.as_bytes = pn_bytes(bytes_); |
| 54 |
} |
| 55 |
|
| 56 |
void scalar_base::set(const pn_atom_t& atom) { |
| 57 |
if (type_id_is_string_like(type_id(atom.type))) { |
| 58 |
set(bin(atom.u.as_bytes), atom.type); |
| 59 |
} else { |
| 60 |
atom_ = atom; |
| 61 |
bytes_.clear(); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
void scalar_base::put_(bool x) { atom_.u.as_bool = x; atom_.type = PN_BOOL; } |
| 66 |
void scalar_base::put_(uint8_t x) { atom_.u.as_ubyte = x; atom_.type = PN_UBYTE; } |
| 67 |
void scalar_base::put_(int8_t x) { atom_.u.as_byte = x; atom_.type = PN_BYTE; } |
| 68 |
void scalar_base::put_(uint16_t x) { atom_.u.as_ushort = x; atom_.type = PN_USHORT; } |
| 69 |
void scalar_base::put_(int16_t x) { atom_.u.as_short = x; atom_.type = PN_SHORT; } |
| 70 |
void scalar_base::put_(uint32_t x) { atom_.u.as_uint = x; atom_.type = PN_UINT; } |
| 71 |
void scalar_base::put_(int32_t x) { atom_.u.as_int = x; atom_.type = PN_INT; } |
| 72 |
void scalar_base::put_(uint64_t x) { atom_.u.as_ulong = x; atom_.type = PN_ULONG; } |
| 73 |
void scalar_base::put_(int64_t x) { atom_.u.as_long = x; atom_.type = PN_LONG; } |
| 74 |
void scalar_base::put_(wchar_t x) { atom_.u.as_char = x; atom_.type = PN_CHAR; } |
| 75 |
void scalar_base::put_(float x) { atom_.u.as_float = x; atom_.type = PN_FLOAT; } |
| 76 |
void scalar_base::put_(double x) { atom_.u.as_double = x; atom_.type = PN_DOUBLE; } |
| 77 |
void scalar_base::put_(timestamp x) { atom_.u.as_timestamp = x.milliseconds(); atom_.type = PN_TIMESTAMP; } |
| 78 |
void scalar_base::put_(const decimal32& x) { byte_copy(atom_.u.as_decimal32, x); atom_.type = PN_DECIMAL32;; } |
| 79 |
void scalar_base::put_(const decimal64& x) { byte_copy(atom_.u.as_decimal64, x); atom_.type = PN_DECIMAL64; } |
| 80 |
void scalar_base::put_(const decimal128& x) { byte_copy(atom_.u.as_decimal128, x); atom_.type = PN_DECIMAL128; } |
| 81 |
void scalar_base::put_(const uuid& x) { byte_copy(atom_.u.as_uuid, x); atom_.type = PN_UUID; } |
| 82 |
void scalar_base::put_(const std::string& x) { set(binary(x), PN_STRING); } |
| 83 |
void scalar_base::put_(const symbol& x) { set(binary(x), PN_SYMBOL); } |
| 84 |
void scalar_base::put_(const binary& x) { set(x, PN_BINARY); } |
| 85 |
void scalar_base::put_(const char* x) { set(binary(std::string(x)), PN_STRING); } |
| 86 |
void scalar_base::put_(const null&) { atom_.type = PN_NULL; } |
| 87 |
void scalar_base::put_(decltype(nullptr)) { atom_.type = PN_NULL; } |
| 88 |
|
| 89 |
|
| 90 |
void scalar_base::ok(pn_type_t t) const { |
| 91 |
if (atom_.type != t) throw make_conversion_error(type_id(t), type()); |
| 92 |
} |
| 93 |
|
| 94 |
void scalar_base::get_(bool& x) const { ok(PN_BOOL); x = atom_.u.as_bool; } |
| 95 |
void scalar_base::get_(uint8_t& x) const { ok(PN_UBYTE); x = atom_.u.as_ubyte; } |
| 96 |
void scalar_base::get_(int8_t& x) const { ok(PN_BYTE); x = atom_.u.as_byte; } |
| 97 |
void scalar_base::get_(uint16_t& x) const { ok(PN_USHORT); x = atom_.u.as_ushort; } |
| 98 |
void scalar_base::get_(int16_t& x) const { ok(PN_SHORT); x = atom_.u.as_short; } |
| 99 |
void scalar_base::get_(uint32_t& x) const { ok(PN_UINT); x = atom_.u.as_uint; } |
| 100 |
void scalar_base::get_(int32_t& x) const { ok(PN_INT); x = atom_.u.as_int; } |
| 101 |
void scalar_base::get_(wchar_t& x) const { ok(PN_CHAR); x = wchar_t(atom_.u.as_char); } |
| 102 |
void scalar_base::get_(uint64_t& x) const { ok(PN_ULONG); x = atom_.u.as_ulong; } |
| 103 |
void scalar_base::get_(int64_t& x) const { ok(PN_LONG); x = atom_.u.as_long; } |
| 104 |
void scalar_base::get_(timestamp& x) const { ok(PN_TIMESTAMP); x = atom_.u.as_timestamp; } |
| 105 |
void scalar_base::get_(float& x) const { ok(PN_FLOAT); x = atom_.u.as_float; } |
| 106 |
void scalar_base::get_(double& x) const { ok(PN_DOUBLE); x = atom_.u.as_double; } |
| 107 |
void scalar_base::get_(decimal32& x) const { ok(PN_DECIMAL32); byte_copy(x, atom_.u.as_decimal32); } |
| 108 |
void scalar_base::get_(decimal64& x) const { ok(PN_DECIMAL64); byte_copy(x, atom_.u.as_decimal64); } |
| 109 |
void scalar_base::get_(decimal128& x) const { ok(PN_DECIMAL128); byte_copy(x, atom_.u.as_decimal128); } |
| 110 |
void scalar_base::get_(uuid& x) const { ok(PN_UUID); byte_copy(x, atom_.u.as_uuid); } |
| 111 |
void scalar_base::get_(std::string& x) const { ok(PN_STRING); x = std::string(bytes_.begin(), bytes_.end()); } |
| 112 |
void scalar_base::get_(symbol& x) const { ok(PN_SYMBOL); x = symbol(bytes_.begin(), bytes_.end()); } |
| 113 |
void scalar_base::get_(binary& x) const { ok(PN_BINARY); x = bytes_; } |
| 114 |
void scalar_base::get_(null&) const { ok(PN_NULL); } |
| 115 |
void scalar_base::get_(decltype(nullptr)&) const { ok(PN_NULL); } |
| 116 |
|
| 117 |
namespace { |
| 118 |
|
| 119 |
struct equal_op { |
| 120 |
const scalar_base& x; |
| 121 |
equal_op(const scalar_base& s) : x(s) {} |
| 122 |
template<class T> bool operator()(const T& y) { return (internal::get<T>(x) == y); } |
| 123 |
}; |
| 124 |
|
| 125 |
struct less_op { |
| 126 |
const scalar_base& x; |
| 127 |
less_op(const scalar_base& s) : x(s) {} |
| 128 |
template<class T> bool operator()(const T& y) { return (y < internal::get<T>(x)); } |
| 129 |
}; |
| 130 |
|
| 131 |
struct ostream_op { |
| 132 |
std::ostream& o; |
| 133 |
ostream_op(std::ostream& o_) : o(o_) {} |
| 134 |
template<class T> std::ostream& operator()(const T& x) { return o << x; } |
| 135 |
}; |
| 136 |
|
| 137 |
} // namespace |
| 138 |
|
| 139 |
bool operator==(const scalar_base& x, const scalar_base& y) { |
| 140 |
if (x.type() != y.type()) return false; |
| 141 |
if (x.type() == NULL_TYPE) return true; |
| 142 |
return internal::visit<bool>(x, equal_op(y)); |
| 143 |
} |
| 144 |
|
| 145 |
bool operator<(const scalar_base& x, const scalar_base& y) { |
| 146 |
if (x.type() != y.type()) return x.type() < y.type(); |
| 147 |
if (x.type() == NULL_TYPE) return false; |
| 148 |
return internal::visit<bool>(x, less_op(y)); |
| 149 |
} |
| 150 |
|
| 151 |
std::ostream& operator<<(std::ostream& o, const scalar_base& s) { |
| 152 |
switch (s.type()) { |
| 153 |
case NULL_TYPE: return o << "null"; |
| 154 |
case BYTE: return o << static_cast<int>(internal::get<int8_t>(s)); |
| 155 |
case UBYTE: return o << static_cast<unsigned int>(internal::get<uint8_t>(s)); |
| 156 |
// Other types printed using normal C++ operator << |
| 157 |
default: return internal::visit<std::ostream&>(s, ostream_op(o)); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
namespace internal { |
| 162 |
|
| 163 |
conversion_error make_coercion_error(const char* cpp, type_id amqp) { |
| 164 |
return conversion_error(std::string("invalid proton::coerce<") + cpp + ">(" + type_name(amqp) + ")"); |
| 165 |
} |
| 166 |
|
| 167 |
} // internal |
| 168 |
|
| 169 |
std::string to_string(const scalar_base& x) { |
| 170 |
std::ostringstream os; |
| 171 |
os << std::boolalpha << x; |
| 172 |
return os.str(); |
| 173 |
} |
| 174 |
|
| 175 |
} // proton |