From 779ddd49e35a803bbbf27ef27945faf1f96eba5d Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Tue, 26 May 2020 13:22:18 +0200 Subject: [PATCH] Replace old memory management tests with simple borrowed_ptr.cpp. --- src/Memory/BorrowedPtr.hpp | 30 ------------------------ src/Memory/NonNullBorrowedPtr.hpp | 32 ------------------------- src/Memory/NonNullOwnPtr.hpp | 38 ------------------------------ src/Memory/OwnPtr.hpp | 39 ------------------------------- src/Memory/borrowed_ptr.cpp | 1 + src/Memory/borrowed_ptr.hpp | 33 ++++++++++++++++++++++++++ tests/MemoryTests.cpp | 24 ------------------- 7 files changed, 34 insertions(+), 163 deletions(-) delete mode 100644 src/Memory/BorrowedPtr.hpp delete mode 100644 src/Memory/NonNullBorrowedPtr.hpp delete mode 100644 src/Memory/NonNullOwnPtr.hpp delete mode 100644 src/Memory/OwnPtr.hpp create mode 100644 src/Memory/borrowed_ptr.cpp create mode 100644 src/Memory/borrowed_ptr.hpp delete mode 100644 tests/MemoryTests.cpp diff --git a/src/Memory/BorrowedPtr.hpp b/src/Memory/BorrowedPtr.hpp deleted file mode 100644 index f4e561e..0000000 --- a/src/Memory/BorrowedPtr.hpp +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef ARBUTILS_BORROWEDPTR_HPP -#define ARBUTILS_BORROWEDPTR_HPP - -namespace Arbutils::Memory { - template class NonNullBorrowedPtr; - - template class BorrowedPtr { - T* _ptr; - - public: - inline BorrowedPtr() { _ptr = nullptr; }; - inline explicit constexpr BorrowedPtr(T* ptr) noexcept : _ptr(ptr){}; - - inline constexpr T* GetUnsafe() noexcept { return _ptr; } - inline constexpr const T* GetUnsafe() const noexcept { return _ptr; } - inline constexpr bool IsNull() const noexcept { return _ptr == nullptr; } - - inline NonNullBorrowedPtr GetNonNull() const { return NonNullBorrowedPtr(_ptr); } - - T* operator->() noexcept { return _ptr; } - T* operator->() const noexcept { return _ptr; } - - inline bool operator==(const BorrowedPtr& rhs) const noexcept { return _ptr == rhs._ptr; } - inline bool operator!=(const BorrowedPtr& rhs) const noexcept { return _ptr != rhs._ptr; } - inline bool operator==(const T* rhs) const noexcept { return _ptr == rhs; } - inline bool operator!=(const T* rhs) const noexcept { return _ptr != rhs; } - }; -} - -#endif // ARBUTILS_BORROWEDPTR_HPP diff --git a/src/Memory/NonNullBorrowedPtr.hpp b/src/Memory/NonNullBorrowedPtr.hpp deleted file mode 100644 index 95022eb..0000000 --- a/src/Memory/NonNullBorrowedPtr.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef ARBUTILS_NONNULLBORROWEDPTR_HPP -#define ARBUTILS_NONNULLBORROWEDPTR_HPP -#include "../Assert.hpp" -#include "BorrowedPtr.hpp" - -namespace Arbutils::Memory { - template class BorrowedPtr; - - template class NonNullBorrowedPtr { - private: - T* _ptr; - - public: - inline NonNullBorrowedPtr(){}; - inline explicit NonNullBorrowedPtr(T* ptr) : _ptr(ptr) { AssertNotNull(ptr); }; - - inline constexpr T* GetUnsafe() noexcept { return _ptr; } - inline constexpr const T* GetUnsafe() const noexcept { return _ptr; } - - inline BorrowedPtr GetOptional() const { return BorrowedPtr(_ptr); } - - T* operator->() noexcept { return _ptr; } - T* operator->() const noexcept { return _ptr; } - - inline bool operator==(const NonNullBorrowedPtr& rhs) const noexcept { return _ptr == rhs._ptr; } - inline bool operator!=(const NonNullBorrowedPtr& rhs) const noexcept { return _ptr != rhs._ptr; } - inline bool operator==(const T* rhs) const noexcept { return _ptr == rhs; } - inline bool operator!=(const T* rhs) const noexcept { return _ptr != rhs; } - }; -} - -#endif // ARBUTILS_NONNULLBORROWEDPTR_HPP diff --git a/src/Memory/NonNullOwnPtr.hpp b/src/Memory/NonNullOwnPtr.hpp deleted file mode 100644 index 17058ae..0000000 --- a/src/Memory/NonNullOwnPtr.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef ARBUTILS_NONNULLOWNPTR_HPP -#define ARBUTILS_NONNULLOWNPTR_HPP - -#include "../Assert.hpp" -#include "BorrowedPtr.hpp" -#include "NonNullBorrowedPtr.hpp" - -namespace Arbutils::Memory { - template class NonNullOwnPtr { - T* _ptr; - - public: - inline NonNullOwnPtr(){}; - inline NonNullOwnPtr(T* ptr) : _ptr(ptr) { AssertNotNull(ptr); }; - - NonNullOwnPtr(const NonNullOwnPtr&) = delete; - - inline ~NonNullOwnPtr() noexcept { delete _ptr; } - - inline constexpr T* GetUnsafe() noexcept { return _ptr; } - inline constexpr const T* GetUnsafe() const noexcept { return _ptr; } - - inline NonNullBorrowedPtr Borrow() { return NonNullBorrowedPtr(_ptr); } - inline const NonNullBorrowedPtr Borrow() const { return NonNullBorrowedPtr(_ptr); } - inline BorrowedPtr BorrowOptional() { return BorrowedPtr(_ptr); } - inline const BorrowedPtr BorrowOptional() const { return BorrowedPtr(_ptr); } - - T* operator->() noexcept { return _ptr; } - T* operator->() const noexcept { return _ptr; } - - inline bool operator==(const NonNullOwnPtr& rhs) const noexcept { return _ptr == rhs._ptr; } - inline bool operator!=(const NonNullOwnPtr& rhs) const noexcept { return _ptr != rhs._ptr; } - inline bool operator==(const T* rhs) const noexcept { return _ptr == rhs; } - inline bool operator!=(const T* rhs) const noexcept { return _ptr != rhs; } - }; -} - -#endif // ARBUTILS_NONNULLOWNPTR_HPP diff --git a/src/Memory/OwnPtr.hpp b/src/Memory/OwnPtr.hpp deleted file mode 100644 index 1ad6a42..0000000 --- a/src/Memory/OwnPtr.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef ARBUTILS_OWNPTR_HPP -#define ARBUTILS_OWNPTR_HPP - -#include -#include "BorrowedPtr.hpp" -#include "NonNullBorrowedPtr.hpp" - -namespace Arbutils::Memory { - template class OwnPtr { - T* _ptr; - - public: - inline OwnPtr() { _ptr = nullptr; }; - inline constexpr OwnPtr(T* ptr) noexcept : _ptr(ptr){}; - - OwnPtr(const OwnPtr&) = delete; - - inline ~OwnPtr() noexcept { delete _ptr; } - - inline constexpr T* GetUnsafe() noexcept { return _ptr; } - inline constexpr const T* GetUnsafe() const noexcept { return _ptr; } - inline constexpr const T* IsNull() const noexcept { return _ptr == nullptr; } - - inline BorrowedPtr Borrow() { return BorrowedPtr(_ptr); } - inline const BorrowedPtr Borrow() const { return BorrowedPtr(_ptr); } - inline NonNullBorrowedPtr BorrowNonNull() { return NonNullBorrowedPtr(_ptr); } - inline const NonNullBorrowedPtr BorrowNonNull() const { return NonNullBorrowedPtr(_ptr); } - - T* operator->() noexcept { return _ptr; } - T* operator->() const noexcept { return _ptr; } - - inline bool operator==(const OwnPtr& rhs) const noexcept { return _ptr == rhs._ptr; } - inline bool operator!=(const OwnPtr& rhs) const noexcept { return _ptr != rhs._ptr; } - inline bool operator==(const T* rhs) const noexcept { return _ptr == rhs; } - inline bool operator!=(const T* rhs) const noexcept { return _ptr != rhs; } - }; -} - -#endif // ARBUTILS_OWNPTR_HPP diff --git a/src/Memory/borrowed_ptr.cpp b/src/Memory/borrowed_ptr.cpp new file mode 100644 index 0000000..cf5bc1c --- /dev/null +++ b/src/Memory/borrowed_ptr.cpp @@ -0,0 +1 @@ +#include "borrowed_ptr.hpp" diff --git a/src/Memory/borrowed_ptr.hpp b/src/Memory/borrowed_ptr.hpp new file mode 100644 index 0000000..d1f1a28 --- /dev/null +++ b/src/Memory/borrowed_ptr.hpp @@ -0,0 +1,33 @@ +#ifndef ARBUTILS_BORROWED_PTR_HPP +#define ARBUTILS_BORROWED_PTR_HPP + +#include + +/// A borrowed pointer is used to indicate a pointer that is not owned by an object, but instead borrowed from another +/// owning object that is assumed to always be kept alive during the entire lifetime of the borrowing object. +template class borrowed_ptr { +private: + T* _raw; + +public: + inline borrowed_ptr(T* ptr) : _raw(ptr){}; + inline borrowed_ptr(const borrowed_ptr& other) : _raw(other._raw){}; + inline borrowed_ptr(const std::unique_ptr& other) : _raw(other.get()){}; + + ~borrowed_ptr() = default; + + inline T* operator->() noexcept { return _raw; } + inline const T* operator->() const noexcept { return _raw; } + + inline T* GetRaw() noexcept { return _raw; } + inline const T* GetRaw() const noexcept { return _raw; } + + inline bool operator==(const borrowed_ptr& rhs) const { return _raw == rhs._raw; } + inline bool operator!=(const borrowed_ptr& rhs) const { return _raw != rhs._raw; } + inline bool operator==(T* rhs) const { return _raw == rhs; } + inline bool operator!=(T* rhs) const { return _raw != rhs; } + inline bool operator==(const T* rhs) const { return _raw == rhs; } + inline bool operator!=(const T* rhs) const { return _raw != rhs; } +}; + +#endif // ARBUTILS_BORROWED_PTR_HPP diff --git a/tests/MemoryTests.cpp b/tests/MemoryTests.cpp deleted file mode 100644 index 9176afe..0000000 --- a/tests/MemoryTests.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#ifdef TESTS_BUILD -#include "../extern/catch.hpp" -#include "../src/Memory/NonNullOwnPtr.hpp" -#include "../src/Memory/OwnPtr.hpp" - -struct TestClass { - bool GetTestBool() { return true; } -}; - -TEST_CASE("Access OwnPtr", "[Utilities]") { - auto v = Arbutils::Memory::OwnPtr(new TestClass()); - CHECK(v->GetTestBool()); -} - -TEST_CASE("Access NonNullOwnPtr", "[Utilities]") { - auto v = Arbutils::Memory::NonNullOwnPtr(new TestClass()); - CHECK(v->GetTestBool()); -} - -TEST_CASE("Instantiate NonNullOwnPtr fails with nullptr", "[Utilities]") { - CHECK_THROWS(Arbutils::Memory::NonNullOwnPtr(nullptr)); -} - -#endif \ No newline at end of file