Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-23 09:21:15

0001 // Copyright (C) 2022 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 // Qt-Security score:significant reason:default
0004 
0005 #ifndef QPOINT_H
0006 #define QPOINT_H
0007 
0008 #include <QtCore/qcheckedint_impl.h>
0009 #include <QtCore/qcompare.h>
0010 #include <QtCore/qnamespace.h>
0011 #include <QtCore/qnumeric.h>
0012 
0013 #include <QtCore/q20type_traits.h>
0014 #include <QtCore/q23utility.h>
0015 
0016 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0017 struct CGPoint;
0018 #endif
0019 
0020 QT_BEGIN_NAMESPACE
0021 
0022 QT_ENABLE_P0846_SEMANTICS_FOR(get)
0023 
0024 class QDataStream;
0025 class QLine;
0026 class QPointF;
0027 class QRect;
0028 
0029 class QPoint
0030 {
0031 public:
0032     constexpr QPoint() noexcept;
0033     constexpr QPoint(int xpos, int ypos) noexcept;
0034 
0035     constexpr inline bool isNull() const noexcept;
0036 
0037     constexpr inline int x() const noexcept;
0038     constexpr inline int y() const noexcept;
0039     constexpr inline void setX(int x) noexcept;
0040     constexpr inline void setY(int y) noexcept;
0041 
0042     constexpr inline int manhattanLength() const;
0043 
0044     constexpr QPoint transposed() const noexcept { return {yp, xp}; }
0045 
0046     constexpr inline int &rx() noexcept;
0047     constexpr inline int &ry() noexcept;
0048 
0049     constexpr inline QPoint &operator+=(const QPoint &p);
0050     constexpr inline QPoint &operator-=(const QPoint &p);
0051 
0052     constexpr inline QPoint &operator*=(float factor);
0053     constexpr inline QPoint &operator*=(double factor);
0054     constexpr inline QPoint &operator*=(int factor);
0055 
0056     constexpr inline QPoint &operator/=(qreal divisor);
0057 
0058     constexpr static inline int dotProduct(const QPoint &p1, const QPoint &p2)
0059     { return int(p1.xp * p2.xp + p1.yp * p2.yp); }
0060 
0061 private:
0062     friend constexpr bool comparesEqual(const QPoint &p1, const QPoint &p2) noexcept
0063     { return p1.xp == p2.xp && p1.yp == p2.yp; }
0064     Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QPoint)
0065     friend constexpr inline QPoint operator+(const QPoint &p1, const QPoint &p2) noexcept
0066     { return QPoint(p1.xp + p2.xp, p1.yp + p2.yp); }
0067     friend constexpr inline QPoint operator-(const QPoint &p1, const QPoint &p2) noexcept
0068     { return QPoint(p1.xp - p2.xp, p1.yp - p2.yp); }
0069     friend constexpr inline QPoint operator*(const QPoint &p, float factor)
0070     { return QPoint(QtPrivate::qSaturateRound(p.x() * factor), QtPrivate::qSaturateRound(p.y() * factor)); }
0071     friend constexpr inline QPoint operator*(const QPoint &p, double factor)
0072     { return QPoint(QtPrivate::qSaturateRound(p.x() * factor), QtPrivate::qSaturateRound(p.y() * factor)); }
0073     friend constexpr inline QPoint operator*(const QPoint &p, int factor) noexcept
0074     { return QPoint(p.xp * factor, p.yp * factor); }
0075     friend constexpr inline QPoint operator*(float factor, const QPoint &p)
0076     { return QPoint(QtPrivate::qSaturateRound(p.x() * factor), QtPrivate::qSaturateRound(p.y() * factor)); }
0077     friend constexpr inline QPoint operator*(double factor, const QPoint &p)
0078     { return QPoint(QtPrivate::qSaturateRound(p.x() * factor), QtPrivate::qSaturateRound(p.y() * factor)); }
0079     friend constexpr inline QPoint operator*(int factor, const QPoint &p) noexcept
0080     { return QPoint(p.xp * factor, p.yp * factor); }
0081     friend constexpr inline QPoint operator+(const QPoint &p) noexcept
0082     { return p; }
0083     friend constexpr inline QPoint operator-(const QPoint &p) noexcept
0084     { return QPoint(-p.xp, -p.yp); }
0085     friend constexpr inline QPoint operator/(const QPoint &p, qreal c)
0086     {
0087         Q_ASSERT(!qFuzzyIsNull(c));
0088         return QPoint(QtPrivate::qSaturateRound(p.x() / c), QtPrivate::qSaturateRound(p.y() / c));
0089     }
0090 
0091 public:
0092 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0093     [[nodiscard]] Q_CORE_EXPORT CGPoint toCGPoint() const noexcept;
0094 #endif
0095     [[nodiscard]] constexpr inline QPointF toPointF() const noexcept;
0096 
0097 private:
0098     using Representation = QtPrivate::QCheckedIntegers::QCheckedInt<int>;
0099 
0100     friend class QRect;
0101     friend class QLine;
0102     constexpr QPoint(Representation xpos, Representation ypos) noexcept
0103         : xp(xpos), yp(ypos) {}
0104 
0105     Representation xp;
0106     Representation yp;
0107 
0108     template <std::size_t I,
0109               typename P,
0110               std::enable_if_t<(I < 2), bool> = true,
0111               std::enable_if_t<std::is_same_v<q20::remove_cvref_t<P>, QPoint>, bool> = true>
0112     friend constexpr decltype(auto) get(P &&p) noexcept
0113     {
0114         if constexpr (I == 0)
0115             return q23::forward_like<P>(p.xp).as_underlying();
0116         else if constexpr (I == 1)
0117             return q23::forward_like<P>(p.yp).as_underlying();
0118     }
0119 };
0120 
0121 Q_DECLARE_TYPEINFO(QPoint, Q_PRIMITIVE_TYPE);
0122 
0123 /*****************************************************************************
0124   QPoint stream functions
0125  *****************************************************************************/
0126 #ifndef QT_NO_DATASTREAM
0127 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPoint &);
0128 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPoint &);
0129 #endif
0130 
0131 /*****************************************************************************
0132   QPoint inline functions
0133  *****************************************************************************/
0134 
0135 constexpr inline QPoint::QPoint() noexcept : xp(0), yp(0) {}
0136 
0137 constexpr inline QPoint::QPoint(int xpos, int ypos) noexcept : xp(xpos), yp(ypos) {}
0138 
0139 constexpr inline bool QPoint::isNull() const noexcept
0140 {
0141     return xp == 0 && yp == 0;
0142 }
0143 
0144 constexpr inline int QPoint::x() const noexcept
0145 {
0146     return xp.value();
0147 }
0148 
0149 constexpr inline int QPoint::y() const noexcept
0150 {
0151     return yp.value();
0152 }
0153 
0154 constexpr inline void QPoint::setX(int xpos) noexcept
0155 {
0156     xp.setValue(xpos);
0157 }
0158 
0159 constexpr inline void QPoint::setY(int ypos) noexcept
0160 {
0161     yp.setValue(ypos);
0162 }
0163 
0164 inline int constexpr QPoint::manhattanLength() const
0165 {
0166     return (qAbs(xp) + qAbs(yp)).value();
0167 }
0168 
0169 constexpr inline int &QPoint::rx() noexcept
0170 {
0171     return xp.as_underlying();
0172 }
0173 
0174 constexpr inline int &QPoint::ry() noexcept
0175 {
0176     return yp.as_underlying();
0177 }
0178 
0179 constexpr inline QPoint &QPoint::operator+=(const QPoint &p)
0180 {
0181     xp += p.xp;
0182     yp += p.yp;
0183     return *this;
0184 }
0185 
0186 constexpr inline QPoint &QPoint::operator-=(const QPoint &p)
0187 {
0188     xp -= p.xp;
0189     yp -= p.yp;
0190     return *this;
0191 }
0192 
0193 constexpr inline QPoint &QPoint::operator*=(float factor)
0194 {
0195     xp.setValue(QtPrivate::qSaturateRound(x() * factor));
0196     yp.setValue(QtPrivate::qSaturateRound(y() * factor));
0197     return *this;
0198 }
0199 
0200 constexpr inline QPoint &QPoint::operator*=(double factor)
0201 {
0202     xp.setValue(QtPrivate::qSaturateRound(x() * factor));
0203     yp.setValue(QtPrivate::qSaturateRound(y() * factor));
0204     return *this;
0205 }
0206 
0207 constexpr inline QPoint &QPoint::operator*=(int factor)
0208 {
0209     xp = xp * factor;
0210     yp = yp * factor;
0211     return *this;
0212 }
0213 
0214 constexpr inline QPoint &QPoint::operator/=(qreal c)
0215 {
0216     Q_ASSERT(!qFuzzyIsNull(c));
0217     xp.setValue(qRound(int(xp) / c));
0218     yp.setValue(qRound(int(yp) / c));
0219     return *this;
0220 }
0221 
0222 #ifndef QT_NO_DEBUG_STREAM
0223 Q_CORE_EXPORT QDebug operator<<(QDebug, const QPoint &);
0224 #endif
0225 
0226 Q_CORE_EXPORT size_t qHash(QPoint key, size_t seed = 0) noexcept;
0227 
0228 
0229 
0230 
0231 class QPointF
0232 {
0233 public:
0234     constexpr QPointF() noexcept;
0235     constexpr QPointF(const QPoint &p) noexcept;
0236     constexpr QPointF(qreal xpos, qreal ypos) noexcept;
0237 
0238     constexpr inline qreal manhattanLength() const;
0239 
0240     inline bool isNull() const noexcept;
0241 
0242     constexpr inline qreal x() const noexcept;
0243     constexpr inline qreal y() const noexcept;
0244     constexpr inline void setX(qreal x) noexcept;
0245     constexpr inline void setY(qreal y) noexcept;
0246 
0247     constexpr QPointF transposed() const noexcept { return {yp, xp}; }
0248 
0249     constexpr inline qreal &rx() noexcept;
0250     constexpr inline qreal &ry() noexcept;
0251 
0252     constexpr inline QPointF &operator+=(const QPointF &p);
0253     constexpr inline QPointF &operator-=(const QPointF &p);
0254     constexpr inline QPointF &operator*=(qreal c);
0255     constexpr inline QPointF &operator/=(qreal c);
0256 
0257     constexpr static inline qreal dotProduct(const QPointF &p1, const QPointF &p2)
0258     {
0259         return p1.xp * p2.xp + p1.yp * p2.yp;
0260     }
0261 
0262 private:
0263     friend constexpr bool qFuzzyCompare(const QPointF &p1, const QPointF &p2) noexcept
0264     {
0265         return QtPrivate::fuzzyCompare(p1.xp, p2.xp)
0266             && QtPrivate::fuzzyCompare(p1.yp, p2.yp);
0267     }
0268     friend constexpr bool qFuzzyIsNull(const QPointF &point) noexcept
0269     {
0270         return qFuzzyIsNull(point.xp) && qFuzzyIsNull(point.yp);
0271     }
0272     friend constexpr bool comparesEqual(const QPointF &p1, const QPointF &p2) noexcept
0273     { return qFuzzyCompare(p1, p2); }
0274     Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QPointF)
0275     friend constexpr bool comparesEqual(const QPointF &p1, const QPoint &p2) noexcept
0276     { return comparesEqual(p1, p2.toPointF()); }
0277     Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QPointF, QPoint)
0278     friend constexpr inline QPointF operator+(const QPointF &p1, const QPointF &p2)
0279     { return QPointF(p1.xp + p2.xp, p1.yp + p2.yp); }
0280     friend constexpr inline QPointF operator-(const QPointF &p1, const QPointF &p2)
0281     { return QPointF(p1.xp - p2.xp, p1.yp - p2.yp); }
0282     friend constexpr inline QPointF operator*(const QPointF &p, qreal c)
0283     { return QPointF(p.xp * c, p.yp * c); }
0284     friend constexpr inline QPointF operator*(qreal c, const QPointF &p)
0285     { return QPointF(p.xp * c, p.yp * c); }
0286     friend constexpr inline QPointF operator+(const QPointF &p)
0287     { return p; }
0288     friend constexpr inline QPointF operator-(const QPointF &p)
0289     { return QPointF(-p.xp, -p.yp); }
0290     friend constexpr inline QPointF operator/(const QPointF &p, qreal divisor)
0291     {
0292         Q_ASSERT(divisor < 0 || divisor > 0);
0293         return QPointF(p.xp / divisor, p.yp / divisor);
0294     }
0295 
0296 public:
0297     constexpr QPoint toPoint() const;
0298 
0299 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0300     [[nodiscard]] Q_CORE_EXPORT static QPointF fromCGPoint(CGPoint point) noexcept;
0301     [[nodiscard]] Q_CORE_EXPORT CGPoint toCGPoint() const noexcept;
0302 #endif
0303 
0304 private:
0305     friend class QTransform;
0306 
0307     qreal xp;
0308     qreal yp;
0309 
0310     template <std::size_t I,
0311               typename P,
0312               std::enable_if_t<(I < 2), bool> = true,
0313               std::enable_if_t<std::is_same_v<q20::remove_cvref_t<P>, QPointF>, bool> = true>
0314     friend constexpr decltype(auto) get(P &&p) noexcept
0315     {
0316         if constexpr (I == 0)
0317             return q23::forward_like<P>(p.xp);
0318         else if constexpr (I == 1)
0319             return q23::forward_like<P>(p.yp);
0320     }
0321 };
0322 
0323 Q_DECLARE_TYPEINFO(QPointF, Q_PRIMITIVE_TYPE);
0324 
0325 size_t qHash(QPointF, size_t seed = 0) = delete;
0326 
0327 /*****************************************************************************
0328   QPointF stream functions
0329  *****************************************************************************/
0330 #ifndef QT_NO_DATASTREAM
0331 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QPointF &);
0332 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QPointF &);
0333 #endif
0334 
0335 /*****************************************************************************
0336   QPointF inline functions
0337  *****************************************************************************/
0338 
0339 constexpr inline QPointF::QPointF() noexcept : xp(0), yp(0) { }
0340 
0341 constexpr inline QPointF::QPointF(qreal xpos, qreal ypos) noexcept : xp(xpos), yp(ypos) { }
0342 
0343 constexpr inline QPointF::QPointF(const QPoint &p) noexcept : xp(p.x()), yp(p.y()) { }
0344 
0345 constexpr inline qreal QPointF::manhattanLength() const
0346 {
0347     return qAbs(x()) + qAbs(y());
0348 }
0349 
0350 inline bool QPointF::isNull() const noexcept
0351 {
0352     return qIsNull(xp) && qIsNull(yp);
0353 }
0354 
0355 constexpr inline qreal QPointF::x() const noexcept
0356 {
0357     return xp;
0358 }
0359 
0360 constexpr inline qreal QPointF::y() const noexcept
0361 {
0362     return yp;
0363 }
0364 
0365 constexpr inline void QPointF::setX(qreal xpos) noexcept
0366 {
0367     xp = xpos;
0368 }
0369 
0370 constexpr inline void QPointF::setY(qreal ypos) noexcept
0371 {
0372     yp = ypos;
0373 }
0374 
0375 constexpr inline qreal &QPointF::rx() noexcept
0376 {
0377     return xp;
0378 }
0379 
0380 constexpr inline qreal &QPointF::ry() noexcept
0381 {
0382     return yp;
0383 }
0384 
0385 constexpr inline QPointF &QPointF::operator+=(const QPointF &p)
0386 {
0387     xp += p.xp;
0388     yp += p.yp;
0389     return *this;
0390 }
0391 
0392 constexpr inline QPointF &QPointF::operator-=(const QPointF &p)
0393 {
0394     xp -= p.xp;
0395     yp -= p.yp;
0396     return *this;
0397 }
0398 
0399 constexpr inline QPointF &QPointF::operator*=(qreal c)
0400 {
0401     xp *= c;
0402     yp *= c;
0403     return *this;
0404 }
0405 
0406 constexpr inline QPointF &QPointF::operator/=(qreal divisor)
0407 {
0408     Q_ASSERT(divisor > 0 || divisor < 0);
0409     xp /= divisor;
0410     yp /= divisor;
0411     return *this;
0412 }
0413 
0414 constexpr QPointF QPoint::toPointF() const noexcept { return *this; }
0415 
0416 constexpr inline QPoint QPointF::toPoint() const
0417 {
0418     return QPoint(QtPrivate::qSaturateRound(xp), QtPrivate::qSaturateRound(yp));
0419 }
0420 
0421 #ifndef QT_NO_DEBUG_STREAM
0422 Q_CORE_EXPORT QDebug operator<<(QDebug d, const QPointF &p);
0423 #endif
0424 
0425 QT_END_NAMESPACE
0426 
0427 /*****************************************************************************
0428   QPoint/QPointF tuple protocol
0429  *****************************************************************************/
0430 
0431 namespace std {
0432     template <>
0433     class tuple_size<QT_PREPEND_NAMESPACE(QPoint)> : public integral_constant<size_t, 2> {};
0434     template <>
0435     class tuple_element<0, QT_PREPEND_NAMESPACE(QPoint)> { public: using type = int; };
0436     template <>
0437     class tuple_element<1, QT_PREPEND_NAMESPACE(QPoint)> { public: using type = int; };
0438 
0439     template <>
0440     class tuple_size<QT_PREPEND_NAMESPACE(QPointF)> : public integral_constant<size_t, 2> {};
0441     template <>
0442     class tuple_element<0, QT_PREPEND_NAMESPACE(QPointF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); };
0443     template <>
0444     class tuple_element<1, QT_PREPEND_NAMESPACE(QPointF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); };
0445 }
0446 
0447 #endif // QPOINT_H