diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-07-14 10:48:42 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-07-14 13:04:43 +0200 |
commit | 5fe8eec392fa0568a03ad566a5b41c3b1df7a465 (patch) | |
tree | 828560d738492fbf7c58b9758e0858d54964c5c6 | |
parent | b1143255469c992a6d8475a070af951b7d09c51d (diff) |
w
-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 | ||||
-rw-r--r-- | test/app.cpp | 1 | ||||
-rw-r--r-- | test/app.hpp | 1 | ||||
-rw-r--r-- | test/bptr.cpp | 10 |
7 files changed, 42 insertions, 85 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); } diff --git a/test/app.cpp b/test/app.cpp index 47b019c7..6ace3ca5 100644 --- a/test/app.cpp +++ b/test/app.cpp @@ -62,7 +62,6 @@ int App::exec() FM_TEST(test_astar_pool), FM_TEST(test_coords), FM_TEST(test_hole), - FM_TEST(test_handle), FM_TEST(test_bptr), FM_TEST(test_iptr), FM_TEST(test_entity), diff --git a/test/app.hpp b/test/app.hpp index d32b9759..b1995e77 100644 --- a/test/app.hpp +++ b/test/app.hpp @@ -23,7 +23,6 @@ void test_coords(); void test_critter(); void test_dijkstra(); void test_entity(); -void test_handle(); void test_hash(); void test_hole(); void test_iptr(); diff --git a/test/bptr.cpp b/test/bptr.cpp index 303bf027..ee254b68 100644 --- a/test/bptr.cpp +++ b/test/bptr.cpp @@ -8,17 +8,13 @@ namespace floormat { namespace { -struct Foo {}; +struct Foo : bptr_base {}; struct Bar : Foo {}; -struct Baz {}; +struct Baz : bptr_base {}; } // namespace // NOLINTBEGIN(*-use-anonymous-namespace) -template struct detail_borrowed_ptr::control_block_impl<Foo>; -template struct detail_borrowed_ptr::control_block_impl<Bar>; -template struct detail_borrowed_ptr::control_block_impl<Baz>; - template bool operator==(const bptr<Foo>&, const bptr<Foo>&) noexcept; template bool operator==(const bptr<Bar>&, const bptr<Bar>&) noexcept; template bool operator==(const bptr<Baz>&, const bptr<Baz>&) noexcept; @@ -44,7 +40,7 @@ namespace { int A_total = 0, A_alive = 0; // NOLINT -struct A +struct A : bptr_base { int val, serial; explicit A(int val) : val{val}, serial{++A_total} { ++A_alive; } |