From ec67a7720a236918188b384c95631c4a5f1abb3c Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 14 Jul 2024 13:03:39 +0200 Subject: w --- compat/borrowed-ptr-cast.hpp | 2 +- compat/borrowed-ptr-fwd.hpp | 4 +--- compat/borrowed-ptr.hpp | 24 +++++++++++----------- compat/borrowed-ptr.inl | 48 ++++++++++++++++++++++++++------------------ test/bptr.cpp | 12 +++++++---- 5 files changed, 51 insertions(+), 39 deletions(-) diff --git a/compat/borrowed-ptr-cast.hpp b/compat/borrowed-ptr-cast.hpp index f202af30..18f0a50e 100644 --- a/compat/borrowed-ptr-cast.hpp +++ b/compat/borrowed-ptr-cast.hpp @@ -22,7 +22,7 @@ bptr static_pointer_cast(const bptr& p) noexcept if constexpr (detail_borrowed_ptr::StaticCastable) { if (p.blk && p.blk->_ptr) [[likely]] - return bptr{p, bptr::private_tag}; + return bptr{p, nullptr}; } else { diff --git a/compat/borrowed-ptr-fwd.hpp b/compat/borrowed-ptr-fwd.hpp index 3bb509d9..8386decc 100644 --- a/compat/borrowed-ptr-fwd.hpp +++ b/compat/borrowed-ptr-fwd.hpp @@ -2,10 +2,8 @@ namespace floormat { template class bptr; - -template bool operator==(const bptr& a, const bptr& b) noexcept; template bptr static_pointer_cast(const bptr& p) noexcept; - template bptr(T* ptr) -> bptr; +template bptr(const T* ptr) -> bptr; } // namespace floormat diff --git a/compat/borrowed-ptr.hpp b/compat/borrowed-ptr.hpp index 31f83c16..affad912 100644 --- a/compat/borrowed-ptr.hpp +++ b/compat/borrowed-ptr.hpp @@ -29,21 +29,18 @@ struct bptr_base template class bptr final // NOLINT(*-special-member-functions) { - static_assert(std::is_convertible_v); - detail_borrowed_ptr::control_block* blk; - struct private_tag_t final {}; - static constexpr private_tag_t private_tag{}; + bptr(detail_borrowed_ptr::control_block* blk) noexcept; - template bptr(const bptr& other, private_tag_t) noexcept; + template bptr(const bptr& other, std::nullptr_t) noexcept; template bptr& _copy_assign(const bptr& other) noexcept; - template bptr(bptr&& other, private_tag_t) noexcept; + template bptr(bptr&& other, std::nullptr_t) noexcept; template bptr& _move_assign(bptr&& other) noexcept; public: template - requires std::is_constructible_v + requires std::is_constructible_v, Ts&&...> explicit bptr(InPlaceInitT, Ts&&... args) noexcept; explicit bptr(T* ptr) noexcept; @@ -55,13 +52,15 @@ public: bptr(const bptr&) noexcept; bptr& operator=(const bptr&) noexcept; - template Y> bptr(const bptr&) noexcept; - template Y> bptr& operator=(const bptr&) noexcept; + template> Y> bptr(const bptr&) noexcept; + template> Y> bptr& operator=(const bptr&) noexcept; bptr(bptr&&) noexcept; bptr& operator=(bptr&&) noexcept; - template Y> bptr(bptr&&) noexcept; - template Y> bptr& operator=(bptr&&) noexcept; + template> Y> bptr(bptr&&) noexcept; + template> Y> bptr& operator=(bptr&&) noexcept; + + operator bptr() const noexcept requires (!std::is_const_v); void reset() noexcept; void destroy() noexcept; @@ -73,7 +72,8 @@ public: T& operator*() const noexcept; explicit operator bool() const noexcept; - friend bool operator==(const bptr& a, const bptr& b) noexcept; + bool operator==(const bptr>& other) const noexcept; + bool operator==(const bptr>& other) const noexcept; template friend class bptr; template friend bptr static_pointer_cast(const bptr& p) noexcept; diff --git a/compat/borrowed-ptr.inl b/compat/borrowed-ptr.inl index 390593bf..acdcb0df 100644 --- a/compat/borrowed-ptr.inl +++ b/compat/borrowed-ptr.inl @@ -41,7 +41,7 @@ namespace floormat { template template -requires std::is_constructible_v +requires std::is_constructible_v, Ts&&...> bptr::bptr(InPlaceInitT, Ts&&... args) noexcept: bptr{ new T{ forward(args...) } } { @@ -50,12 +50,7 @@ bptr{ new T{ forward(args...) } } template bptr::bptr(std::nullptr_t) noexcept: blk{nullptr} {} template bptr::bptr() noexcept: bptr{nullptr} {} -template -bptr::bptr(T* ptr) noexcept: - blk{ptr ? new detail_borrowed_ptr::control_block{ptr, 1} : nullptr} -{ - fm_bptr_assert(!blk || blk->_count == 1 && blk->_ptr); -} +template bptr::bptr(T* ptr) noexcept: blk{ptr ? new detail_borrowed_ptr::control_block{ptr, 1} : nullptr} {} template bptr::~bptr() noexcept @@ -64,30 +59,40 @@ bptr::~bptr() noexcept blk->decrement(blk); } -template bptr::bptr(const bptr& other) noexcept: bptr{other, private_tag} {} +template bptr::bptr(const bptr& other) noexcept: bptr{other, nullptr} {} template bptr& bptr::operator=(const bptr& other) noexcept { return _copy_assign(other); } template -template Y> -bptr::bptr(const bptr& other) noexcept: bptr{other, private_tag} {} +template> Y> +bptr::bptr(const bptr& other) noexcept: bptr{other, nullptr} {} template -template Y> +template> Y> bptr& bptr::operator=(const bptr& other) noexcept { return _copy_assign(other); } template bptr& bptr::operator=(bptr&& other) noexcept { return _move_assign(move(other)); } -template bptr::bptr(bptr&& other) noexcept: bptr{move(other), private_tag} {} +template bptr::bptr(bptr&& other) noexcept: bptr{move(other), nullptr} {} template -template Y> -bptr::bptr(bptr&& other) noexcept: bptr{move(other), private_tag} {} +template> Y> +bptr::bptr(bptr&& other) noexcept: bptr{move(other), nullptr} {} template -template Y> +template> Y> bptr& bptr::operator=(bptr&& other) noexcept { return _move_assign(move(other)); } +template +bptr::operator bptr() const noexcept requires (!std::is_const_v) { + if (blk && blk->_ptr) + { + ++blk->_count; + return bptr{}; + } + return bptr{nullptr}; +} + template void bptr::reset() noexcept { @@ -109,9 +114,12 @@ void bptr::destroy() noexcept template bptr& bptr::operator=(std::nullptr_t) noexcept { reset(); return *this; } +template +bptr::bptr(detail_borrowed_ptr::control_block* blk) noexcept: blk{blk} { } + template template -bptr::bptr(const bptr& other, private_tag_t) noexcept: +bptr::bptr(const bptr& other, std::nullptr_t) noexcept: blk{other.blk} { if (blk) @@ -127,7 +135,7 @@ bptr& bptr::_copy_assign(const bptr& other) noexcept { if (blk != other.blk) { - CORRADE_ASSUME(this != &other); // todo! see if helps + CORRADE_ASSUME(this != &other); // todo see if helps if (blk) blk->decrement(blk); blk = other.blk; @@ -139,7 +147,7 @@ bptr& bptr::_copy_assign(const bptr& other) noexcept template template -bptr::bptr(bptr&& other, private_tag_t) noexcept: +bptr::bptr(bptr&& other, std::nullptr_t) noexcept: blk{other.blk} { other.blk = nullptr; @@ -172,8 +180,10 @@ template T* bptr::operator->() const noexcept } template T& bptr::operator*() const noexcept { return *operator->(); } -template bool operator==(const bptr& a, const bptr& b) noexcept { return a.get() == b.get(); } + template bptr::operator bool() const noexcept { return get(); } +template bool bptr::operator==(const bptr>& other) const noexcept { return get() == other.get(); } +template bool bptr::operator==(const bptr>& other) const noexcept { return get() == other.get(); } template void bptr::swap(bptr& other) noexcept diff --git a/test/bptr.cpp b/test/bptr.cpp index ee254b68..3a62de53 100644 --- a/test/bptr.cpp +++ b/test/bptr.cpp @@ -15,10 +15,6 @@ struct Baz : bptr_base {}; // NOLINTBEGIN(*-use-anonymous-namespace) -template bool operator==(const bptr&, const bptr&) noexcept; -template bool operator==(const bptr&, const bptr&) noexcept; -template bool operator==(const bptr&, const bptr&) noexcept; - template class bptr; template class bptr; template class bptr; @@ -333,6 +329,13 @@ void test9() fm_assert(A_total == 1 && A_alive == 0); }; +void test10() +{ + fm_assert(bptr{} == bptr{}); + fm_assert(bptr{} == bptr{}); + fm_assert(bptr{} == bptr{}); +} + } // namespace void Test::test_bptr() @@ -346,6 +349,7 @@ void Test::test_bptr() test7(); test8(); test9(); + test10(); } } // namespace floormat -- cgit v1.2.3