From 6f0a7b3478d4275614a248467a70f1700624ebe1 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 15 Jul 2024 10:17:58 +0200 Subject: w --- compat/borrowed-ptr.hpp | 8 ++--- compat/borrowed-ptr.inl | 79 ++++++++++++++++++------------------------------- 2 files changed, 31 insertions(+), 56 deletions(-) diff --git a/compat/borrowed-ptr.hpp b/compat/borrowed-ptr.hpp index 28d35b8f..d8cff42b 100644 --- a/compat/borrowed-ptr.hpp +++ b/compat/borrowed-ptr.hpp @@ -34,11 +34,6 @@ class bptr final // NOLINT(*-special-member-functions) { detail_bptr::control_block* blk; - template bptr(const bptr& other, std::nullptr_t) noexcept; - template bptr(bptr&& other, std::nullptr_t) noexcept; - template bptr& _copy_assign(const bptr& other) noexcept; - template bptr& _move_assign(bptr&& other) noexcept; - public: template requires std::is_constructible_v, Ts&&...> @@ -69,6 +64,7 @@ public: explicit operator bool() const noexcept; bool operator==(const bptr>& other) const noexcept; bool operator==(const bptr>& other) const noexcept; + bool operator==(const std::nullptr_t& other) const noexcept; template friend class bptr; @@ -82,7 +78,7 @@ requires detail_bptr::StaticCastable bptr static_pointer_cast(const bptr& p) noexcept { if (p.blk && p.blk->_ptr) [[likely]] - return bptr{p, nullptr}; + return bptr{p}; return bptr{nullptr}; } diff --git a/compat/borrowed-ptr.inl b/compat/borrowed-ptr.inl index 7260d7ed..fa83dace 100644 --- a/compat/borrowed-ptr.inl +++ b/compat/borrowed-ptr.inl @@ -57,24 +57,45 @@ template bptr::~bptr() noexcept { if (blk) blk->decrement(blk); } template template Y> bptr::bptr(const bptr& other) noexcept: - bptr{other, nullptr} -{} + blk{other.blk} +{ + if (blk) + ++blk->_count; +} template template Y> bptr& bptr::operator=(const bptr& other) noexcept -{ return _copy_assign(other); } +{ + if (blk != other.blk) + { + if (blk) + blk->decrement(blk); + blk = other.blk; + if (blk) + ++blk->_count; + } + return *this; +} template template Y> bptr::bptr(bptr&& other) noexcept: - bptr{move(other), nullptr} -{} + blk{other.blk} +{ + other.blk = nullptr; +} template template Y> bptr& bptr::operator=(bptr&& other) noexcept -{ return _move_assign(move(other)); } +{ + if (blk) + blk->decrement(blk); + blk = other.blk; + other.blk = nullptr; + return *this; +} template void bptr::reset() noexcept @@ -94,49 +115,6 @@ void bptr::destroy() noexcept template bptr& bptr::operator=(std::nullptr_t) noexcept { reset(); return *this; } -template -template -bptr::bptr(const bptr& other, std::nullptr_t) noexcept: - blk{other.blk} -{ - if (blk) - ++blk->_count; -} - -template -template -bptr::bptr(bptr&& other, std::nullptr_t) noexcept: - blk{other.blk} -{ - other.blk = nullptr; -} - -template -template -bptr& bptr::_copy_assign(const bptr& other) noexcept -{ - if (blk != other.blk) - { - if (blk) - blk->decrement(blk); - blk = other.blk; - if (blk) - ++blk->_count; - } - return *this; -} - -template -template -bptr& bptr::_move_assign(bptr&& other) noexcept -{ - if (blk) - blk->decrement(blk); - blk = other.blk; - other.blk = nullptr; - return *this; -} - template T* bptr::get() const noexcept { @@ -155,9 +133,10 @@ T* bptr::operator->() const noexcept } template T& bptr::operator*() const noexcept { return *operator->(); } -template bptr::operator bool() const noexcept { return get(); } +template bptr::operator bool() const noexcept { return blk && blk->_ptr; } 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 bool bptr::operator==(const std::nullptr_t&) const noexcept { return !blk || !blk->_ptr; } template void bptr::swap(bptr& other) noexcept { floormat::swap(blk, other.blk); } template -- cgit v1.2.3