| 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 "proton_bits.hpp" |
| 21 |
#include "proton/internal/data.hpp" |
| 22 |
#include "proton/value.hpp" |
| 23 |
#include "proton/types.hpp" |
| 24 |
#include "proton/scalar.hpp" |
| 25 |
#include "proton/error.hpp" |
| 26 |
|
| 27 |
#include <ostream> |
| 28 |
#include <sstream> |
| 29 |
|
| 30 |
namespace proton { |
| 31 |
|
| 32 |
using codec::decoder; |
| 33 |
using codec::encoder; |
| 34 |
using codec::start; |
| 35 |
|
| 36 |
value::value() = default; |
| 37 |
value::value(pn_data_t *d) { data_ = make_wrapper(d); } |
| 38 |
value::value(const value& x) { *this = x; } |
| 39 |
value::value(value&& x) { swap(*this, x); } |
| 40 |
value& value::operator=(value&& x) { swap(*this, x); return *this; } |
| 41 |
|
| 42 |
value& value::operator=(const value& x) { |
| 43 |
if (this != &x) { |
| 44 |
if (x.empty()) |
| 45 |
clear(); |
| 46 |
else |
| 47 |
data().copy(x.data_); |
| 48 |
} |
| 49 |
return *this; |
| 50 |
} |
| 51 |
|
| 52 |
void swap(value& x, value& y) { std::swap(x.data_, y.data_); } |
| 53 |
|
| 54 |
void value::clear() { if (!!data_) data_.clear(); } |
| 55 |
|
| 56 |
namespace internal { |
| 57 |
|
| 58 |
// On demand |
| 59 |
internal::data& value_base::data() { |
| 60 |
if (!data_) |
| 61 |
data_ = internal::data::create(); |
| 62 |
return data_; |
| 63 |
} |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
type_id value::type() const { |
| 68 |
return (!data_ || data_.empty()) ? NULL_TYPE : codec::decoder(*this).next_type(); |
| 69 |
} |
| 70 |
|
| 71 |
bool value::empty() const { return type() == NULL_TYPE; } |
| 72 |
|
| 73 |
namespace { |
| 74 |
|
| 75 |
// Compare nodes, return -1 if a<b, 0 if a==b, +1 if a>b |
| 76 |
// Forward-declare so we can use it recursively. |
| 77 |
int compare_next(decoder& a, decoder& b); |
| 78 |
|
| 79 |
template <class T> int compare(const T& a, const T& b) { |
| 80 |
if (a < b) return -1; |
| 81 |
else if (a > b) return +1; |
| 82 |
else return 0; |
| 83 |
} |
| 84 |
|
| 85 |
int compare_container(decoder& a, decoder& b) { |
| 86 |
start sa, sb; |
| 87 |
a >> sa; |
| 88 |
b >> sb; |
| 89 |
// Compare described vs. not-described. |
| 90 |
int cmp = compare(sa.is_described, sb.is_described); |
| 91 |
if (cmp) return cmp; |
| 92 |
// Lexical sort (including descriptor if there is one) |
| 93 |
size_t min_size = std::min(sa.size, sb.size) + size_t(sa.is_described); |
| 94 |
for (size_t i = 0; i < min_size; ++i) { |
| 95 |
cmp = compare_next(a, b); |
| 96 |
if (cmp) return cmp; |
| 97 |
} |
| 98 |
return compare(sa.size, sb.size); |
| 99 |
} |
| 100 |
|
| 101 |
template <class T> int compare_simple(decoder& a, decoder& b) { |
| 102 |
T va = T(); |
| 103 |
T vb = T(); |
| 104 |
a >> va; |
| 105 |
b >> vb; |
| 106 |
return compare(va, vb); |
| 107 |
} |
| 108 |
|
| 109 |
int compare_next(decoder& a, decoder& b) { |
| 110 |
// Sort by type_id first. |
| 111 |
type_id ta = a.next_type(), tb = b.next_type(); |
| 112 |
int cmp = compare(ta, tb); |
| 113 |
if (cmp) return cmp; |
| 114 |
|
| 115 |
switch (ta) { |
| 116 |
case NULL_TYPE: return 0; |
| 117 |
case ARRAY: |
| 118 |
case LIST: |
| 119 |
case MAP: |
| 120 |
case DESCRIBED: |
| 121 |
return compare_container(a, b); |
| 122 |
case BOOLEAN: return compare_simple<bool>(a, b); |
| 123 |
case UBYTE: return compare_simple<uint8_t>(a, b); |
| 124 |
case BYTE: return compare_simple<int8_t>(a, b); |
| 125 |
case USHORT: return compare_simple<uint16_t>(a, b); |
| 126 |
case SHORT: return compare_simple<int16_t>(a, b); |
| 127 |
case UINT: return compare_simple<uint32_t>(a, b); |
| 128 |
case INT: return compare_simple<int32_t>(a, b); |
| 129 |
case ULONG: return compare_simple<uint64_t>(a, b); |
| 130 |
case LONG: return compare_simple<int64_t>(a, b); |
| 131 |
case CHAR: return compare_simple<wchar_t>(a, b); |
| 132 |
case TIMESTAMP: return compare_simple<timestamp>(a, b); |
| 133 |
case FLOAT: return compare_simple<float>(a, b); |
| 134 |
case DOUBLE: return compare_simple<double>(a, b); |
| 135 |
case DECIMAL32: return compare_simple<decimal32>(a, b); |
| 136 |
case DECIMAL64: return compare_simple<decimal64>(a, b); |
| 137 |
case DECIMAL128: return compare_simple<decimal128>(a, b); |
| 138 |
case UUID: return compare_simple<uuid>(a, b); |
| 139 |
case BINARY: return compare_simple<binary>(a, b); |
| 140 |
case STRING: return compare_simple<std::string>(a, b); |
| 141 |
case SYMBOL: return compare_simple<symbol>(a, b); |
| 142 |
} |
| 143 |
// Avoid unreached diagnostic from clang |
| 144 |
#if defined(__clang__) |
| 145 |
#pragma GCC diagnostic push |
| 146 |
#pragma GCC diagnostic ignored "-Wunreachable-code" |
| 147 |
#endif |
| 148 |
// Invalid but equal type_id, treat as equal. |
| 149 |
return 0; |
| 150 |
#if defined(__clang__) |
| 151 |
#pragma GCC diagnostic pop |
| 152 |
#endif |
| 153 |
} |
| 154 |
|
| 155 |
int compare(const value& x, const value& y) { |
| 156 |
decoder a(x), b(y); |
| 157 |
internal::state_guard s1(a), s2(b); |
| 158 |
a.rewind(); |
| 159 |
b.rewind(); |
| 160 |
while (a.more() && b.more()) { |
| 161 |
int cmp = compare_next(a, b); |
| 162 |
if (cmp != 0) return cmp; |
| 163 |
} |
| 164 |
if (b.more()) return -1; |
| 165 |
if (a.more()) return 1; |
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
} // namespace |
| 170 |
|
| 171 |
bool operator==(const value& x, const value& y) { |
| 172 |
if (x.empty() && y.empty()) return true; |
| 173 |
if (x.empty() || y.empty()) return false; |
| 174 |
return compare(x, y) == 0; |
| 175 |
} |
| 176 |
|
| 177 |
bool operator<(const value& x, const value& y) { |
| 178 |
if (x.empty() && y.empty()) return false; |
| 179 |
if (x.empty()) return true; // empty is < !empty |
| 180 |
return compare(x, y) < 0; |
| 181 |
} |
| 182 |
|
| 183 |
std::ostream& operator<<(std::ostream& o, const value& x) { |
| 184 |
if (type_id_is_scalar(x.type())) |
| 185 |
return o << proton::get<scalar>(x); // Print as a scalar |
| 186 |
// Use pn_inspect for complex types. |
| 187 |
proton::decoder d(x); |
| 188 |
return o << d; |
| 189 |
} |
| 190 |
|
| 191 |
std::string to_string(const value& x) { |
| 192 |
std::ostringstream os; |
| 193 |
os << std::boolalpha << x; |
| 194 |
return os.str(); |
| 195 |
} |
| 196 |
|
| 197 |
void value::reset(pn_data_t *d) { data_ = make_wrapper(d); } |
| 198 |
|
| 199 |
} // namespace proton |