diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2013-11-13 11:37:37 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-11-17 09:47:07 +0100 |
commit | 3e803b5180e7059c01c15ea84cadd54982e1a221 (patch) | |
tree | 4ce472724bf7ef840c270f89e9ccffac523b36ae /src/corelib | |
parent | 6c04c21c101b70401ad9cb08de1562dc90166e9c (diff) |
QCollator: enable move semantics
This necessitates adding d==0 checks in QCollator. By documenting that
moved-from instances can only be assigned to or destroyed, we can
limit the functions in which to check for d==0 to the assignment
operator and the destructor.
Doing otherwise would destroy all advantages of move semantics by
introducing a heap allocation to re-populate other.d.
Add a test for this (QCollator didn't have any before).
Change-Id: Ic6ff202072822bebfd5e48259c3d0fa345a63118
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/tools/qcollator.cpp | 26 | ||||
-rw-r--r-- | src/corelib/tools/qcollator.h | 6 |
2 files changed, 29 insertions, 3 deletions
diff --git a/src/corelib/tools/qcollator.cpp b/src/corelib/tools/qcollator.cpp index cfb5fe77ae..80d330bfca 100644 --- a/src/corelib/tools/qcollator.cpp +++ b/src/corelib/tools/qcollator.cpp @@ -99,7 +99,7 @@ QCollator::QCollator(const QCollator &other) */ QCollator::~QCollator() { - if (!d->ref.deref()) + if (d && !d->ref.deref()) delete d; } @@ -109,14 +109,34 @@ QCollator::~QCollator() QCollator &QCollator::operator=(const QCollator &other) { if (this != &other) { - if (!d->ref.deref()) + if (d && !d->ref.deref()) delete d; d = other.d; - d->ref.ref(); + if (d) d->ref.ref(); } return *this; } +/* + \fn void QCollator::QCollator(QCollator &&other) + + Move constructor. Moves from \a other into this collator. + + Note that a moved-from QCollator can only be destroyed or assigned + to. The effect of calling other functions than the destructor or + one of the assignment operators is undefined. +*/ + +/* + \fn QCollator &QCollator::operator=(QCollator &&other) + + Move-assigns from \a other to this collator. + + Note that a moved-from QCollator can only be destroyed or assigned + to. The effect of calling other functions than the destructor or + one of the assignment operators is undefined. +*/ + /*! \fn void QCollator::swap(QCollator &other) diff --git a/src/corelib/tools/qcollator.h b/src/corelib/tools/qcollator.h index e5abc91967..1007aa7623 100644 --- a/src/corelib/tools/qcollator.h +++ b/src/corelib/tools/qcollator.h @@ -85,6 +85,12 @@ public: QCollator(const QCollator &); ~QCollator(); QCollator &operator=(const QCollator &); +#ifdef Q_COMPILER_RVALUE_REFS + QCollator(QCollator &&other) + : d(other.d) { other.d = 0; } + QCollator &operator=(QCollator &&other) + { swap(other); return *this; } +#endif void swap(QCollator &other) { qSwap(d, other.d); } |