From 9eb370bd519a8648905f8b3b869fac8e8be3ada5 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 3 Aug 2009 14:10:00 +0200 Subject: Remove the need for internal API in QSharedPointer. That way, this code can be compiled with an earlier version of Qt and should still work in new ones. Reviewed-by: Trust Me --- src/corelib/tools/qsharedpointer_impl.h | 4 +- tests/auto/qsharedpointer/qsharedpointer.pro | 1 - tests/auto/qsharedpointer/tst_qsharedpointer.cpp | 95 ++++++++++++++---------- 3 files changed, 56 insertions(+), 44 deletions(-) diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h index b8f4139..089c479 100644 --- a/src/corelib/tools/qsharedpointer_impl.h +++ b/src/corelib/tools/qsharedpointer_impl.h @@ -350,9 +350,7 @@ namespace QtSharedPointer { this->value = d && d->strongref ? actual : 0; } -#if defined(QT_BUILD_INTERNAL) - public: -#endif + protected: Data *d; private: diff --git a/tests/auto/qsharedpointer/qsharedpointer.pro b/tests/auto/qsharedpointer/qsharedpointer.pro index 90fde06..30c81cb 100644 --- a/tests/auto/qsharedpointer/qsharedpointer.pro +++ b/tests/auto/qsharedpointer/qsharedpointer.pro @@ -4,6 +4,5 @@ SOURCES += tst_qsharedpointer.cpp \ forwarddeclared.cpp QT = core DEFINES += SRCDIR=\\\"$$PWD/\\\" -requires(contains(QT_CONFIG,private_tests)) include(externaltests.pri) HEADERS += forwarddeclared.h diff --git a/tests/auto/qsharedpointer/tst_qsharedpointer.cpp b/tests/auto/qsharedpointer/tst_qsharedpointer.cpp index 74f4507..1c2c60f 100644 --- a/tests/auto/qsharedpointer/tst_qsharedpointer.cpp +++ b/tests/auto/qsharedpointer/tst_qsharedpointer.cpp @@ -80,8 +80,23 @@ public slots: void cleanup() { check(); } public: - inline void check() { QtSharedPointer::internalSafetyCheckCleanCheck(); } + inline void check() + { +#ifdef QT_BUILD_INTERNAL + QtSharedPointer::internalSafetyCheckCleanCheck(); +#endif + } +}; + +template +class RefCountHack: public Base +{ +public: + using Base::d; }; +template static inline +QtSharedPointer::ExternalRefCountData *refCountData(const Base &b) +{ return static_cast *>(&b)->d; } class Data { @@ -166,8 +181,8 @@ void tst_QSharedPointer::basics() QVERIFY(! (ptr == otherData)); QVERIFY(! (otherData == ptr)); } - QVERIFY(!ptr.d || ptr.d->weakref == 1); - QVERIFY(!ptr.d || ptr.d->strongref == 1); + QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref == 1); + QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref == 1); { // create another object: @@ -179,8 +194,8 @@ void tst_QSharedPointer::basics() // otherData is deleted here } - QVERIFY(!ptr.d || ptr.d->weakref == 1); - QVERIFY(!ptr.d || ptr.d->strongref == 1); + QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref == 1); + QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref == 1); { // create a copy: @@ -196,8 +211,8 @@ void tst_QSharedPointer::basics() QCOMPARE(copy.data(), aData); QVERIFY(copy == aData); } - QVERIFY(!ptr.d || ptr.d->weakref == 1); - QVERIFY(!ptr.d || ptr.d->strongref == 1); + QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref == 1); + QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref == 1); { // create a weak reference: @@ -228,8 +243,8 @@ void tst_QSharedPointer::basics() QVERIFY(strong == ptr); QCOMPARE(strong.data(), aData); } - QVERIFY(!ptr.d || ptr.d->weakref == 1); - QVERIFY(!ptr.d || ptr.d->strongref == 1); + QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref == 1); + QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref == 1); // aData is deleted here } @@ -445,15 +460,15 @@ void tst_QSharedPointer::upCast() QVERIFY(baseptr == derivedptr); QCOMPARE(static_cast(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(baseptr.d->weakref), 1); - QCOMPARE(int(baseptr.d->strongref), 1); + QCOMPARE(int(refCountData(baseptr)->weakref), 1); + QCOMPARE(int(refCountData(baseptr)->strongref), 1); { QWeakPointer derivedptr = qWeakPointerCast(baseptr); QVERIFY(baseptr == derivedptr); } - QCOMPARE(int(baseptr.d->weakref), 1); - QCOMPARE(int(baseptr.d->strongref), 1); + QCOMPARE(int(refCountData(baseptr)->weakref), 1); + QCOMPARE(int(refCountData(baseptr)->strongref), 1); { QWeakPointer weakptr = baseptr; @@ -461,16 +476,16 @@ void tst_QSharedPointer::upCast() QVERIFY(baseptr == derivedptr); QCOMPARE(static_cast(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(baseptr.d->weakref), 1); - QCOMPARE(int(baseptr.d->strongref), 1); + QCOMPARE(int(refCountData(baseptr)->weakref), 1); + QCOMPARE(int(refCountData(baseptr)->strongref), 1); { QSharedPointer derivedptr = baseptr.staticCast(); QVERIFY(baseptr == derivedptr); QCOMPARE(static_cast(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(baseptr.d->weakref), 1); - QCOMPARE(int(baseptr.d->strongref), 1); + QCOMPARE(int(refCountData(baseptr)->weakref), 1); + QCOMPARE(int(refCountData(baseptr)->strongref), 1); } class OtherObject: public QObject @@ -680,8 +695,8 @@ void tst_QSharedPointer::dynamicCast() QCOMPARE(derivedptr.data(), aData); QCOMPARE(static_cast(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(baseptr.d->weakref), 1); - QCOMPARE(int(baseptr.d->strongref), 1); + QCOMPARE(int(refCountData(baseptr)->weakref), 1); + QCOMPARE(int(refCountData(baseptr)->strongref), 1); { QWeakPointer weakptr = baseptr; @@ -690,8 +705,8 @@ void tst_QSharedPointer::dynamicCast() QCOMPARE(derivedptr.data(), aData); QCOMPARE(static_cast(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(baseptr.d->weakref), 1); - QCOMPARE(int(baseptr.d->strongref), 1); + QCOMPARE(int(refCountData(baseptr)->weakref), 1); + QCOMPARE(int(refCountData(baseptr)->strongref), 1); { QSharedPointer derivedptr = baseptr.dynamicCast(); @@ -699,8 +714,8 @@ void tst_QSharedPointer::dynamicCast() QCOMPARE(derivedptr.data(), aData); QCOMPARE(static_cast(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(baseptr.d->weakref), 1); - QCOMPARE(int(baseptr.d->strongref), 1); + QCOMPARE(int(refCountData(baseptr)->weakref), 1); + QCOMPARE(int(refCountData(baseptr)->strongref), 1); } void tst_QSharedPointer::dynamicCastDifferentPointers() @@ -715,8 +730,8 @@ void tst_QSharedPointer::dynamicCastDifferentPointers() QCOMPARE(derivedptr.data(), aData); QCOMPARE(static_cast(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(baseptr.d->weakref), 1); - QCOMPARE(int(baseptr.d->strongref), 1); + QCOMPARE(int(refCountData(baseptr)->weakref), 1); + QCOMPARE(int(refCountData(baseptr)->strongref), 1); { QWeakPointer weakptr = baseptr; @@ -725,8 +740,8 @@ void tst_QSharedPointer::dynamicCastDifferentPointers() QCOMPARE(derivedptr.data(), aData); QCOMPARE(static_cast(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(baseptr.d->weakref), 1); - QCOMPARE(int(baseptr.d->strongref), 1); + QCOMPARE(int(refCountData(baseptr)->weakref), 1); + QCOMPARE(int(refCountData(baseptr)->strongref), 1); { QSharedPointer derivedptr = baseptr.dynamicCast(); @@ -734,8 +749,8 @@ void tst_QSharedPointer::dynamicCastDifferentPointers() QCOMPARE(derivedptr.data(), aData); QCOMPARE(static_cast(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(baseptr.d->weakref), 1); - QCOMPARE(int(baseptr.d->strongref), 1); + QCOMPARE(int(refCountData(baseptr)->weakref), 1); + QCOMPARE(int(refCountData(baseptr)->strongref), 1); { Stuffing *nakedptr = dynamic_cast(baseptr.data()); @@ -760,8 +775,8 @@ void tst_QSharedPointer::dynamicCastVirtualBase() QCOMPARE(derivedptr.data(), aData); QCOMPARE(static_cast(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(baseptr.d->weakref), 1); - QCOMPARE(int(baseptr.d->strongref), 1); + QCOMPARE(int(refCountData(baseptr)->weakref), 1); + QCOMPARE(int(refCountData(baseptr)->strongref), 1); { QWeakPointer weakptr = baseptr; @@ -770,8 +785,8 @@ void tst_QSharedPointer::dynamicCastVirtualBase() QCOMPARE(derivedptr.data(), aData); QCOMPARE(static_cast(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(baseptr.d->weakref), 1); - QCOMPARE(int(baseptr.d->strongref), 1); + QCOMPARE(int(refCountData(baseptr)->weakref), 1); + QCOMPARE(int(refCountData(baseptr)->strongref), 1); { QSharedPointer derivedptr = baseptr.dynamicCast(); @@ -779,8 +794,8 @@ void tst_QSharedPointer::dynamicCastVirtualBase() QCOMPARE(derivedptr.data(), aData); QCOMPARE(static_cast(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(baseptr.d->weakref), 1); - QCOMPARE(int(baseptr.d->strongref), 1); + QCOMPARE(int(refCountData(baseptr)->weakref), 1); + QCOMPARE(int(refCountData(baseptr)->strongref), 1); } void tst_QSharedPointer::dynamicCastFailure() @@ -792,15 +807,15 @@ void tst_QSharedPointer::dynamicCastFailure() QSharedPointer derivedptr = qSharedPointerDynamicCast(baseptr); QVERIFY(derivedptr.isNull()); } - QCOMPARE(int(baseptr.d->weakref), 1); - QCOMPARE(int(baseptr.d->strongref), 1); + QCOMPARE(int(refCountData(baseptr)->weakref), 1); + QCOMPARE(int(refCountData(baseptr)->strongref), 1); { QSharedPointer derivedptr = baseptr.dynamicCast(); QVERIFY(derivedptr.isNull()); } - QCOMPARE(int(baseptr.d->weakref), 1); - QCOMPARE(int(baseptr.d->strongref), 1); + QCOMPARE(int(refCountData(baseptr)->weakref), 1); + QCOMPARE(int(refCountData(baseptr)->strongref), 1); } #endif @@ -1066,7 +1081,7 @@ void tst_QSharedPointer::creating() { QSharedPointer ptr = QSharedPointer::create(); QWeakPointer weakptr = ptr; - QtSharedPointer::ExternalRefCountData *d = ptr.d; + QtSharedPointer::ExternalRefCountData *d = refCountData(ptr); ptr.clear(); QVERIFY(ptr.isNull()); -- cgit v0.12