Support for pointer holder outline.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-03-28 21:08:55 +01:00
parent bfedd15560
commit 872c275bc7
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
5 changed files with 138 additions and 0 deletions

View File

@ -0,0 +1,21 @@
#ifndef ARBUTILS_BORROWEDPTR_HPP
#define ARBUTILS_BORROWEDPTR_HPP
namespace Arbutils::Memory{
template <class T>
class BorrowedPtr{
T* _ptr;
public:
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; }
T* operator->() noexcept { return _ptr; }
const T* operator->() const noexcept { return _ptr; }
};
}
#endif // ARBUTILS_BORROWEDPTR_HPP

View File

@ -0,0 +1,24 @@
#ifndef ARBUTILS_NONNULLBORROWEDPTR_HPP
#define ARBUTILS_NONNULLBORROWEDPTR_HPP
#include "../Assert.hpp"
#include "BorrowedPtr.hpp"
namespace Arbutils::Memory {
template <class T> class NonNullBorrowedPtr {
private:
T* _ptr;
public:
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; }
T* operator->() noexcept { return _ptr; }
const T* operator->() const noexcept { return _ptr; }
};
}
#endif // ARBUTILS_NONNULLBORROWEDPTR_HPP

View File

@ -0,0 +1,34 @@
#ifndef ARBUTILS_NONNULLOWNPTR_HPP
#define ARBUTILS_NONNULLOWNPTR_HPP
#include "OwnPtr.hpp"
#include "../Assert.hpp"
#include "NonNullBorrowedPtr.hpp"
namespace Arbutils::Memory {
template <class T> class NonNullOwnPtr{
T* _ptr;
public:
inline explicit NonNullOwnPtr(T* ptr) : _ptr(ptr){
AssertNotNull(ptr);
};
NonNullOwnPtr<T>(const NonNullOwnPtr<T>&) = delete;
NonNullOwnPtr<T>& operator=(OwnPtr<T> const&) = delete;
inline ~NonNullOwnPtr() noexcept { delete _ptr; }
inline constexpr T* GetUnsafe() noexcept { return _ptr; }
inline constexpr const T* GetUnsafe() const noexcept { return _ptr; }
inline NonNullBorrowedPtr<T> Borrow(){
return NonNullBorrowedPtr<T>(_ptr);
}
T* operator->() noexcept { return _ptr; }
const T* operator->() const noexcept { return _ptr; }
};
}
#endif // ARBUTILS_NONNULLOWNPTR_HPP

32
src/Memory/OwnPtr.hpp Normal file
View File

@ -0,0 +1,32 @@
#ifndef ARBUTILS_OWNPTR_HPP
#define ARBUTILS_OWNPTR_HPP
#include <memory>
#include "BorrowedPtr.hpp"
namespace Arbutils::Memory {
template <class T> class OwnPtr {
T* _ptr;
public:
inline explicit constexpr OwnPtr(T* ptr) noexcept : _ptr(ptr){};
OwnPtr<T>(const OwnPtr<T>&) = delete;
OwnPtr<T>& operator=(OwnPtr<T> const&) = delete;
inline ~OwnPtr() noexcept { delete _ptr; }
inline constexpr T* GetUnsafe() noexcept { return _ptr; }
inline constexpr const T* GetUnsafe() const noexcept { return _ptr; }
inline BorrowedPtr<T> Borrow(){
return BorrowedPtr<T>(_ptr);
}
T* operator->() noexcept { return _ptr; }
const T* operator->() const noexcept { return _ptr; }
};
}
#endif // ARBUTILS_OWNPTR_HPP

27
tests/MemoryTests.cpp Normal file
View File

@ -0,0 +1,27 @@
#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<TestClass>(new TestClass());
CHECK(v->GetTestBool());
}
TEST_CASE("Access NonNullOwnPtr", "[Utilities]") {
auto v = Arbutils::Memory::NonNullOwnPtr<TestClass>(new TestClass());
CHECK(v->GetTestBool());
}
TEST_CASE("Instantiate NonNullOwnPtr fails with nullptr", "[Utilities]") {
CHECK_THROWS(Arbutils::Memory::NonNullOwnPtr<TestClass>(nullptr));
}
#endif