Add support for rvalue-references in QArrayDataPointer

I love how this magically makes SimpleVector move-aware :-)

Change-Id: I5cb75954d70cf256863c33e684ebc4551ac94f87
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
This commit is contained in:
João Abecasis 2012-02-10 16:13:56 +01:00 committed by Qt by Nokia
parent 113e921684
commit f9872d12a6
2 changed files with 60 additions and 0 deletions

View File

@ -86,6 +86,20 @@ public:
return *this;
}
#ifdef Q_COMPILER_RVALUE_REFS
QArrayDataPointer(QArrayDataPointer &&other)
: d(other.d)
{
other.d = Data::sharedNull();
}
QArrayDataPointer &operator=(QArrayDataPointer &&other)
{
this->swap(other);
return *this;
}
#endif
DataOps &operator*() const
{
Q_ASSERT(d);

View File

@ -84,6 +84,7 @@ private slots:
void fromRawData();
void literals();
void variadicLiterals();
void rValueReferences();
};
template <class T> const T &const_(const T &t) { return t; }
@ -1274,5 +1275,50 @@ void tst_QArrayData::variadicLiterals()
#endif // defined(Q_COMPILER_VARIADIC_MACROS)
}
#ifdef Q_COMPILER_RVALUE_REFS
// std::remove_reference is in C++11, but requires library support
template <class T> struct RemoveReference { typedef T Type; };
template <class T> struct RemoveReference<T &> { typedef T Type; };
// single-argument std::move is in C++11, but requires library support
template <class T>
typename RemoveReference<T>::Type &&cxx11Move(T &&t)
{
return static_cast<typename RemoveReference<T>::Type &&>(t);
}
#endif
void tst_QArrayData::rValueReferences()
{
#ifdef Q_COMPILER_RVALUE_REFS
SimpleVector<int> v1(1, 42);
SimpleVector<int> v2;
const SimpleVector<int>::const_iterator begin = v1.constBegin();
QVERIFY(!v1.isNull());
QVERIFY(v2.isNull());
// move-assign
v2 = cxx11Move(v1);
QVERIFY(v1.isNull());
QVERIFY(!v2.isNull());
QCOMPARE(v2.constBegin(), begin);
SimpleVector<int> v3(cxx11Move(v2));
QVERIFY(v1.isNull());
QVERIFY(v2.isNull());
QVERIFY(!v3.isNull());
QCOMPARE(v3.constBegin(), begin);
QCOMPARE(v3.size(), size_t(1));
QCOMPARE(v3.front(), 42);
#else
QSKIP("RValue references are not supported in current configuration");
#endif
}
QTEST_APPLESS_MAIN(tst_QArrayData)
#include "tst_qarraydata.moc"