Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
jsonpointer_token.h
1#ifndef SOURCEMETA_CORE_JSONPOINTER_TOKEN_H_
2#define SOURCEMETA_CORE_JSONPOINTER_TOKEN_H_
3
4#include <sourcemeta/core/json.h>
5
6#include <cassert> // assert
7
8namespace sourcemeta::core {
9
13template <typename PropertyT, typename Hash> class GenericToken {
14public:
16 using Value = JSON;
18 using Property = PropertyT;
20 using Index = Value::Array::size_type;
21
25 GenericToken(Property value, const Hash::HashType &property_hash)
26 : as_property_{true}, property_{std::move(value)},
27 data_hash{property_hash} {}
28
38 GenericToken(const Property &value) : GenericToken{value, HASHER(value)} {}
39
51 : as_property_{true}, property_{std::move(value)},
52 data_hash{HASHER(property_)} {}
53
63 GenericToken(const JSON::Char *const value)
64 : GenericToken{value, HASHER(value)} {}
65
76 : GenericToken{Property{value}, HASHER(Property{value})} {}
77
87 GenericToken(const Index value)
88 : as_property_{false}, property_{DEFAULT_PROPERTY}, data_index{value} {}
89
99 GenericToken(const int value)
100 : as_property_{false}, property_{DEFAULT_PROPERTY},
101 data_index{static_cast<Index>(value)} {
102 assert(value >= 0);
103 }
104
105#if defined(_MSC_VER)
115 GenericToken(const unsigned long value)
116 : as_property_{false}, property_{DEFAULT_PROPERTY}, data_index{value} {}
117#endif
118
129 [[nodiscard]] auto is_property() const noexcept -> bool {
130 return this->as_property_;
131 }
132
145 [[nodiscard]] auto is_hyphen() const noexcept -> bool {
146 return this->as_property_ && this->property_.size() == 1 &&
147 this->property_.front() == '\u002D';
148 }
149
160 [[nodiscard]] auto is_index() const noexcept -> bool {
161 return !this->as_property_;
162 }
163
176 [[nodiscard]] auto to_property() const noexcept -> const auto & {
177 assert(this->is_property());
178 if constexpr (requires { this->property_.get(); }) {
179 return this->property_.get();
180 } else {
181 return this->property_;
182 }
183 }
184
196 [[nodiscard]] auto property_hash() const noexcept -> const Hash::HashType & {
197 assert(this->is_property());
198 return this->data_hash;
199 }
200
214 [[nodiscard]] auto
216 const Hash::HashType &value_hash) const noexcept -> bool {
217 assert(this->is_property());
218 assert(HASHER(value.data(), value.size()) == value_hash);
219 if constexpr (requires { HASHER.is_perfect(value_hash); }) {
220 // A perfect hash is unique for the property it was computed from, so
221 // matching it settles the comparison on its own
222 return this->data_hash == value_hash &&
223 (HASHER.is_perfect(value_hash) || this->to_property() == value);
224 } else {
225 return this->data_hash == value_hash && this->to_property() == value;
226 }
227 }
228
240 [[nodiscard]] auto to_index() const noexcept -> Index {
241 assert(this->is_index());
242 return this->data_index;
243 }
244
261 [[nodiscard]] auto to_json() const -> JSON {
262 if (this->is_property()) {
263 return JSON{this->to_property()};
264 }
265 return JSON{this->to_index()};
266 }
267
269 auto operator==(const GenericToken<PropertyT, Hash> &other) const noexcept
270 -> bool {
271 if (this->as_property_ != other.as_property_) {
272 return false;
273 }
274 if (this->as_property_) {
275 if constexpr (requires { HASHER.is_perfect(this->data_hash); }) {
276 // A perfect hash is unique for the property it was computed from, so
277 // matching it settles the comparison on its own
278 if (HASHER.is_perfect(this->data_hash) &&
279 HASHER.is_perfect(other.data_hash)) {
280 return this->data_hash == other.data_hash;
281 }
282 }
283
284 return this->data_hash == other.data_hash &&
285 this->to_property() == other.to_property();
286 }
287 return this->data_index == other.data_index;
288 }
289
292 auto operator<(const GenericToken<PropertyT, Hash> &other) const noexcept
293 -> bool {
294 if (this->as_property_ && !other.as_property_) {
295 return true;
296 }
297 if (!this->as_property_ && other.as_property_) {
298 return false;
299 }
300 if (this->as_property_) {
301 return this->to_property() < other.to_property();
302 }
303 return this->data_index < other.data_index;
304 }
305
306private:
307 // We need this as a member for making WeakPointer work
308 inline static const Value::String DEFAULT_PROPERTY{};
309 inline static const Hash HASHER;
310
311 bool as_property_;
312 Property property_;
313 // A token holds a property or an index and never both, and the flag above
314 // tells which one is live, so the two share their storage
315 union {
316 Hash::HashType data_hash;
317 Index data_index;
318 };
319};
320
321} // namespace sourcemeta::core
322
323#endif
char Char
The character type used by the JSON document.
Definition json_value.h:42
std::basic_string< Char, CharTraits, Allocator< Char > > String
The string type used by the JSON document.
Definition json_value.h:52
std::basic_string_view< Char, CharTraits > StringView
The string view type used by the JSON document.
Definition json_value.h:54
Definition json_value.h:39
auto property_equals(const JSON::StringView value, const Hash::HashType &value_hash) const noexcept -> bool
Definition jsonpointer_token.h:215
auto to_json() const -> JSON
Definition jsonpointer_token.h:261
JSON Value
The JSON value type these tokens convert to.
Definition jsonpointer_token.h:16
GenericToken(const JSON::Char value)
Definition jsonpointer_token.h:75
Value::Array::size_type Index
The stored type of an array index token.
Definition jsonpointer_token.h:20
GenericToken(const Index value)
Definition jsonpointer_token.h:87
GenericToken(const int value)
Definition jsonpointer_token.h:99
auto is_hyphen() const noexcept -> bool
Definition jsonpointer_token.h:145
auto property_hash() const noexcept -> const Hash::HashType &
Definition jsonpointer_token.h:196
auto is_index() const noexcept -> bool
Definition jsonpointer_token.h:160
GenericToken(const Property &value)
Definition jsonpointer_token.h:38
GenericToken(Property &&value)
Definition jsonpointer_token.h:50
auto to_index() const noexcept -> Index
Definition jsonpointer_token.h:240
auto is_property() const noexcept -> bool
Definition jsonpointer_token.h:129
GenericToken(Property value, const Hash::HashType &property_hash)
Definition jsonpointer_token.h:25
auto to_property() const noexcept -> const auto &
Definition jsonpointer_token.h:176
GenericToken(const JSON::Char *const value)
Definition jsonpointer_token.h:63
PropertyT Property
The stored type of an object property token.
Definition jsonpointer_token.h:18
Definition jsonpointer_token.h:13