summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2024-05-04 14:37:06 +0200
committerStanislaw Halik <sthalik@misaki.pl>2024-05-05 03:31:19 +0200
commitc1ad7d0ead5132520956c8d6ee6a9c6887d38556 (patch)
tree1272a4555a544b06da5e315336ad102c53667dd7
parentbbe2474e248dceffe89fe7f23ad7abce5452fc97 (diff)
a w
-rw-r--r--compat/borrowed-ptr-fwd.hpp10
-rw-r--r--compat/borrowed-ptr.cpp8
-rw-r--r--compat/borrowed-ptr.hpp16
-rw-r--r--compat/borrowed-ptr.inl21
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<typename... Ts>
requires std::is_constructible_v<T, Ts&&...>
@@ -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<typename U>
- requires detail_borrowed_ptr::StaticCastable<T, U>
- bptr<U> static_pointer_cast() noexcept;
+ template<typename U> friend class bptr;
+ template<typename Tʹ, typename U>
+ friend bptr<U> static_pointer_cast(const bptr<Tʹ>& 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<Foo>;
+template class bptr<Foo>;
+template bptr<Bar> static_pointer_cast(const bptr<Foo>&) 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<typename T> constexpr bptr<T>::bptr(NoInitT) noexcept {};
template<typename T> constexpr bptr<T>::bptr(std::nullptr_t) noexcept: ptr{nullptr}, blk{nullptr} {}
template<typename T> constexpr bptr<T>::bptr() noexcept: bptr{nullptr} {}
-template<typename T> bptr(T* ptr) -> bptr<T>;
-
template<typename T, typename U>
-CORRADE_ALWAYS_INLINE bptr<U> static_pointer_cast(const bptr<T>& p) noexcept
+bptr<U> static_pointer_cast(const bptr<T>& p) noexcept
{
- return p.template static_pointer_cast<U>();
+ static_assert(detail_borrowed_ptr::StaticCastable<T, U>);
+
+ if (auto* blk = p.blk) [[likely]]
+ {
+ auto* ptr = static_cast<U*>(p.ptr);
+ blk->incr();
+ return bptr<U>{DirectInit, ptr, blk};
+ }
+ else
+ return bptr<U>{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<Ts...>(args...) } }
{
}
+template<typename T> constexpr bptr<T>::bptr(NoInitT) noexcept {};
+
+template<typename T>
+constexpr bptr<T>::bptr(DirectInitT, T* ptr, detail_borrowed_ptr::control_block_* blk) noexcept:
+ ptr{ptr}, blk{blk}
+{
+}
+
template<typename T>
bptr<T>::bptr(T* ptr) noexcept:
ptr{ptr},
@@ -139,17 +147,4 @@ bptr<T>& bptr<T>::operator=(bptr<Y>&& other) noexcept
return *this;
}
-template<typename T>
-template<typename U>
-requires detail_borrowed_ptr::StaticCastable<T, U>
-bptr<U> bptr<T>::static_pointer_cast() noexcept
-{
- auto ret = bptr<T>{NoInit};
- ret.blk = blk;
- if (ret.blk) [[likely]]
- ret.blk->incr();
- ret.ptr = static_cast<T*>(ptr);
- return ret;
-}
-
} // namespace floormat