diff options
author | Nico Weber <thakis@chromium.org> | 2023-02-10 19:40:35 -0500 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-02-11 10:36:48 +0100 |
commit | ca6090889a26c0de00905c45a4359ad17e759e0c (patch) | |
tree | f790664adfa62815677b95dd8be3a58ea0dd1400 /AK | |
parent | cf73e15dc158eab18cae59f2779510a8b750755f (diff) | |
download | serenity-ca6090889a26c0de00905c45a4359ad17e759e0c.zip |
AK: Move adopt_nonnull_own_or_enomem() to NonnullOwnPtr.h
Rewrite the implementation to not depend on OwnPtr.h.
No intended behavior change.
Diffstat (limited to 'AK')
-rw-r--r-- | AK/NonnullOwnPtr.h | 10 | ||||
-rw-r--r-- | AK/OwnPtr.h | 10 |
2 files changed, 10 insertions, 10 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 diff --git a/AK/OwnPtr.h b/AK/OwnPtr.h index c5452b8661..3f8a61095b 100644 --- a/AK/OwnPtr.h +++ b/AK/OwnPtr.h @@ -192,15 +192,6 @@ inline OwnPtr<T> adopt_own_if_nonnull(T* object) return {}; } -template<typename T> -inline ErrorOr<NonnullOwnPtr<T>> 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<typename T, class... Args> requires(IsConstructible<T, Args...>) inline ErrorOr<NonnullOwnPtr<T>> try_make(Args&&... args) { @@ -226,7 +217,6 @@ struct Traits<OwnPtr<T>> : public GenericTraits<OwnPtr<T>> { } #if USING_AK_GLOBALLY -using AK::adopt_nonnull_own_or_enomem; using AK::adopt_own_if_nonnull; using AK::OwnPtr; using AK::try_make; |