-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSharedPtr.h
165 lines (143 loc) · 3.73 KB
/
SharedPtr.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
#ifndef SHAREDPTR_H
#define SHAREDPTR_H
#include <cstddef>
#include <utility>
#include <functional>
template <typename T> class SharedPtr;
template <typename T> void swap(SharedPtr<T> &, SharedPtr<T> &);
template <typename T>
class SharedPtr {
friend void swap<T>(SharedPtr<T> &, SharedPtr<T> &);
public:
using element_type = T;
SharedPtr() : element(nullptr), use(new std::size_t(0)) {}
SharedPtr(T *p) : element(p), use(new std::size_t(p ? 1 : 0)) {}
template <typename Deleter>
SharedPtr(T *p, Deleter d) : element(p), use(new std::size_t(p ? 1 : 0)), del(d) {}
SharedPtr(const SharedPtr &p) : element(p.element), use(p.use), del(p.del) {
if (p)
++*use;
}
template <typename Deleter>
SharedPtr(const SharedPtr &p, Deleter d) : element(p.element), use(p.use), del(d) {
if (p)
++*use;
}
SharedPtr(SharedPtr &&p) noexcept : element(p.element), use(p.use), del(std::move(p.del)) {
p.element = nullptr;
p.use = nullptr;
}
SharedPtr &operator=(const SharedPtr &rhs) {
if (this != &rhs) {
deletion_logic();
element = rhs.element;
use = rhs.use;
del = rhs.del;
if (element)
++*use;
}
return *this;
}
SharedPtr &operator=(SharedPtr &&rhs) noexcept {
if (this != &rhs) {
deletion_logic();
element = rhs.element;
use = rhs.use;
del = std::move(rhs.del);
rhs.element = nullptr;
rhs.use = nullptr;
}
return *this;
}
~SharedPtr() {
deletion_logic();
}
std::size_t use_count() const { return *use; }
bool unique() const { return use_count() == 1; }
T *get() { return element; }
const T *get() const { return element; }
void swap(SharedPtr &s) {
::swap(*this, s);
}
void reset() {
deletion_logic();
element = nullptr;
use = new std::size_t(0);
del = std::function<void (T *)>();
}
void reset(T *p) {
deletion_logic();
element = p;
if (p)
use = new std::size_t(1);
else
use = new std::size_t(0);
del = std::function<void (T *)>();
}
template <typename Deleter>
void reset(T *p, Deleter d) {
deletion_logic();
element = p;
if (p)
use = new std::size_t(1);
else
use = new std::size_t(0);
del = d;
}
T &operator*() { return *element; }
const T &operator*() const { return *element; }
T *operator->() { return &this->operator*(); }
const T *operator->() const { return &this->operator*(); }
explicit operator bool() const { return element; }
private:
// Decrement reference count, and if there are no users, delete the pointed-to element
void deletion_logic() {
if (use) {
if (*use == 0) {
delete use;
} else if (--*use == 0) {
del ? del(element) : delete element;
delete use;
}
}
}
T *element;
std::size_t *use;
std::function<void (T *)> del;
};
template <typename T>
inline void swap(SharedPtr<T> &lhs, SharedPtr<T> &rhs) {
using std::swap;
swap(lhs.element, rhs.element);
swap(lhs.use, rhs.use);
swap(lhs.del, rhs.del);
}
template <typename T>
inline bool operator==(const SharedPtr<T> &lhs, const SharedPtr<T> &rhs) {
return lhs.get() == rhs.get();
}
template <typename T>
inline bool operator!=(const SharedPtr<T> &lhs, const SharedPtr<T> &rhs) {
return !(lhs == rhs);
}
template <typename T>
inline bool operator<(const SharedPtr<T> &lhs, const SharedPtr<T> &rhs) {
return std::less<const T *>()(lhs.get(), rhs.get());
}
template <typename T>
inline bool operator>(const SharedPtr<T> &lhs, const SharedPtr<T> &rhs) {
return (rhs < lhs);
}
template <typename T>
inline bool operator<=(const SharedPtr<T> &lhs, const SharedPtr<T> &rhs) {
return !(rhs < lhs);
}
template <typename T>
inline bool operator>=(const SharedPtr<T> &lhs, const SharedPtr<T> &rhs) {
return !(lhs < rhs);
}
template <typename T, typename... Args>
SharedPtr<T> MakeShared(Args&&... args) {
return SharedPtr<T>(new T(std::forward<Args>(args)...));
}
#endif