From ed198ee6aef4fcc2283ff8d7bdf86f2b07fbeb76 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Fri, 10 Feb 2023 19:47:07 -0500 Subject: [PATCH] AK: Move adopt_nonnull_ref_or_enomem() to NonnullRefPtr.h Rewrite the implementation to not depend on OwnPtr.h. No intended behavior change. --- AK/NonnullRefPtr.h | 10 ++++++++++ AK/RefPtr.h | 10 ---------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/AK/NonnullRefPtr.h b/AK/NonnullRefPtr.h index 7f3a054dd1e..7be705bb3f7 100644 --- a/AK/NonnullRefPtr.h +++ b/AK/NonnullRefPtr.h @@ -226,6 +226,15 @@ inline NonnullRefPtr adopt_ref(T& object) return NonnullRefPtr(NonnullRefPtr::Adopt, object); } +// Use like `adopt_nonnull_own_or_enomem(new (nothrow) T(args...))`. +template +inline ErrorOr> adopt_nonnull_ref_or_enomem(T* object) +{ + if (!object) + return Error::from_errno(ENOMEM); + return NonnullRefPtr(NonnullRefPtr::Adopt, *object); +} + template struct Formatter> : Formatter { ErrorOr format(FormatBuilder& builder, NonnullRefPtr const& value) @@ -274,6 +283,7 @@ struct Traits> : public GenericTraits> { } #if USING_AK_GLOBALLY +using AK::adopt_nonnull_ref_or_enomem; using AK::adopt_ref; using AK::make_ref_counted; using AK::NonnullRefPtr; diff --git a/AK/RefPtr.h b/AK/RefPtr.h index f99ac2c9fae..528cf7c38c8 100644 --- a/AK/RefPtr.h +++ b/AK/RefPtr.h @@ -341,19 +341,9 @@ inline ErrorOr> try_make_ref_counted(Args&&... args) return adopt_nonnull_ref_or_enomem(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 Error::from_errno(ENOMEM); - return result.release_nonnull(); -} - } #if USING_AK_GLOBALLY -using AK::adopt_nonnull_ref_or_enomem; using AK::adopt_ref_if_nonnull; using AK::RefPtr; using AK::static_ptr_cast;