From ca6090889a26c0de00905c45a4359ad17e759e0c Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Fri, 10 Feb 2023 19:40:35 -0500 Subject: [PATCH] AK: Move adopt_nonnull_own_or_enomem() to NonnullOwnPtr.h Rewrite the implementation to not depend on OwnPtr.h. No intended behavior change. --- AK/NonnullOwnPtr.h | 10 ++++++++++ AK/OwnPtr.h | 10 ---------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/AK/NonnullOwnPtr.h b/AK/NonnullOwnPtr.h index 1d693d29758..2c207e05ae7 100644 --- a/AK/NonnullOwnPtr.h +++ b/AK/NonnullOwnPtr.h @@ -161,6 +161,15 @@ inline NonnullOwnPtr make(Args&&... args) #endif +// Use like `adopt_nonnull_own_or_enomem(new (nothrow) T(args...))`. +template +inline ErrorOr> adopt_nonnull_own_or_enomem(T* object) +{ + if (!object) + return Error::from_errno(ENOMEM); + return NonnullOwnPtr(NonnullOwnPtr::Adopt, *object); +} + template struct Traits> : public GenericTraits> { using PeekType = T*; @@ -190,5 +199,6 @@ struct Formatter> : Formatter { using AK::adopt_own; using AK::make; # endif +using AK::adopt_nonnull_own_or_enomem; using AK::NonnullOwnPtr; #endif diff --git a/AK/OwnPtr.h b/AK/OwnPtr.h index c5452b8661b..3f8a61095b7 100644 --- a/AK/OwnPtr.h +++ b/AK/OwnPtr.h @@ -192,15 +192,6 @@ inline OwnPtr adopt_own_if_nonnull(T* object) return {}; } -template -inline ErrorOr> adopt_nonnull_own_or_enomem(T* object) -{ - auto result = adopt_own_if_nonnull(object); - if (!result) - return Error::from_errno(ENOMEM); - return result.release_nonnull(); -} - template requires(IsConstructible) inline ErrorOr> try_make(Args&&... args) { @@ -226,7 +217,6 @@ struct Traits> : public GenericTraits> { } #if USING_AK_GLOBALLY -using AK::adopt_nonnull_own_or_enomem; using AK::adopt_own_if_nonnull; using AK::OwnPtr; using AK::try_make;