diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-07-15 10:17:58 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-07-15 10:17:58 +0200 |
commit | 6f0a7b3478d4275614a248467a70f1700624ebe1 (patch) | |
tree | 662c6f50760d28653f93ecc955863459a80605c3 | |
parent | 0c0fe15bdd5fb7f137c6807ba43336f24f6df35b (diff) |
w
-rw-r--r-- | compat/borrowed-ptr.hpp | 8 | ||||
-rw-r--r-- | 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<typename Y> bptr(const bptr<Y>& other, std::nullptr_t) noexcept; - template<typename Y> bptr(bptr<Y>&& other, std::nullptr_t) noexcept; - template<typename Y> bptr& _copy_assign(const bptr<Y>& other) noexcept; - template<typename Y> bptr& _move_assign(bptr<Y>&& other) noexcept; - public: template<typename... Ts> requires std::is_constructible_v<std::remove_const_t<T>, Ts&&...> @@ -69,6 +64,7 @@ public: explicit operator bool() const noexcept; bool operator==(const bptr<const std::remove_const_t<T>>& other) const noexcept; bool operator==(const bptr<std::remove_const_t<T>>& other) const noexcept; + bool operator==(const std::nullptr_t& other) const noexcept; template<typename U> friend class bptr; @@ -82,7 +78,7 @@ requires detail_bptr::StaticCastable<From, To> bptr<To> static_pointer_cast(const bptr<From>& p) noexcept { if (p.blk && p.blk->_ptr) [[likely]] - return bptr<To>{p, nullptr}; + return bptr<To>{p}; return bptr<To>{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<typename T> bptr<T>::~bptr() noexcept { if (blk) blk->decrement(blk); } template<typename T> template<detail_bptr::DerivedFrom<T> Y> bptr<T>::bptr(const bptr<Y>& other) noexcept: - bptr{other, nullptr} -{} + blk{other.blk} +{ + if (blk) + ++blk->_count; +} template<typename T> template<detail_bptr::DerivedFrom<T> Y> bptr<T>& bptr<T>::operator=(const bptr<Y>& 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<typename T> template<detail_bptr::DerivedFrom<T> Y> bptr<T>::bptr(bptr<Y>&& other) noexcept: - bptr{move(other), nullptr} -{} + blk{other.blk} +{ + other.blk = nullptr; +} template<typename T> template<detail_bptr::DerivedFrom<T> Y> bptr<T>& bptr<T>::operator=(bptr<Y>&& other) noexcept -{ return _move_assign(move(other)); } +{ + if (blk) + blk->decrement(blk); + blk = other.blk; + other.blk = nullptr; + return *this; +} template<typename T> void bptr<T>::reset() noexcept @@ -95,49 +116,6 @@ void bptr<T>::destroy() noexcept template<typename T> bptr<T>& bptr<T>::operator=(std::nullptr_t) noexcept { reset(); return *this; } template<typename T> -template<typename Y> -bptr<T>::bptr(const bptr<Y>& other, std::nullptr_t) noexcept: - blk{other.blk} -{ - if (blk) - ++blk->_count; -} - -template<typename T> -template<typename Y> -bptr<T>::bptr(bptr<Y>&& other, std::nullptr_t) noexcept: - blk{other.blk} -{ - other.blk = nullptr; -} - -template<typename T> -template<typename Y> -bptr<T>& bptr<T>::_copy_assign(const bptr<Y>& other) noexcept -{ - if (blk != other.blk) - { - if (blk) - blk->decrement(blk); - blk = other.blk; - if (blk) - ++blk->_count; - } - return *this; -} - -template<typename T> -template<typename Y> -bptr<T>& bptr<T>::_move_assign(bptr<Y>&& other) noexcept -{ - if (blk) - blk->decrement(blk); - blk = other.blk; - other.blk = nullptr; - return *this; -} - -template<typename T> T* bptr<T>::get() const noexcept { if (blk) [[likely]] @@ -155,9 +133,10 @@ T* bptr<T>::operator->() const noexcept } template<typename T> T& bptr<T>::operator*() const noexcept { return *operator->(); } -template<typename T> bptr<T>::operator bool() const noexcept { return get(); } +template<typename T> bptr<T>::operator bool() const noexcept { return blk && blk->_ptr; } template<typename T> bool bptr<T>::operator==(const bptr<const std::remove_const_t<T>>& other) const noexcept { return get() == other.get(); } template<typename T> bool bptr<T>::operator==(const bptr<std::remove_const_t<T>>& other) const noexcept { return get() == other.get(); } +template<typename T> bool bptr<T>::operator==(const std::nullptr_t&) const noexcept { return !blk || !blk->_ptr; } template<typename T> void bptr<T>::swap(bptr& other) noexcept { floormat::swap(blk, other.blk); } template<typename T> |