Skip to content

Commit

Permalink
Add operator< to , to allow use as a map key
Browse files Browse the repository at this point in the history
  • Loading branch information
khiner committed Aug 5, 2022
1 parent 7b6cf59 commit 9ebfeb7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
22 changes: 22 additions & 0 deletions include/nlohmann/detail/json_pointer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,15 @@ class json_pointer
{
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
/// @brief compares two JSON pointers for equality
/// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
Expand Down Expand Up @@ -904,6 +913,12 @@ class json_pointer
// NOLINTNEXTLINE(readability-redundant-declaration)
friend bool operator!=(const StringType& lhs,
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

private:
Expand Down Expand Up @@ -958,6 +973,13 @@ inline bool operator!=(const StringType& lhs,
{
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

NLOHMANN_JSON_NAMESPACE_END
22 changes: 22 additions & 0 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14464,6 +14464,15 @@ class json_pointer
{
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
/// @brief compares two JSON pointers for equality
/// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
Expand Down Expand Up @@ -14506,6 +14515,12 @@ class json_pointer
// NOLINTNEXTLINE(readability-redundant-declaration)
friend bool operator!=(const StringType& lhs,
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

private:
Expand Down Expand Up @@ -14560,6 +14575,13 @@ inline bool operator!=(const StringType& lhs,
{
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

NLOHMANN_JSON_NAMESPACE_END
Expand Down

0 comments on commit 9ebfeb7

Please sign in to comment.