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>
bb10
Volker Hilsheimer 2020-10-16 11:23:57 +02:00
parent a9982e64eb
commit 833a6e18bf
3 changed files with 76 additions and 4 deletions

View File

@ -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;
}

View File

@ -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;

View File

@ -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;