Replace old memory management tests with simple borrowed_ptr.cpp.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
394042ec65
commit
779ddd49e3
|
@ -1,30 +0,0 @@
|
||||||
#ifndef ARBUTILS_BORROWEDPTR_HPP
|
|
||||||
#define ARBUTILS_BORROWEDPTR_HPP
|
|
||||||
|
|
||||||
namespace Arbutils::Memory {
|
|
||||||
template <class> class NonNullBorrowedPtr;
|
|
||||||
|
|
||||||
template <class T> 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<T> GetNonNull() const { return NonNullBorrowedPtr<T>(_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
|
|
|
@ -1,32 +0,0 @@
|
||||||
#ifndef ARBUTILS_NONNULLBORROWEDPTR_HPP
|
|
||||||
#define ARBUTILS_NONNULLBORROWEDPTR_HPP
|
|
||||||
#include "../Assert.hpp"
|
|
||||||
#include "BorrowedPtr.hpp"
|
|
||||||
|
|
||||||
namespace Arbutils::Memory {
|
|
||||||
template <class> class BorrowedPtr;
|
|
||||||
|
|
||||||
template <class T> 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<T> GetOptional() const { return BorrowedPtr<T>(_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
|
|
|
@ -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 T> class NonNullOwnPtr {
|
|
||||||
T* _ptr;
|
|
||||||
|
|
||||||
public:
|
|
||||||
inline NonNullOwnPtr(){};
|
|
||||||
inline NonNullOwnPtr(T* ptr) : _ptr(ptr) { AssertNotNull(ptr); };
|
|
||||||
|
|
||||||
NonNullOwnPtr<T>(const NonNullOwnPtr<T>&) = 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); }
|
|
||||||
inline const NonNullBorrowedPtr<const T> Borrow() const { return NonNullBorrowedPtr<const T>(_ptr); }
|
|
||||||
inline BorrowedPtr<T> BorrowOptional() { return BorrowedPtr<T>(_ptr); }
|
|
||||||
inline const BorrowedPtr<const T> BorrowOptional() const { return BorrowedPtr<const T>(_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
|
|
|
@ -1,39 +0,0 @@
|
||||||
#ifndef ARBUTILS_OWNPTR_HPP
|
|
||||||
#define ARBUTILS_OWNPTR_HPP
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
#include "BorrowedPtr.hpp"
|
|
||||||
#include "NonNullBorrowedPtr.hpp"
|
|
||||||
|
|
||||||
namespace Arbutils::Memory {
|
|
||||||
template <class T> class OwnPtr {
|
|
||||||
T* _ptr;
|
|
||||||
|
|
||||||
public:
|
|
||||||
inline OwnPtr() { _ptr = nullptr; };
|
|
||||||
inline constexpr OwnPtr(T* ptr) noexcept : _ptr(ptr){};
|
|
||||||
|
|
||||||
OwnPtr<T>(const OwnPtr<T>&) = 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<T> Borrow() { return BorrowedPtr<T>(_ptr); }
|
|
||||||
inline const BorrowedPtr<const T> Borrow() const { return BorrowedPtr<const T>(_ptr); }
|
|
||||||
inline NonNullBorrowedPtr<T> BorrowNonNull() { return NonNullBorrowedPtr<T>(_ptr); }
|
|
||||||
inline const NonNullBorrowedPtr<const T> BorrowNonNull() const { return NonNullBorrowedPtr<const T>(_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
|
|
|
@ -0,0 +1 @@
|
||||||
|
#include "borrowed_ptr.hpp"
|
|
@ -0,0 +1,33 @@
|
||||||
|
#ifndef ARBUTILS_BORROWED_PTR_HPP
|
||||||
|
#define ARBUTILS_BORROWED_PTR_HPP
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
/// 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 T> class borrowed_ptr {
|
||||||
|
private:
|
||||||
|
T* _raw;
|
||||||
|
|
||||||
|
public:
|
||||||
|
inline borrowed_ptr<T>(T* ptr) : _raw(ptr){};
|
||||||
|
inline borrowed_ptr(const borrowed_ptr<T>& other) : _raw(other._raw){};
|
||||||
|
inline borrowed_ptr(const std::unique_ptr<T>& 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
|
|
@ -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<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
|
|
Loading…
Reference in New Issue