Add operator< to , to allow use as a map key

This commit is contained in:
Karl Hiner 2022-08-05 14:35:38 -04:00
parent 7b6cf5918b
commit 9ebfeb7087
2 changed files with 44 additions and 0 deletions

View File

@ -862,6 +862,15 @@ class json_pointer
{ {
return *this == json_pointer(rhs); return *this == json_pointer(rhs);
} }
/// @brief compares JSON pointer and string for less-than
template<typename RefStringTypeLhs, typename RefStringTypeRhs>
friend bool operator<(json_pointer<RefStringTypeLhs> const& lhs,
json_pointer<RefStringTypeRhs> const& rhs) noexcept
{
return lhs.reference_tokens < rhs.reference_tokens;
}
#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 +913,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 string and JSON pointer for less-than
template<typename RefStringTypeRhs, typename StringType>
// NOLINTNEXTLINE(readability-redundant-declaration)
friend bool operator<(const StringType& lhs,
const json_pointer<RefStringTypeRhs>& rhs);
#endif #endif
private: private:
@ -958,6 +973,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

@ -14464,6 +14464,15 @@ class json_pointer
{ {
return *this == json_pointer(rhs); return *this == json_pointer(rhs);
} }
/// @brief compares JSON pointer and string for less-than
template<typename RefStringTypeLhs, typename RefStringTypeRhs>
friend bool operator<(json_pointer<RefStringTypeLhs> const& lhs,
json_pointer<RefStringTypeRhs> const& rhs) noexcept
{
return lhs.reference_tokens < rhs.reference_tokens;
}
#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 +14515,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 string and JSON pointer for less-than
template<typename RefStringTypeRhs, typename StringType>
// NOLINTNEXTLINE(readability-redundant-declaration)
friend bool operator<(const StringType& lhs,
const json_pointer<RefStringTypeRhs>& rhs);
#endif #endif
private: private:
@ -14560,6 +14575,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