diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index 5b7632676..28de45028 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -846,55 +846,118 @@ class json_pointer return result; } - /*! - @brief compares two JSON pointers for equality + public: +#ifdef JSON_HAS_CPP_20 + /// @brief compares two JSON pointers for equality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ + template + bool operator==(const json_pointer& rhs) const noexcept + { + return reference_tokens == rhs.reference_tokens; + } - @param[in] lhs JSON pointer to compare - @param[in] rhs JSON pointer to compare - @return whether @a lhs is equal to @a rhs - - @complexity Linear in the length of the JSON pointer - - @exceptionsafety No-throw guarantee: this function never throws exceptions. - */ + /// @brief compares JSON pointer and string for equality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ + bool operator==(const string_t& rhs) const + { + return *this == json_pointer(rhs); + } +#else + /// @brief compares two JSON pointers for equality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ template // NOLINTNEXTLINE(readability-redundant-declaration) - friend bool operator==(json_pointer const& lhs, - json_pointer const& rhs) noexcept; + friend bool operator==(const json_pointer& lhs, + const json_pointer& rhs) noexcept; - /*! - @brief compares two JSON pointers for inequality + /// @brief compares JSON pointer and string for equality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ + template + // NOLINTNEXTLINE(readability-redundant-declaration) + friend bool operator==(const json_pointer& lhs, + const StringType& rhs); - @param[in] lhs JSON pointer to compare - @param[in] rhs JSON pointer to compare - @return whether @a lhs is not equal @a rhs + /// @brief compares string and JSON pointer for equality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ + template + // NOLINTNEXTLINE(readability-redundant-declaration) + friend bool operator==(const StringType& lhs, + const json_pointer& rhs); - @complexity Linear in the length of the JSON pointer - - @exceptionsafety No-throw guarantee: this function never throws exceptions. - */ + /// @brief compares two JSON pointers for inequality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/ template // NOLINTNEXTLINE(readability-redundant-declaration) - friend bool operator!=(json_pointer const& lhs, - json_pointer const& rhs) noexcept; + friend bool operator!=(const json_pointer& lhs, + const json_pointer& rhs) noexcept; + /// @brief compares JSON pointer and string for inequality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/ + template + // NOLINTNEXTLINE(readability-redundant-declaration) + friend bool operator!=(const json_pointer& lhs, + const StringType& rhs); + + /// @brief compares string and JSON pointer for inequality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/ + template + // NOLINTNEXTLINE(readability-redundant-declaration) + friend bool operator!=(const StringType& lhs, + const json_pointer& rhs); +#endif + + private: /// the reference tokens std::vector reference_tokens; }; +#ifndef JSON_HAS_CPP_20 // functions cannot be defined inside class due to ODR violations template -inline bool operator==(json_pointer const& lhs, - json_pointer const& rhs) noexcept +inline bool operator==(const json_pointer& lhs, + const json_pointer& rhs) noexcept { return lhs.reference_tokens == rhs.reference_tokens; } +template::string_t> +inline bool operator==(const json_pointer& lhs, + const StringType& rhs) +{ + return lhs == json_pointer(rhs); +} + +template::string_t> +inline bool operator==(const StringType& lhs, + const json_pointer& rhs) +{ + return json_pointer(lhs) == rhs; +} + template -inline bool operator!=(json_pointer const& lhs, - json_pointer const& rhs) noexcept +inline bool operator!=(const json_pointer& lhs, + const json_pointer& rhs) noexcept { return !(lhs == rhs); } +template::string_t> +inline bool operator!=(const json_pointer& lhs, + const StringType& rhs) +{ + return !(lhs == rhs); +} + +template::string_t> +inline bool operator!=(const StringType& lhs, + const json_pointer& rhs) +{ + return !(lhs == rhs); +} +#endif + NLOHMANN_JSON_NAMESPACE_END diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 4d86493e1..beee0136c 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3373,7 +3373,7 @@ NLOHMANN_JSON_NAMESPACE_END /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document /// @sa https://json.nlohmann.me/api/json_pointer/ - template + template class json_pointer; /*! @@ -14448,57 +14448,120 @@ class json_pointer return result; } - /*! - @brief compares two JSON pointers for equality + public: +#ifdef JSON_HAS_CPP_20 + /// @brief compares two JSON pointers for equality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ + template + bool operator==(const json_pointer& rhs) const noexcept + { + return reference_tokens == rhs.reference_tokens; + } - @param[in] lhs JSON pointer to compare - @param[in] rhs JSON pointer to compare - @return whether @a lhs is equal to @a rhs - - @complexity Linear in the length of the JSON pointer - - @exceptionsafety No-throw guarantee: this function never throws exceptions. - */ + /// @brief compares JSON pointer and string for equality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ + bool operator==(const string_t& rhs) const + { + return *this == json_pointer(rhs); + } +#else + /// @brief compares two JSON pointers for equality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ template // NOLINTNEXTLINE(readability-redundant-declaration) - friend bool operator==(json_pointer const& lhs, - json_pointer const& rhs) noexcept; + friend bool operator==(const json_pointer& lhs, + const json_pointer& rhs) noexcept; - /*! - @brief compares two JSON pointers for inequality + /// @brief compares JSON pointer and string for equality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ + template + // NOLINTNEXTLINE(readability-redundant-declaration) + friend bool operator==(const json_pointer& lhs, + const StringType& rhs); - @param[in] lhs JSON pointer to compare - @param[in] rhs JSON pointer to compare - @return whether @a lhs is not equal @a rhs + /// @brief compares string and JSON pointer for equality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ + template + // NOLINTNEXTLINE(readability-redundant-declaration) + friend bool operator==(const StringType& lhs, + const json_pointer& rhs); - @complexity Linear in the length of the JSON pointer - - @exceptionsafety No-throw guarantee: this function never throws exceptions. - */ + /// @brief compares two JSON pointers for inequality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/ template // NOLINTNEXTLINE(readability-redundant-declaration) - friend bool operator!=(json_pointer const& lhs, - json_pointer const& rhs) noexcept; + friend bool operator!=(const json_pointer& lhs, + const json_pointer& rhs) noexcept; + /// @brief compares JSON pointer and string for inequality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/ + template + // NOLINTNEXTLINE(readability-redundant-declaration) + friend bool operator!=(const json_pointer& lhs, + const StringType& rhs); + + /// @brief compares string and JSON pointer for inequality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/ + template + // NOLINTNEXTLINE(readability-redundant-declaration) + friend bool operator!=(const StringType& lhs, + const json_pointer& rhs); +#endif + + private: /// the reference tokens std::vector reference_tokens; }; +#ifndef JSON_HAS_CPP_20 // functions cannot be defined inside class due to ODR violations template -inline bool operator==(json_pointer const& lhs, - json_pointer const& rhs) noexcept +inline bool operator==(const json_pointer& lhs, + const json_pointer& rhs) noexcept { return lhs.reference_tokens == rhs.reference_tokens; } +template::string_t> +inline bool operator==(const json_pointer& lhs, + const StringType& rhs) +{ + return lhs == json_pointer(rhs); +} + +template::string_t> +inline bool operator==(const StringType& lhs, + const json_pointer& rhs) +{ + return json_pointer(lhs) == rhs; +} + template -inline bool operator!=(json_pointer const& lhs, - json_pointer const& rhs) noexcept +inline bool operator!=(const json_pointer& lhs, + const json_pointer& rhs) noexcept { return !(lhs == rhs); } +template::string_t> +inline bool operator!=(const json_pointer& lhs, + const StringType& rhs) +{ + return !(lhs == rhs); +} + +template::string_t> +inline bool operator!=(const StringType& lhs, + const json_pointer& rhs) +{ + return !(lhs == rhs); +} +#endif + NLOHMANN_JSON_NAMESPACE_END // #include