From c1ad7d0ead5132520956c8d6ee6a9c6887d38556 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 4 May 2024 14:37:06 +0200 Subject: a w --- compat/borrowed-ptr-fwd.hpp | 10 ++++++---- compat/borrowed-ptr.cpp | 8 ++++++++ compat/borrowed-ptr.hpp | 16 +++++++++++----- compat/borrowed-ptr.inl | 21 ++++++++------------- 4 files changed, 33 insertions(+), 22 deletions(-) diff --git a/compat/borrowed-ptr-fwd.hpp b/compat/borrowed-ptr-fwd.hpp index b28c6a71..e773a089 100644 --- a/compat/borrowed-ptr-fwd.hpp +++ b/compat/borrowed-ptr-fwd.hpp @@ -19,6 +19,9 @@ class bptr final // NOLINT(*-special-member-functions) T* ptr; // todo add simple_bptr that doesn't allow casting. should only have the control block member variable. detail_borrowed_ptr::control_block_* blk; + explicit constexpr bptr(NoInitT) noexcept; + explicit constexpr bptr(DirectInitT, T* ptr, detail_borrowed_ptr::control_block_* blk) noexcept; + public: template requires std::is_constructible_v @@ -26,7 +29,6 @@ public: constexpr bptr(std::nullptr_t) noexcept; // NOLINT(*-explicit-conversions) constexpr bptr() noexcept; - explicit constexpr bptr(NoInitT) noexcept; explicit bptr(T* ptr) noexcept; ~bptr() noexcept; @@ -43,9 +45,9 @@ public: uint32_t use_count() const noexcept; explicit operator bool() const noexcept; - template - requires detail_borrowed_ptr::StaticCastable - bptr static_pointer_cast() noexcept; + template friend class bptr; + template + friend bptr static_pointer_cast(const bptr& p) noexcept; }; } // namespace floormat diff --git a/compat/borrowed-ptr.cpp b/compat/borrowed-ptr.cpp index 2eac9ad7..67a55ed7 100644 --- a/compat/borrowed-ptr.cpp +++ b/compat/borrowed-ptr.cpp @@ -36,3 +36,11 @@ struct Foo {}; struct Bar : Foo {}; struct Baz {}; } // namespace + +namespace floormat { + +template struct detail_borrowed_ptr::control_block; +template class bptr; +template bptr static_pointer_cast(const bptr&) noexcept; + +} // namespace floormat diff --git a/compat/borrowed-ptr.hpp b/compat/borrowed-ptr.hpp index 5134f279..872902a2 100644 --- a/compat/borrowed-ptr.hpp +++ b/compat/borrowed-ptr.hpp @@ -3,16 +3,22 @@ namespace floormat { -template constexpr bptr::bptr(NoInitT) noexcept {}; template constexpr bptr::bptr(std::nullptr_t) noexcept: ptr{nullptr}, blk{nullptr} {} template constexpr bptr::bptr() noexcept: bptr{nullptr} {} -template bptr(T* ptr) -> bptr; - template -CORRADE_ALWAYS_INLINE bptr static_pointer_cast(const bptr& p) noexcept +bptr static_pointer_cast(const bptr& p) noexcept { - return p.template static_pointer_cast(); + static_assert(detail_borrowed_ptr::StaticCastable); + + if (auto* blk = p.blk) [[likely]] + { + auto* ptr = static_cast(p.ptr); + blk->incr(); + return bptr{DirectInit, ptr, blk}; + } + else + return bptr{nullptr}; } } // namespace floormat diff --git a/compat/borrowed-ptr.inl b/compat/borrowed-ptr.inl index 7cb373ca..2faca999 100644 --- a/compat/borrowed-ptr.inl +++ b/compat/borrowed-ptr.inl @@ -69,6 +69,14 @@ bptr{ new T{ forward(args...) } } { } +template constexpr bptr::bptr(NoInitT) noexcept {}; + +template +constexpr bptr::bptr(DirectInitT, T* ptr, detail_borrowed_ptr::control_block_* blk) noexcept: + ptr{ptr}, blk{blk} +{ +} + template bptr::bptr(T* ptr) noexcept: ptr{ptr}, @@ -139,17 +147,4 @@ bptr& bptr::operator=(bptr&& other) noexcept return *this; } -template -template -requires detail_borrowed_ptr::StaticCastable -bptr bptr::static_pointer_cast() noexcept -{ - auto ret = bptr{NoInit}; - ret.blk = blk; - if (ret.blk) [[likely]] - ret.blk->incr(); - ret.ptr = static_cast(ptr); - return ret; -} - } // namespace floormat -- cgit v1.2.3