1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#pragma once
#include "borrowed-ptr-fwd.hpp"
#include <compare>
#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<typename From, typename To>
concept StaticCastable = requires(From* from, To* to) {
static_cast<To*>(from);
static_cast<const bptr_base*>(from);
static_cast<const bptr_base*>(to);
};
template<typename From, typename To>
concept DerivedFrom = requires(From* from, To* to) {
requires std::is_convertible_v<From&, To&>;
};
} // namespace floormat::detail_bptr
namespace floormat {
template<typename T>
class bptr final // NOLINT(*-special-member-functions)
{
detail_bptr::control_block* blk;
template<typename Y> bptr(const bptr<Y>& other, std::nullptr_t) noexcept;
template<typename Y> bptr(bptr<Y>&& other, std::nullptr_t) noexcept;
template<typename Y> bptr& _copy_assign(const bptr<Y>& other) noexcept;
template<typename Y> bptr& _move_assign(bptr<Y>&& other) noexcept;
public:
template<typename... Ts>
//requires std::is_constructible_v<std::remove_const_t<T>, Ts&&...>
explicit bptr(InPlaceInitT, Ts&&... args) noexcept;
explicit bptr(T* ptr) noexcept;
bptr() noexcept;
~bptr() noexcept;
bptr(std::nullptr_t) noexcept; // NOLINT(*-explicit-conversions)
bptr& operator=(std::nullptr_t) noexcept;
bptr(const bptr<std::remove_const_t<T>>& ptr) noexcept requires std::is_const_v<T>;
bptr(bptr<std::remove_const_t<T>>&& ptr) noexcept requires std::is_const_v<T>;
bptr(const bptr&) noexcept;
bptr& operator=(const bptr&) noexcept;
template<detail_bptr::DerivedFrom<T> Y> bptr(const bptr<Y>&) noexcept;
template<detail_bptr::DerivedFrom<T> Y> bptr& operator=(const bptr<Y>&) noexcept;
bptr(bptr&&) noexcept;
bptr& operator=(bptr&&) noexcept;
template<detail_bptr::DerivedFrom<T> Y> bptr(bptr<Y>&&) noexcept;
template<detail_bptr::DerivedFrom<T> Y> bptr& operator=(bptr<Y>&&) noexcept;
void reset() noexcept;
template<detail_bptr::DerivedFrom<T> 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<const T>& other) const noexcept;
bool operator==(const bptr<T>& other) const noexcept requires (!std::is_const_v<T>);
bool operator==(const std::nullptr_t& other) const noexcept;
std::strong_ordering operator<=>(const bptr<const T>& other) const noexcept;
std::strong_ordering operator<=>(const bptr<T>& other) const noexcept requires (!std::is_const_v<T>);
std::strong_ordering operator<=>(const std::nullptr_t&) const noexcept;
template<typename U> friend class bptr;
template<typename U> friend class weak_bptr;
template<typename To, typename From>
requires detail_bptr::StaticCastable<From, To>
friend bptr<To> static_pointer_cast(bptr<From>&& p) noexcept;
template<typename To, typename From>
requires detail_bptr::StaticCastable<From, To>
friend bptr<To> static_pointer_cast(const bptr<From>& p) noexcept;
};
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
{
if (p.blk && p.blk->_ptr) [[likely]]
{
bptr<To> ret{nullptr};
ret.blk = p.blk;
p.blk = nullptr;
return ret;
}
return bptr<To>{nullptr};
}
template<typename To, typename From>
requires detail_bptr::StaticCastable<From, To>
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;
}
return bptr<To>{nullptr};
}
} // namespace floormat
|