summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2024-05-04 13:49:20 +0200
committerStanislaw Halik <sthalik@misaki.pl>2024-05-05 03:31:18 +0200
commitbbe2474e248dceffe89fe7f23ad7abce5452fc97 (patch)
treea1ea17f5dbda92501b46c52822d7b10096698006
parent5798acdb8cad0c8af2bf5fdb387234a5932ec17d (diff)
w a
-rw-r--r--compat/borrowed-ptr-fwd.hpp51
-rw-r--r--compat/borrowed-ptr.cpp13
-rw-r--r--compat/borrowed-ptr.hpp48
-rw-r--r--compat/borrowed-ptr.inl11
4 files changed, 70 insertions, 53 deletions
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<typename From, typename To>
+concept StaticCastable = requires(From* ptr) {
+ static_cast<To*>(ptr);
+};
+
+} // namespace floormat::detail_borrowed_ptr
+
+namespace floormat {
+
+template<typename T>
+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<typename... Ts>
+ requires std::is_constructible_v<T, Ts&&...>
+ 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<typename Y> requires std::is_convertible_v<Y*, T*> bptr(const bptr<Y>&) noexcept;
+ template<typename Y> requires std::is_convertible_v<Y*, T*> bptr(bptr<Y>&&) noexcept;
+ template<typename Y> requires std::is_convertible_v<Y*, T*> bptr& operator=(const bptr<Y>&) noexcept;
+ template<typename Y> requires std::is_convertible_v<Y*, T*> bptr& operator=(bptr<Y>&&) 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<typename U>
+ requires detail_borrowed_ptr::StaticCastable<T, U>
+ bptr<U> 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<Foo>;
-template class bptr<Foo>;
-
-} // 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<typename T> class bptr;
-template<typename T> bptr<T> static_pointer_cast(const bptr<T>& p);
-
-template<typename T>
-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<typename... Ts>
- requires std::is_constructible_v<T, Ts&&...>
- 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<typename Y> requires std::is_convertible_v<Y*, T*> bptr(const bptr<Y>&) noexcept;
- template<typename Y> requires std::is_convertible_v<Y*, T*> bptr(bptr<Y>&&) noexcept;
- template<typename Y> requires std::is_convertible_v<Y*, T*> bptr& operator=(const bptr<Y>&) noexcept;
- template<typename Y> requires std::is_convertible_v<Y*, T*> bptr& operator=(bptr<Y>&&) 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<T> static_pointer_cast<T>(const bptr<T>& p);
-};
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
+{
+ return p.template static_pointer_cast<U>();
+}
+
} // 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<T>::~bptr() noexcept
if (blk)
blk->decr();
//blk = reinterpret_cast<T*>(-1);
+ //blk = nullptr;
}
template<typename T>
@@ -138,14 +139,16 @@ bptr<T>& bptr<T>::operator=(bptr<Y>&& other) noexcept
return *this;
}
-template<typename T, typename U>
-bptr<T> static_pointer_cast(const bptr<U>& p)
+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 = p.blk;
+ ret.blk = blk;
if (ret.blk) [[likely]]
ret.blk->incr();
- ret.ptr = static_cast<T*>(p.ptr);
+ ret.ptr = static_cast<T*>(ptr);
return ret;
}