diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2020-01-03 17:18:02 +0100 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2020-10-03 11:47:17 +0200 |
commit | 889d40ebe2d9d0e92caea2749608720f7c088173 (patch) | |
tree | a68214fbf5a0dc9b1488e4ae79a499dabc1cc445 /src/gui/image | |
parent | 990e2e4fb8ef546b89b5e83e659771edd5b4ed3e (diff) |
Centralize the implementation of move assignment operators
At the moment we have two main strategies for dealing with move
assignment in Qt:
1) move-and-swap, used by "containers" (in the broad sense): containers,
but also smart pointers and similar classes that can hold user-defined
types;
2) pure swap, used by containers that hold only memory (e.g. QString,
QByteArray, ...) as well as most implicitly shared datatypes.
Given the fact that a move assignment operator's code is just
boilerplate (whether it's move-and-swap or pure swap), provide two
_strictly internal_ macros to help write them, and apply the macros
across corelib and gui, porting away from the hand-rolled
implementations.
The rule of thumb when porting to the new macros is:
* Try to stick to the existing code behavior, unless broken
* if changing, then follow this checklist:
* if the class does not have a move constructor => pure swap
(but consider ADDING a move constructor, if possible!)
* if the class does have a move constructor, try to follow the
criteria above, namely:
* if the class holds only memory, pure swap;
* if the class may hold anything else but memory (file handles,
etc.), then move and swap.
Noteworthy details:
* some operators planned to be removed in Qt 6 were not ported;
* as drive-by, some move constructors were simplified to be using
qExchange(); others were outright broken and got fixed;
* some contained some more interesting code and were not touched.
Change-Id: Idaab3489247dcbabb6df3fa1e5286b69e1d372e9
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src/gui/image')
-rw-r--r-- | src/gui/image/qicon.h | 7 | ||||
-rw-r--r-- | src/gui/image/qimage.h | 7 | ||||
-rw-r--r-- | src/gui/image/qpicture.h | 3 | ||||
-rw-r--r-- | src/gui/image/qpixmap.h | 3 |
4 files changed, 8 insertions, 12 deletions
diff --git a/src/gui/image/qicon.h b/src/gui/image/qicon.h index 964c29e785..fab8c3fe21 100644 --- a/src/gui/image/qicon.h +++ b/src/gui/image/qicon.h @@ -62,14 +62,13 @@ public: QIcon(const QPixmap &pixmap); QIcon(const QIcon &other); QIcon(QIcon &&other) noexcept - : d(other.d) - { other.d = nullptr; } + : d(qExchange(other.d, nullptr)) + {} explicit QIcon(const QString &fileName); // file or resource name explicit QIcon(QIconEngine *engine); ~QIcon(); QIcon &operator=(const QIcon &other); - inline QIcon &operator=(QIcon &&other) noexcept - { swap(other); return *this; } + QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QIcon) inline void swap(QIcon &other) noexcept { qSwap(d, other.d); } bool operator==(const QIcon &) const = delete; diff --git a/src/gui/image/qimage.h b/src/gui/image/qimage.h index 89a696d5d6..d00c11b669 100644 --- a/src/gui/image/qimage.h +++ b/src/gui/image/qimage.h @@ -125,13 +125,12 @@ public: QImage(const QImage &); inline QImage(QImage &&other) noexcept - : QPaintDevice(), d(nullptr) - { qSwap(d, other.d); } + : QPaintDevice(), d(qExchange(other.d, nullptr)) + {} ~QImage(); QImage &operator=(const QImage &); - inline QImage &operator=(QImage &&other) noexcept - { qSwap(d, other.d); return *this; } + QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QImage) inline void swap(QImage &other) noexcept { qSwap(d, other.d); } diff --git a/src/gui/image/qpicture.h b/src/gui/image/qpicture.h index 34860985ed..c3e622a1fe 100644 --- a/src/gui/image/qpicture.h +++ b/src/gui/image/qpicture.h @@ -77,8 +77,7 @@ public: void setBoundingRect(const QRect &r); QPicture& operator=(const QPicture &p); - inline QPicture &operator=(QPicture &&other) noexcept - { qSwap(d_ptr, other.d_ptr); return *this; } + QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QPicture) inline void swap(QPicture &other) noexcept { d_ptr.swap(other.d_ptr); } void detach(); diff --git a/src/gui/image/qpixmap.h b/src/gui/image/qpixmap.h index b30bef0f6b..e731597659 100644 --- a/src/gui/image/qpixmap.h +++ b/src/gui/image/qpixmap.h @@ -75,8 +75,7 @@ public: ~QPixmap(); QPixmap &operator=(const QPixmap &); - inline QPixmap &operator=(QPixmap &&other) noexcept - { qSwap(data, other.data); return *this; } + QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QPixmap) inline void swap(QPixmap &other) noexcept { qSwap(data, other.data); } bool operator==(const QPixmap &) const = delete; |