This repository was archived by the owner on Apr 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathex16_28_shared_ptr.cpp
218 lines (179 loc) · 5.73 KB
/
ex16_28_shared_ptr.cpp
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
#include "headers.h"
class Debug_deleter {
public:
Debug_deleter(ostream& os = cerr) : os(os) {}
template <typename T>
void operator()(T* ptr) {
os << "Deleting unique_ptr." << endl;
delete ptr;
}
private:
ostream& os;
};
template <typename T>
class my_shared_ptr {
friend void swap(my_shared_ptr<T> & msp1, my_shared_ptr<T> & msp2) {
msp1.swap(msp2);
}
public:
// Default constructor
my_shared_ptr() : n_ptr(new int(1)), p(nullptr) {
cout << "my_shared_ptr()" << endl;
}
// Constructor that takes a pointer from placement new
my_shared_ptr(T* p) : n_ptr(new int(1)), p(p) {
cout << "my_shared_ptr(const T* p)" << endl;
}
// Constructor that takes a pointer from placement new, AND a callable deleter
// 7.27 TODO: Here we are passing a reference to a function object, then we
// use the address-of operator to store as a member.
// However, I cannot find a way to pass in pointer to function-object.
// Function-object is also called "functor".
// 7.28: There's no reason to store a pointer to functor though.
// Using a functor directly seem to be good enough.
my_shared_ptr(T* p, const function<void(T*)>& p_del)
: n_ptr(new int(1)), p(p), p_del(p_del) {
cout << "my_shared_ptr(T* p, const function<void(T*)>& p_del)" << endl;
}
// Constructor that assums the ownership from a unique_ptr
my_shared_ptr(unique_ptr<T> up) : n_ptr(new int(1)) {
// u.release(): relinquish the control of the pointer held by u and
// returns the pointer. Set u to null.
p = up.release();
}
// Copy constrcutor
my_shared_ptr(const my_shared_ptr&);
// Variant of the copy constructor
my_shared_ptr(const my_shared_ptr&, const function<void(T*)>& p_del);
// Copu assignment operator
my_shared_ptr& operator=(const my_shared_ptr<T>&);
// Destructor
~my_shared_ptr() {
cout << "~my_shared_ptr()" << endl;
free();
}
T& operator*() const {
return *p;
}
T* operator->() const {
return p;
}
T* get() const {
return p;
}
void swap(my_shared_ptr<T>& other) {
auto other_n_ptr = other.n_ptr;
auto other_p = other.p;
other.n_ptr = n_ptr;
other.p = p;
n_ptr = other_n_ptr;
p = other_p;
}
bool unique() { return use_count() == 1; }
int use_count() {
if (n_ptr) {
return *n_ptr;
}
return -1;
}
void reset() { // Do not free object if shared by other
if (--*n_ptr == 0) {
free();
} else {
p = nullptr;
n_ptr = nullptr;
}
}
void reset(T* p) {
reset();
*n_ptr = 1;
this->p = p;
}
void reset(T* p, const function<void(T*)>& p_del) {
this->p_del = p_del;
reset(p);
}
private:
void free();
int* n_ptr;
T* p;
const function<void(T*)> p_del;
};
template <typename T>
my_shared_ptr<T>::my_shared_ptr(const my_shared_ptr& other)
: n_ptr(&++*(other.n_ptr)), p(other.p) {
cout << "my_shared_ptr(const my_shared_ptr&). Counter: " << *n_ptr << endl;
}
template <typename T>
my_shared_ptr<T>::my_shared_ptr(const my_shared_ptr& other, const function<void(T*)>& p_del)
: n_ptr(&++*(other.n_ptr)), p(other.p), p_del(&p_del) {
cout << "my_shared_ptr(const my_shared_ptr&, const function<void(T*)>&). Counter: " << *n_ptr << endl;
}
template <typename T>
my_shared_ptr<T>& my_shared_ptr<T>::operator=(const my_shared_ptr<T>& rhs) {
cout << "my_shared_ptr<T>::operator=(const my_shared_ptr<T>&)" << endl;
auto new_n_ptr = &(++*(rhs.n_ptr));
auto new_p = rhs.p;
if (--*n_ptr == 0) {
free();
}
n_ptr = new_n_ptr;
p = new_p;
return *this;
}
template <typename T>
void my_shared_ptr<T>::free() {
// Should be deleted separately !!!
if (p_del) {
p_del(p);
} else {
delete p;
}
delete n_ptr;
n_ptr = nullptr;
p = nullptr;
cout << "Resource freed!" << endl;
}
int main() {
// Test copy constructor and copy assignment
my_shared_ptr<int> sp1;
my_shared_ptr<int> sp2(sp1);
my_shared_ptr<int> sp3;
sp3 = sp1;
// Test with placement new
my_shared_ptr<int> test_new(new int(10));
// Test deleter
my_shared_ptr<int> test_del2(new int(10), Debug_deleter());
// Test operator*
my_shared_ptr<int> test_deref(new int(10));
cout << *test_deref << endl;
*test_deref = 20;
cout << *test_deref << endl;
// Test operator->
my_shared_ptr<string> test_arrow(new string("hello world!"));
cout << test_arrow->length() << endl;
// Test get()
my_shared_ptr<string> test_get(new string("Hello again!"));
auto p = test_get.get();
cout << *p << endl;
// Test swap(my_shared_ptr<T> &) and swap(my_shared_ptr<T> &, my_shared_ptr<T>&)
my_shared_ptr<string> msp1(new string("A"));
my_shared_ptr<string> msp2(new string("B"));
my_shared_ptr<string> msp3(msp1);
cout << *msp1 << " " << *msp2 << endl;
cout << msp1.use_count() << " " << msp2.use_count() << endl;
cout << *msp3 << endl;
msp1.swap(msp2);
cout << *msp1 << " " << *msp2 << endl;
cout << msp1.use_count() << " " << msp2.use_count() << endl;
cout << *msp3 << endl;
cout << endl;
swap(msp1, msp2);
cout << *msp1 << " " << *msp2 << endl;
cout << msp1.use_count() << " " << msp2.use_count() << endl;
cout << *msp3 << endl;
cout << "----------" << endl;
}
// When you want a object constructed with default constructor in C++,
// use ClassName identifier; instead of ClassName identifier();
// https://stackoverflow.com/questions/11963849/c-constructor-not-being-called