From 54c5422cda2cd6cfd4930bc7ab562646e5c83d6e Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 29 Aug 2021 16:23:04 +0200 Subject: [PATCH] Adds helper functions for ScopedPtr --- src/Memory/__OptionalScopedPtr.hpp | 9 +++++---- src/Memory/__ScopedPtr.hpp | 8 +++++++- tests/MemoryTests.cpp | 11 +++++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 tests/MemoryTests.cpp diff --git a/src/Memory/__OptionalScopedPtr.hpp b/src/Memory/__OptionalScopedPtr.hpp index 2590c41..5cf9c3f 100644 --- a/src/Memory/__OptionalScopedPtr.hpp +++ b/src/Memory/__OptionalScopedPtr.hpp @@ -43,15 +43,14 @@ 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. - inline T* GetValue() const noexcept { return _raw; } - - /// @brief Get the raw underlying pointer. + /// @brief Get the raw underlying pointer, and take ownership of it. This removes the existing pointer in here. inline T* TakeOwnership() noexcept { auto raw = _raw; _raw = nullptr; return raw; } + /// @brief Get the raw underlying pointer, without taking ownership of it. + inline T* GetValue() noexcept { return _raw; } /// @brief Check equality of two ScopedPtr objects inline bool operator==(const OptionalScopedPtr& rhs) const noexcept { return _raw == rhs._raw; } @@ -61,6 +60,8 @@ namespace ArbUt { inline bool operator!=(const OptionalScopedPtr& rhs) const noexcept { return _raw != rhs._raw; } /// @brief Check equality of pointers inline bool operator!=(T* rhs) const noexcept { return _raw != rhs; } + /// @brief Implicit cast to retrieve optional borrowed pointer. + inline operator OptionalBorrowedPtr() const noexcept { return _raw; } }; } diff --git a/src/Memory/__ScopedPtr.hpp b/src/Memory/__ScopedPtr.hpp index 68a3c16..e8cebe1 100644 --- a/src/Memory/__ScopedPtr.hpp +++ b/src/Memory/__ScopedPtr.hpp @@ -48,12 +48,14 @@ namespace ArbUt { /// @brief Operator for access into underlying pointer. inline T* operator->() const noexcept { return _raw; } - /// @brief Get the raw underlying pointer. + /// @brief Get the raw underlying pointer, and take ownership of it. This removes the existing pointer in here. inline T* TakeOwnership() noexcept { auto raw = _raw; _raw = nullptr; return raw; } + /// @brief Get the raw underlying pointer, without taking ownership of it. + inline T* GetValue() noexcept { return _raw; } /// @brief Delete comparison with nullptr, ScopedPtr can't be null inline bool operator==(std::nullptr_t) const = delete; @@ -68,6 +70,10 @@ namespace ArbUt { /// @brief Implicit cast to retrieve raw pointer. inline operator T*() const noexcept { return _raw; } + /// @brief Implicit cast to retrieve borrowed ptr. + inline operator BorrowedPtr() const noexcept { return _raw; } + /// @brief Implicit cast to retrieve optional borrowed pointer. + inline operator OptionalBorrowedPtr() const noexcept { return _raw; } }; } diff --git a/tests/MemoryTests.cpp b/tests/MemoryTests.cpp new file mode 100644 index 0000000..b578803 --- /dev/null +++ b/tests/MemoryTests.cpp @@ -0,0 +1,11 @@ +#ifdef TESTS_BUILD +#include "../extern/doctest.hpp" +#include "../src/Memory/Memory.hpp" +using namespace ArbUt; + +TEST_CASE("Scoped Pointer deletes child after out of scope") { + auto a = ScopedPtr(new uint32_t(100)); +} + + +#endif \ No newline at end of file