summaryrefslogtreecommitdiffstats
path: root/src/gui
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-01-03 17:18:02 +0100
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-10-03 11:47:17 +0200
commit889d40ebe2d9d0e92caea2749608720f7c088173 (patch)
treea68214fbf5a0dc9b1488e4ae79a499dabc1cc445 /src/gui
parent990e2e4fb8ef546b89b5e83e659771edd5b4ed3e (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')
-rw-r--r--src/gui/image/qicon.h7
-rw-r--r--src/gui/image/qimage.h7
-rw-r--r--src/gui/image/qpicture.h3
-rw-r--r--src/gui/image/qpixmap.h3
-rw-r--r--src/gui/kernel/qcursor.h5
-rw-r--r--src/gui/kernel/qkeysequence.h2
-rw-r--r--src/gui/kernel/qpalette.h9
-rw-r--r--src/gui/painting/qbrush.h3
-rw-r--r--src/gui/painting/qcolorspace.h7
-rw-r--r--src/gui/painting/qcolortransform.h6
-rw-r--r--src/gui/painting/qpagelayout.h2
-rw-r--r--src/gui/painting/qpagesize.h2
-rw-r--r--src/gui/painting/qpainterpath.h3
-rw-r--r--src/gui/painting/qpen.h5
-rw-r--r--src/gui/painting/qregion.h5
-rw-r--r--src/gui/text/qfont.h3
-rw-r--r--src/gui/text/qfontengine_p.h9
-rw-r--r--src/gui/text/qfontmetrics.h6
-rw-r--r--src/gui/text/qglyphrun.h2
-rw-r--r--src/gui/text/qrawfont.h2
-rw-r--r--src/gui/text/qstatictext.h2
-rw-r--r--src/gui/text/qtextcursor.h2
22 files changed, 34 insertions, 61 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;
diff --git a/src/gui/kernel/qcursor.h b/src/gui/kernel/qcursor.h
index 1ae60d9311..c1d45cc41b 100644
--- a/src/gui/kernel/qcursor.h
+++ b/src/gui/kernel/qcursor.h
@@ -87,9 +87,8 @@ public:
QCursor(const QCursor &cursor);
~QCursor();
QCursor &operator=(const QCursor &cursor);
- QCursor(QCursor &&other) noexcept : d(other.d) { other.d = nullptr; }
- inline QCursor &operator=(QCursor &&other) noexcept
- { swap(other); return *this; }
+ QCursor(QCursor &&other) noexcept : d(qExchange(other.d, nullptr)) {}
+ QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QCursor)
void swap(QCursor &other) noexcept { qSwap(d, other.d); }
diff --git a/src/gui/kernel/qkeysequence.h b/src/gui/kernel/qkeysequence.h
index 880d0a581a..61001dddbc 100644
--- a/src/gui/kernel/qkeysequence.h
+++ b/src/gui/kernel/qkeysequence.h
@@ -185,7 +185,7 @@ public:
operator QVariant() const;
QKeyCombination operator[](uint i) const;
QKeySequence &operator=(const QKeySequence &other);
- QKeySequence &operator=(QKeySequence &&other) noexcept { swap(other); return *this; }
+ QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QKeySequence)
void swap(QKeySequence &other) noexcept { qSwap(d, other.d); }
bool operator==(const QKeySequence &other) const;
diff --git a/src/gui/kernel/qpalette.h b/src/gui/kernel/qpalette.h
index 8990f58512..7724480138 100644
--- a/src/gui/kernel/qpalette.h
+++ b/src/gui/kernel/qpalette.h
@@ -68,12 +68,9 @@ public:
~QPalette();
QPalette &operator=(const QPalette &palette);
QPalette(QPalette &&other) noexcept
- : d(other.d), data(other.data)
- { other.d = nullptr; }
- inline QPalette &operator=(QPalette &&other) noexcept
- {
- swap(other); return *this;
- }
+ : d(qExchange(other.d, nullptr)), data(other.data)
+ {}
+ QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QPalette)
void swap(QPalette &other) noexcept
{
diff --git a/src/gui/painting/qbrush.h b/src/gui/painting/qbrush.h
index 3b7b949bde..fea5afad57 100644
--- a/src/gui/painting/qbrush.h
+++ b/src/gui/painting/qbrush.h
@@ -78,8 +78,7 @@ public:
~QBrush();
QBrush &operator=(const QBrush &brush);
- inline QBrush &operator=(QBrush &&other) noexcept
- { qSwap(d, other.d); return *this; }
+ QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QBrush)
inline void swap(QBrush &other) noexcept
{ qSwap(d, other.d); }
diff --git a/src/gui/painting/qcolorspace.h b/src/gui/painting/qcolorspace.h
index 08c9944301..7505c6eca7 100644
--- a/src/gui/painting/qcolorspace.h
+++ b/src/gui/painting/qcolorspace.h
@@ -95,12 +95,7 @@ public:
QColorSpace(QColorSpace &&colorSpace) noexcept
: d_ptr(qExchange(colorSpace.d_ptr, nullptr))
{ }
- QColorSpace &operator=(QColorSpace &&colorSpace) noexcept
- {
- // Make the deallocation of this->d_ptr happen in ~QColorSpace()
- QColorSpace(std::move(colorSpace)).swap(*this);
- return *this;
- }
+ QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QColorSpace)
void swap(QColorSpace &colorSpace) noexcept
{ qSwap(d_ptr, colorSpace.d_ptr); }
diff --git a/src/gui/painting/qcolortransform.h b/src/gui/painting/qcolortransform.h
index 94b6b3a385..fa52f2e152 100644
--- a/src/gui/painting/qcolortransform.h
+++ b/src/gui/painting/qcolortransform.h
@@ -64,11 +64,7 @@ public:
QColorTransform{other}.swap(*this);
return *this;
}
- QColorTransform &operator=(QColorTransform &&other) noexcept
- {
- QColorTransform{std::move(other)}.swap(*this);
- return *this;
- }
+ QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QColorTransform)
void swap(QColorTransform &other) noexcept { qSwap(d, other.d); }
diff --git a/src/gui/painting/qpagelayout.h b/src/gui/painting/qpagelayout.h
index 6965606252..a7858edfaf 100644
--- a/src/gui/painting/qpagelayout.h
+++ b/src/gui/painting/qpagelayout.h
@@ -81,7 +81,7 @@ public:
const QMarginsF &margins, Unit units = Point,
const QMarginsF &minMargins = QMarginsF(0, 0, 0, 0));
QPageLayout(const QPageLayout &other);
- QPageLayout &operator=(QPageLayout &&other) noexcept { swap(other); return *this; }
+ QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QPageLayout)
QPageLayout &operator=(const QPageLayout &other);
~QPageLayout();
diff --git a/src/gui/painting/qpagesize.h b/src/gui/painting/qpagesize.h
index 1fa6c86081..fbaec0da3f 100644
--- a/src/gui/painting/qpagesize.h
+++ b/src/gui/painting/qpagesize.h
@@ -232,7 +232,7 @@ public:
const QString &name = QString(),
SizeMatchPolicy matchPolicy = FuzzyMatch);
QPageSize(const QPageSize &other);
- QPageSize &operator=(QPageSize &&other) noexcept { swap(other); return *this; }
+ QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QPageSize)
QPageSize &operator=(const QPageSize &other);
~QPageSize();
diff --git a/src/gui/painting/qpainterpath.h b/src/gui/painting/qpainterpath.h
index a839a73b4b..0099a7408a 100644
--- a/src/gui/painting/qpainterpath.h
+++ b/src/gui/painting/qpainterpath.h
@@ -92,8 +92,7 @@ public:
explicit QPainterPath(const QPointF &startPoint);
QPainterPath(const QPainterPath &other);
QPainterPath &operator=(const QPainterPath &other);
- inline QPainterPath &operator=(QPainterPath &&other) noexcept
- { qSwap(d_ptr, other.d_ptr); return *this; }
+ QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QPainterPath)
~QPainterPath();
inline void swap(QPainterPath &other) noexcept { d_ptr.swap(other.d_ptr); }
diff --git a/src/gui/painting/qpen.h b/src/gui/painting/qpen.h
index 38282bda39..db280c765b 100644
--- a/src/gui/painting/qpen.h
+++ b/src/gui/painting/qpen.h
@@ -71,9 +71,8 @@ public:
QPen &operator=(const QPen &pen) noexcept;
QPen(QPen &&other) noexcept
- : d(other.d) { other.d = nullptr; }
- QPen &operator=(QPen &&other) noexcept
- { qSwap(d, other.d); return *this; }
+ : d(qExchange(other.d, nullptr)) {}
+ QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QPen)
void swap(QPen &other) noexcept { qSwap(d, other.d); }
Qt::PenStyle style() const;
diff --git a/src/gui/painting/qregion.h b/src/gui/painting/qregion.h
index 8b991f2fb2..8f52185ed9 100644
--- a/src/gui/painting/qregion.h
+++ b/src/gui/painting/qregion.h
@@ -70,12 +70,11 @@ public:
QRegion(const QPolygon &pa, Qt::FillRule fillRule = Qt::OddEvenFill);
QRegion(const QRegion &region);
QRegion(QRegion &&other) noexcept
- : d(other.d) { other.d = const_cast<QRegionData*>(&shared_empty); }
+ : d(qExchange(other.d, const_cast<QRegionData*>(&shared_empty))) {}
QRegion(const QBitmap &bitmap);
~QRegion();
QRegion &operator=(const QRegion &);
- inline QRegion &operator=(QRegion &&other) noexcept
- { qSwap(d, other.d); return *this; }
+ QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QRegion);
inline void swap(QRegion &other) noexcept { qSwap(d, other.d); }
bool isEmpty() const;
bool isNull() const;
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();