From bbe2474e248dceffe89fe7f23ad7abce5452fc97 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 4 May 2024 13:49:20 +0200 Subject: w a --- compat/borrowed-ptr-fwd.hpp | 51 +++++++++++++++++++++++++++++++++++++++++++++ compat/borrowed-ptr.cpp | 13 +++++------- compat/borrowed-ptr.hpp | 48 +++++++----------------------------------- compat/borrowed-ptr.inl | 11 ++++++---- 4 files changed, 70 insertions(+), 53 deletions(-) create mode 100644 compat/borrowed-ptr-fwd.hpp diff --git a/compat/borrowed-ptr-fwd.hpp b/compat/borrowed-ptr-fwd.hpp new file mode 100644 index 00000000..b28c6a71 --- /dev/null +++ b/compat/borrowed-ptr-fwd.hpp @@ -0,0 +1,51 @@ +#pragma once + +namespace floormat::detail_borrowed_ptr { + +struct control_block_; + +template +concept StaticCastable = requires(From* ptr) { + static_cast(ptr); +}; + +} // namespace floormat::detail_borrowed_ptr + +namespace floormat { + +template +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; + +public: + template + requires std::is_constructible_v + explicit bptr(InPlaceInitT, Ts&&... args) noexcept; + + constexpr bptr(std::nullptr_t) noexcept; // NOLINT(*-explicit-conversions) + constexpr bptr() noexcept; + explicit constexpr bptr(NoInitT) noexcept; + explicit bptr(T* ptr) noexcept; + ~bptr() noexcept; + + template requires std::is_convertible_v bptr(const bptr&) noexcept; + template requires std::is_convertible_v bptr(bptr&&) noexcept; + template requires std::is_convertible_v bptr& operator=(const bptr&) noexcept; + template requires std::is_convertible_v bptr& operator=(bptr&&) noexcept; + + void swap() noexcept; + T* get() noexcept; + const T* get() const noexcept; + T& operator*() const noexcept; + T* operator->() const noexcept; + uint32_t use_count() const noexcept; + explicit operator bool() const noexcept; + + template + requires detail_borrowed_ptr::StaticCastable + bptr static_pointer_cast() noexcept; +}; + +} // namespace floormat diff --git a/compat/borrowed-ptr.cpp b/compat/borrowed-ptr.cpp index 8c05b751..2eac9ad7 100644 --- a/compat/borrowed-ptr.cpp +++ b/compat/borrowed-ptr.cpp @@ -31,11 +31,8 @@ uint32_t control_block_::count() const noexcept { return _count; } } // namespace floormat::detail_borrowed_ptr -namespace floormat { - -namespace { struct Foo {}; } - -template struct detail_borrowed_ptr::control_block; -template class bptr; - -} // namespace floormat +namespace { +struct Foo {}; +struct Bar : Foo {}; +struct Baz {}; +} // namespace diff --git a/compat/borrowed-ptr.hpp b/compat/borrowed-ptr.hpp index 10b95be9..5134f279 100644 --- a/compat/borrowed-ptr.hpp +++ b/compat/borrowed-ptr.hpp @@ -1,52 +1,18 @@ #pragma once - -namespace floormat::detail_borrowed_ptr { -struct control_block_; -} // namespace floormat::detail_borrowed_ptr +#include "borrowed-ptr-fwd.hpp" namespace floormat { -template class bptr; -template bptr static_pointer_cast(const bptr& p); - -template -class bptr final // NOLINT(*-special-member-functions) -{ - T* ptr; // todo add simple_bptr that doesn't allow casting. should only have the control block element. - detail_borrowed_ptr::control_block_* blk; - - constexpr bptr(NoInitT) noexcept; - -public: - template - requires std::is_constructible_v - explicit bptr(InPlaceInitT, Ts&&... args) noexcept; - - constexpr bptr(std::nullptr_t) noexcept; // NOLINT(*-explicit-conversions) - constexpr bptr() noexcept; - explicit bptr(T* ptr) noexcept; - ~bptr() noexcept; - - template requires std::is_convertible_v bptr(const bptr&) noexcept; - template requires std::is_convertible_v bptr(bptr&&) noexcept; - template requires std::is_convertible_v bptr& operator=(const bptr&) noexcept; - template requires std::is_convertible_v bptr& operator=(bptr&&) noexcept; - - void swap() noexcept; - T* get() noexcept; - const T* get() const noexcept; - T& operator*() const noexcept; - T* operator->() const noexcept; - uint32_t use_count() const noexcept; - explicit operator bool() const noexcept; - - friend bptr static_pointer_cast(const bptr& p); -}; 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 +{ + return p.template static_pointer_cast(); +} + } // namespace floormat diff --git a/compat/borrowed-ptr.inl b/compat/borrowed-ptr.inl index 7302c857..7cb373ca 100644 --- a/compat/borrowed-ptr.inl +++ b/compat/borrowed-ptr.inl @@ -82,6 +82,7 @@ bptr::~bptr() noexcept if (blk) blk->decr(); //blk = reinterpret_cast(-1); + //blk = nullptr; } template @@ -138,14 +139,16 @@ bptr& bptr::operator=(bptr&& other) noexcept return *this; } -template -bptr static_pointer_cast(const bptr& p) +template +template +requires detail_borrowed_ptr::StaticCastable +bptr bptr::static_pointer_cast() noexcept { auto ret = bptr{NoInit}; - ret.blk = p.blk; + ret.blk = blk; if (ret.blk) [[likely]] ret.blk->incr(); - ret.ptr = static_cast(p.ptr); + ret.ptr = static_cast(ptr); return ret; } -- cgit v1.2.3