Fix constructing a QSharedPointer<const> of a QEnableSharedFromThis type

It should compile, since the std::shared_ptr does.

[ChangeLog][QtCore][QSharedPointer] Fixed a problem that would cause a
compilation error when constructing a QSharedPointer of a const type
when the type derives from QEnableSharedFromThis.

Task-number: QTBUG-49748
Change-Id: I8de47ed6c7be4847b99bffff141c84f5e0b6bea8
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
Thiago Macieira 2015-12-03 13:49:31 -08:00
parent 7499e642e0
commit 401507b348
2 changed files with 46 additions and 8 deletions

View File

@ -499,7 +499,7 @@ private:
template <class X>
inline void enableSharedFromThis(const QEnableSharedFromThis<X> *ptr)
{
ptr->initializeFromSharedPointer(*this);
ptr->initializeFromSharedPointer(constCast<typename QtPrivate::remove_cv<T>::type>());
}
inline void enableSharedFromThis(...) {}

View File

@ -94,18 +94,18 @@ private slots:
void creatingQObject();
void mixTrackingPointerCode();
void reentrancyWhileDestructing();
void threadStressTest_data();
void threadStressTest();
void map();
void hash();
void validConstructs();
void invalidConstructs_data();
void invalidConstructs();
void qvariantCast();
void sharedFromThis();
void threadStressTest_data();
void threadStressTest();
void validConstructs();
void invalidConstructs_data();
void invalidConstructs();
// let invalidConstructs be the last test, because it's the slowest;
// add new tests above this block
public slots:
void cleanup() { safetyCheck(); }
@ -231,6 +231,14 @@ void tst_QSharedPointer::basics()
QCOMPARE(sizeof(weakref), 2*sizeof(void*));
}
{
QSharedPointer<const Data> ptr;
QWeakPointer<const Data> weakref;
QCOMPARE(sizeof(ptr), 2*sizeof(void*));
QCOMPARE(sizeof(weakref), 2*sizeof(void*));
}
QFETCH(bool, isNull);
Data *aData = 0;
if (!isNull)
@ -2171,6 +2179,16 @@ void tst_QSharedPointer::sharedFromThis()
QVERIFY(const_scp.isNull());
QCOMPARE(Data::generationCounter, generations + 1);
QCOMPARE(Data::destructorCounter, destructions);
QWeakPointer<SomeClass> wcp = sc.sharedFromThis();
QVERIFY(wcp.isNull());
QCOMPARE(Data::generationCounter, generations + 1);
QCOMPARE(Data::destructorCounter, destructions);
QWeakPointer<const SomeClass> const_wcp = sc.sharedFromThis();
QVERIFY(const_wcp.isNull());
QCOMPARE(Data::generationCounter, generations + 1);
QCOMPARE(Data::destructorCounter, destructions);
}
QCOMPARE(Data::generationCounter, generations + 1);
@ -2182,6 +2200,11 @@ void tst_QSharedPointer::sharedFromThis()
QVERIFY(const_scp.isNull());
QCOMPARE(Data::generationCounter, generations + 2);
QCOMPARE(Data::destructorCounter, destructions + 1);
QWeakPointer<const SomeClass> const_wcp = sc.sharedFromThis();
QVERIFY(const_wcp.isNull());
QCOMPARE(Data::generationCounter, generations + 2);
QCOMPARE(Data::destructorCounter, destructions + 1);
}
QCOMPARE(Data::generationCounter, generations + 2);
@ -2373,6 +2396,21 @@ void tst_QSharedPointer::sharedFromThis()
QCOMPARE(Data::generationCounter, generations + 5);
QCOMPARE(Data::destructorCounter, destructions + 5);
{
QSharedPointer<const SomeClass> scp2(new SomeClass());
QVERIFY(!scp2.isNull());
QCOMPARE(Data::generationCounter, generations + 6);
QCOMPARE(Data::destructorCounter, destructions + 5);
QWeakPointer<const SomeClass> wcp2(scp2.constCast<SomeClass>());
QVERIFY(!wcp2.isNull());
QCOMPARE(Data::generationCounter, generations + 6);
QCOMPARE(Data::destructorCounter, destructions + 5);
}
QCOMPARE(Data::generationCounter, generations + 6);
QCOMPARE(Data::destructorCounter, destructions + 6);
}
namespace ReentrancyWhileDestructing {