#pragma once #include "borrowed-ptr-fwd.hpp" #include #define FM_BPTR_DEBUG #define FM_NO_WEAK_BPTR #ifdef __CLION_IDE__ #define fm_bptr_assert(...) (void(__VA_ARGS__)) #elif defined FM_BPTR_DEBUG && !defined FM_NO_DEBUG #define fm_bptr_assert(...) fm_assert(__VA_ARGS__) #else #define fm_bptr_assert(...) void() #endif namespace floormat { struct bptr_base { virtual ~bptr_base() noexcept; bptr_base() noexcept; bptr_base(const bptr_base&) noexcept; bptr_base(bptr_base&&) noexcept; bptr_base& operator=(const bptr_base&) noexcept; bptr_base& operator=(bptr_base&&) noexcept; }; } // namespace floormat namespace floormat::detail_bptr { struct control_block final { bptr_base* _ptr; #ifndef FM_NO_WEAK_BPTR uint32_t _soft_count; #endif uint32_t _hard_count; static void decrement(control_block*& blk) noexcept; #ifndef FM_NO_WEAK_BPTR static void weak_decrement(control_block*& blk) noexcept; #endif }; template concept StaticCastable = requires(From* from, To* to) { static_cast(from); static_cast(from); static_cast(to); }; template concept DerivedFrom = requires(From* from, To* to) { requires std::is_convertible_v; }; } // namespace floormat::detail_bptr namespace floormat { template requires detail_bptr::StaticCastable bptr static_pointer_cast(bptr&& p) noexcept; template requires detail_bptr::StaticCastable bptr static_pointer_cast(const bptr& p) noexcept; template 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&&...> CORRADE_ALWAYS_INLINE explicit bptr(InPlaceInitT, Ts&&... args) noexcept; CORRADE_ALWAYS_INLINE explicit bptr(T* ptr) noexcept; CORRADE_ALWAYS_INLINE bptr() noexcept; CORRADE_ALWAYS_INLINE bptr(std::nullptr_t) noexcept; // NOLINT(*-explicit-conversions) ~bptr() noexcept; bptr& operator=(std::nullptr_t) noexcept; bptr(const bptr>& ptr) noexcept requires std::is_const_v; bptr(bptr>&& ptr) noexcept requires std::is_const_v; 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; void reset() noexcept; template Y> void reset(Y* ptr) noexcept; void destroy() noexcept; void swap(bptr& other) noexcept; uint32_t use_count() const noexcept; T* get() const noexcept; T* operator->() const noexcept; T& operator*() const noexcept; explicit operator bool() const noexcept; bool operator==(const bptr& other) const noexcept; bool operator==(const bptr& other) const noexcept requires (!std::is_const_v); bool operator==(const std::nullptr_t& other) const noexcept; std::strong_ordering operator<=>(const bptr& other) const noexcept; std::strong_ordering operator<=>(const bptr& other) const noexcept requires (!std::is_const_v); std::strong_ordering operator<=>(const std::nullptr_t&) const noexcept; template friend class bptr; template friend class weak_bptr; template requires detail_bptr::StaticCastable friend bptr static_pointer_cast(bptr&& p) noexcept; template requires detail_bptr::StaticCastable friend bptr static_pointer_cast(const bptr& p) noexcept; }; #ifdef __GNUG__ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-function" #endif template template //requires std::is_constructible_v, Ts&&...> bptr::bptr(InPlaceInitT, Ts&&... args) noexcept: bptr{ new std::remove_const_t{ forward(args)... } } {} template bptr::bptr(std::nullptr_t) noexcept: blk{nullptr} {} template bptr::bptr() noexcept: bptr{nullptr} {} template bptr::bptr(T* ptr) noexcept: blk{ptr ? new detail_bptr::control_block{const_cast*>(ptr), 1, #ifndef FM_NO_WEAK_BPTR 1, #endif } : nullptr} {} #ifdef __GNUG__ #pragma GCC diagnostic pop #endif } // namespace floormat