diff options
Diffstat (limited to 'src/corelib/tools/qpoint.h')
-rw-r--r-- | src/corelib/tools/qpoint.h | 132 |
1 files changed, 83 insertions, 49 deletions
diff --git a/src/corelib/tools/qpoint.h b/src/corelib/tools/qpoint.h index d69e793d3a..50b4c864be 100644 --- a/src/corelib/tools/qpoint.h +++ b/src/corelib/tools/qpoint.h @@ -1,46 +1,15 @@ -/**************************************************************************** -** -** 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 QPOINT_H #define QPOINT_H +#include <QtCore/qcompare.h> #include <QtCore/qnamespace.h> +#include <QtCore/qnumeric.h> + +#include <QtCore/q20type_traits.h> +#include <QtCore/q23utility.h> #if defined(Q_OS_DARWIN) || defined(Q_QDOC) struct CGPoint; @@ -48,6 +17,10 @@ struct CGPoint; QT_BEGIN_NAMESPACE +QT_ENABLE_P0846_SEMANTICS_FOR(get) + +class QPointF; + class QPoint { public: @@ -80,10 +53,10 @@ public: constexpr static inline int dotProduct(const QPoint &p1, const QPoint &p2) { return p1.xp * p2.xp + p1.yp * p2.yp; } - friend constexpr inline bool operator==(const QPoint &p1, const QPoint &p2) noexcept +private: + friend constexpr bool comparesEqual(const QPoint &p1, const QPoint &p2) noexcept { return p1.xp == p2.xp && p1.yp == p2.yp; } - friend constexpr inline bool operator!=(const QPoint &p1, const QPoint &p2) noexcept - { return p1.xp != p2.xp || p1.yp != p2.yp; } + Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QPoint) friend constexpr inline QPoint operator+(const QPoint &p1, const QPoint &p2) noexcept { return QPoint(p1.xp + p2.xp, p1.yp + p2.yp); } friend constexpr inline QPoint operator-(const QPoint &p1, const QPoint &p2) noexcept @@ -107,17 +80,31 @@ public: friend constexpr inline QPoint operator/(const QPoint &p, qreal c) { return QPoint(qRound(p.xp / c), qRound(p.yp / c)); } +public: #if defined(Q_OS_DARWIN) || defined(Q_QDOC) [[nodiscard]] Q_CORE_EXPORT CGPoint toCGPoint() const noexcept; #endif + [[nodiscard]] constexpr inline QPointF toPointF() const noexcept; private: friend class QTransform; int xp; int yp; + + template <std::size_t I, + typename P, + std::enable_if_t<(I < 2), bool> = true, + std::enable_if_t<std::is_same_v<q20::remove_cvref_t<P>, QPoint>, bool> = true> + friend constexpr decltype(auto) get(P &&p) noexcept + { + if constexpr (I == 0) + return q23::forward_like<P>(p.xp); + else if constexpr (I == 1) + return q23::forward_like<P>(p.yp); + } }; -Q_DECLARE_TYPEINFO(QPoint, Q_MOVABLE_TYPE); +Q_DECLARE_TYPEINFO(QPoint, Q_PRIMITIVE_TYPE); /***************************************************************************** QPoint stream functions @@ -257,19 +244,25 @@ public: return p1.xp * p2.xp + p1.yp * p2.yp; } +private: QT_WARNING_PUSH QT_WARNING_DISABLE_FLOAT_COMPARE - friend constexpr inline bool operator==(const QPointF &p1, const QPointF &p2) + friend constexpr bool qFuzzyCompare(const QPointF &p1, const QPointF &p2) noexcept { return ((!p1.xp || !p2.xp) ? qFuzzyIsNull(p1.xp - p2.xp) : qFuzzyCompare(p1.xp, p2.xp)) && ((!p1.yp || !p2.yp) ? qFuzzyIsNull(p1.yp - p2.yp) : qFuzzyCompare(p1.yp, p2.yp)); } - friend constexpr inline bool operator!=(const QPointF &p1, const QPointF &p2) + QT_WARNING_POP + friend constexpr bool qFuzzyIsNull(const QPointF &point) noexcept { - return !(p1 == p2); + return qFuzzyIsNull(point.xp) && qFuzzyIsNull(point.yp); } - QT_WARNING_POP - + friend constexpr bool comparesEqual(const QPointF &p1, const QPointF &p2) noexcept + { return qFuzzyCompare(p1, p2); } + Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QPointF) + friend constexpr bool comparesEqual(const QPointF &p1, const QPoint &p2) noexcept + { return comparesEqual(p1, p2.toPointF()); } + Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QPointF, QPoint) friend constexpr inline QPointF operator+(const QPointF &p1, const QPointF &p2) { return QPointF(p1.xp + p2.xp, p1.yp + p2.yp); } friend constexpr inline QPointF operator-(const QPointF &p1, const QPointF &p2) @@ -283,8 +276,12 @@ public: friend constexpr inline QPointF operator-(const QPointF &p) { return QPointF(-p.xp, -p.yp); } friend constexpr inline QPointF operator/(const QPointF &p, qreal divisor) - { return QPointF(p.xp / divisor, p.yp / divisor); } + { + Q_ASSERT(divisor < 0 || divisor > 0); + return QPointF(p.xp / divisor, p.yp / divisor); + } +public: constexpr QPoint toPoint() const; #if defined(Q_OS_DARWIN) || defined(Q_QDOC) @@ -297,9 +294,23 @@ private: qreal xp; qreal yp; + + template <std::size_t I, + typename P, + std::enable_if_t<(I < 2), bool> = true, + std::enable_if_t<std::is_same_v<q20::remove_cvref_t<P>, QPointF>, bool> = true> + friend constexpr decltype(auto) get(P &&p) noexcept + { + if constexpr (I == 0) + return q23::forward_like<P>(p.xp); + else if constexpr (I == 1) + return q23::forward_like<P>(p.yp); + } }; -Q_DECLARE_TYPEINFO(QPointF, Q_MOVABLE_TYPE); +Q_DECLARE_TYPEINFO(QPointF, Q_PRIMITIVE_TYPE); + +size_t qHash(QPointF, size_t seed = 0) = delete; /***************************************************************************** QPointF stream functions @@ -382,11 +393,14 @@ constexpr inline QPointF &QPointF::operator*=(qreal c) constexpr inline QPointF &QPointF::operator/=(qreal divisor) { + Q_ASSERT(divisor > 0 || divisor < 0); xp /= divisor; yp /= divisor; return *this; } +constexpr QPointF QPoint::toPointF() const noexcept { return *this; } + constexpr inline QPoint QPointF::toPoint() const { return QPoint(qRound(xp), qRound(yp)); @@ -398,4 +412,24 @@ Q_CORE_EXPORT QDebug operator<<(QDebug d, const QPointF &p); QT_END_NAMESPACE +/***************************************************************************** + QPoint/QPointF tuple protocol + *****************************************************************************/ + +namespace std { + template <> + class tuple_size<QT_PREPEND_NAMESPACE(QPoint)> : public integral_constant<size_t, 2> {}; + template <> + class tuple_element<0, QT_PREPEND_NAMESPACE(QPoint)> { public: using type = int; }; + template <> + class tuple_element<1, QT_PREPEND_NAMESPACE(QPoint)> { public: using type = int; }; + + template <> + class tuple_size<QT_PREPEND_NAMESPACE(QPointF)> : public integral_constant<size_t, 2> {}; + template <> + class tuple_element<0, QT_PREPEND_NAMESPACE(QPointF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); }; + template <> + class tuple_element<1, QT_PREPEND_NAMESPACE(QPointF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); }; +} + #endif // QPOINT_H |