Make json_pointer usable as map key

This commit is contained in:
Florian Albrechtskirchinger 2022-08-07 09:43:42 +02:00
parent b0422f8013
commit 5fe8b92951
No known key found for this signature in database
GPG Key ID: 19618CE9B2D4BE6D
2 changed files with 44 additions and 4 deletions

View File

@ -847,7 +847,7 @@ class json_pointer
} }
public: public:
#ifdef JSON_HAS_CPP_20 #if JSON_HAS_THREE_WAY_COMPARISON
/// @brief compares two JSON pointers for equality /// @brief compares two JSON pointers for equality
/// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
template<typename RefStringTypeRhs> template<typename RefStringTypeRhs>
@ -862,6 +862,13 @@ class json_pointer
{ {
return *this == json_pointer(rhs); return *this == json_pointer(rhs);
} }
/// @brief 3-way compares two JSON pointers
template<typename RefStringTypeRhs>
std::strong_ordering operator<=>(const json_pointer<RefStringTypeRhs>& rhs) const noexcept // *NOPAD*
{
return reference_tokens <=> rhs.reference_tokens; // *NOPAD*
}
#else #else
/// @brief compares two JSON pointers for equality /// @brief compares two JSON pointers for equality
/// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
@ -904,6 +911,12 @@ class json_pointer
// NOLINTNEXTLINE(readability-redundant-declaration) // NOLINTNEXTLINE(readability-redundant-declaration)
friend bool operator!=(const StringType& lhs, friend bool operator!=(const StringType& lhs,
const json_pointer<RefStringTypeRhs>& rhs); const json_pointer<RefStringTypeRhs>& rhs);
/// @brief compares two JSON pointer for less-than
template<typename RefStringTypeLhs, typename RefStringTypeRhs>
// NOLINTNEXTLINE(readability-redundant-declaration)
friend bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
const json_pointer<RefStringTypeRhs>& rhs) noexcept;
#endif #endif
private: private:
@ -911,7 +924,7 @@ class json_pointer
std::vector<string_t> reference_tokens; std::vector<string_t> reference_tokens;
}; };
#ifndef JSON_HAS_CPP_20 #if !JSON_HAS_THREE_WAY_COMPARISON
// functions cannot be defined inside class due to ODR violations // functions cannot be defined inside class due to ODR violations
template<typename RefStringTypeLhs, typename RefStringTypeRhs> template<typename RefStringTypeLhs, typename RefStringTypeRhs>
inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs, inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
@ -958,6 +971,13 @@ inline bool operator!=(const StringType& lhs,
{ {
return !(lhs == rhs); return !(lhs == rhs);
} }
template<typename RefStringTypeLhs, typename RefStringTypeRhs>
inline bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
const json_pointer<RefStringTypeRhs>& rhs) noexcept
{
return lhs.reference_tokens < rhs.reference_tokens;
}
#endif #endif
NLOHMANN_JSON_NAMESPACE_END NLOHMANN_JSON_NAMESPACE_END

View File

@ -14449,7 +14449,7 @@ class json_pointer
} }
public: public:
#ifdef JSON_HAS_CPP_20 #if JSON_HAS_THREE_WAY_COMPARISON
/// @brief compares two JSON pointers for equality /// @brief compares two JSON pointers for equality
/// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
template<typename RefStringTypeRhs> template<typename RefStringTypeRhs>
@ -14464,6 +14464,13 @@ class json_pointer
{ {
return *this == json_pointer(rhs); return *this == json_pointer(rhs);
} }
/// @brief 3-way compares two JSON pointers
template<typename RefStringTypeRhs>
std::strong_ordering operator<=>(const json_pointer<RefStringTypeRhs>& rhs) const noexcept // *NOPAD*
{
return reference_tokens <=> rhs.reference_tokens; // *NOPAD*
}
#else #else
/// @brief compares two JSON pointers for equality /// @brief compares two JSON pointers for equality
/// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
@ -14506,6 +14513,12 @@ class json_pointer
// NOLINTNEXTLINE(readability-redundant-declaration) // NOLINTNEXTLINE(readability-redundant-declaration)
friend bool operator!=(const StringType& lhs, friend bool operator!=(const StringType& lhs,
const json_pointer<RefStringTypeRhs>& rhs); const json_pointer<RefStringTypeRhs>& rhs);
/// @brief compares two JSON pointer for less-than
template<typename RefStringTypeLhs, typename RefStringTypeRhs>
// NOLINTNEXTLINE(readability-redundant-declaration)
friend bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
const json_pointer<RefStringTypeRhs>& rhs) noexcept;
#endif #endif
private: private:
@ -14513,7 +14526,7 @@ class json_pointer
std::vector<string_t> reference_tokens; std::vector<string_t> reference_tokens;
}; };
#ifndef JSON_HAS_CPP_20 #if !JSON_HAS_THREE_WAY_COMPARISON
// functions cannot be defined inside class due to ODR violations // functions cannot be defined inside class due to ODR violations
template<typename RefStringTypeLhs, typename RefStringTypeRhs> template<typename RefStringTypeLhs, typename RefStringTypeRhs>
inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs, inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
@ -14560,6 +14573,13 @@ inline bool operator!=(const StringType& lhs,
{ {
return !(lhs == rhs); return !(lhs == rhs);
} }
template<typename RefStringTypeLhs, typename RefStringTypeRhs>
inline bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
const json_pointer<RefStringTypeRhs>& rhs) noexcept
{
return lhs.reference_tokens < rhs.reference_tokens;
}
#endif #endif
NLOHMANN_JSON_NAMESPACE_END NLOHMANN_JSON_NAMESPACE_END