More functionality for memory classes.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-03-29 18:09:55 +02:00
parent 872c275bc7
commit 74ac2dbbc3
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
4 changed files with 29 additions and 11 deletions

View File

@ -2,8 +2,7 @@
#define ARBUTILS_BORROWEDPTR_HPP
namespace Arbutils::Memory {
template <class T>
class BorrowedPtr{
template <class T> 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; }
};
}

View File

@ -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; }
};
}

View File

@ -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 T> class NonNullOwnPtr {
T* _ptr;
public:
inline explicit NonNullOwnPtr(T* ptr) : _ptr(ptr){
AssertNotNull(ptr);
};
inline explicit NonNullOwnPtr(T* ptr) : _ptr(ptr) { AssertNotNull(ptr); };
NonNullOwnPtr<T>(const NonNullOwnPtr<T>&) = delete;
NonNullOwnPtr<T>& operator=(OwnPtr<T> 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<T> Borrow(){
return NonNullBorrowedPtr<T>(_ptr);
}
inline NonNullBorrowedPtr<T> Borrow() { return NonNullBorrowedPtr<T>(_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; }
};
}

View File

@ -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<T> Borrow(){
return BorrowedPtr<T>(_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; }
};
}