[common] add Ptr to use as base of OwnedPtr and RetainPtr (#7195)

This commit adds a new class `Ptr<Type>` which represents a wrapper
over a raw pointer. This is then used as the base class of `OwnedPtr`
and `RetainPtr` providing the common simple methods (avoiding repeating
the same code in each smart pointter). The new `Ptr` class also adds
overloads of `==` and `!=` allowing `Ptr` to be compared with a given
raw pointer or another `Ptr` instance.
This commit is contained in:
Abtin Keshavarzian
2021-11-24 21:14:49 -08:00
committed by Jonathan Hui
parent 2d16fa25e4
commit 4acdf1d0fb
6 changed files with 218 additions and 147 deletions
+13
View File
@@ -67,11 +67,14 @@ void VerifyPointer(const PointerType &aPointer,
{
VerifyOrQuit(aPointer.IsNull());
VerifyOrQuit(aPointer.Get() == nullptr);
VerifyOrQuit(aPointer == nullptr);
}
else
{
VerifyOrQuit(!aPointer.IsNull());
VerifyOrQuit(aPointer.Get() == aObject);
VerifyOrQuit(aPointer == aObject);
VerifyOrQuit(aPointer != nullptr);
VerifyOrQuit(!aPointer->WasFreed());
VerifyOrQuit(!(*aPointer).WasFreed());
@@ -81,6 +84,8 @@ void VerifyPointer(const PointerType &aPointer,
VerifyOrQuit(aObject->GetRetainCount() == aRetainCount);
}
}
VerifyOrQuit(aPointer == aPointer);
}
void TestOwnedPtr(void)
@@ -388,27 +393,35 @@ void TestRetainPtr(void)
VerifyPointer(ptr2, &obj2, 1);
VerifyPointer(ptr3, nullptr);
VerifyOrQuit(ptr1 != ptr2);
VerifyOrQuit(ptr1 != ptr3);
VerifyOrQuit(ptr2 != ptr3);
// Set from non-null (ptr1) to non-null (ptr2)
ptr2 = ptr1;
VerifyPointer(ptr1, &obj1, 2);
VerifyPointer(ptr2, &obj1, 2);
VerifyOrQuit(obj2.WasFreed());
VerifyOrQuit(ptr1 == ptr2);
// Set from null (ptr3) to non-null (ptr1)
ptr1 = ptr3;
VerifyPointer(ptr1, nullptr);
VerifyPointer(ptr3, nullptr);
VerifyPointer(ptr2, &obj1, 1);
VerifyOrQuit(ptr1 == ptr3);
// Move from null (ptr1) to null (ptr3)
ptr3 = ptr1;
VerifyPointer(ptr1, nullptr);
VerifyPointer(ptr3, nullptr);
VerifyOrQuit(ptr1 == ptr3);
// Move from non-null (ptr2) to null (ptr3)
ptr3 = ptr2;
VerifyPointer(ptr2, &obj1, 2);
VerifyPointer(ptr3, &obj1, 2);
VerifyOrQuit(ptr2 == ptr3);
}
VerifyOrQuit(obj1.WasFreed());