diff options
Diffstat (limited to 'compat')
-rw-r--r-- | compat/borrowed-ptr-cast.hpp | 6 | ||||
-rw-r--r-- | compat/borrowed-ptr.cpp | 17 | ||||
-rw-r--r-- | compat/borrowed-ptr.hpp | 17 | ||||
-rw-r--r-- | compat/borrowed-ptr.inl | 75 |
4 files changed, 39 insertions, 76 deletions
diff --git a/compat/borrowed-ptr-cast.hpp b/compat/borrowed-ptr-cast.hpp index b9db2e5b..f202af30 100644 --- a/compat/borrowed-ptr-cast.hpp +++ b/compat/borrowed-ptr-cast.hpp @@ -22,11 +22,7 @@ bptr<To> static_pointer_cast(const bptr<From>& p) noexcept if constexpr (detail_borrowed_ptr::StaticCastable<From, To>) { if (p.blk && p.blk->_ptr) [[likely]] - { - fm_assert(p.casted_ptr); - auto* ret = static_cast<To*>(p.casted_ptr); - return bptr<To>{DirectInit, ret, p.blk}; - } + return bptr<To>{p, bptr<To>::private_tag}; } else { diff --git a/compat/borrowed-ptr.cpp b/compat/borrowed-ptr.cpp index e66f31bc..fc548726 100644 --- a/compat/borrowed-ptr.cpp +++ b/compat/borrowed-ptr.cpp @@ -20,11 +20,11 @@ void control_block::decrement(control_block*& blk) noexcept fm_bptr_assert(c != (uint32_t)-1); if (c == 0) { - blk->free_ptr(); + delete blk->_ptr; delete blk; } - //blk = nullptr; - blk = (control_block*)-1; + blk = nullptr; + //blk = (control_block*)-1; } #ifdef __clang__ @@ -36,3 +36,14 @@ void control_block::decrement(control_block*& blk) noexcept #endif } // namespace floormat::detail_borrowed_ptr + +namespace floormat { + +bptr_base::~bptr_base() noexcept = default; +bptr_base::bptr_base() noexcept = default; +bptr_base::bptr_base(const bptr_base&) noexcept = default; +bptr_base::bptr_base(bptr_base&&) noexcept = default; +bptr_base& bptr_base::operator=(const bptr_base&) noexcept = default; +bptr_base& bptr_base::operator=(bptr_base&&) noexcept = default; + +} // namespace floormat diff --git a/compat/borrowed-ptr.hpp b/compat/borrowed-ptr.hpp index e622f6d8..31f83c16 100644 --- a/compat/borrowed-ptr.hpp +++ b/compat/borrowed-ptr.hpp @@ -16,13 +16,22 @@ namespace floormat { template<typename T> class bptr; +struct bptr_base +{ + virtual ~bptr_base() noexcept; + bptr_base() noexcept; + bptr_base(const bptr_base&) noexcept; + bptr_base(bptr_base&&) noexcept; + bptr_base& operator=(const bptr_base&) noexcept; + bptr_base& operator=(bptr_base&&) noexcept; +}; + template<typename T> class bptr final // NOLINT(*-special-member-functions) { - mutable T* casted_ptr; - detail_borrowed_ptr::control_block* blk; + static_assert(std::is_convertible_v<T*, bptr_base*>); - explicit bptr(DirectInitT, T* casted_ptr, detail_borrowed_ptr::control_block* blk) noexcept; + detail_borrowed_ptr::control_block* blk; struct private_tag_t final {}; static constexpr private_tag_t private_tag{}; @@ -55,7 +64,7 @@ public: template<detail_borrowed_ptr::DerivedFrom<T> Y> bptr& operator=(bptr<Y>&&) noexcept; void reset() noexcept; - template<bool MaybeEmpty = true> void destroy() noexcept; + void destroy() noexcept; void swap(bptr& other) noexcept; uint32_t use_count() const noexcept; diff --git a/compat/borrowed-ptr.inl b/compat/borrowed-ptr.inl index a29e6b1c..390593bf 100644 --- a/compat/borrowed-ptr.inl +++ b/compat/borrowed-ptr.inl @@ -26,58 +26,19 @@ namespace floormat::detail_borrowed_ptr { #endif struct control_block { - void* _ptr; // todo maybe add directly embeddable objects? + bptr_base* _ptr; uint32_t _count; - virtual void free_ptr() noexcept = 0; static void decrement(control_block*& blk) noexcept; }; #ifdef __GNUG__ #pragma GCC diagnostic pop -#endif - -template<typename T> -struct control_block_impl final: control_block -{ - void free_ptr() noexcept override; - [[nodiscard]] static control_block* create(T* ptr) noexcept; -protected: - explicit control_block_impl(T* ptr) noexcept; -}; - -template <typename T> -control_block_impl<T>::control_block_impl(T* ptr) noexcept -{ - fm_bptr_assert(ptr); - _ptr = ptr; - _count = 1; -} - -template <typename T> -void control_block_impl<T>::free_ptr() noexcept -{ - delete static_cast<T*>(_ptr); -} -template <typename T> -control_block* control_block_impl<T>::create(T* ptr) noexcept -{ - if (ptr) - { - auto* __restrict ret = new control_block_impl<T>{ptr}; - return ret; - } - else - return nullptr; -} +#endif } // namespace floormat::detail_borrowed_ptr namespace floormat { -template<typename T> bptr<T>::bptr(DirectInitT, T* casted_ptr, detail_borrowed_ptr::control_block* blk) noexcept: - casted_ptr{casted_ptr}, blk{blk} -{} - template<typename T> template<typename... Ts> requires std::is_constructible_v<T, Ts&&...> @@ -86,13 +47,12 @@ bptr{ new T{ forward<Ts...>(args...) } } { } -template<typename T> bptr<T>::bptr(std::nullptr_t) noexcept: casted_ptr{nullptr}, blk{nullptr} {} +template<typename T> bptr<T>::bptr(std::nullptr_t) noexcept: blk{nullptr} {} template<typename T> bptr<T>::bptr() noexcept: bptr{nullptr} {} template<typename T> bptr<T>::bptr(T* ptr) noexcept: - casted_ptr{ptr}, - blk{detail_borrowed_ptr::control_block_impl<T>::create(ptr)} + blk{ptr ? new detail_borrowed_ptr::control_block{ptr, 1} : nullptr} { fm_bptr_assert(!blk || blk->_count == 1 && blk->_ptr); } @@ -133,23 +93,18 @@ void bptr<T>::reset() noexcept { if (blk) { - fm_bptr_assert(casted_ptr); blk->decrement(blk); - casted_ptr = nullptr; blk = nullptr; } } template<typename T> -template<bool MaybeEmpty> void bptr<T>::destroy() noexcept { - if constexpr(MaybeEmpty) - if (!blk) - return; - blk->free_ptr(); + if (!blk) + return; + delete blk->_ptr; blk->_ptr = nullptr; - casted_ptr = nullptr; } template<typename T> bptr<T>& bptr<T>::operator=(std::nullptr_t) noexcept { reset(); return *this; } @@ -157,7 +112,7 @@ template<typename T> bptr<T>& bptr<T>::operator=(std::nullptr_t) noexcept { rese template<typename T> template<typename Y> bptr<T>::bptr(const bptr<Y>& other, private_tag_t) noexcept: - casted_ptr{other.casted_ptr}, blk{other.blk} + blk{other.blk} { if (blk) { @@ -175,7 +130,6 @@ bptr<T>& bptr<T>::_copy_assign(const bptr<Y>& other) noexcept CORRADE_ASSUME(this != &other); // todo! see if helps if (blk) blk->decrement(blk); - casted_ptr = other.casted_ptr; blk = other.blk; if (blk) ++blk->_count; @@ -186,9 +140,8 @@ bptr<T>& bptr<T>::_copy_assign(const bptr<Y>& other) noexcept template<typename T> template<typename Y> bptr<T>::bptr(bptr<Y>&& other, private_tag_t) noexcept: - casted_ptr{other.casted_ptr}, blk{other.blk} + blk{other.blk} { - other.casted_ptr = nullptr; other.blk = nullptr; } @@ -198,20 +151,15 @@ bptr<T>& bptr<T>::_move_assign(bptr<Y>&& other) noexcept { if (blk) blk->decrement(blk); - casted_ptr = other.casted_ptr; blk = other.blk; - other.casted_ptr = nullptr; other.blk = nullptr; return *this; } template<typename T> T* bptr<T>::get() const noexcept { - if (blk && blk->_ptr) [[likely]] - { - fm_bptr_assert(casted_ptr); - return casted_ptr; - } + if (blk) [[likely]] + return static_cast<T*>(blk->_ptr); else return nullptr; } @@ -231,7 +179,6 @@ template<typename T> void bptr<T>::swap(bptr& other) noexcept { using floormat::swap; - swap(casted_ptr, other.casted_ptr); swap(blk, other.blk); } |