-
Notifications
You must be signed in to change notification settings - Fork 0
/
SharedPtr.hpp
260 lines (227 loc) · 5.53 KB
/
SharedPtr.hpp
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
#include <cstddef>
#include <atomic>
#include <typeinfo>
#include <iostream>
#include <pthread.h>
namespace cs540{
struct spDestructor{
virtual void destroy() = 0;
virtual ~spDestructor() = default;
};
template <typename T>
struct tspDestructor: public spDestructor{
T* data;
tspDestructor() : data(nullptr) { }
tspDestructor(T* ptr) : data(ptr) { }
void destroy() override{
delete data;
}
};
struct refCnt{
std::atomic_int refs;
spDestructor* destructor;
pthread_mutex_t refsLock;
refCnt() : refs(0), destructor(nullptr), refsLock(PTHREAD_MUTEX_INITIALIZER){ }
template <typename T>
refCnt(T* ptr) : refs(1), destructor(new tspDestructor<T>(ptr)), refsLock(PTHREAD_MUTEX_INITIALIZER) { }
~refCnt(){
if(destructor){
destructor->destroy();
delete destructor;
}
pthread_mutex_unlock(&refsLock);
}
};
template <typename T>
class SharedPtr{
public:
T* ptrData;
refCnt* refObj;
SharedPtr(){
//std::cout << "Regular constructor called" << std::endl;
refObj = nullptr;
ptrData = nullptr;
}
template <typename U>
explicit SharedPtr(U * ptr){
//std::cout << "Templated constructor called" << std::endl;
refObj = new refCnt(ptr);
ptrData = ptr;
}
SharedPtr(const SharedPtr &p){
//std::cout << "Normal copy constructor called" << std::endl;
refObj = p.refObj;
increment();
ptrData = p.ptrData;
}
template <typename U>
SharedPtr(const SharedPtr<U> &p){
//std::cout << "Templated copy constructor called" << std::endl;
refObj = p.refObj;
increment();
ptrData = p.ptrData;
}
SharedPtr(SharedPtr &&p){
//std::cout << "Move constructor called" << std::endl;
refObj = std::move(p.refObj);
ptrData = std::move(p.ptrData);
p.refObj = nullptr;
p.ptrData = nullptr;
}
template <typename U>
SharedPtr(SharedPtr<U> &&p){
//std::cout << "Templated move constructor called" << std::endl;
refObj = std::move(p.refObj);
ptrData = std::move(p.ptrData);
p.refObj = nullptr;
p.ptrData = nullptr;
}
template <typename U>
SharedPtr(const SharedPtr<U> &ptr, T* other){
refObj = ptr.refObj;
ptrData = other;
increment();
}
SharedPtr &operator=(const SharedPtr &other){
if(&other == this || this->refObj == other.refObj){
return *this;
}
decrement();
refObj = other.refObj;
increment();
ptrData = other.ptrData;
return *this;
}
template <typename U>
SharedPtr<T> &operator=(const SharedPtr<U> &other){
if(this->refObj == other.refObj){
ptrData = other.ptrData;
return *this;
}
decrement();
refObj = other.refObj;
increment();
ptrData = other.ptrData;
return *this;
}
SharedPtr &operator=(SharedPtr &&p){
if(this->refObj == p.refObj){
return *this;
}
decrement();
refObj = std::move(p.refObj);
ptrData = std::move(p.ptrData);
p.refObj = nullptr;
p.ptrData = nullptr;
return *this;
}
template <typename U>
SharedPtr &operator=(SharedPtr<U> &&p){
if(this->refObj == p.refObj){
return *this;
}
decrement();
refObj = std::move(p.refObj);
ptrData = std::move(p.ptrData);
p.refObj = nullptr;
p.ptrData = nullptr;
return *this;
}
~SharedPtr(){
decrement();
}
void reset(){
//std::cout << "reset() called" << std::endl;
decrement();
refObj = nullptr;
ptrData = nullptr;
}
template <typename U>
void reset(U *p){
//std::cout << "reset(U *p) called" << std::endl;
try{
decrement();
refObj = new refCnt(p);
ptrData = p;
}catch(...){
throw "An exception occurred in template <typename U> SharedPtr::reset(U *p)\n";
}
}
T *get() const{
return ptrData;
}
T &operator*() const{
return *ptrData;
}
T *operator->() const{
return ptrData;
}
explicit operator bool() const{
if(ptrData){
return true;
}else{
return false;
}
}
private:
void increment(){
if(refObj){
pthread_mutex_lock(&refObj->refsLock);
refObj->refs++;
pthread_mutex_unlock(&refObj->refsLock);
}
}
void decrement(){
if(refObj){
pthread_mutex_lock(&refObj->refsLock);
//std::cout << "refsLocking decrement() on refObj address: " << refObj << std::endl;
refObj->refs--;
if(refObj->refs == 0){
//pthread_mutex_unlock(&refObj->refsLock);
delete refObj;
refObj = nullptr;
ptrData = nullptr;
return;
}
pthread_mutex_unlock(&refObj->refsLock);
//std::cout << "unrefsLocking decrement() on refObj address: " << refObj << std::endl;
}
}
};
template <typename T1, typename T2>
bool operator==(const SharedPtr<T1> &lhs, const SharedPtr<T2> &rhs){
return lhs.get() == rhs.get();
}
template <typename T>
bool operator==(const SharedPtr<T> &lhs, std::nullptr_t rhs){
return !lhs;
}
template <typename T>
bool operator==(std::nullptr_t lhs, const SharedPtr<T> &rhs){
return !rhs;
}
template <typename T1, typename T2>
bool operator!=(const SharedPtr<T1>& lhs, const SharedPtr<T2> &rhs){
return !(lhs == rhs);
}
template <typename T>
bool operator!=(const SharedPtr<T> &lhs, std::nullptr_t rhs){
return (bool) lhs;
}
template <typename T>
bool operator!=(std::nullptr_t lhs, const SharedPtr<T> &rhs){
return (bool) rhs;
}
template <typename T, typename U>
SharedPtr<T> static_pointer_cast(const SharedPtr<U> &r) noexcept{
return SharedPtr<T>(r, static_cast<T*>(r.get()));
}
template <typename T, typename U>
SharedPtr<T> dynamic_pointer_cast(const SharedPtr<U> &sp){
if (auto p = dynamic_cast<T*>(sp.get())){
return SharedPtr<T>(sp, p);
}else {
return SharedPtr<T>();
}
}
}