-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintrusive.h
202 lines (162 loc) · 3.83 KB
/
intrusive.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
#pragma once
#include <cstddef> // for std::nullptr_t
#include <utility> // for std::exchange / std::swap
#include <iostream>
class SimpleCounter {
public:
SimpleCounter& operator=(const SimpleCounter&) {
return *this;
}
SimpleCounter& operator=(SimpleCounter&&) {
return *this;
}
size_t IncRef() {
count_++;
return count_;
};
size_t DecRef() {
count_--;
return count_;
};
size_t RefCount() const {
return count_;
};
private:
size_t count_ = 0;
};
struct DefaultDelete {
template <typename T>
static void Destroy(T* object) {
delete object;
}
};
template <typename Derived, typename Counter, typename Deleter>
class RefCounted {
public:
// Increase reference counter.
void IncRef() {
counter_.IncRef();
};
// Decrease reference counter.
// Destroy object using Deleter when the last instance dies.
void DecRef() {
counter_.DecRef();
if (counter_.RefCount() == 0) {
Deleter::Destroy(static_cast<Derived*>(this));
}
};
// Get current counter value (the number of strong references).
size_t RefCount() const {
return counter_.RefCount();
};
private:
Counter counter_;
};
template <typename Derived, typename D = DefaultDelete>
using SimpleRefCounted = RefCounted<Derived, SimpleCounter, D>;
template <typename T>
class IntrusivePtr {
private:
template <typename Y>
friend class IntrusivePtr;
public:
// Constructors
IntrusivePtr() : ptr_{nullptr} {};
IntrusivePtr(std::nullptr_t) : ptr_{nullptr} {};
IntrusivePtr(T* ptr) : ptr_{ptr} {
if (ptr_ != nullptr) {
ptr_->IncRef();
}
};
template <typename Y>
IntrusivePtr(const IntrusivePtr<Y>& other) {
ptr_ = other.ptr_;
if (ptr_ != nullptr) {
ptr_->IncRef();
}
};
template <typename Y>
IntrusivePtr(IntrusivePtr<Y>&& other) {
ptr_ = other.ptr_;
other.ptr_ = nullptr;
};
IntrusivePtr(const IntrusivePtr& other) {
ptr_ = other.ptr_;
if (ptr_ != nullptr) {
ptr_->IncRef();
}
};
IntrusivePtr(IntrusivePtr&& other) {
ptr_ = other.ptr_;
other.ptr_ = nullptr;
};
// `operator=`-s
IntrusivePtr& operator=(const IntrusivePtr& other) {
if (other.Get() == this->Get()) {
return *this;
}
Reset();
ptr_ = other.ptr_;
if (ptr_ != nullptr) {
ptr_->IncRef();
}
return *this;
};
IntrusivePtr& operator=(IntrusivePtr&& other) {
if (other.Get() == this->Get()) {
return *this;
}
Reset();
ptr_ = other.ptr_;
other.ptr_ = nullptr;
return *this;
};
// Destructor
~IntrusivePtr() {
Reset();
};
// Modifiers
void Reset() {
if (ptr_ != nullptr) {
ptr_->DecRef();
ptr_ = nullptr;
}
}
void Reset(T* ptr) {
if (ptr_ != nullptr) {
ptr_->DecRef();
}
ptr_ = ptr;
if (ptr_) {
ptr_->IncRef();
}
}
void Swap(IntrusivePtr& other) {
std::swap(ptr_, other.ptr_);
};
// Observers
T* Get() const {
return ptr_;
};
T& operator*() const {
return *ptr_;
};
T* operator->() const {
return ptr_;
};
size_t UseCount() const {
if (ptr_ == nullptr) {
return 0;
}
return ptr_->RefCount();
};
explicit operator bool() const {
return ptr_ != nullptr;
};
private:
T* ptr_ = nullptr;
};
template <typename T, typename... Args>
IntrusivePtr<T> MakeIntrusive(Args&&... args) {
return IntrusivePtr<T>(new T(std::forward<Args>(args)...));
};