summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2024-07-16 18:56:36 +0200
committerStanislaw Halik <sthalik@misaki.pl>2024-07-16 18:56:36 +0200
commitd71c3ca30ea79d6a54446be8c5a9da169d7ccd1e (patch)
tree0fb292bea536aa80f06e35b9b6877f1ce0a02153
parent54a90fdfe41abfefca1a4e28969c6e4eff93bd83 (diff)
w
-rw-r--r--compat/borrowed-ptr.cpp8
-rw-r--r--compat/borrowed-ptr.hpp30
-rw-r--r--compat/borrowed-ptr.inl25
-rw-r--r--compat/weak-borrowed-ptr.hpp4
4 files changed, 48 insertions, 19 deletions
diff --git a/compat/borrowed-ptr.cpp b/compat/borrowed-ptr.cpp
index 3048e48a..1dded12a 100644
--- a/compat/borrowed-ptr.cpp
+++ b/compat/borrowed-ptr.cpp
@@ -10,8 +10,11 @@ void control_block::decrement(control_block*& blk) noexcept
{
delete blk->_ptr;
blk->_ptr = nullptr;
+#ifdef FM_NO_WEAK_BPTR
+ delete blk;
+#endif
}
-
+#ifndef FM_NO_WEAK_BPTR
auto c = --blk->_soft_count;
fm_bptr_assert(c != (uint32_t)-1);
if (c == 0)
@@ -19,9 +22,11 @@ void control_block::decrement(control_block*& blk) noexcept
fm_bptr_assert(!blk->_ptr);
delete blk;
}
+#endif
blk = nullptr;
}
+#ifndef FM_NO_WEAK_BPTR
void control_block::weak_decrement(control_block*& blk) noexcept
{
if (!blk)
@@ -36,6 +41,7 @@ void control_block::weak_decrement(control_block*& blk) noexcept
}
blk = nullptr;
}
+#endif
} // namespace floormat::detail_bptr
diff --git a/compat/borrowed-ptr.hpp b/compat/borrowed-ptr.hpp
index 741b3e53..9ba28fc1 100644
--- a/compat/borrowed-ptr.hpp
+++ b/compat/borrowed-ptr.hpp
@@ -3,6 +3,7 @@
#include <compare>
#define FM_BPTR_DEBUG
+#define FM_NO_WEAK_BPTR
#ifdef __CLION_IDE__
#define fm_bptr_assert(...) (void(__VA_ARGS__))
@@ -29,9 +30,14 @@ namespace floormat::detail_bptr {
struct control_block final
{
bptr_base* _ptr;
- uint32_t _soft_count, _hard_count;
+#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<typename From, typename To>
@@ -65,7 +71,7 @@ public:
//requires std::is_constructible_v<std::remove_const_t<T>, Ts&&...>
explicit bptr(InPlaceInitT, Ts&&... args) noexcept;
- template<detail_bptr::DerivedFrom<T> Y> explicit bptr(Y* ptr) noexcept;
+ explicit bptr(T* ptr) noexcept;
bptr() noexcept;
~bptr() noexcept;
@@ -119,6 +125,24 @@ public:
template<typename T> bptr<T>::bptr(std::nullptr_t) noexcept: blk{nullptr} {}
+template<typename T>
+template<typename... Ts>
+//requires std::is_constructible_v<std::remove_const_t<T>, Ts&&...>
+bptr<T>::bptr(InPlaceInitT, Ts&&... args) noexcept:
+ bptr{ new std::remove_const_t<T>{ forward<Ts>(args)... } }
+{}
+
+template<typename T> bptr<T>::bptr() noexcept: bptr{nullptr} {}
+
+template<typename T>
+bptr<T>::bptr(T* ptr) noexcept:
+ blk{ptr ? new detail_bptr::control_block{const_cast<std::remove_const_t<T>*>(ptr), 1,
+#ifndef FM_NO_WEAK_BPTR
+ 1,
+#endif
+ } : nullptr}
+{}
+
template<typename To, typename From>
requires detail_bptr::StaticCastable<From, To>
bptr<To> static_pointer_cast(bptr<From>&& p) noexcept
@@ -140,7 +164,9 @@ bptr<To> static_pointer_cast(const bptr<From>& p) noexcept
if (p.blk && p.blk->_ptr) [[likely]]
{
bptr<To> ret{nullptr};
+#ifndef FM_NO_WEAK_BPTR
++p.blk->_soft_count;
+#endif
++p.blk->_hard_count;
ret.blk = p.blk;
return ret;
diff --git a/compat/borrowed-ptr.inl b/compat/borrowed-ptr.inl
index b31326e7..79dd5e51 100644
--- a/compat/borrowed-ptr.inl
+++ b/compat/borrowed-ptr.inl
@@ -9,21 +9,6 @@
namespace floormat {
-template<typename T>
-template<typename... Ts>
-//requires std::is_constructible_v<std::remove_const_t<T>, Ts&&...>
-bptr<T>::bptr(InPlaceInitT, Ts&&... args) noexcept:
- bptr{ new std::remove_const_t<T>{ forward<Ts>(args)... } }
-{}
-
-template<typename T> bptr<T>::bptr() noexcept: bptr{nullptr} {}
-
-template<typename T>
-template<detail_bptr::DerivedFrom<T> Y>
-bptr<T>::bptr(Y* ptr) noexcept:
- blk{ptr ? new detail_bptr::control_block{const_cast<std::remove_const_t<Y>*>(ptr), 1, 1} : nullptr}
-{}
-
template<typename T> bptr<T>::~bptr() noexcept { if (blk) detail_bptr::control_block::decrement(blk); }
template<typename T> bptr<T>::bptr(const bptr<std::remove_const_t<T>>& ptr) noexcept requires std::is_const_v<T>: bptr{ptr, nullptr} {}
@@ -64,7 +49,11 @@ void bptr<T>::reset(Y* ptr) noexcept
{
if (blk)
detail_bptr::control_block::decrement(blk);
- blk = ptr ? new detail_bptr::control_block{const_cast<std::remove_const_t<Y>*>(ptr), 1, 1} : nullptr;
+ blk = ptr ? new detail_bptr::control_block{const_cast<std::remove_const_t<Y>*>(ptr), 1,
+#ifndef FM_NO_WEAK_BPTR
+ 1,
+#endif
+ } : nullptr;
}
template<typename T>
@@ -85,7 +74,9 @@ bptr<T>::bptr(const bptr<Y>& other, std::nullptr_t) noexcept:
{
if (blk)
{
+#ifndef FM_NO_WEAK_BPTR
++blk->_soft_count;
+#endif
++blk->_hard_count;
}
}
@@ -109,7 +100,9 @@ bptr<T>& bptr<T>::_copy_assign(const bptr<Y>& other) noexcept
blk = other.blk;
if (blk)
{
+#ifndef FM_NO_WEAK_BPTR
++blk->_soft_count;
+#endif
++blk->_hard_count;
}
}
diff --git a/compat/weak-borrowed-ptr.hpp b/compat/weak-borrowed-ptr.hpp
index ac512c5c..e9ceceaf 100644
--- a/compat/weak-borrowed-ptr.hpp
+++ b/compat/weak-borrowed-ptr.hpp
@@ -1,6 +1,10 @@
#pragma once
#include "borrowed-ptr.hpp"
+#ifdef FM_NO_WEAK_BPTR
+#error weak_bptr not supported!
+#endif
+
namespace floormat {
template<typename T>