diff options
Diffstat (limited to 'src/corelib/tools/qsize.h')
-rw-r--r-- | src/corelib/tools/qsize.h | 284 |
1 files changed, 150 insertions, 134 deletions
diff --git a/src/corelib/tools/qsize.h b/src/corelib/tools/qsize.h index d2598afbea..a5eaf34afe 100644 --- a/src/corelib/tools/qsize.h +++ b/src/corelib/tools/qsize.h @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #ifndef QSIZE_H #define QSIZE_H @@ -44,12 +8,18 @@ #include <QtCore/qhashfunctions.h> #include <QtCore/qmargins.h> +#include <QtCore/q20type_traits.h> +#include <QtCore/q23utility.h> + #if defined(Q_OS_DARWIN) || defined(Q_QDOC) struct CGSize; #endif QT_BEGIN_NAMESPACE +// QT_ENABLE_P0846_SEMANTICS_FOR(get) // from qmargins.h + +class QSizeF; class Q_CORE_EXPORT QSize { @@ -66,19 +36,19 @@ public: constexpr inline void setWidth(int w) noexcept; constexpr inline void setHeight(int h) noexcept; void transpose() noexcept; - Q_REQUIRED_RESULT constexpr inline QSize transposed() const noexcept; + [[nodiscard]] constexpr inline QSize transposed() const noexcept; inline void scale(int w, int h, Qt::AspectRatioMode mode) noexcept; inline void scale(const QSize &s, Qt::AspectRatioMode mode) noexcept; - Q_REQUIRED_RESULT QSize scaled(int w, int h, Qt::AspectRatioMode mode) const noexcept; - Q_REQUIRED_RESULT QSize scaled(const QSize &s, Qt::AspectRatioMode mode) const noexcept; + [[nodiscard]] QSize scaled(int w, int h, Qt::AspectRatioMode mode) const noexcept; + [[nodiscard]] QSize scaled(const QSize &s, Qt::AspectRatioMode mode) const noexcept; - Q_REQUIRED_RESULT constexpr inline QSize expandedTo(const QSize &) const noexcept; - Q_REQUIRED_RESULT constexpr inline QSize boundedTo(const QSize &) const noexcept; + [[nodiscard]] constexpr inline QSize expandedTo(const QSize &) const noexcept; + [[nodiscard]] constexpr inline QSize boundedTo(const QSize &) const noexcept; - Q_REQUIRED_RESULT constexpr QSize grownBy(QMargins m) const noexcept + [[nodiscard]] constexpr QSize grownBy(QMargins m) const noexcept { return {width() + m.left() + m.right(), height() + m.top() + m.bottom()}; } - Q_REQUIRED_RESULT constexpr QSize shrunkBy(QMargins m) const noexcept + [[nodiscard]] constexpr QSize shrunkBy(QMargins m) const noexcept { return {width() - m.left() - m.right(), height() - m.top() - m.bottom()}; } constexpr inline int &rwidth() noexcept; @@ -89,24 +59,45 @@ public: constexpr inline QSize &operator*=(qreal c) noexcept; inline QSize &operator/=(qreal c); - friend inline constexpr bool operator==(const QSize &, const QSize &) noexcept; - friend inline constexpr bool operator!=(const QSize &, const QSize &) noexcept; + friend inline constexpr bool operator==(const QSize &s1, const QSize &s2) noexcept + { return s1.wd == s2.wd && s1.ht == s2.ht; } + friend inline constexpr bool operator!=(const QSize &s1, const QSize &s2) noexcept + { return s1.wd != s2.wd || s1.ht != s2.ht; } + friend inline constexpr QSize operator+(const QSize &s1, const QSize &s2) noexcept + { return QSize(s1.wd + s2.wd, s1.ht + s2.ht); } + friend inline constexpr QSize operator-(const QSize &s1, const QSize &s2) noexcept + { return QSize(s1.wd - s2.wd, s1.ht - s2.ht); } + friend inline constexpr QSize operator*(const QSize &s, qreal c) noexcept + { return QSize(qRound(s.wd * c), qRound(s.ht * c)); } + friend inline constexpr QSize operator*(qreal c, const QSize &s) noexcept + { return s * c; } + friend inline QSize operator/(const QSize &s, qreal c) + { Q_ASSERT(!qFuzzyIsNull(c)); return QSize(qRound(s.wd / c), qRound(s.ht / c)); } friend inline constexpr size_t qHash(const QSize &, size_t) noexcept; - friend inline constexpr const QSize operator+(const QSize &, const QSize &) noexcept; - friend inline constexpr const QSize operator-(const QSize &, const QSize &) noexcept; - friend inline constexpr const QSize operator*(const QSize &, qreal) noexcept; - friend inline constexpr const QSize operator*(qreal, const QSize &) noexcept; - friend inline const QSize operator/(const QSize &, qreal); #if defined(Q_OS_DARWIN) || defined(Q_QDOC) - Q_REQUIRED_RESULT CGSize toCGSize() const noexcept; + [[nodiscard]] CGSize toCGSize() const noexcept; #endif + [[nodiscard]] inline constexpr QSizeF toSizeF() const noexcept; + private: int wd; int ht; + + template <std::size_t I, + typename S, + std::enable_if_t<(I < 2), bool> = true, + std::enable_if_t<std::is_same_v<q20::remove_cvref_t<S>, QSize>, bool> = true> + friend constexpr decltype(auto) get(S &&s) noexcept + { + if constexpr (I == 0) + return q23::forward_like<S>(s.wd); + else if constexpr (I == 1) + return q23::forward_like<S>(s.ht); + } }; -Q_DECLARE_TYPEINFO(QSize, Q_MOVABLE_TYPE); +Q_DECLARE_TYPEINFO(QSize, Q_RELOCATABLE_TYPE); /***************************************************************************** QSize stream functions @@ -127,13 +118,13 @@ constexpr inline QSize::QSize() noexcept : wd(-1), ht(-1) {} constexpr inline QSize::QSize(int w, int h) noexcept : wd(w), ht(h) {} constexpr inline bool QSize::isNull() const noexcept -{ return wd==0 && ht==0; } +{ return wd == 0 && ht == 0; } constexpr inline bool QSize::isEmpty() const noexcept -{ return wd<1 || ht<1; } +{ return wd < 1 || ht < 1; } constexpr inline bool QSize::isValid() const noexcept -{ return wd>=0 && ht>=0; } +{ return wd >= 0 && ht >= 0; } constexpr inline int QSize::width() const noexcept { return wd; } @@ -166,48 +157,37 @@ constexpr inline int &QSize::rheight() noexcept { return ht; } constexpr inline QSize &QSize::operator+=(const QSize &s) noexcept -{ wd+=s.wd; ht+=s.ht; return *this; } +{ + wd += s.wd; + ht += s.ht; + return *this; +} constexpr inline QSize &QSize::operator-=(const QSize &s) noexcept -{ wd-=s.wd; ht-=s.ht; return *this; } +{ + wd -= s.wd; + ht -= s.ht; + return *this; +} constexpr inline QSize &QSize::operator*=(qreal c) noexcept -{ wd = qRound(wd*c); ht = qRound(ht*c); return *this; } - -constexpr inline bool operator==(const QSize &s1, const QSize &s2) noexcept -{ return s1.wd == s2.wd && s1.ht == s2.ht; } - -constexpr inline bool operator!=(const QSize &s1, const QSize &s2) noexcept -{ return s1.wd != s2.wd || s1.ht != s2.ht; } +{ + wd = qRound(wd * c); + ht = qRound(ht * c); + return *this; +} constexpr inline size_t qHash(const QSize &s, size_t seed = 0) noexcept { return qHashMulti(seed, s.wd, s.ht); } -constexpr inline const QSize operator+(const QSize & s1, const QSize & s2) noexcept -{ return QSize(s1.wd+s2.wd, s1.ht+s2.ht); } - -constexpr inline const QSize operator-(const QSize &s1, const QSize &s2) noexcept -{ return QSize(s1.wd-s2.wd, s1.ht-s2.ht); } - -constexpr inline const QSize operator*(const QSize &s, qreal c) noexcept -{ return QSize(qRound(s.wd*c), qRound(s.ht*c)); } - -constexpr inline const QSize operator*(qreal c, const QSize &s) noexcept -{ return QSize(qRound(s.wd*c), qRound(s.ht*c)); } - inline QSize &QSize::operator/=(qreal c) { Q_ASSERT(!qFuzzyIsNull(c)); - wd = qRound(wd/c); ht = qRound(ht/c); + wd = qRound(wd / c); + ht = qRound(ht / c); return *this; } -inline const QSize operator/(const QSize &s, qreal c) -{ - Q_ASSERT(!qFuzzyIsNull(c)); - return QSize(qRound(s.wd/c), qRound(s.ht/c)); -} - constexpr inline QSize QSize::expandedTo(const QSize & otherSize) const noexcept { return QSize(qMax(wd,otherSize.wd), qMax(ht,otherSize.ht)); @@ -239,19 +219,19 @@ public: constexpr inline void setWidth(qreal w) noexcept; constexpr inline void setHeight(qreal h) noexcept; void transpose() noexcept; - Q_REQUIRED_RESULT constexpr inline QSizeF transposed() const noexcept; + [[nodiscard]] constexpr inline QSizeF transposed() const noexcept; inline void scale(qreal w, qreal h, Qt::AspectRatioMode mode) noexcept; inline void scale(const QSizeF &s, Qt::AspectRatioMode mode) noexcept; - Q_REQUIRED_RESULT QSizeF scaled(qreal w, qreal h, Qt::AspectRatioMode mode) const noexcept; - Q_REQUIRED_RESULT QSizeF scaled(const QSizeF &s, Qt::AspectRatioMode mode) const noexcept; + [[nodiscard]] QSizeF scaled(qreal w, qreal h, Qt::AspectRatioMode mode) const noexcept; + [[nodiscard]] QSizeF scaled(const QSizeF &s, Qt::AspectRatioMode mode) const noexcept; - Q_REQUIRED_RESULT constexpr inline QSizeF expandedTo(const QSizeF &) const noexcept; - Q_REQUIRED_RESULT constexpr inline QSizeF boundedTo(const QSizeF &) const noexcept; + [[nodiscard]] constexpr inline QSizeF expandedTo(const QSizeF &) const noexcept; + [[nodiscard]] constexpr inline QSizeF boundedTo(const QSizeF &) const noexcept; - Q_REQUIRED_RESULT constexpr QSizeF grownBy(QMarginsF m) const noexcept + [[nodiscard]] constexpr QSizeF grownBy(QMarginsF m) const noexcept { return {width() + m.left() + m.right(), height() + m.top() + m.bottom()}; } - Q_REQUIRED_RESULT constexpr QSizeF shrunkBy(QMarginsF m) const noexcept + [[nodiscard]] constexpr QSizeF shrunkBy(QMarginsF m) const noexcept { return {width() - m.left() - m.right(), height() - m.top() - m.bottom()}; } constexpr inline qreal &rwidth() noexcept; @@ -262,26 +242,51 @@ public: constexpr inline QSizeF &operator*=(qreal c) noexcept; inline QSizeF &operator/=(qreal c); - friend constexpr inline bool operator==(const QSizeF &, const QSizeF &) noexcept; - friend constexpr inline bool operator!=(const QSizeF &, const QSizeF &) noexcept; - friend constexpr inline const QSizeF operator+(const QSizeF &, const QSizeF &) noexcept; - friend constexpr inline const QSizeF operator-(const QSizeF &, const QSizeF &) noexcept; - friend constexpr inline const QSizeF operator*(const QSizeF &, qreal) noexcept; - friend constexpr inline const QSizeF operator*(qreal, const QSizeF &) noexcept; - friend inline const QSizeF operator/(const QSizeF &, qreal); + QT_WARNING_PUSH + QT_WARNING_DISABLE_FLOAT_COMPARE + friend constexpr inline bool operator==(const QSizeF &s1, const QSizeF &s2) + { + return ((!s1.wd || !s2.wd) ? qFuzzyIsNull(s1.wd - s2.wd) : qFuzzyCompare(s1.wd, s2.wd)) + && ((!s1.ht || !s2.ht) ? qFuzzyIsNull(s1.ht - s2.ht) : qFuzzyCompare(s1.ht, s2.ht)); + } + QT_WARNING_POP + friend constexpr inline bool operator!=(const QSizeF &s1, const QSizeF &s2) + { return !(s1 == s2); } + friend constexpr inline QSizeF operator+(const QSizeF &s1, const QSizeF &s2) noexcept + { return QSizeF(s1.wd + s2.wd, s1.ht + s2.ht); } + friend constexpr inline QSizeF operator-(const QSizeF &s1, const QSizeF &s2) noexcept + { return QSizeF(s1.wd - s2.wd, s1.ht - s2.ht); } + friend constexpr inline QSizeF operator*(const QSizeF &s, qreal c) noexcept + { return QSizeF(s.wd * c, s.ht * c); } + friend constexpr inline QSizeF operator*(qreal c, const QSizeF &s) noexcept + { return s * c; } + friend inline QSizeF operator/(const QSizeF &s, qreal c) + { Q_ASSERT(!qFuzzyIsNull(c)); return QSizeF(s.wd / c, s.ht / c); } constexpr inline QSize toSize() const noexcept; #if defined(Q_OS_DARWIN) || defined(Q_QDOC) - Q_REQUIRED_RESULT static QSizeF fromCGSize(CGSize size) noexcept; - Q_REQUIRED_RESULT CGSize toCGSize() const noexcept; + [[nodiscard]] static QSizeF fromCGSize(CGSize size) noexcept; + [[nodiscard]] CGSize toCGSize() const noexcept; #endif private: qreal wd; qreal ht; + + template <std::size_t I, + typename S, + std::enable_if_t<(I < 2), bool> = true, + std::enable_if_t<std::is_same_v<q20::remove_cvref_t<S>, QSizeF>, bool> = true> + friend constexpr decltype(auto) get(S &&s) noexcept + { + if constexpr (I == 0) + return q23::forward_like<S>(s.wd); + else if constexpr (I == 1) + return q23::forward_like<S>(s.ht); + } }; -Q_DECLARE_TYPEINFO(QSizeF, Q_MOVABLE_TYPE); +Q_DECLARE_TYPEINFO(QSizeF, Q_RELOCATABLE_TYPE); /***************************************************************************** @@ -344,53 +349,42 @@ constexpr inline qreal &QSizeF::rheight() noexcept { return ht; } constexpr inline QSizeF &QSizeF::operator+=(const QSizeF &s) noexcept -{ wd += s.wd; ht += s.ht; return *this; } +{ + wd += s.wd; + ht += s.ht; + return *this; +} constexpr inline QSizeF &QSizeF::operator-=(const QSizeF &s) noexcept -{ wd -= s.wd; ht -= s.ht; return *this; } +{ + wd -= s.wd; + ht -= s.ht; + return *this; +} constexpr inline QSizeF &QSizeF::operator*=(qreal c) noexcept -{ wd *= c; ht *= c; return *this; } - -constexpr inline bool operator==(const QSizeF &s1, const QSizeF &s2) noexcept -{ return qFuzzyCompare(s1.wd, s2.wd) && qFuzzyCompare(s1.ht, s2.ht); } - -constexpr inline bool operator!=(const QSizeF &s1, const QSizeF &s2) noexcept -{ return !qFuzzyCompare(s1.wd, s2.wd) || !qFuzzyCompare(s1.ht, s2.ht); } - -constexpr inline const QSizeF operator+(const QSizeF & s1, const QSizeF & s2) noexcept -{ return QSizeF(s1.wd+s2.wd, s1.ht+s2.ht); } - -constexpr inline const QSizeF operator-(const QSizeF &s1, const QSizeF &s2) noexcept -{ return QSizeF(s1.wd-s2.wd, s1.ht-s2.ht); } - -constexpr inline const QSizeF operator*(const QSizeF &s, qreal c) noexcept -{ return QSizeF(s.wd*c, s.ht*c); } - -constexpr inline const QSizeF operator*(qreal c, const QSizeF &s) noexcept -{ return QSizeF(s.wd*c, s.ht*c); } - -inline QSizeF &QSizeF::operator/=(qreal c) { - Q_ASSERT(!qFuzzyIsNull(c)); - wd = wd/c; ht = ht/c; + wd *= c; + ht *= c; return *this; } -inline const QSizeF operator/(const QSizeF &s, qreal c) +inline QSizeF &QSizeF::operator/=(qreal c) { - Q_ASSERT(!qFuzzyIsNull(c)); - return QSizeF(s.wd/c, s.ht/c); + Q_ASSERT(!qFuzzyIsNull(c) && qIsFinite(c)); + wd = wd / c; + ht = ht / c; + return *this; } -constexpr inline QSizeF QSizeF::expandedTo(const QSizeF & otherSize) const noexcept +constexpr inline QSizeF QSizeF::expandedTo(const QSizeF &otherSize) const noexcept { - return QSizeF(qMax(wd,otherSize.wd), qMax(ht,otherSize.ht)); + return QSizeF(qMax(wd, otherSize.wd), qMax(ht, otherSize.ht)); } -constexpr inline QSizeF QSizeF::boundedTo(const QSizeF & otherSize) const noexcept +constexpr inline QSizeF QSizeF::boundedTo(const QSizeF &otherSize) const noexcept { - return QSizeF(qMin(wd,otherSize.wd), qMin(ht,otherSize.ht)); + return QSizeF(qMin(wd, otherSize.wd), qMin(ht, otherSize.ht)); } constexpr inline QSize QSizeF::toSize() const noexcept @@ -398,10 +392,32 @@ constexpr inline QSize QSizeF::toSize() const noexcept return QSize(qRound(wd), qRound(ht)); } +constexpr QSizeF QSize::toSizeF() const noexcept { return *this; } + #ifndef QT_NO_DEBUG_STREAM Q_CORE_EXPORT QDebug operator<<(QDebug, const QSizeF &); #endif QT_END_NAMESPACE +/***************************************************************************** + QSize/QSizeF tuple protocol + *****************************************************************************/ + +namespace std { + template <> + class tuple_size<QT_PREPEND_NAMESPACE(QSize)> : public integral_constant<size_t, 2> {}; + template <> + class tuple_element<0, QT_PREPEND_NAMESPACE(QSize)> { public: using type = int; }; + template <> + class tuple_element<1, QT_PREPEND_NAMESPACE(QSize)> { public: using type = int; }; + + template <> + class tuple_size<QT_PREPEND_NAMESPACE(QSizeF)> : public integral_constant<size_t, 2> {}; + template <> + class tuple_element<0, QT_PREPEND_NAMESPACE(QSizeF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); }; + template <> + class tuple_element<1, QT_PREPEND_NAMESPACE(QSizeF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); }; +} + #endif // QSIZE_H |