From efdce7b74c1400bc712505fd50bb1fee67e6eb67 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 11 Dec 2020 15:12:17 +0100 Subject: [PATCH] Several fixes for new memory model. --- src/Memory/BorrowedPtr.hpp | 7 +++++-- src/Memory/OptionalBorrowedPtr.hpp | 2 ++ src/Memory/OptionalUniquePtr.hpp | 5 +++-- src/Memory/ScopedPtr.hpp | 4 ++-- src/Memory/UniquePtr.hpp | 19 ++++++++++++------- 5 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/Memory/BorrowedPtr.hpp b/src/Memory/BorrowedPtr.hpp index 37f338f..f923d69 100644 --- a/src/Memory/BorrowedPtr.hpp +++ b/src/Memory/BorrowedPtr.hpp @@ -15,12 +15,14 @@ namespace ArbUt { public: /// @brief Initialise a BorrowedPtr with a specific raw pointer. - inline BorrowedPtr(__attribute__((nonnull)) T* ptr) : _raw(ptr) { AssertNotNull(ptr); }; + inline BorrowedPtr(T* ptr) : _raw(ptr) { AssertNotNull(ptr); }; /// @brief Initialise a BorrowedPtr from a copy. inline BorrowedPtr(const BorrowedPtr& other) : _raw(other._raw){}; /// @brief Initialise a BorrowedPtr with a std unique_ptr. inline BorrowedPtr(const std::unique_ptr& other) : _raw(other.get()){}; + BorrowedPtr(std::nullptr_t) = delete; + ~BorrowedPtr() noexcept = default; /// @brief Copy operator. @@ -31,13 +33,14 @@ namespace ArbUt { return *this; } - inline BorrowedPtr& operator=(__attribute__((nonnull)) T* rhs) { + inline BorrowedPtr& operator=(T* rhs) { if (_raw == &rhs) return *this; AssertNotNull(rhs); _raw = rhs; return *this; } + inline BorrowedPtr& operator=(std::nullptr_t) = delete; /// @brief Operator for access into underlying pointer. /// @warning Note that this asserts that the underlying pointer is not null first, to prevent segfaults. diff --git a/src/Memory/OptionalBorrowedPtr.hpp b/src/Memory/OptionalBorrowedPtr.hpp index 36c7f2a..bad57d6 100644 --- a/src/Memory/OptionalBorrowedPtr.hpp +++ b/src/Memory/OptionalBorrowedPtr.hpp @@ -14,6 +14,8 @@ namespace ArbUt { T* _raw; public: + /// @brief Initialise a BorrowedPtr with a nullptr. + inline OptionalBorrowedPtr() : _raw(nullptr) {} /// @brief Initialise a BorrowedPtr with a specific raw pointer. inline OptionalBorrowedPtr(T* ptr) : _raw(ptr){}; /// @brief Initialise a BorrowedPtr from a copy. diff --git a/src/Memory/OptionalUniquePtr.hpp b/src/Memory/OptionalUniquePtr.hpp index 60d61eb..87ba440 100644 --- a/src/Memory/OptionalUniquePtr.hpp +++ b/src/Memory/OptionalUniquePtr.hpp @@ -13,8 +13,10 @@ namespace ArbUt { T* _raw; public: + /// @brief Initialise a BorrowedPtr with a nullptr. + inline OptionalUniquePtr() : _raw(nullptr) {} /// @brief Initialise a OptionalUniquePtr with a specific raw pointer. - inline OptionalUniquePtr(__attribute__((nonnull)) T* ptr) : _raw(ptr){}; + inline OptionalUniquePtr(T* ptr) : _raw(ptr){}; /// @brief Initialise a OptionalUniquePtr from a copy. inline OptionalUniquePtr(const OptionalUniquePtr& other) : _raw(other._raw){}; /// @brief Initialise a OptionalUniquePtr with a std unique_ptr. @@ -33,7 +35,6 @@ namespace ArbUt { inline OptionalUniquePtr& operator=(__attribute__((nonnull)) T* rhs) { if (_raw == &rhs) return *this; - AssertNotNull(rhs); _raw = rhs; return *this; } diff --git a/src/Memory/ScopedPtr.hpp b/src/Memory/ScopedPtr.hpp index 65139a2..6f69c1c 100644 --- a/src/Memory/ScopedPtr.hpp +++ b/src/Memory/ScopedPtr.hpp @@ -14,7 +14,7 @@ namespace ArbUt { public: /// @brief Initialise a ScopedPtr with a specific raw pointer. - inline ScopedPtr(__attribute__((nonnull)) T* ptr) : _raw(ptr){}; + inline ScopedPtr(T* ptr) : _raw(ptr){}; /// @brief Initialise a ScopedPtr from a copy. inline ScopedPtr(const ScopedPtr& other) : _raw(other._raw){}; /// @brief Initialise a ScopedPtr with a std unique_ptr. @@ -30,7 +30,7 @@ namespace ArbUt { return *this; } - inline ScopedPtr& operator=(__attribute__((nonnull)) T* rhs) { + inline ScopedPtr& operator=(T* rhs) { if (_raw == &rhs) return *this; AssertNotNull(rhs); diff --git a/src/Memory/UniquePtr.hpp b/src/Memory/UniquePtr.hpp index 7cc48b4..08376b4 100644 --- a/src/Memory/UniquePtr.hpp +++ b/src/Memory/UniquePtr.hpp @@ -5,7 +5,8 @@ namespace ArbUt { /// @brief A unique pointer is used to indicate a pointer that is owned by its holder, and will be deleted when its - /// owner is deleted. As with all Arbutils pointers, this cannot be assigned null. Use an OptionalUniquePtr for pointers that can be null. + /// owner is deleted. As with all Arbutils pointers, this cannot be assigned null. Use an OptionalUniquePtr for + /// pointers that can be null. /// @details A unique pointer is used to indicate a pointer that is owned by an object, and that needs to be deleted /// when its owner is deleted. template class UniquePtr { @@ -14,31 +15,35 @@ namespace ArbUt { public: /// @brief Initialise a UniquePtr with a specific raw pointer. - inline UniquePtr(__attribute__((nonnull)) T* ptr) : _raw(ptr) { AssertNotNull(ptr); }; + inline UniquePtr(T* ptr) : _raw(ptr) { AssertNotNull(ptr); }; /// @brief Initialise a UniquePtr from a copy. inline UniquePtr(const UniquePtr& other) : _raw(other._raw){}; /// @brief Initialise a UniquePtr with a std unique_ptr. inline UniquePtr(const std::unique_ptr& other) : _raw(other.get()){}; - ~UniquePtr() noexcept{ - delete _raw; - } + UniquePtr(std::nullptr_t) = delete; + + ~UniquePtr() noexcept { delete _raw; } /// @brief Copy operator. inline UniquePtr& operator=(const UniquePtr& rhs) { if (this == &rhs) return *this; + delete _raw; _raw = rhs._raw; return *this; } - inline UniquePtr& operator=(__attribute__((nonnull)) T* rhs) { - if (_raw == &rhs) + inline UniquePtr& operator=(T* rhs) { + if (_raw == &rhs) { return *this; + } + delete _raw; AssertNotNull(rhs); _raw = rhs; return *this; } + inline UniquePtr& operator=(std::nullptr_t) = delete; /// @brief Operator for access into underlying pointer. /// @warning Note that this asserts that the underlying pointer is not null first, to prevent segfaults.