diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2020-10-16 11:23:57 +0200 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2020-10-17 08:21:52 +0200 |
commit | 833a6e18bfeda0e33963b1231c8ed9224332c4b3 (patch) | |
tree | a18fed2bc7520862ef39aa0a1695f70a573135c6 | |
parent | a9982e64eb5108e5c4b51a4e5738c08eff6d07e7 (diff) |
Implement move and comparison operators for QEventPoint
QEventPoints are equal when all data values are equal, the
refcount is ignored.
Change-Id: I6ef70faf0b12129eaa22bfc1f0a45bab2422d421
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
-rw-r--r-- | src/gui/kernel/qevent.cpp | 52 | ||||
-rw-r--r-- | src/gui/kernel/qevent.h | 6 | ||||
-rw-r--r-- | src/gui/kernel/qevent_p.h | 22 |
3 files changed, 76 insertions, 4 deletions
diff --git a/src/gui/kernel/qevent.cpp b/src/gui/kernel/qevent.cpp index 38906df6cb..d34b74d9d0 100644 --- a/src/gui/kernel/qevent.cpp +++ b/src/gui/kernel/qevent.cpp @@ -243,24 +243,68 @@ QEventPoint::QEventPoint(int id, const QPointingDevice *device) QEventPoint::QEventPoint(int pointId, State state, const QPointF &scenePosition, const QPointF &globalPosition) : d(new QEventPointPrivate(pointId, state, scenePosition, globalPosition)) {} +/*! + Constructs an event point by making a shallow copy of \a other. +*/ QEventPoint::QEventPoint(const QEventPoint &other) : d(other.d) { - d->refCount++; + if (d) + d->refCount++; } +/*! + Assigns \a other to this event point and returns a reference to this + event point. +*/ QEventPoint &QEventPoint::operator=(const QEventPoint &other) { - other.d->refCount++; - if (!(--d->refCount)) + if (other.d) + other.d->refCount++; + if (d && !(--d->refCount)) delete d; d = other.d; return *this; } +/*! + \fn QEventPoint::QEventPoint(QEventPoint &&other) noexcept + + Constructs an event point by moving \a other. +*/ + +/*! + \fn QEventPoint &QEventPoint QEventPoint::operator=(QEventPoint &&other) noexcept + + Move-assigns \a other to this event point instance. +*/ + +/*! + Returns \c true if this event point is equal to \a other, otherwise + return \c false. +*/ +bool QEventPoint::operator==(const QEventPoint &other) const noexcept +{ + if (d == other.d) + return true; + if (!d || !other.d) + return false; + return *d == *other.d; +} + +/*! + \fn bool QEventPoint::operator!=(const QEventPoint &other) const noexcept + + Returns \c true if this event point is not equal to \a other, otherwise + return \c false. +*/ + +/*! + Destroys the event point. +*/ QEventPoint::~QEventPoint() { - if (!(--d->refCount)) + if (d && !(--d->refCount)) delete d; } diff --git a/src/gui/kernel/qevent.h b/src/gui/kernel/qevent.h index 190959cb16..6abbae07be 100644 --- a/src/gui/kernel/qevent.h +++ b/src/gui/kernel/qevent.h @@ -124,8 +124,14 @@ public: QEventPoint(int id = -1, const QPointingDevice *device = nullptr); QEventPoint(int pointId, State state, const QPointF &scenePosition, const QPointF &globalPosition); QEventPoint(const QEventPoint &other); + QEventPoint(QEventPoint && other) noexcept : d(std::move(other.d)) { other.d = nullptr; } QEventPoint &operator=(const QEventPoint &other); + QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QEventPoint) + bool operator==(const QEventPoint &other) const noexcept; + inline bool operator!=(const QEventPoint &other) const noexcept { return !operator==(other); } ~QEventPoint(); + inline void swap(QEventPoint &other) noexcept + { qSwap(d, other.d); } QPointF position() const; QPointF pressPosition() const; diff --git a/src/gui/kernel/qevent_p.h b/src/gui/kernel/qevent_p.h index c923af92c9..205673d53d 100644 --- a/src/gui/kernel/qevent_p.h +++ b/src/gui/kernel/qevent_p.h @@ -70,6 +70,28 @@ struct QEventPointPrivate { if (state == QEventPoint::State::Released) pressure = 0; } + inline bool operator==(const QEventPointPrivate &other) const + { + return device == other.device + && window == other.window + && target == other.target + && pos == other.pos + && scenePos == other.scenePos + && globalPos == other.globalPos + && globalPressPos == other.globalPressPos + && globalGrabPos == other.globalGrabPos + && globalLastPos == other.globalLastPos + && pressure == other.pressure + && rotation == other.rotation + && ellipseDiameters == other.ellipseDiameters + && velocity == other.velocity + && timestamp == other.timestamp + && lastTimestamp == other.lastTimestamp + && pressTimestamp == other.pressTimestamp + && uniqueId == other.uniqueId + && pointId == other.pointId + && state == other.state; + } const QPointingDevice *device = nullptr; QPointer<QWindow> window; |