-
Notifications
You must be signed in to change notification settings - Fork 14
/
shared_ptr2.h
180 lines (156 loc) · 3.27 KB
/
shared_ptr2.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
170
171
172
173
174
175
176
177
178
179
180
#ifndef SHARED_PTR2_H
#define SHARED_PTR2_H
#include<functional>
#include<memory>
#include<stdexcept>
template <typename T> class shared_ptr2;
template <typename T>
void swap(shared_ptr2<T> & lhs, shared_ptr2<T> & rhs);
template <typename T>
class shared_ptr2
{
public:
shared_ptr2() : p(nullptr), ip(nullptr), delFun(nullptr) { }
shared_ptr2(T * tp, std::function<void (T *)> fp = nullptr) : p(tp), ip(new int(1)), delFun(fp) { }
~shared_ptr2();
shared_ptr2(const shared_ptr2 & sp, std::function<void (T *)> fp = nullptr);
shared_ptr2 & operator=(const shared_ptr2 &);
shared_ptr2(shared_ptr2 && sp, std::function<void (T *)> fp = nullptr);
shared_ptr2 & operator=(shared_ptr2 &&);
void swap(shared_ptr2 & sp);
T * get() const { return p; };
T & operator*() const;
T * operator->() const;
explicit operator bool() const;
bool unique() const;
int use_count() const;
void reset(T * sptr = nullptr, std::function<void (T *)> fp = nullptr);
private:
void destroy();
T * p;
int * ip;
std::function<void (T *)> delFun;
};
template <typename T>
shared_ptr2<T>::~shared_ptr2()
{
destroy();
}
template <typename T>
void shared_ptr2<T>::destroy()
{
if(ip != nullptr && p != nullptr && --*ip == 0)
{
delFun ? delFun(p) : delete p;
p = nullptr;
delete ip;
ip = nullptr;
delFun = nullptr;
}
}
template <typename T>
shared_ptr2<T>::shared_ptr2(const shared_ptr2 & sp, std::function<void (T *)> fp) : p(sp.p), ip(sp.ip)
{
++*ip;
if(fp == nullptr)
delFun = sp.delFun;
else
delFun = fp;
}
template <typename T>
shared_ptr2<T> & shared_ptr2<T>::operator=(const shared_ptr2 & rhs)
{
if(p != rhs.p)
{
destroy();
p = rhs.p;
ip = rhs.ip;
delFun = rhs.delFun;
++*ip;
}
return *this;
}
template <typename T>
shared_ptr2<T>::shared_ptr2(shared_ptr2 && sp, std::function<void (T *)> fp) : p(std::move(sp.p)), ip(std::move(sp.ip))
{
if(fp == nullptr)
delFun = sp.delFun;
else
delFun = fp;
sp.p = nullptr;
sp.ip =nullptr;
sp.delFun = nullptr;
}
template <typename T>
shared_ptr2<T> & shared_ptr2<T>::operator=(shared_ptr2 && rhs)
{
if(p != rhs.p)
{
destroy();
p = std::move(rhs.p);
ip = std::move(rhs.ip);
delFun = std::move(rhs.delFun);
rhs.p = nullptr;
rhs.ip = nullptr;
rhs.delFun =nullptr;
}
else if(this != &rhs)
rhs.destroy();
return *this;
}
template <typename T>
int shared_ptr2<T>::use_count() const
{
if(ip != nullptr)
return *ip;
throw std::runtime_error("no count!");
}
template <typename T>
bool shared_ptr2<T>::unique() const
{
if(use_count() == 1)
return true;
return false;
}
template <typename T>
T & shared_ptr2<T>::operator*() const
{
if(p == nullptr)
throw std::runtime_error("no object!");
return *p;
}
template <typename T>
T * shared_ptr2<T>::operator->() const
{
return & this->operator*();
}
template <typename T>
shared_ptr2<T>::operator bool() const
{
if(p == nullptr)
return false;
return true;
}
template <typename T>
void swap(shared_ptr2<T> & lhs, shared_ptr2<T> & rhs)
{
lhs.swap(rhs);
}
template <typename T>
void shared_ptr2<T>::swap(shared_ptr2 & sp)
{
using std::swap;
swap(p, sp.p);
swap(ip, sp.ip);
swap(delFun, sp.delFun);
}
template <typename T>
void shared_ptr2<T>::reset(T * sptr, std::function<void (T *)> fp)
{
destroy();
p = sptr;
if(p != nullptr)
ip = new int(1);
delFun = fp;
}
#endif