QSharedPointer: add get() for STL compatibility

[ChangeLog][QtCore][QSharedPointer] Added get(), for compatibility
with std::shared_ptr.

Change-Id: I77189c89bc6a222313b9b2353f1fb3969433c750
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Giuseppe D'Angelo 2017-09-12 11:36:04 +01:00
parent 764854dc47
commit 71a2dd955c
4 changed files with 25 additions and 0 deletions

View File

@ -576,6 +576,15 @@
QSharedPointer or QWeakPointer objects. QSharedPointer or QWeakPointer objects.
*/ */
/*!
\fn T *QSharedPointer::get() const
\since 5.11
Same as data().
This function is provided for API compatibility with \c{std::shared_ptr}.
*/
/*! /*!
\fn T &QSharedPointer::operator *() const \fn T &QSharedPointer::operator *() const

View File

@ -59,6 +59,7 @@ class QSharedPointer
public: public:
// basic accessor functions // basic accessor functions
T *data() const; T *data() const;
T *get() const;
bool isNull() const; bool isNull() const;
operator bool() const; operator bool() const;
bool operator!() const; bool operator!() const;

View File

@ -303,6 +303,7 @@ public:
typedef qptrdiff difference_type; typedef qptrdiff difference_type;
T *data() const Q_DECL_NOTHROW { return value; } T *data() const Q_DECL_NOTHROW { return value; }
T *get() const Q_DECL_NOTHROW { return value; }
bool isNull() const Q_DECL_NOTHROW { return !data(); } bool isNull() const Q_DECL_NOTHROW { return !data(); }
operator RestrictedBool() const Q_DECL_NOTHROW { return isNull() ? Q_NULLPTR : &QSharedPointer::value; } operator RestrictedBool() const Q_DECL_NOTHROW { return isNull() ? Q_NULLPTR : &QSharedPointer::value; }
bool operator !() const Q_DECL_NOTHROW { return isNull(); } bool operator !() const Q_DECL_NOTHROW { return isNull(); }

View File

@ -272,6 +272,7 @@ void tst_QSharedPointer::basics()
QCOMPARE(!ptr, isNull); QCOMPARE(!ptr, isNull);
QCOMPARE(ptr.data(), aData); QCOMPARE(ptr.data(), aData);
QCOMPARE(ptr.get(), aData);
QCOMPARE(ptr.operator->(), aData); QCOMPARE(ptr.operator->(), aData);
if (!isNull) { if (!isNull) {
Data &dataReference = *ptr; Data &dataReference = *ptr;
@ -316,6 +317,7 @@ void tst_QSharedPointer::basics()
QCOMPARE(copy.isNull(), isNull); QCOMPARE(copy.isNull(), isNull);
QCOMPARE(copy.data(), aData); QCOMPARE(copy.data(), aData);
QCOMPARE(copy.get(), aData);
QVERIFY(copy == aData); QVERIFY(copy == aData);
} }
QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref.load() == 1); QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref.load() == 1);
@ -349,6 +351,7 @@ void tst_QSharedPointer::basics()
QVERIFY(strong == weak); QVERIFY(strong == weak);
QVERIFY(strong == ptr); QVERIFY(strong == ptr);
QCOMPARE(strong.data(), aData); QCOMPARE(strong.data(), aData);
QCOMPARE(strong.get(), aData);
} }
QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref.load() == 1); QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref.load() == 1);
QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref.load() == 1); QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref.load() == 1);
@ -362,11 +365,14 @@ void tst_QSharedPointer::operators()
QSharedPointer<char> p2(new char); QSharedPointer<char> p2(new char);
qptrdiff diff = p2.data() - p1.data(); qptrdiff diff = p2.data() - p1.data();
QVERIFY(p1.data() != p2.data()); QVERIFY(p1.data() != p2.data());
QVERIFY(p1.get() != p2.get());
QVERIFY(diff != 0); QVERIFY(diff != 0);
// operator- // operator-
QCOMPARE(p2 - p1.data(), diff); QCOMPARE(p2 - p1.data(), diff);
QCOMPARE(p2 - p1.get(), diff);
QCOMPARE(p2.data() - p1, diff); QCOMPARE(p2.data() - p1, diff);
QCOMPARE(p2.get() - p1, diff);
QCOMPARE(p2 - p1, diff); QCOMPARE(p2 - p1, diff);
QCOMPARE(p1 - p2, -diff); QCOMPARE(p1 - p2, -diff);
QCOMPARE(p1 - p1, qptrdiff(0)); QCOMPARE(p1 - p1, qptrdiff(0));
@ -374,7 +380,9 @@ void tst_QSharedPointer::operators()
// operator< // operator<
QVERIFY(p1 < p2.data()); QVERIFY(p1 < p2.data());
QVERIFY(p1 < p2.get());
QVERIFY(p1.data() < p2); QVERIFY(p1.data() < p2);
QVERIFY(p1.get() < p2);
QVERIFY(p1 < p2); QVERIFY(p1 < p2);
QVERIFY(!(p2 < p1)); QVERIFY(!(p2 < p1));
QVERIFY(!(p2 < p2)); QVERIFY(!(p2 < p2));
@ -382,7 +390,9 @@ void tst_QSharedPointer::operators()
// qHash // qHash
QCOMPARE(qHash(p1), qHash(p1.data())); QCOMPARE(qHash(p1), qHash(p1.data()));
QCOMPARE(qHash(p1), qHash(p1.get()));
QCOMPARE(qHash(p2), qHash(p2.data())); QCOMPARE(qHash(p2), qHash(p2.data()));
QCOMPARE(qHash(p2), qHash(p2.get()));
} }
void tst_QSharedPointer::nullptrOps() void tst_QSharedPointer::nullptrOps()
@ -396,11 +406,13 @@ void tst_QSharedPointer::nullptrOps()
QVERIFY(nullptr == p1); QVERIFY(nullptr == p1);
QVERIFY(!p1); QVERIFY(!p1);
QVERIFY(!p1.data()); QVERIFY(!p1.data());
QVERIFY(!p1.get());
QVERIFY(p2 == null); QVERIFY(p2 == null);
QVERIFY(p2 == nullptr); QVERIFY(p2 == nullptr);
QVERIFY(nullptr == p2); QVERIFY(nullptr == p2);
QVERIFY(!p2); QVERIFY(!p2);
QVERIFY(!p2.data()); QVERIFY(!p2.data());
QVERIFY(!p2.get());
QVERIFY(p1 == p2); QVERIFY(p1 == p2);
QSharedPointer<char> p3 = p1; QSharedPointer<char> p3 = p1;
@ -409,6 +421,7 @@ void tst_QSharedPointer::nullptrOps()
QVERIFY(p3 == nullptr); QVERIFY(p3 == nullptr);
QVERIFY(nullptr == p3); QVERIFY(nullptr == p3);
QVERIFY(!p3.data()); QVERIFY(!p3.data());
QVERIFY(!p3.get());
p3 = nullptr; p3 = nullptr;
@ -421,6 +434,7 @@ void tst_QSharedPointer::nullptrOps()
QSharedPointer<char> p4(new char); QSharedPointer<char> p4(new char);
QVERIFY(p4); QVERIFY(p4);
QVERIFY(p4.data()); QVERIFY(p4.data());
QVERIFY(p4.get());
QVERIFY(p4 != nullptr); QVERIFY(p4 != nullptr);
QVERIFY(nullptr != p4); QVERIFY(nullptr != p4);
QVERIFY(p4 != p1); QVERIFY(p4 != p1);