From d71c3ca30ea79d6a54446be8c5a9da169d7ccd1e Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Tue, 16 Jul 2024 18:56:36 +0200 Subject: w --- compat/borrowed-ptr.cpp | 8 +++++++- compat/borrowed-ptr.hpp | 30 ++++++++++++++++++++++++++++-- compat/borrowed-ptr.inl | 25 +++++++++---------------- compat/weak-borrowed-ptr.hpp | 4 ++++ 4 files changed, 48 insertions(+), 19 deletions(-) diff --git a/compat/borrowed-ptr.cpp b/compat/borrowed-ptr.cpp index 3048e48a..1dded12a 100644 --- a/compat/borrowed-ptr.cpp +++ b/compat/borrowed-ptr.cpp @@ -10,8 +10,11 @@ void control_block::decrement(control_block*& blk) noexcept { delete blk->_ptr; blk->_ptr = nullptr; +#ifdef FM_NO_WEAK_BPTR + delete blk; +#endif } - +#ifndef FM_NO_WEAK_BPTR auto c = --blk->_soft_count; fm_bptr_assert(c != (uint32_t)-1); if (c == 0) @@ -19,9 +22,11 @@ void control_block::decrement(control_block*& blk) noexcept fm_bptr_assert(!blk->_ptr); delete blk; } +#endif blk = nullptr; } +#ifndef FM_NO_WEAK_BPTR void control_block::weak_decrement(control_block*& blk) noexcept { if (!blk) @@ -36,6 +41,7 @@ void control_block::weak_decrement(control_block*& blk) noexcept } blk = nullptr; } +#endif } // namespace floormat::detail_bptr diff --git a/compat/borrowed-ptr.hpp b/compat/borrowed-ptr.hpp index 741b3e53..9ba28fc1 100644 --- a/compat/borrowed-ptr.hpp +++ b/compat/borrowed-ptr.hpp @@ -3,6 +3,7 @@ #include #define FM_BPTR_DEBUG +#define FM_NO_WEAK_BPTR #ifdef __CLION_IDE__ #define fm_bptr_assert(...) (void(__VA_ARGS__)) @@ -29,9 +30,14 @@ namespace floormat::detail_bptr { struct control_block final { bptr_base* _ptr; - uint32_t _soft_count, _hard_count; +#ifndef FM_NO_WEAK_BPTR + uint32_t _soft_count; +#endif + uint32_t _hard_count; static void decrement(control_block*& blk) noexcept; +#ifndef FM_NO_WEAK_BPTR static void weak_decrement(control_block*& blk) noexcept; +#endif }; template @@ -65,7 +71,7 @@ public: //requires std::is_constructible_v, Ts&&...> explicit bptr(InPlaceInitT, Ts&&... args) noexcept; - template Y> explicit bptr(Y* ptr) noexcept; + explicit bptr(T* ptr) noexcept; bptr() noexcept; ~bptr() noexcept; @@ -119,6 +125,24 @@ public: template bptr::bptr(std::nullptr_t) noexcept: blk{nullptr} {} +template +template +//requires std::is_constructible_v, Ts&&...> +bptr::bptr(InPlaceInitT, Ts&&... args) noexcept: + bptr{ new std::remove_const_t{ forward(args)... } } +{} + +template bptr::bptr() noexcept: bptr{nullptr} {} + +template +bptr::bptr(T* ptr) noexcept: + blk{ptr ? new detail_bptr::control_block{const_cast*>(ptr), 1, +#ifndef FM_NO_WEAK_BPTR + 1, +#endif + } : nullptr} +{} + template requires detail_bptr::StaticCastable bptr static_pointer_cast(bptr&& p) noexcept @@ -140,7 +164,9 @@ bptr static_pointer_cast(const bptr& p) noexcept if (p.blk && p.blk->_ptr) [[likely]] { bptr ret{nullptr}; +#ifndef FM_NO_WEAK_BPTR ++p.blk->_soft_count; +#endif ++p.blk->_hard_count; ret.blk = p.blk; return ret; diff --git a/compat/borrowed-ptr.inl b/compat/borrowed-ptr.inl index b31326e7..79dd5e51 100644 --- a/compat/borrowed-ptr.inl +++ b/compat/borrowed-ptr.inl @@ -9,21 +9,6 @@ namespace floormat { -template -template -//requires std::is_constructible_v, Ts&&...> -bptr::bptr(InPlaceInitT, Ts&&... args) noexcept: - bptr{ new std::remove_const_t{ forward(args)... } } -{} - -template bptr::bptr() noexcept: bptr{nullptr} {} - -template -template Y> -bptr::bptr(Y* ptr) noexcept: - blk{ptr ? new detail_bptr::control_block{const_cast*>(ptr), 1, 1} : nullptr} -{} - template bptr::~bptr() noexcept { if (blk) detail_bptr::control_block::decrement(blk); } template bptr::bptr(const bptr>& ptr) noexcept requires std::is_const_v: bptr{ptr, nullptr} {} @@ -64,7 +49,11 @@ void bptr::reset(Y* ptr) noexcept { if (blk) detail_bptr::control_block::decrement(blk); - blk = ptr ? new detail_bptr::control_block{const_cast*>(ptr), 1, 1} : nullptr; + blk = ptr ? new detail_bptr::control_block{const_cast*>(ptr), 1, +#ifndef FM_NO_WEAK_BPTR + 1, +#endif + } : nullptr; } template @@ -85,7 +74,9 @@ bptr::bptr(const bptr& other, std::nullptr_t) noexcept: { if (blk) { +#ifndef FM_NO_WEAK_BPTR ++blk->_soft_count; +#endif ++blk->_hard_count; } } @@ -109,7 +100,9 @@ bptr& bptr::_copy_assign(const bptr& other) noexcept blk = other.blk; if (blk) { +#ifndef FM_NO_WEAK_BPTR ++blk->_soft_count; +#endif ++blk->_hard_count; } } diff --git a/compat/weak-borrowed-ptr.hpp b/compat/weak-borrowed-ptr.hpp index ac512c5c..e9ceceaf 100644 --- a/compat/weak-borrowed-ptr.hpp +++ b/compat/weak-borrowed-ptr.hpp @@ -1,6 +1,10 @@ #pragma once #include "borrowed-ptr.hpp" +#ifdef FM_NO_WEAK_BPTR +#error weak_bptr not supported! +#endif + namespace floormat { template -- cgit v1.2.3