From 79078ff21e80c389dc239d964af601a58bd146ea Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 14 Feb 2023 19:59:44 +0100 Subject: [PATCH] SharedPtr: Implement make_shared using adopt_shared --- luna/include/luna/SharedPtr.h | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/luna/include/luna/SharedPtr.h b/luna/include/luna/SharedPtr.h index 5eb9f918..728cc229 100644 --- a/luna/include/luna/SharedPtr.h +++ b/luna/include/luna/SharedPtr.h @@ -104,19 +104,6 @@ template class SharedPtr RefCount* m_ref_count; }; -template Result> make_shared(Args... args) -{ - using RefCount = __detail::RefCount; - - RefCount* const ref_count = TRY(make()); - auto guard = make_scope_guard([&] { delete ref_count; }); - - T* const ptr = TRY(make(args...)); - guard.deactivate(); - - return SharedPtr { ptr, ref_count }; -} - // NOTE: ptr is deleted if any of the adopt_shared* functions fail to construct a SharedPtr. template Result> adopt_shared(T* ptr) { @@ -131,6 +118,12 @@ template Result> adopt_shared(T* ptr) return SharedPtr { ptr, ref_count }; } +template Result> make_shared(Args... args) +{ + T* raw_ptr = TRY(make(args...)); + return adopt_shared(raw_ptr); +} + template Result> adopt_shared_if_nonnull(T* ptr) { if (ptr) return adopt_shared(ptr);