From 5fe8b92951581e46953b196fea36a9a4d19f810f Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Sun, 7 Aug 2022 09:43:42 +0200 Subject: [PATCH] Make json_pointer usable as map key --- include/nlohmann/detail/json_pointer.hpp | 24 ++++++++++++++++++++++-- single_include/nlohmann/json.hpp | 24 ++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index 28de45028..ce593e843 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -847,7 +847,7 @@ class json_pointer } public: -#ifdef JSON_HAS_CPP_20 +#if JSON_HAS_THREE_WAY_COMPARISON /// @brief compares two JSON pointers for equality /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ template @@ -862,6 +862,13 @@ class json_pointer { return *this == json_pointer(rhs); } + + /// @brief 3-way compares two JSON pointers + template + std::strong_ordering operator<=>(const json_pointer& rhs) const noexcept // *NOPAD* + { + return reference_tokens <=> rhs.reference_tokens; // *NOPAD* + } #else /// @brief compares two JSON pointers for equality /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ @@ -904,6 +911,12 @@ class json_pointer // NOLINTNEXTLINE(readability-redundant-declaration) friend bool operator!=(const StringType& lhs, const json_pointer& rhs); + + /// @brief compares two JSON pointer for less-than + template + // NOLINTNEXTLINE(readability-redundant-declaration) + friend bool operator<(const json_pointer& lhs, + const json_pointer& rhs) noexcept; #endif private: @@ -911,7 +924,7 @@ class json_pointer std::vector reference_tokens; }; -#ifndef JSON_HAS_CPP_20 +#if !JSON_HAS_THREE_WAY_COMPARISON // functions cannot be defined inside class due to ODR violations template inline bool operator==(const json_pointer& lhs, @@ -958,6 +971,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..578fbc9b7 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -14449,7 +14449,7 @@ class json_pointer } public: -#ifdef JSON_HAS_CPP_20 +#if JSON_HAS_THREE_WAY_COMPARISON /// @brief compares two JSON pointers for equality /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ template @@ -14464,6 +14464,13 @@ class json_pointer { return *this == json_pointer(rhs); } + + /// @brief 3-way compares two JSON pointers + template + std::strong_ordering operator<=>(const json_pointer& rhs) const noexcept // *NOPAD* + { + return reference_tokens <=> rhs.reference_tokens; // *NOPAD* + } #else /// @brief compares two JSON pointers for equality /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ @@ -14506,6 +14513,12 @@ class json_pointer // NOLINTNEXTLINE(readability-redundant-declaration) friend bool operator!=(const StringType& lhs, const json_pointer& rhs); + + /// @brief compares two JSON pointer for less-than + template + // NOLINTNEXTLINE(readability-redundant-declaration) + friend bool operator<(const json_pointer& lhs, + const json_pointer& rhs) noexcept; #endif private: @@ -14513,7 +14526,7 @@ class json_pointer std::vector reference_tokens; }; -#ifndef JSON_HAS_CPP_20 +#if !JSON_HAS_THREE_WAY_COMPARISON // functions cannot be defined inside class due to ODR violations template inline bool operator==(const json_pointer& lhs, @@ -14560,6 +14573,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