mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 21:27:35 +00:00
AK: Add adopt_nonnull_ref_or_enomem() for userspace
We already had this mechanism in the kernel. Let's have it in userspace as well. This return an ErrorOr<NonnullRefPt<T>>. :^)
This commit is contained in:
parent
e2eabb4132
commit
56992f90b7
1 changed files with 10 additions and 0 deletions
10
AK/RefPtr.h
10
AK/RefPtr.h
|
@ -14,6 +14,7 @@
|
|||
|
||||
# include <AK/Assertions.h>
|
||||
# include <AK/Atomic.h>
|
||||
# include <AK/Error.h>
|
||||
# include <AK/Format.h>
|
||||
# include <AK/NonnullRefPtr.h>
|
||||
# include <AK/StdLibExtras.h>
|
||||
|
@ -347,6 +348,15 @@ inline RefPtr<T> try_make_ref_counted(Args&&... args)
|
|||
return adopt_ref_if_nonnull(new (nothrow) T { forward<Args>(args)... });
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline ErrorOr<NonnullRefPtr<T>> adopt_nonnull_ref_or_enomem(T* object)
|
||||
{
|
||||
auto result = adopt_ref_if_nonnull(object);
|
||||
if (!result)
|
||||
return ENOMEM;
|
||||
return result.release_nonnull();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
using AK::adopt_ref_if_nonnull;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue