diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2021-07-21 10:43:28 +0200 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2021-07-22 11:05:05 +0200 |
commit | f30b06cd5dbbbbc8a724c279b36d4e09e74e2346 (patch) | |
tree | ac3f2fba83911e12a24ba501d62233b95af34657 | |
parent | 5eae96e777f92ca3294a6e69541d8f32f2627992 (diff) | |
download | qtlocation-f30b06cd5dbbbbc8a724c279b36d4e09e74e2346.tar.gz |
QGeoCoordinate: support move operations
Add move-constructor and move-assignment operator, as well as a swap
method. Specialize the type as shared using Q_DECLARE_SHARED.
Task-number: QTBUG-95163
Pick-to: 6.2
Change-Id: Ie5e3bd319f9386c7e864122994a5dbcf0fb81240
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
-rw-r--r-- | src/positioning/qgeocoordinate.cpp | 25 | ||||
-rw-r--r-- | src/positioning/qgeocoordinate.h | 8 | ||||
-rw-r--r-- | tests/auto/qgeocoordinate/tst_qgeocoordinate.cpp | 33 |
3 files changed, 65 insertions, 1 deletions
diff --git a/src/positioning/qgeocoordinate.cpp b/src/positioning/qgeocoordinate.cpp index 0dff0925..fd31c0d9 100644 --- a/src/positioning/qgeocoordinate.cpp +++ b/src/positioning/qgeocoordinate.cpp @@ -253,6 +253,17 @@ QGeoCoordinate::QGeoCoordinate(const QGeoCoordinate &other) {} /*! + \fn QGeoCoordinate::QGeoCoordinate(QGeoCoordinate &&other) + \since 6.2 + + Constructs a coordinate by moving from \a other. + + \note The moved-from QGeoCoordinate object can only be destroyed or + assigned to. The effect of calling other functions than the destructor + or one of the assignment operators is undefined. +*/ + +/*! Assigns \a other to this coordinate and returns a reference to this coordinate. */ QGeoCoordinate &QGeoCoordinate::operator=(const QGeoCoordinate &other) @@ -265,12 +276,26 @@ QGeoCoordinate &QGeoCoordinate::operator=(const QGeoCoordinate &other) } /*! + \fn QGeoCoordinate &QGeoCoordinate::operator=(QGeoCoordinate &&other) + \since 6.2 + + Move-assigns \a other to this coordinate and returns a reference to this + coordinate. + + \note The moved-from QGeoCoordinate object can only be destroyed or + assigned to. The effect of calling other functions than the destructor + or one of the assignment operators is undefined. +*/ + +/*! Destroys the coordinate object. */ QGeoCoordinate::~QGeoCoordinate() { } +QT_DEFINE_QSDP_SPECIALIZATION_DTOR(QGeoCoordinatePrivate) + /*! \fn bool QGeoCoordinate::operator==(const QGeoCoordinate &lhs, const QGeoCoordinate &rhs) diff --git a/src/positioning/qgeocoordinate.h b/src/positioning/qgeocoordinate.h index 03c59235..46a539a2 100644 --- a/src/positioning/qgeocoordinate.h +++ b/src/positioning/qgeocoordinate.h @@ -52,6 +52,8 @@ class QDebug; class QDataStream; class QGeoCoordinatePrivate; +QT_DECLARE_QSDP_SPECIALIZATION_DTOR_WITH_EXPORT(QGeoCoordinatePrivate, Q_POSITIONING_EXPORT) + class Q_POSITIONING_EXPORT QGeoCoordinate { Q_GADGET @@ -83,9 +85,13 @@ public: QGeoCoordinate(double latitude, double longitude); QGeoCoordinate(double latitude, double longitude, double altitude); QGeoCoordinate(const QGeoCoordinate &other); + QGeoCoordinate(QGeoCoordinate &&other) noexcept = default; ~QGeoCoordinate(); QGeoCoordinate &operator=(const QGeoCoordinate &other); + QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QGeoCoordinate) + + void swap(QGeoCoordinate &other) noexcept { d.swap(other.d); } friend bool operator==(const QGeoCoordinate &lhs, const QGeoCoordinate &rhs) { @@ -142,7 +148,7 @@ private: #endif }; -Q_DECLARE_TYPEINFO(QGeoCoordinate, Q_RELOCATABLE_TYPE); +Q_DECLARE_SHARED(QGeoCoordinate) Q_POSITIONING_EXPORT size_t qHash(const QGeoCoordinate &coordinate, size_t seed = 0); diff --git a/tests/auto/qgeocoordinate/tst_qgeocoordinate.cpp b/tests/auto/qgeocoordinate/tst_qgeocoordinate.cpp index 368ea774..820d904d 100644 --- a/tests/auto/qgeocoordinate/tst_qgeocoordinate.cpp +++ b/tests/auto/qgeocoordinate/tst_qgeocoordinate.cpp @@ -217,6 +217,19 @@ private slots: QTest::newRow("latitude, longitude, altitude arguments too low latitude & longitude") << QGeoCoordinate(-90.1, -180.1, DBL_MAX); } + void move_constructor() + { + QFETCH(QGeoCoordinate, c); + + const QGeoCoordinate coordinateCopy = c; + QCOMPARE(std::move(c), coordinateCopy); + } + + void move_constructor_data() + { + copy_constructor_data(); + } + void destructor() { QGeoCoordinate *coordinate; @@ -274,6 +287,26 @@ private slots: copy_constructor_data(); } + void move_assign() + { + QFETCH(QGeoCoordinate, c); + + QGeoCoordinate copy = c; + + QGeoCoordinate otherCoordinate; + otherCoordinate = std::move(c); + QCOMPARE(otherCoordinate, copy); + + // check that (move)assinging to a moved-from object is fine + c = std::move(copy); + QCOMPARE(c, otherCoordinate); + } + + void move_assign_data() + { + copy_constructor_data(); + } + void comparison() { QFETCH(QGeoCoordinate, c1); |