mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-04 13:30:31 +00:00
OwnPtr: Add a way to turn an OwnPtr into a NonnullOwnPtr
Okay, so, OwnPtr<T>::release_nonnull() returns a NonnullOwnPtr<T>. It assumes that the OwnPtr is non-null to begin with. Note that this removes the value from the OwnPtr, as there can only be a single owner.
This commit is contained in:
parent
99ed4ce30c
commit
f75b1127b2
Notes:
sideshowbarker
2024-07-19 12:41:38 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/f75b1127b2f
2 changed files with 23 additions and 0 deletions
|
@ -120,6 +120,12 @@ public:
|
|||
return leaked_ptr;
|
||||
}
|
||||
|
||||
NonnullOwnPtr<T> release_nonnull()
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return NonnullOwnPtr<T>(NonnullOwnPtr<T>::Adopt, *leak_ptr());
|
||||
}
|
||||
|
||||
T* ptr() { return m_ptr; }
|
||||
const T* ptr() const { return m_ptr; }
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <AK/TestSuite.h>
|
||||
|
||||
#include <AK/AKString.h>
|
||||
#include <AK/NonnullOwnPtrVector.h>
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <AK/Vector.h>
|
||||
|
||||
|
@ -252,4 +253,20 @@ TEST_CASE(vector_remove)
|
|||
EXPECT_EQ(ints[0], 4);
|
||||
}
|
||||
|
||||
TEST_CASE(nonnullownptrvector)
|
||||
{
|
||||
struct Object {
|
||||
String string;
|
||||
};
|
||||
NonnullOwnPtrVector<Object> objects;
|
||||
|
||||
objects.append(make<Object>());
|
||||
EXPECT_EQ(objects.size(), 1);
|
||||
|
||||
OwnPtr<Object> o = make<Object>();
|
||||
objects.append(o.release_nonnull());
|
||||
EXPECT(o == nullptr);
|
||||
EXPECT_EQ(objects.size(), 2);
|
||||
}
|
||||
|
||||
TEST_MAIN(Vector)
|
||||
|
|
Loading…
Reference in a new issue