From 5fe8eec392fa0568a03ad566a5b41c3b1df7a465 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 14 Jul 2024 10:48:42 +0200 Subject: w --- compat/borrowed-ptr-cast.hpp | 6 +--- compat/borrowed-ptr.cpp | 17 ++++++++-- compat/borrowed-ptr.hpp | 17 +++++++--- compat/borrowed-ptr.inl | 75 +++++++------------------------------------- 4 files changed, 39 insertions(+), 76 deletions(-) (limited to 'compat') 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 static_pointer_cast(const bptr& p) noexcept if constexpr (detail_borrowed_ptr::StaticCastable) { if (p.blk && p.blk->_ptr) [[likely]] - { - fm_assert(p.casted_ptr); - auto* ret = static_cast(p.casted_ptr); - return bptr{DirectInit, ret, p.blk}; - } + return bptr{p, bptr::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 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 class bptr final // NOLINT(*-special-member-functions) { - mutable T* casted_ptr; - detail_borrowed_ptr::control_block* blk; + static_assert(std::is_convertible_v); - 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 Y> bptr& operator=(bptr&&) noexcept; void reset() noexcept; - template 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 -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 -control_block_impl::control_block_impl(T* ptr) noexcept -{ - fm_bptr_assert(ptr); - _ptr = ptr; - _count = 1; -} - -template -void control_block_impl::free_ptr() noexcept -{ - delete static_cast(_ptr); -} -template -control_block* control_block_impl::create(T* ptr) noexcept -{ - if (ptr) - { - auto* __restrict ret = new control_block_impl{ptr}; - return ret; - } - else - return nullptr; -} +#endif } // namespace floormat::detail_borrowed_ptr namespace floormat { -template bptr::bptr(DirectInitT, T* casted_ptr, detail_borrowed_ptr::control_block* blk) noexcept: - casted_ptr{casted_ptr}, blk{blk} -{} - template template requires std::is_constructible_v @@ -86,13 +47,12 @@ bptr{ new T{ forward(args...) } } { } -template bptr::bptr(std::nullptr_t) noexcept: casted_ptr{nullptr}, blk{nullptr} {} +template bptr::bptr(std::nullptr_t) noexcept: blk{nullptr} {} template bptr::bptr() noexcept: bptr{nullptr} {} template bptr::bptr(T* ptr) noexcept: - casted_ptr{ptr}, - blk{detail_borrowed_ptr::control_block_impl::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::reset() noexcept { if (blk) { - fm_bptr_assert(casted_ptr); blk->decrement(blk); - casted_ptr = nullptr; blk = nullptr; } } template -template void bptr::destroy() noexcept { - if constexpr(MaybeEmpty) - if (!blk) - return; - blk->free_ptr(); + if (!blk) + return; + delete blk->_ptr; blk->_ptr = nullptr; - casted_ptr = nullptr; } template bptr& bptr::operator=(std::nullptr_t) noexcept { reset(); return *this; } @@ -157,7 +112,7 @@ template bptr& bptr::operator=(std::nullptr_t) noexcept { rese template template bptr::bptr(const bptr& other, private_tag_t) noexcept: - casted_ptr{other.casted_ptr}, blk{other.blk} + blk{other.blk} { if (blk) { @@ -175,7 +130,6 @@ bptr& bptr::_copy_assign(const bptr& 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& bptr::_copy_assign(const bptr& other) noexcept template template bptr::bptr(bptr&& 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& bptr::_move_assign(bptr&& 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 T* bptr::get() const noexcept { - if (blk && blk->_ptr) [[likely]] - { - fm_bptr_assert(casted_ptr); - return casted_ptr; - } + if (blk) [[likely]] + return static_cast(blk->_ptr); else return nullptr; } @@ -231,7 +179,6 @@ template void bptr::swap(bptr& other) noexcept { using floormat::swap; - swap(casted_ptr, other.casted_ptr); swap(blk, other.blk); } -- cgit v1.2.3