summaryrefslogtreecommitdiffstats
path: root/src/corelib/text
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/corelib/text
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/corelib/text')
-rw-r--r--src/corelib/text/qbytearray.h3
-rw-r--r--src/corelib/text/qcollator.h6
-rw-r--r--src/corelib/text/qlocale.h2
-rw-r--r--src/corelib/text/qregularexpression.h3
-rw-r--r--src/corelib/text/qstring.h3
5 files changed, 6 insertions, 11 deletions
diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h
index 8f5e1d9f7f..60f1bd01f5 100644
--- a/src/corelib/text/qbytearray.h
+++ b/src/corelib/text/qbytearray.h
@@ -170,8 +170,7 @@ public:
QByteArray &operator=(const char *str);
inline QByteArray(QByteArray && other) noexcept
{ qSwap(d, other.d); }
- inline QByteArray &operator=(QByteArray &&other) noexcept
- { qSwap(d, other.d); return *this; }
+ QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QByteArray)
inline void swap(QByteArray &other) noexcept
{ qSwap(d, other.d); }
diff --git a/src/corelib/text/qcollator.h b/src/corelib/text/qcollator.h
index de8c8b7e3c..793d1cedd4 100644
--- a/src/corelib/text/qcollator.h
+++ b/src/corelib/text/qcollator.h
@@ -57,8 +57,7 @@ public:
QCollatorSortKey(const QCollatorSortKey &other);
~QCollatorSortKey();
QCollatorSortKey &operator=(const QCollatorSortKey &other);
- inline QCollatorSortKey &operator=(QCollatorSortKey &&other) noexcept
- { swap(other); return *this; }
+ QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QCollatorSortKey)
void swap(QCollatorSortKey &other) noexcept
{ d.swap(other.d); }
@@ -88,8 +87,7 @@ public:
QCollator &operator=(const QCollator &);
QCollator(QCollator &&other) noexcept
: d(other.d) { other.d = nullptr; }
- QCollator &operator=(QCollator &&other) noexcept
- { swap(other); return *this; }
+ QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QCollator)
void swap(QCollator &other) noexcept
{ qSwap(d, other.d); }
diff --git a/src/corelib/text/qlocale.h b/src/corelib/text/qlocale.h
index 0ff2e4d469..8b49eb89f9 100644
--- a/src/corelib/text/qlocale.h
+++ b/src/corelib/text/qlocale.h
@@ -944,7 +944,7 @@ public:
QLocale(Language language, Country country = AnyCountry);
QLocale(Language language, Script script, Country country);
QLocale(const QLocale &other);
- QLocale &operator=(QLocale &&other) noexcept { swap(other); return *this; }
+ QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QLocale)
QLocale &operator=(const QLocale &other);
~QLocale();
diff --git a/src/corelib/text/qregularexpression.h b/src/corelib/text/qregularexpression.h
index ee6c421399..6067d94c76 100644
--- a/src/corelib/text/qregularexpression.h
+++ b/src/corelib/text/qregularexpression.h
@@ -88,8 +88,7 @@ public:
QRegularExpression(const QRegularExpression &re);
~QRegularExpression();
QRegularExpression &operator=(const QRegularExpression &re);
- QRegularExpression &operator=(QRegularExpression &&re) noexcept
- { d.swap(re.d); return *this; }
+ QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QRegularExpression)
void swap(QRegularExpression &other) noexcept { d.swap(other.d); }
diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h
index ea78dd017f..0e564b20ed 100644
--- a/src/corelib/text/qstring.h
+++ b/src/corelib/text/qstring.h
@@ -337,8 +337,7 @@ public:
QString &operator=(QLatin1String latin1);
inline QString(QString &&other) noexcept
{ qSwap(d, other.d); }
- inline QString &operator=(QString &&other) noexcept
- { qSwap(d, other.d); return *this; }
+ QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QString)
inline void swap(QString &other) noexcept { qSwap(d, other.d); }
inline qsizetype size() const { return d.size; }
inline qsizetype count() const { return d.size; }