mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +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
Notes:
sideshowbarker
2024-07-18 01:25:11 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/56992f90b79
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/Assertions.h>
|
||||||
# include <AK/Atomic.h>
|
# include <AK/Atomic.h>
|
||||||
|
# include <AK/Error.h>
|
||||||
# include <AK/Format.h>
|
# include <AK/Format.h>
|
||||||
# include <AK/NonnullRefPtr.h>
|
# include <AK/NonnullRefPtr.h>
|
||||||
# include <AK/StdLibExtras.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)... });
|
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;
|
using AK::adopt_ref_if_nonnull;
|
||||||
|
|
Loading…
Reference in a new issue