From 74ac2dbbc36a8fe63fceadadf288ee6cd4a81a3b Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 29 Mar 2020 18:09:55 +0200 Subject: [PATCH] More functionality for memory classes. --- src/Memory/BorrowedPtr.hpp | 10 +++++++--- src/Memory/NonNullBorrowedPtr.hpp | 5 +++++ src/Memory/NonNullOwnPtr.hpp | 17 +++++++++-------- src/Memory/OwnPtr.hpp | 8 ++++++++ 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/Memory/BorrowedPtr.hpp b/src/Memory/BorrowedPtr.hpp index b043d17..1823993 100644 --- a/src/Memory/BorrowedPtr.hpp +++ b/src/Memory/BorrowedPtr.hpp @@ -1,9 +1,8 @@ #ifndef ARBUTILS_BORROWEDPTR_HPP #define ARBUTILS_BORROWEDPTR_HPP -namespace Arbutils::Memory{ - template - class BorrowedPtr{ +namespace Arbutils::Memory { + template class BorrowedPtr { T* _ptr; public: @@ -11,10 +10,15 @@ namespace Arbutils::Memory{ 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; } T* operator->() noexcept { return _ptr; } const 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; } }; } diff --git a/src/Memory/NonNullBorrowedPtr.hpp b/src/Memory/NonNullBorrowedPtr.hpp index d545171..4482f9b 100644 --- a/src/Memory/NonNullBorrowedPtr.hpp +++ b/src/Memory/NonNullBorrowedPtr.hpp @@ -18,6 +18,11 @@ namespace Arbutils::Memory { T* operator->() noexcept { return _ptr; } const 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; } }; } diff --git a/src/Memory/NonNullOwnPtr.hpp b/src/Memory/NonNullOwnPtr.hpp index c844dff..ea50678 100644 --- a/src/Memory/NonNullOwnPtr.hpp +++ b/src/Memory/NonNullOwnPtr.hpp @@ -1,18 +1,16 @@ #ifndef ARBUTILS_NONNULLOWNPTR_HPP #define ARBUTILS_NONNULLOWNPTR_HPP -#include "OwnPtr.hpp" #include "../Assert.hpp" #include "NonNullBorrowedPtr.hpp" +#include "OwnPtr.hpp" namespace Arbutils::Memory { - template class NonNullOwnPtr{ + template class NonNullOwnPtr { T* _ptr; public: - inline explicit NonNullOwnPtr(T* ptr) : _ptr(ptr){ - AssertNotNull(ptr); - }; + inline explicit NonNullOwnPtr(T* ptr) : _ptr(ptr) { AssertNotNull(ptr); }; NonNullOwnPtr(const NonNullOwnPtr&) = delete; NonNullOwnPtr& operator=(OwnPtr const&) = delete; @@ -22,12 +20,15 @@ namespace Arbutils::Memory { inline constexpr T* GetUnsafe() noexcept { return _ptr; } inline constexpr const T* GetUnsafe() const noexcept { return _ptr; } - inline NonNullBorrowedPtr Borrow(){ - return NonNullBorrowedPtr(_ptr); - } + inline NonNullBorrowedPtr Borrow() { return NonNullBorrowedPtr(_ptr); } T* operator->() noexcept { return _ptr; } const 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; } }; } diff --git a/src/Memory/OwnPtr.hpp b/src/Memory/OwnPtr.hpp index 406cb8b..b0c5d13 100644 --- a/src/Memory/OwnPtr.hpp +++ b/src/Memory/OwnPtr.hpp @@ -18,6 +18,9 @@ namespace Arbutils::Memory { 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); @@ -25,6 +28,11 @@ namespace Arbutils::Memory { T* operator->() noexcept { return _ptr; } const 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; } }; }