-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared.h
378 lines (308 loc) · 9.87 KB
/
shared.h
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#pragma once
#include "sw_fwd.h" // Forward declaration
#include <cstddef> // std::nullptr_t
#include <type_traits>
// the base class for Enable Shared From This
class ESFTBase {};
// Control Blocks
// Base class for other control blocks
class IBlock {
private:
size_t shared_count_ = 1;
size_t weak_count_ = 0;
virtual void Deleter() {};
public:
IBlock() {
shared_count_ = 1;
weak_count_ = 0;
}
// api for shared obj refs
size_t SharedCount() {
return shared_count_;
}
void IncShared() {
++shared_count_;
}
void DecShared() {
// std::cout << "DecShared() strong: " << shared_count_ << ", weak: " << weak_count_ <<
// "\n";
--shared_count_;
if (shared_count_ == 0) {
// std::cout << "DecShared Deleter()\n";
Deleter();
// if (WeakCount() == 0) {
// std::cout << "Deleting object: " << this << std::endl;
// delete this;
// }
}
}
// api for weak obj refs
size_t WeakCount() {
return weak_count_;
}
void IncWeak() {
weak_count_++;
}
void DecWeak() {
weak_count_--;
// std::cout << "DecWeak() strong: " << shared_count_ << ", weak: " << weak_count_ << "\n";
if (shared_count_ == 0 && WeakCount() == 0) {
// std::cout << "Deleting object: " << this << std::endl;
delete this;
}
}
// virtual methods
virtual ~IBlock(){};
};
// Control Block for shared_ptr(T* ptr)
template <typename T>
class RawPtrBlock : public IBlock {
private:
void Deleter() override {
delete ptr_;
}
public:
T* ptr_;
RawPtrBlock(T* ptr) : IBlock(), ptr_{ptr} {};
~RawPtrBlock() override {
// std::cout << "~RawPtrBlock()\n";
}
};
// Control Block for make_shared(Args&&...)
template <typename T>
class SingleAllocateBlock : public IBlock {
private:
void Deleter() override {
reinterpret_cast<T*>(ptr_)->~T();
};
public:
char bytes_[sizeof(T)] = {};
T* ptr_ = nullptr;
SingleAllocateBlock() : IBlock() {
ptr_ = new (bytes_) T();
}
template <typename... Args>
SingleAllocateBlock(Args&&... args) : IBlock() {
ptr_ = new (bytes_) T(std::forward<Args>(args)...);
}
~SingleAllocateBlock() override {
}
};
template <typename T>
class SharedPtr {
public:
////////////////////////////////////////////////////////////////////////////////////////////////
// Constructors
SharedPtr() {
ptr_ = nullptr;
ctrl_block_ = nullptr;
};
SharedPtr(std::nullptr_t) {
ptr_ = nullptr;
ctrl_block_ = nullptr;
};
template <typename Y>
SharedPtr(SingleAllocateBlock<Y>* ctrl_block)
: ptr_{ctrl_block->ptr_}, ctrl_block_{ctrl_block} {
// std::cout << ptr_ << ", " << ctrl_block_ << "\n";
// std::cout << ctrl_block_->SharedCount() << "\n";
if constexpr (std::is_convertible_v<Y*, ESFTBase*>) {
ptr_->weak_this_ = WeakPtr<T>(*this);
// ctrl_block_->IncWeak();
}
// std::cout << ctrl_block_->SharedCount() << ", weak: " << ctrl_block_->WeakCount() <<
// "\n";
}
template <typename Y>
explicit SharedPtr(Y* ptr) : ptr_{ptr}, ctrl_block_{new RawPtrBlock<Y>(ptr)} {
// std::cout << "SharedPtr(T* ptr)" << "\n";
if constexpr (std::is_convertible_v<Y*, ESFTBase*>) {
// std::cout << "BEFORE WEAK THIS: strong: " << ctrl_block_->SharedCount() << ", weak: "
// << ctrl_block_->WeakCount() << "\n";
ptr_->weak_this_ = WeakPtr<T>(*this);
}
// std::cout << "--- AFTER WEAK THIS: strong: " << ctrl_block_->SharedCount() << ",
// weak: " << ctrl_block_->WeakCount() << "\n";
};
// copy contructor for working SharedPtr<const int> s2 = s1;
template <typename Y>
SharedPtr(const SharedPtr<Y>& other) : ptr_{other.ptr_}, ctrl_block_{other.ctrl_block_} {
// increment the count of refs on block
// std::cout << "SharedPtr(const SharedPtr<Y>& other)" << "\n";
if (ctrl_block_ != nullptr) {
ctrl_block_->IncShared();
}
};
SharedPtr(const SharedPtr& other) : ptr_{other.ptr_}, ctrl_block_{other.ctrl_block_} {
// std::cout << "SharedPtr(const SharedPtr& other)" << "\n";
// increment the count of refs on block
if (ctrl_block_ != nullptr) {
ctrl_block_->IncShared();
}
};
// move
SharedPtr(SharedPtr&& other) {
// std::cout << "SharedPtr(SharedPtr&& other)" << "\n";
std::swap(ptr_, other.ptr_);
std::swap(ctrl_block_, other.ctrl_block_);
};
// move contstructor for working this SharedPtr<const int> s3 = std::move(s1);
// in operator=(SharedPtr<Y>&& other) use SharedPtr(std::move(other)).Swap(*this);
template <typename Y>
SharedPtr(SharedPtr<Y>&& other) {
// std::cout << "SharedPtr(SharedPtr<Y>&& other)" << "\n";
ptr_ = other.ptr_;
ctrl_block_ = other.ctrl_block_;
other.ptr_ = nullptr;
other.ctrl_block_ = nullptr;
}
// Aliasing constructor
// #8 from https://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr
template <typename Y>
SharedPtr(const SharedPtr<Y>& other, T* ptr) {
// std::cout << "SharedPtr(SharedPtr<Y>&other, T* ptr)" << "\n";
ptr_ = ptr;
ctrl_block_ = other.ctrl_block_;
if (ctrl_block_ != nullptr) {
ctrl_block_->IncShared();
}
};
// Promote `WeakPtr`
// #11 from https://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr
explicit SharedPtr(const WeakPtr<T>& other) {
if (other.Expired()) {
throw BadWeakPtr();
return;
}
ptr_ = other.ptr_;
ctrl_block_ = other.ctrl_block_;
if (ctrl_block_ != nullptr) {
ctrl_block_->IncShared();
}
};
////////////////////////////////////////////////////////////////////////////////////////////////
// `operator=`-s
SharedPtr& operator=(const SharedPtr& other) noexcept {
SharedPtr<T>(other).Swap(*this);
return *this;
};
template <typename Y, typename = std::enable_if_t<std::is_convertible_v<Y, T>>>
SharedPtr<T>& operator=(const SharedPtr<Y>& other) noexcept {
SharedPtr<T>(other).Swap(*this);
return *this;
};
SharedPtr& operator=(SharedPtr&& other) noexcept {
if (ctrl_block_ != nullptr) {
ctrl_block_->DecShared();
}
ptr_ = other.ptr_;
ctrl_block_ = other.ctrl_block_;
other.ptr_ = nullptr;
other.ctrl_block_ = nullptr;
return *this;
};
template <typename Y, typename = std::enable_if_t<std::is_convertible_v<Y, T>>>
SharedPtr<T>& operator=(SharedPtr<Y>&& other) {
SharedPtr<T>(std::move(other)).Swap(*this);
return *this;
};
////////////////////////////////////////////////////////////////////////////////////////////////
// Destructor
~SharedPtr() {
Reset();
};
////////////////////////////////////////////////////////////////////////////////////////////////
// Modifiers
void Reset() {
if (ctrl_block_ != nullptr) {
ctrl_block_->IncWeak();
ctrl_block_->DecShared();
ctrl_block_->DecWeak();
if (ctrl_block_->SharedCount() == 0 && ctrl_block_->WeakCount() == 0) {
delete ctrl_block_;
}
}
ptr_ = nullptr;
ctrl_block_ = nullptr;
};
template <typename Y>
void Reset(Y* ptr) {
Reset();
ptr_ = ptr;
ctrl_block_ = new RawPtrBlock<Y>(ptr);
};
template <typename Y>
void Swap(SharedPtr<Y>& other) {
std::swap(ptr_, other.ptr_);
std::swap(ctrl_block_, other.ctrl_block_);
};
void Swap(SharedPtr& other) {
std::swap(ptr_, other.ptr_);
std::swap(ctrl_block_, other.ctrl_block_);
};
////////////////////////////////////////////////////////////////////////////////////////////////
// Observers
T* Get() const {
return ptr_;
};
T& operator*() const {
return *ptr_;
};
T* operator->() const {
return ptr_;
};
size_t UseCount() const {
if (ctrl_block_ != nullptr) {
return ctrl_block_->SharedCount();
}
return 0;
};
explicit operator bool() const {
return ptr_ != nullptr;
};
private:
T* ptr_ = nullptr;
IBlock* ctrl_block_ = nullptr;
// fiend class
template <typename Y>
friend class SharedPtr;
template <typename U, typename... Args>
friend SharedPtr<U> MakeShared(Args&&... args);
template <typename Y>
friend class WeakPtr;
};
template <typename T, typename U>
inline bool operator==(const SharedPtr<T>& left, const SharedPtr<U>& right) {
return left.Get() == right.Get();
};
// Allocate memory only once
template <typename T, typename... Args>
SharedPtr<T> MakeShared(Args&&... args) {
// std::cout << "SharedPtr(ControlBlock)\n";
return SharedPtr<T>(new SingleAllocateBlock<T>(std::forward<Args>(args)...));
};
template <typename T>
class EnableSharedFromThis : public ESFTBase {
public:
SharedPtr<T> SharedFromThis() {
return SharedPtr<T>(weak_this_);
};
SharedPtr<const T> SharedFromThis() const {
return SharedPtr<const T>(weak_this_);
};
WeakPtr<T> WeakFromThis() noexcept {
return WeakPtr<T>(weak_this_);
};
WeakPtr<const T> WeakFromThis() const noexcept {
return WeakPtr<const T>(weak_this_);
};
template <class _Up>
friend class SharedPtr;
template <typename U, typename... Args>
friend SharedPtr<U> MakeShared(Args&&... args);
virtual ~EnableSharedFromThis() {
// std::cout << "~EnableSharedFromThis\n";
}
private:
WeakPtr<T> weak_this_;
};