From aafea67cf6349f0cbe8f986e571a9ed0fbdf0d0a Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Sun, 2 May 2021 02:13:29 +0200 Subject: QFlags: add operator== and != It's a value type, and we don't need detours through implicit conversions (which are a real disgrace, as they make nonsense like `flags != 3.14` well-formed). [ChangeLog][QtCore][QFlags] Added overloads of operator== and operator!= between QFlags objects, and between a QFlags object and an object of the flag's enumeration. Change-Id: I36701dea8fcd4cc64941e78af58fa7d38a15a8c7 Reviewed-by: Thiago Macieira --- src/corelib/global/qflags.h | 13 +++++++++++++ src/corelib/global/qglobal.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+) (limited to 'src') diff --git a/src/corelib/global/qflags.h b/src/corelib/global/qflags.h index 2396a8b2fe..82fcbdd54d 100644 --- a/src/corelib/global/qflags.h +++ b/src/corelib/global/qflags.h @@ -151,6 +151,19 @@ public: return on ? (*this |= flag) : (*this &= ~QFlags(flag)); } + friend constexpr inline bool operator==(QFlags lhs, QFlags rhs) noexcept + { return lhs.i == rhs.i; } + friend constexpr inline bool operator!=(QFlags lhs, QFlags rhs) noexcept + { return lhs.i != rhs.i; } + friend constexpr inline bool operator==(QFlags lhs, Enum rhs) noexcept + { return lhs == QFlags(rhs); } + friend constexpr inline bool operator!=(QFlags lhs, Enum rhs) noexcept + { return lhs != QFlags(rhs); } + friend constexpr inline bool operator==(Enum lhs, QFlags rhs) noexcept + { return QFlags(lhs) == rhs; } + friend constexpr inline bool operator!=(Enum lhs, QFlags rhs) noexcept + { return QFlags(lhs) != rhs; } + private: constexpr static inline Int initializer_list_helper(typename std::initializer_list::const_iterator it, typename std::initializer_list::const_iterator end) diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index c1a6e7a268..84cb8def80 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -538,6 +538,30 @@ static_assert(sizeof(qint64) == 8, "Internal error, qint64 is misdefined"); to seed the calcualtion. */ +/*! + \fn template bool operator==(QFlags lhs, QFlags rhs) + \fn template bool operator==(QFlags lhs, Enum rhs) + \fn template bool operator==(Enum lhs, QFlags rhs) + \since 6.2 + \relates QFlags + + Compares \a lhs and \a rhs for equality; the two arguments are + considered equal if they represent exactly the same value + (bitmask). +*/ + +/*! + \fn template bool operator!=(QFlags lhs, QFlags rhs) + \fn template bool operator!=(QFlags lhs, Enum rhs) + \fn template bool operator!=(Enum lhs, QFlags rhs) + \since 6.2 + \relates QFlags + + Compares \a lhs and \a rhs for inequality; the two arguments are + considered different if they don't represent exactly the same value + (bitmask). +*/ + /*! \macro Q_DISABLE_COPY(Class) \relates QObject -- cgit v1.2.3