本文整理汇总了C++中DataPtr::reset方法的典型用法代码示例。如果您正苦于以下问题:C++ DataPtr::reset方法的具体用法?C++ DataPtr::reset怎么用?C++ DataPtr::reset使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataPtr
的用法示例。
在下文中一共展示了DataPtr::reset方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: testThreadSafety
void TestSharedPtr::testThreadSafety()
{
DataPtr ptr = Data::create();
WeakPtr<Data> weakPtr(ptr);
Data *savedData = ptr.data();
QVERIFY(savedData != NULL);
QVERIFY(!ptr.isNull());
QVERIFY(!weakPtr.isNull());
Thread *t[5];
for (int i = 0; i < 5; ++i) {
t[i] = new Thread(ptr, this);
t[i]->start();
}
for (int i = 0; i < 5; ++i) {
t[i]->wait();
delete t[i];
}
QCOMPARE(ptr.data(), savedData);
QVERIFY(!ptr.isNull());
QVERIFY(!weakPtr.isNull());
for (int i = 0; i < 5; ++i) {
t[i] = new Thread(ptr, this);
t[i]->start();
}
QVERIFY(!ptr.isNull());
QVERIFY(!weakPtr.isNull());
ptr.reset();
QVERIFY(ptr.isNull());
for (int i = 0; i < 5; ++i) {
t[i]->wait();
delete t[i];
}
QVERIFY(!ptr.data());
QVERIFY(ptr.isNull());
QVERIFY(weakPtr.isNull());
DataPtr promotedPtr(weakPtr);
QVERIFY(!promotedPtr.data());
QVERIFY(promotedPtr.isNull());
}
示例2: testWeakPtrBoolConversion
void TestSharedPtr::testWeakPtrBoolConversion()
{
WeakPtr<Data> nullPtr1;
DataPtr strongNullPtr2 = Data::createNull();
WeakPtr<Data> nullPtr2 = strongNullPtr2;
DataPtr strongValidPtr1 = Data::create();
WeakPtr<Data> validPtr1 = strongValidPtr1;
WeakPtr<Data> validPtr2 = validPtr1;
DataPtr strongValidPtrAlternative = Data::create();
WeakPtr<Data> validPtrAlternative = strongValidPtrAlternative;
// Boolean conditions
QVERIFY(!validPtr1.isNull());
QVERIFY(nullPtr1.isNull());
QVERIFY(validPtr1 ? true : false);
QVERIFY(!validPtr1 ? false : true);
QVERIFY(nullPtr1 ? false : true);
QVERIFY(!nullPtr1 ? true : false);
QVERIFY(validPtr1);
QVERIFY(!!validPtr1);
QVERIFY(!nullPtr1);
// Supported operators
QVERIFY(nullPtr1 == nullPtr1);
QVERIFY(nullPtr1 == nullPtr2);
QVERIFY(validPtr1 == validPtr1);
QVERIFY(validPtr1 == validPtr2);
// XXX why not comparison operator?
//QVERIFY(validPtr1 != validPtrAlternative);
//QCOMPARE(validPtr1 == validPtrAlternative, false);
QVERIFY(validPtr1 != nullPtr1);
QCOMPARE(validPtr1 == nullPtr1, false);
// Supported conversions, constructors and copy operators
bool trueBool1 = validPtr1;
QVERIFY(trueBool1);
bool trueBool2(validPtr2);
QVERIFY(trueBool2);
trueBool1 = validPtrAlternative;
QVERIFY(trueBool1);
bool falseBool1 = nullPtr1;
QVERIFY(!falseBool1);
bool falseBool2(nullPtr2);
QVERIFY(!falseBool2);
falseBool1 = nullPtr1;
QVERIFY(!falseBool1);
#if 0
// Unsupported operators, this should not compile
bool condition;
condition = validPtr1 > nullPtr1;
condition = validPtr1 + nullPtr1;
// Unsupported conversions, this should not compile
int validInt1 = validPtr1;
int validInt2(validPtr1);
validInt1 = validPtr1;
int nullInt1 = nullPtr1;
int nullInt2(nullPtr1);
nullInt1 = nullPtr1;
float validFloat1 = validPtr1;
float validFloat2(validPtr1);
validFloat1 = validPtr1;
float nullFloat1 = nullPtr1;
float nullFloat2(nullPtr1);
nullFloat1 = nullPtr1;
Q_UNUSED(validInt1);
Q_UNUSED(validInt2);
Q_UNUSED(nullInt1);
Q_UNUSED(nullInt2);
Q_UNUSED(validFloat1);
Q_UNUSED(validFloat2);
Q_UNUSED(nullFloat1);
Q_UNUSED(nullFloat2);
#endif
// Test the boolean operations after the main SharedPtr is gone
strongValidPtrAlternative.reset();
QVERIFY(validPtrAlternative.isNull());
QVERIFY(validPtrAlternative ? false : true);
QVERIFY(!validPtrAlternative ? true : false);
}