From 9ebfeb708718c45d234724acab94fdfb5327bca5 Mon Sep 17 00:00:00 2001 From: Karl Hiner Date: Fri, 5 Aug 2022 14:35:38 -0400 Subject: [PATCH] Add `operator<` to , to allow use as a map key --- include/nlohmann/detail/json_pointer.hpp | 22 ++++++++++++++++++++++ single_include/nlohmann/json.hpp | 22 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index 28de45028..bdeae78f0 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -862,6 +862,15 @@ class json_pointer { return *this == json_pointer(rhs); } + + /// @brief compares JSON pointer and string for less-than + template + friend bool operator<(json_pointer const& lhs, + json_pointer 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/ @@ -904,6 +913,12 @@ class json_pointer // NOLINTNEXTLINE(readability-redundant-declaration) friend bool operator!=(const StringType& lhs, const json_pointer& rhs); + + /// @brief compares string and JSON pointer for less-than + template + // NOLINTNEXTLINE(readability-redundant-declaration) + friend bool operator<(const StringType& lhs, + const json_pointer& rhs); #endif private: @@ -958,6 +973,13 @@ inline bool operator!=(const StringType& lhs, { return !(lhs == rhs); } + +template +inline bool operator<(const json_pointer& lhs, + const json_pointer& rhs) noexcept +{ + return lhs.reference_tokens < rhs.reference_tokens; +} #endif NLOHMANN_JSON_NAMESPACE_END diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index beee0136c..3bf1b92fb 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -14464,6 +14464,15 @@ class json_pointer { return *this == json_pointer(rhs); } + + /// @brief compares JSON pointer and string for less-than + template + friend bool operator<(json_pointer const& lhs, + json_pointer 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/ @@ -14506,6 +14515,12 @@ class json_pointer // NOLINTNEXTLINE(readability-redundant-declaration) friend bool operator!=(const StringType& lhs, const json_pointer& rhs); + + /// @brief compares string and JSON pointer for less-than + template + // NOLINTNEXTLINE(readability-redundant-declaration) + friend bool operator<(const StringType& lhs, + const json_pointer& rhs); #endif private: @@ -14560,6 +14575,13 @@ inline bool operator!=(const StringType& lhs, { return !(lhs == rhs); } + +template +inline bool operator<(const json_pointer& lhs, + const json_pointer& rhs) noexcept +{ + return lhs.reference_tokens < rhs.reference_tokens; +} #endif NLOHMANN_JSON_NAMESPACE_END