-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshared_ptr.h
169 lines (133 loc) · 3.65 KB
/
shared_ptr.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
#pragma once
#include <functional>
#include <algorithm>
#include <atomic>
namespace util {
namespace impl {
class DefaultDeleter {
public:
template <typename T>
void operator()(T *ptr) {
delete ptr;
}
};
class DestructorDeleter {
public:
template <typename T>
void operator()(T *ptr) {
ptr->~T();
}
};
template <typename T>
class SharedPtrImpl {
public:
template <typename D = DefaultDeleter>
SharedPtrImpl(T *ptr = nullptr, D deleter = D()) : m_ptr(ptr), m_deleter(deleter), m_counter(1) { }
template <typename D = DestructorDeleter, typename... Args>
SharedPtrImpl(T *ptr, D deleter, Args&&... args) : m_ptr(ptr), m_deleter(deleter), m_counter(1) {
new (m_ptr) T(std::forward<Args>(args)...);
}
~SharedPtrImpl() {
m_deleter(m_ptr);
}
T * operator->() const {
return m_ptr;
}
T & operator*() const {
return *m_ptr;
}
T * get() const {
return m_ptr;
}
void hold() {
++m_counter;
}
bool release() {
--m_counter;
return m_counter == 0;
}
private:
T *m_ptr = nullptr;
std::function<void(T *)> m_deleter;
std::atomic<unsigned int> m_counter;
};
} // namespace impl
template <typename T>
class SharedPtr {
public:
explicit SharedPtr(impl::SharedPtrImpl<T> * impl) : m_impl(impl) { }
template <typename D = impl::DefaultDeleter>
explicit SharedPtr(T *ptr = nullptr, D deleter = D()) : m_impl(new impl::SharedPtrImpl<T>(ptr, deleter)) { }
template <typename U, typename D = impl::DefaultDeleter>
explicit SharedPtr(U *ptr = nullptr, D deleter = D()) : m_impl(new impl::SharedPtrImpl<T>(ptr, deleter)) { }
~SharedPtr() {
if (m_impl->release()) {
delete m_impl;
}
}
SharedPtr(const SharedPtr<T> &other) {
m_impl = other.m_impl;
m_impl->hold();
}
template <typename U>
SharedPtr(const SharedPtr<U> &other) {
m_impl = other.m_impl;
m_impl->hold();
}
SharedPtr<T> & operator=(const SharedPtr<T> &other) {
SharedPtr<T> tmp(other);
swap(tmp);
return *this;
}
template <typename U>
SharedPtr<T> & operator=(const SharedPtr<U> &other) {
SharedPtr<T> tmp(other);
swap(tmp);
return *this;
}
T * operator->() const {
return m_impl->operator->();
}
T & operator*() const {
return **m_impl;
}
template <typename U>
bool operator<(const SharedPtr<U> &other) {
return m_impl->get() < other.get();
}
T * get() const {
return m_impl->get();
}
template <typename D = impl::DefaultDeleter>
void reset(T *ptr = nullptr, D deleter = D()) {
if (m_impl->release()) {
delete m_impl;
}
m_impl = new impl::SharedPtrImpl<T>(ptr, deleter);
}
void swap(SharedPtr<T> &other) {
std::swap(m_impl, other.m_impl);
}
private:
impl::SharedPtrImpl<T> *m_impl;
};
template <typename T>
void swap(util::SharedPtr<T> &left, util::SharedPtr<T> &right) {
left.swap(right);
}
template <typename T, typename... Args>
SharedPtr<T> make_shared(Args&&... args) {
char * ptr = static_cast<char *>(operator new(sizeof(T) * sizeof(impl::SharedPtrImpl<T>)));
if (ptr == nullptr) {
throw std::bad_alloc();
}
auto t_ptr = reinterpret_cast<T *>(ptr + sizeof(impl::SharedPtrImpl<T>));
try {
auto impl = new(ptr) impl::SharedPtrImpl<T>(t_ptr, impl::DestructorDeleter(), std::forward<T>(args)...);
return SharedPtr<T>(impl);
} catch (...) {
operator delete(ptr);
throw;
}
}
} // namespace util