mirror of
https://github.com/espressif/openthread.git
synced 2026-08-10 04:37:47 +00:00
[owned-ptr] add method PassOwnership() (#7684)
This commit is contained in:
@@ -144,6 +144,14 @@ public:
|
||||
return pointer;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method allows passing of the ownership to another `OwnedPtr` using move semantics.
|
||||
*
|
||||
* @returns An rvalue reference of the pointer to move from.
|
||||
*
|
||||
*/
|
||||
OwnedPtr &&PassOwnership(void) { return static_cast<OwnedPtr &&>(*this); }
|
||||
|
||||
/**
|
||||
* This method overload the assignment operator `=` to replace the object owned by the `OwnedPtr` with another one
|
||||
* using move semantics.
|
||||
|
||||
@@ -121,7 +121,7 @@ void TestOwnedPtr(void)
|
||||
|
||||
{
|
||||
OwnedPtr<TestObject> ptr1(&obj1);
|
||||
OwnedPtr<TestObject> ptr2(static_cast<OwnedPtr<TestObject> &&>(ptr1));
|
||||
OwnedPtr<TestObject> ptr2(ptr1.PassOwnership());
|
||||
|
||||
VerifyPointer(ptr1, nullptr);
|
||||
VerifyPointer(ptr2, &obj1);
|
||||
@@ -209,26 +209,26 @@ void TestOwnedPtr(void)
|
||||
VerifyPointer(ptr3, &obj3);
|
||||
|
||||
// Move from non-null (ptr1) to non-null (ptr2)
|
||||
ptr2 = static_cast<OwnedPtr<TestObject> &&>(ptr1);
|
||||
ptr2 = ptr1.PassOwnership();
|
||||
VerifyPointer(ptr1, nullptr);
|
||||
VerifyPointer(ptr2, &obj1);
|
||||
VerifyOrQuit(!obj1.WasFreed());
|
||||
VerifyOrQuit(obj2.WasFreed());
|
||||
|
||||
// Move from null (ptr1) to non-null (ptr3)
|
||||
ptr3 = static_cast<OwnedPtr<TestObject> &&>(ptr1);
|
||||
ptr3 = ptr1.PassOwnership();
|
||||
VerifyPointer(ptr1, nullptr);
|
||||
VerifyPointer(ptr3, nullptr);
|
||||
VerifyOrQuit(obj3.WasFreed());
|
||||
|
||||
// Move from non-null (ptr2) to null (ptr1)
|
||||
ptr1 = static_cast<OwnedPtr<TestObject> &&>(ptr2);
|
||||
ptr1 = ptr2.PassOwnership();
|
||||
VerifyPointer(ptr1, &obj1);
|
||||
VerifyPointer(ptr2, nullptr);
|
||||
VerifyOrQuit(!obj1.WasFreed());
|
||||
|
||||
// Move from null (ptr2) to null (ptr3)
|
||||
ptr3 = static_cast<OwnedPtr<TestObject> &&>(ptr2);
|
||||
ptr3 = ptr2.PassOwnership();
|
||||
VerifyPointer(ptr2, nullptr);
|
||||
VerifyPointer(ptr3, nullptr);
|
||||
VerifyOrQuit(!obj1.WasFreed());
|
||||
@@ -247,11 +247,11 @@ void TestOwnedPtr(void)
|
||||
VerifyPointer(ptr2, nullptr);
|
||||
|
||||
// Move from non-null (ptr1) to itself
|
||||
ptr1 = static_cast<OwnedPtr<TestObject> &&>(ptr1);
|
||||
ptr1 = ptr1.PassOwnership();
|
||||
VerifyPointer(ptr1, &obj1);
|
||||
|
||||
// Move from null (ptr2) to itself
|
||||
ptr2 = static_cast<OwnedPtr<TestObject> &&>(ptr2);
|
||||
ptr2 = ptr2.PassOwnership();
|
||||
VerifyPointer(ptr2, nullptr);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user