From 48ef8ef7c0c1f9976b201a363db72d027ff5ead7 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 15 Jul 2024 11:06:58 +0200 Subject: w --- compat/borrowed-ptr.hpp | 12 +++++- compat/borrowed-ptr.inl | 101 +++++++++++++++++++++++++++++------------------- 2 files changed, 72 insertions(+), 41 deletions(-) diff --git a/compat/borrowed-ptr.hpp b/compat/borrowed-ptr.hpp index d8cff42b..65115f94 100644 --- a/compat/borrowed-ptr.hpp +++ b/compat/borrowed-ptr.hpp @@ -13,6 +13,7 @@ concept StaticCastable = requires(From* from) { template concept DerivedFrom = requires(From* x) { std::is_convertible_v; + std::is_convertible_v; }; } // namespace floormat::detail_bptr @@ -34,21 +35,30 @@ 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&&...> explicit bptr(InPlaceInitT, Ts&&... args) noexcept; - explicit bptr(T* ptr) noexcept requires std::is_convertible_v; + template Y> explicit bptr(Y* ptr) noexcept; bptr() noexcept; ~bptr() noexcept; bptr(std::nullptr_t) noexcept; // NOLINT(*-explicit-conversions) bptr& operator=(std::nullptr_t) noexcept; + bptr(const bptr&) noexcept; + 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; diff --git a/compat/borrowed-ptr.inl b/compat/borrowed-ptr.inl index fa83dace..ccc6ced4 100644 --- a/compat/borrowed-ptr.inl +++ b/compat/borrowed-ptr.inl @@ -12,27 +12,22 @@ #define fm_bptr_assert(...) void() #endif +#if 0 #ifdef __GNUG__ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-function" +#pragma GCC diagnostic ignored "-Wnon-virtual-dtor" +#endif #endif namespace floormat::detail_bptr { -#ifdef __GNUG__ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wnon-virtual-dtor" -#endif struct control_block { bptr_base* _ptr; uint32_t _count; static void decrement(control_block*& blk) noexcept; }; -#ifdef __GNUG__ -#pragma GCC diagnostic pop - -#endif } // namespace floormat::detail_bptr @@ -48,15 +43,57 @@ bptr::bptr(InPlaceInitT, Ts&&... args) noexcept: template bptr::bptr(std::nullptr_t) noexcept: blk{nullptr} {} template bptr::bptr() noexcept: bptr{nullptr} {} -template bptr::bptr(T* ptr) noexcept requires std::is_convertible_v: - blk{ptr ? new detail_bptr::control_block{const_cast*>(ptr), 1} : nullptr} +template +template Y> +bptr::bptr(Y* ptr) noexcept: + blk{ptr ? new detail_bptr::control_block{const_cast*>(ptr), 1} : nullptr} {} template bptr::~bptr() noexcept { if (blk) blk->decrement(blk); } +template bptr::bptr(const bptr& other) noexcept: bptr{other, nullptr} {} +template bptr::bptr(bptr&& other) noexcept: bptr{move(other), nullptr} {} +template bptr& bptr::operator=(const bptr& other) noexcept { return _copy_assign(other); } +template bptr& bptr::operator=(bptr&& other) noexcept { return _move_assign(move(other)); } + template template Y> bptr::bptr(const bptr& other) noexcept: + bptr{other, nullptr} +{} + +template +template Y> +bptr& bptr::operator=(const bptr& other) noexcept +{ return _copy_assign(other); } + +template +template Y> +bptr::bptr(bptr&& other) noexcept: + bptr{move(other), nullptr} +{} + +template +template Y> +bptr& bptr::operator=(bptr&& other) noexcept +{ return _move_assign(move(other)); } + +template void bptr::reset() noexcept { if (blk) blk->decrement(blk); } + +template +void bptr::destroy() noexcept +{ + if (!blk) + return; + delete blk->_ptr; + blk->_ptr = nullptr; +} + +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) @@ -64,8 +101,16 @@ bptr::bptr(const bptr& other) noexcept: } template -template Y> -bptr& bptr::operator=(const bptr& other) noexcept +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) { @@ -79,16 +124,8 @@ bptr& bptr::operator=(const bptr& other) noexcept } template -template Y> -bptr::bptr(bptr&& other) noexcept: - blk{other.blk} -{ - other.blk = nullptr; -} - -template -template Y> -bptr& bptr::operator=(bptr&& other) noexcept +template +bptr& bptr::_move_assign(bptr&& other) noexcept { if (blk) blk->decrement(blk); @@ -97,24 +134,6 @@ bptr& bptr::operator=(bptr&& other) noexcept return *this; } -template -void bptr::reset() noexcept -{ - if (blk) - blk->decrement(blk); -} - -template -void bptr::destroy() noexcept -{ - if (!blk) - return; - delete blk->_ptr; - blk->_ptr = nullptr; -} - -template bptr& bptr::operator=(std::nullptr_t) noexcept { reset(); return *this; } - template T* bptr::get() const noexcept { @@ -150,6 +169,8 @@ uint32_t bptr::use_count() const noexcept } // namespace floormat +#if 0 #ifdef __GNUG__ #pragma GCC diagnostic pop #endif +#endif -- cgit v1.2.3