summaryrefslogtreecommitdiff
path: root/AK/NonnullOwnPtr.h
diff options
context:
space:
mode:
Diffstat (limited to 'AK/NonnullOwnPtr.h')
-rw-r--r--AK/NonnullOwnPtr.h10
1 files changed, 10 insertions, 0 deletions
diff --git a/AK/NonnullOwnPtr.h b/AK/NonnullOwnPtr.h
index 1d693d2975..2c207e05ae 100644
--- a/AK/NonnullOwnPtr.h
+++ b/AK/NonnullOwnPtr.h
@@ -161,6 +161,15 @@ inline NonnullOwnPtr<T> make(Args&&... args)
#endif
+// Use like `adopt_nonnull_own_or_enomem(new (nothrow) T(args...))`.
+template<typename T>
+inline ErrorOr<NonnullOwnPtr<T>> adopt_nonnull_own_or_enomem(T* object)
+{
+ if (!object)
+ return Error::from_errno(ENOMEM);
+ return NonnullOwnPtr<T>(NonnullOwnPtr<T>::Adopt, *object);
+}
+
template<typename T>
struct Traits<NonnullOwnPtr<T>> : public GenericTraits<NonnullOwnPtr<T>> {
using PeekType = T*;
@@ -190,5 +199,6 @@ struct Formatter<NonnullOwnPtr<T>> : Formatter<T const*> {
using AK::adopt_own;
using AK::make;
# endif
+using AK::adopt_nonnull_own_or_enomem;
using AK::NonnullOwnPtr;
#endif