From 4c1ca2e8220799e2ef2168b7af6f1c67dd6a63af Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Mon, 30 Mar 2020 18:52:08 +0200 Subject: [PATCH] Allow transferring borrowedPtr into NonNull variant and vice versa. --- src/Memory/BorrowedPtr.hpp | 7 +++++++ src/Memory/NonNullBorrowedPtr.hpp | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/src/Memory/BorrowedPtr.hpp b/src/Memory/BorrowedPtr.hpp index 3197214..600ca0f 100644 --- a/src/Memory/BorrowedPtr.hpp +++ b/src/Memory/BorrowedPtr.hpp @@ -2,6 +2,9 @@ #define ARBUTILS_BORROWEDPTR_HPP namespace Arbutils::Memory { + template + class NonNullBorrowedPtr; + template class BorrowedPtr { T* _ptr; @@ -13,6 +16,10 @@ namespace Arbutils::Memory { inline constexpr const T* GetUnsafe() const noexcept { return _ptr; } inline constexpr bool IsNull() const noexcept { return _ptr == nullptr; } + inline NonNullBorrowedPtr GetNonNull(){ + return NonNullBorrowedPtr(_ptr); + } + T* operator->() noexcept { return _ptr; } T* operator->() const noexcept { return _ptr; } diff --git a/src/Memory/NonNullBorrowedPtr.hpp b/src/Memory/NonNullBorrowedPtr.hpp index d075ec3..5b94f80 100644 --- a/src/Memory/NonNullBorrowedPtr.hpp +++ b/src/Memory/NonNullBorrowedPtr.hpp @@ -4,6 +4,9 @@ #include "BorrowedPtr.hpp" namespace Arbutils::Memory { + template + class BorrowedPtr; + template class NonNullBorrowedPtr { private: T* _ptr; @@ -15,6 +18,10 @@ namespace Arbutils::Memory { inline constexpr T* GetUnsafe() noexcept { return _ptr; } inline constexpr const T* GetUnsafe() const noexcept { return _ptr; } + inline BorrowedPtr GetOptional(){ + return BorrowedPtr(_ptr); + } + T* operator->() noexcept { return _ptr; } T* operator->() const noexcept { return _ptr; }