summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNico Weber <thakis@chromium.org>2023-02-10 19:47:07 -0500
committerAndreas Kling <kling@serenityos.org>2023-02-11 10:36:48 +0100
commited198ee6aef4fcc2283ff8d7bdf86f2b07fbeb76 (patch)
tree8370f79dc72e7a45bb60ced27716f62dd5ce9211
parentca6090889a26c0de00905c45a4359ad17e759e0c (diff)
downloadserenity-ed198ee6aef4fcc2283ff8d7bdf86f2b07fbeb76.zip
AK: Move adopt_nonnull_ref_or_enomem() to NonnullRefPtr.h
Rewrite the implementation to not depend on OwnPtr.h. No intended behavior change.
-rw-r--r--AK/NonnullRefPtr.h10
-rw-r--r--AK/RefPtr.h10
2 files changed, 10 insertions, 10 deletions
diff --git a/AK/NonnullRefPtr.h b/AK/NonnullRefPtr.h
index 7f3a054dd1..7be705bb3f 100644
--- a/AK/NonnullRefPtr.h
+++ b/AK/NonnullRefPtr.h
@@ -226,6 +226,15 @@ inline NonnullRefPtr<T> adopt_ref(T& object)
return NonnullRefPtr<T>(NonnullRefPtr<T>::Adopt, object);
}
+// Use like `adopt_nonnull_own_or_enomem(new (nothrow) T(args...))`.
+template<typename T>
+inline ErrorOr<NonnullRefPtr<T>> adopt_nonnull_ref_or_enomem(T* object)
+{
+ if (!object)
+ return Error::from_errno(ENOMEM);
+ return NonnullRefPtr<T>(NonnullRefPtr<T>::Adopt, *object);
+}
+
template<Formattable T>
struct Formatter<NonnullRefPtr<T>> : Formatter<T> {
ErrorOr<void> format(FormatBuilder& builder, NonnullRefPtr<T> const& value)
@@ -274,6 +283,7 @@ struct Traits<NonnullRefPtr<T>> : public GenericTraits<NonnullRefPtr<T>> {
}
#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 f99ac2c9fa..528cf7c38c 100644
--- a/AK/RefPtr.h
+++ b/AK/RefPtr.h
@@ -341,19 +341,9 @@ inline ErrorOr<NonnullRefPtr<T>> try_make_ref_counted(Args&&... args)
return adopt_nonnull_ref_or_enomem(new (nothrow) T { forward<Args>(args)... });
}
-template<typename T>
-inline ErrorOr<NonnullRefPtr<T>> 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;