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/text | |
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/text')
-rw-r--r-- | src/gui/text/qfont.h | 3 | ||||
-rw-r--r-- | src/gui/text/qfontengine_p.h | 9 | ||||
-rw-r--r-- | src/gui/text/qfontmetrics.h | 6 | ||||
-rw-r--r-- | src/gui/text/qglyphrun.h | 2 | ||||
-rw-r--r-- | src/gui/text/qrawfont.h | 2 | ||||
-rw-r--r-- | src/gui/text/qstatictext.h | 2 | ||||
-rw-r--r-- | src/gui/text/qtextcursor.h | 2 |
7 files changed, 10 insertions, 16 deletions
diff --git a/src/gui/text/qfont.h b/src/gui/text/qfont.h index 4079e48dda..1ef0497d39 100644 --- a/src/gui/text/qfont.h +++ b/src/gui/text/qfont.h @@ -252,8 +252,7 @@ public: bool operator<(const QFont &) const; operator QVariant() const; bool isCopyOf(const QFont &) const; - inline QFont &operator=(QFont &&other) noexcept - { qSwap(d, other.d); qSwap(resolve_mask, other.resolve_mask); return *this; } + QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QFont) QString key() const; diff --git a/src/gui/text/qfontengine_p.h b/src/gui/text/qfontengine_p.h index a81e877677..5d73e2cf8e 100644 --- a/src/gui/text/qfontengine_p.h +++ b/src/gui/text/qfontengine_p.h @@ -304,14 +304,11 @@ public: explicit Holder(void *p, qt_destroy_func_t d) : ptr(p), destroy_func(d) {} ~Holder() { if (ptr && destroy_func) destroy_func(ptr); } Holder(Holder &&other) noexcept - : ptr(other.ptr), - destroy_func(other.destroy_func) + : ptr(qExchange(other.ptr, nullptr)), + destroy_func(qExchange(other.destroy_func, nullptr)) { - other.ptr = nullptr; - other.destroy_func = nullptr; } - Holder &operator=(Holder &&other) noexcept - { swap(other); return *this; } + QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(Holder) void swap(Holder &other) noexcept { diff --git a/src/gui/text/qfontmetrics.h b/src/gui/text/qfontmetrics.h index 097d40bf8d..a199e20373 100644 --- a/src/gui/text/qfontmetrics.h +++ b/src/gui/text/qfontmetrics.h @@ -60,8 +60,7 @@ public: ~QFontMetrics(); QFontMetrics &operator=(const QFontMetrics &); - inline QFontMetrics &operator=(QFontMetrics &&other) noexcept - { qSwap(d, other.d); return *this; } + QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QFontMetrics) void swap(QFontMetrics &other) noexcept { qSwap(d, other.d); } @@ -131,8 +130,7 @@ public: QFontMetricsF &operator=(const QFontMetricsF &); QFontMetricsF &operator=(const QFontMetrics &); - inline QFontMetricsF &operator=(QFontMetricsF &&other) noexcept - { qSwap(d, other.d); return *this; } + QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QFontMetricsF) void swap(QFontMetricsF &other) noexcept { qSwap(d, other.d); } diff --git a/src/gui/text/qglyphrun.h b/src/gui/text/qglyphrun.h index 0da8eddda8..0e9f0ce468 100644 --- a/src/gui/text/qglyphrun.h +++ b/src/gui/text/qglyphrun.h @@ -66,7 +66,7 @@ public: QGlyphRun(); QGlyphRun(const QGlyphRun &other); - QGlyphRun &operator=(QGlyphRun &&other) noexcept { swap(other); return *this; } + QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QGlyphRun) QGlyphRun &operator=(const QGlyphRun &other); ~QGlyphRun(); diff --git a/src/gui/text/qrawfont.h b/src/gui/text/qrawfont.h index e7e12df040..51f7ba8044 100644 --- a/src/gui/text/qrawfont.h +++ b/src/gui/text/qrawfont.h @@ -79,7 +79,7 @@ public: qreal pixelSize, QFont::HintingPreference hintingPreference = QFont::PreferDefaultHinting); QRawFont(const QRawFont &other); - QRawFont &operator=(QRawFont &&other) noexcept { swap(other); return *this; } + QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QRawFont) QRawFont &operator=(const QRawFont &other); ~QRawFont(); diff --git a/src/gui/text/qstatictext.h b/src/gui/text/qstatictext.h index e8c94a6add..778861ed79 100644 --- a/src/gui/text/qstatictext.h +++ b/src/gui/text/qstatictext.h @@ -64,7 +64,7 @@ public: QStaticText(); explicit QStaticText(const QString &text); QStaticText(const QStaticText &other); - QStaticText &operator=(QStaticText &&other) noexcept { swap(other); return *this; } + QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QStaticText) QStaticText &operator=(const QStaticText &); ~QStaticText(); diff --git a/src/gui/text/qtextcursor.h b/src/gui/text/qtextcursor.h index 7cad3cc5e8..59b32e0874 100644 --- a/src/gui/text/qtextcursor.h +++ b/src/gui/text/qtextcursor.h @@ -73,7 +73,7 @@ public: explicit QTextCursor(QTextFrame *frame); explicit QTextCursor(const QTextBlock &block); QTextCursor(const QTextCursor &cursor); - QTextCursor &operator=(QTextCursor &&other) noexcept { swap(other); return *this; } + QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QTextCursor) QTextCursor &operator=(const QTextCursor &other); ~QTextCursor(); |