From 56992f90b7908d6665a9581e28452462ba563d2f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 6 Nov 2021 10:14:14 +0100 Subject: [PATCH] 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>. :^) --- AK/RefPtr.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/AK/RefPtr.h b/AK/RefPtr.h index 2df91099870..7c56b287e8f 100644 --- a/AK/RefPtr.h +++ b/AK/RefPtr.h @@ -14,6 +14,7 @@ # include # include +# include # include # include # include @@ -347,6 +348,15 @@ inline RefPtr try_make_ref_counted(Args&&... args) return adopt_ref_if_nonnull(new (nothrow) T { forward(args)... }); } +template +inline ErrorOr> 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;