C++简易版shared_ptr

C++简易版shared_ptr

实现了shared_ptr的计数器,普通构造,拷贝构造,移动构造,析构,拷贝赋值运算符,移动赋值运算符,*运算符,->运算符,reset函数。

smartPtr

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
#include <iostream>
#include <memory>
#include <atomic>

//智能指针的计数器
class Count {
public:
constexpr Count()noexcept
:count(1) {}
int increase(){
return ++count;
}
int decrease() {
return --count;
}
public:
std::atomic<int> count;
};

//智能指针模板类
template<typename T>
class smartPtr {
public:
using value_type = T;
using value_reference = T&;
using value_pointer = T*;

constexpr smartPtr()noexcept
:ptr_(nullptr),pCount(nullptr){}
//防止不必要的隐式转换发生,避免出现意外的行为。
explicit smartPtr(value_pointer ptr)
:ptr_(ptr),pCount(new Count){}
//拷贝构造
smartPtr(const smartPtr<T>& rhs)noexcept
:ptr_(rhs.ptr_),pCount(rhs.pCount) {
if (pCount) {
pCount->increase();
}
}
//移动构造
smartPtr(smartPtr<T>&& rhs) noexcept {
change(rhs);
}

//拷贝赋值
smartPtr<T>& operator=(const smartPtr<T>& rhs) noexcept {
//先自减再赋值
decreaseCount();
ptr_ = rhs.ptr_;
pCount = rhs.pCount;
pCount->increase();
return *this;
}
//移动赋值
smartPtr<T>& operator=(smartPtr<T>&& rhs) noexcept {
decreaseCount();
change(rhs);
return *this;
}
//析构函数自带noexcept
~smartPtr() {
decreaseCount();
}

//get
value_pointer get() {
return ptr_;
}
//->
value_pointer operator->() {
return ptr_;
}
//*
value_reference operator*() {
return *ptr_;
}

//use_count
int use_count() {
return pCount ? pCount->count.load() : 0;
}
//reset
void reset() noexcept {
decreaseCount();
}
void reset(value_pointer ptr) {
decreaseCount();
ptr_ = ptr;
pCount = new Count;
}
private:
void change(smartPtr<T>& rhs) noexcept {
ptr_ = rhs.ptr_;
pCount = rhs.pCount;
rhs.ptr_ = nullptr;
rhs.pCount = nullptr;
}

void decreaseCount() noexcept {
int count = pCount ? pCount->decrease() : 0;
if (0 == count) {
delete ptr_;
ptr_ = nullptr;
delete pCount;
pCount = nullptr;
}
}
value_pointer ptr_;
Count* pCount;
};

struct testStruct {
testStruct(int num):num_(num){}
int num_;
};

int main()
{
std::cout << "smartPtr" << std::endl;
//构造 拷贝赋值 -> *
smartPtr<testStruct> myp1;
{
smartPtr<testStruct> myp2(new testStruct(123));
myp1 = myp2;
std::cout << myp1.use_count() << std::endl;
std::cout << myp2.use_count() << std::endl;
}
std::cout << myp1.use_count() << std::endl;
std::cout << myp1->num_ << std::endl;
std::cout << (*myp1).num_ << std::endl;
std::cout << "-----------------" << std::endl;

//拷贝构造
smartPtr<testStruct> myp3(new testStruct(123));
{
smartPtr<testStruct> myp4(myp3);
std::cout << myp3.use_count() << std::endl;
std::cout << myp4.use_count() << std::endl;
}
std::cout << myp3.use_count() << std::endl;
std::cout << "-----------------" << std::endl;

//移动构造
smartPtr<testStruct> myp5(new testStruct(123));
{
smartPtr<testStruct> myp6(std::move(myp5));
std::cout << myp5.use_count() << std::endl;
std::cout << myp6.use_count() << std::endl;
}
std::cout << myp5.use_count() << std::endl;
std::cout << "-----------------" << std::endl;

//移动赋值
smartPtr<testStruct> myp7(new testStruct(123));
{
smartPtr<testStruct> myp8;
myp8 = std::move(myp7);
std::cout << myp7.use_count() << std::endl;
std::cout << myp8.use_count() << std::endl;
}
std::cout << myp7.use_count() << std::endl;
std::cout << "-----------------" << std::endl;

//reset
smartPtr<testStruct> myp9(new testStruct(123));
myp9.reset(new testStruct(321));
std::cout << myp9.use_count() << std::endl;
std::cout << myp9->num_ << std::endl;
myp9.reset();
std::cout << myp9.use_count() << std::endl;
std::cout << "-----------------" << std::endl;

std::cout << "shared_ptr:" << std::endl;
//构造 拷贝赋值 -> *
std::shared_ptr<testStruct> p1;
{
std::shared_ptr<testStruct> p2(new testStruct(123));
p1 = p2;
std::cout << p1.use_count() << std::endl;
std::cout << p2.use_count() << std::endl;
}
std::cout << p1.use_count() << std::endl;
std::cout << p1->num_ << std::endl;
std::cout << (*p1).num_ << std::endl;
std::cout << "-----------------" << std::endl;
//拷贝构造
std::shared_ptr<testStruct> p3(new testStruct(123));
{
std::shared_ptr<testStruct> p4(p3);
std::cout << p3.use_count() << std::endl;
std::cout << p4.use_count() << std::endl;
}
std::cout << p3.use_count() << std::endl;
std::cout << "-----------------" << std::endl;
//移动构造
std::shared_ptr<testStruct> p5(new testStruct(123));
{
std::shared_ptr<testStruct> p6(std::move(p5));
std::cout << p5.use_count() << std::endl;
std::cout << p6.use_count() << std::endl;
}
std::cout << p5.use_count() << std::endl;
std::cout << "-----------------" << std::endl;
//移动赋值
std::shared_ptr<testStruct> p7(new testStruct(123));
{
std::shared_ptr<testStruct> p8;
p8 = std::move(p7);
std::cout << p7.use_count() << std::endl;
std::cout << p8.use_count() << std::endl;
}
std::cout << p7.use_count() << std::endl;
std::cout << "-----------------" << std::endl;
//reset
std::shared_ptr<testStruct> p9(new testStruct(123));
p9.reset(new testStruct(321));
std::cout << p9.use_count() << std::endl;
std::cout << p9->num_ << std::endl;
p9.reset();
std::cout << p9.use_count() << std::endl;
std::cout << "-----------------" << std::endl;

return 0;
}