From 74ef6b5bdcb0326621e372baf6bb25080181f768 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 14 May 2022 15:32:10 +0200 Subject: [PATCH] Adds functions to take ownership from a unique ptr --- src/Memory/__OptionalUniquePtr.hpp | 7 +++++++ src/Memory/__UniquePtr.hpp | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/Memory/__OptionalUniquePtr.hpp b/src/Memory/__OptionalUniquePtr.hpp index b4eeac8..e77e296 100644 --- a/src/Memory/__OptionalUniquePtr.hpp +++ b/src/Memory/__OptionalUniquePtr.hpp @@ -36,6 +36,13 @@ namespace ArbUt { /// @brief Return whether the pointer is null or not. [[nodiscard]] inline bool HasValue() const noexcept { return _raw != nullptr; } + /// @brief Get the raw underlying pointer, and take ownership of it. This removes the existing pointer in here. + inline T* nullable TakeOwnership() noexcept { + auto raw = _raw; + _raw = nullptr; + return raw; + } + /// @brief Get the raw underlying pointer. inline T* nullable GetValue() const noexcept { return _raw; } diff --git a/src/Memory/__UniquePtr.hpp b/src/Memory/__UniquePtr.hpp index 273626d..686d7eb 100644 --- a/src/Memory/__UniquePtr.hpp +++ b/src/Memory/__UniquePtr.hpp @@ -56,6 +56,16 @@ namespace ArbUt { /// @warning Note that this asserts that the underlying pointer is not null first, to prevent segfaults. inline T* non_null operator->() const noexcept { return _raw; } + /// @brief Get the raw underlying pointer, and take ownership of it. This removes the existing pointer in here. +#if defined(__clang__) + [[clang::no_sanitize("nullability-assign")]] +#endif + inline T* non_null + TakeOwnership() noexcept { + auto raw = _raw; + _raw = nullptr; + return raw; + } /// @brief Get the raw underlying pointer. inline T* non_null GetRaw() const noexcept { return _raw; }