diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2021-05-02 02:13:29 +0200 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2021-05-11 16:54:57 +0200 |
commit | aafea67cf6349f0cbe8f986e571a9ed0fbdf0d0a (patch) | |
tree | d5698ce16945ffff2963bb1b0a1fea0cf758ab0c | |
parent | 3a62f9e0c98d1cc311846394b744c8553ea9739c (diff) | |
download | qtbase-aafea67cf6349f0cbe8f986e571a9ed0fbdf0d0a.tar.gz |
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 <thiago.macieira@intel.com>
-rw-r--r-- | src/corelib/global/qflags.h | 13 | ||||
-rw-r--r-- | src/corelib/global/qglobal.cpp | 24 |
2 files changed, 37 insertions, 0 deletions
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<Enum>::const_iterator it, typename std::initializer_list<Enum>::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 @@ -539,6 +539,30 @@ static_assert(sizeof(qint64) == 8, "Internal error, qint64 is misdefined"); */ /*! + \fn template <typename Enum> bool operator==(QFlags<Enum> lhs, QFlags<Enum> rhs) + \fn template <typename Enum> bool operator==(QFlags<Enum> lhs, Enum rhs) + \fn template <typename Enum> bool operator==(Enum lhs, QFlags<Enum> 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 <typename Enum> bool operator!=(QFlags<Enum> lhs, QFlags<Enum> rhs) + \fn template <typename Enum> bool operator!=(QFlags<Enum> lhs, Enum rhs) + \fn template <typename Enum> bool operator!=(Enum lhs, QFlags<Enum> 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 |