Adds helper functions for ScopedPtr
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2021-08-29 16:23:04 +02:00
parent b58b8537b5
commit 54c5422cda
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 23 additions and 5 deletions

View File

@ -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<T>() const noexcept { return _raw; }
};
}

View File

@ -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<T>() const noexcept { return _raw; }
/// @brief Implicit cast to retrieve optional borrowed pointer.
inline operator OptionalBorrowedPtr<T>() const noexcept { return _raw; }
};
}

11
tests/MemoryTests.cpp Normal file
View File

@ -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<uint32_t>(new uint32_t(100));
}
#endif