QPalette: add move constructor

As with many other implicitly shared classes, efficient move semantics
requires setting the d-pointer to nullptr, which then needs to be checked
for in the dtor and the copy assignment operator.

Change-Id: I654d181a1dfdd9a16e2f9fb96b57475cdd0b4561
Reviewed-by: J-P Nurmi <jpnurmi@digia.com>
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
bb10
Marc Mutz 2014-07-25 23:17:55 +02:00
parent fa31f9761a
commit de8a9dee5a
3 changed files with 23 additions and 2 deletions

View File

@ -642,12 +642,23 @@ QPalette::QPalette(const QPalette &p)
d->ref.ref();
}
/*!
\fn QPalette::QPalette(QPalette &&other)
\since 5.4
Move-constructs a QPalette instance, making it point at the same
object that \a other was pointing to.
After being moved from, you can only assign to or destroy \a other.
Any other operation will result in undefined behavior.
*/
/*!
Destroys the palette.
*/
QPalette::~QPalette()
{
if(!d->ref.deref())
if (d && !d->ref.deref())
delete d;
}
@ -668,7 +679,7 @@ QPalette &QPalette::operator=(const QPalette &p)
{
p.d->ref.ref();
data = p.data;
if(!d->ref.deref())
if (d && !d->ref.deref())
delete d;
d = p.d;
return *this;

View File

@ -70,6 +70,8 @@ public:
~QPalette();
QPalette &operator=(const QPalette &palette);
#ifdef Q_COMPILER_RVALUE_REFS
QPalette(QPalette &&other) Q_DECL_NOTHROW
: d(other.d), data(other.data) { other.d = Q_NULLPTR; }
inline QPalette &operator=(QPalette &&other)
{
for_faster_swapping_dont_use = other.for_faster_swapping_dont_use;

View File

@ -106,6 +106,14 @@ void tst_QPalette::moveSemantics()
src = control; // check moved-from 'src' can still be assigned to (doesn't crash)
QVERIFY(src.isCopyOf(dst));
QVERIFY(src.isCopyOf(control));
QPalette dst2(qMove(src)); // move construction
QVERIFY(!src.isCopyOf(dst));
QVERIFY(!src.isCopyOf(dst2));
QVERIFY(!src.isCopyOf(control));
QCOMPARE(dst2, control);
QVERIFY(dst2.isCopyOf(dst));
QVERIFY(dst2.isCopyOf(control));
// check moved-from 'src' can still be destroyed (doesn't crash)
#else
QSKIP("Compiler doesn't support C++11 move semantics");
#endif