-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmart_ptr.h
223 lines (203 loc) · 5.35 KB
/
smart_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
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
#ifndef SMART_PTR_H
#define SMART_PTR_H
#include <algorithm>
namespace wiz {
template < class T >
class SmartPtr
{
private:
class Inner {
public:
T* ptr = nullptr;
int count = 0;
};
private:
Inner* inner = nullptr;
int option;
private:
void quit()
{
if (inner) {
inner->count--;
if (0 == inner->count) {
delete inner;
}
inner = nullptr;
}
}
void enter(const SmartPtr<T>& sp)
{
//wizard::assertNotnullptr( sp );
//wizard::assertnullptr( this->ptr );
if (nullptr == sp.inner)
{
this->quit();
}
else if (nullptr == this->inner)
{
this->inner = sp.inner;
sp.inner->count++;
this->option = sp.option; ///
}
}
void initFromOther(const SmartPtr<T>& sp)
{
//wizard::assertNotEquals( this, &sp );
// if( this == sp.getThis() ) { return; }
// delete or quit
if (this->inner)
{
if (isOnlyOne()) //
{
remove();
}
else
{
quit();
}
}
// enter
enter(sp);
return;
}
public:
SmartPtr(T* ptr = nullptr)
: option(0)
{
// ptr <- new T(~~);
if (ptr) {
this->inner = new Inner();
this->inner->ptr = ptr;
this->inner->count = 1;
}
}
SmartPtr(T* ptr, const int option) // option 1,2,..
: option(option)
{
// ptr <- new T(~~);
if (ptr) {
this->inner = new Inner();
this->inner->ptr = ptr;
this->inner->count = 1;
}
}
SmartPtr(const SmartPtr<T>& sp)
: option(sp.option)
{
initFromOther(sp);
}
SmartPtr(SmartPtr<T>&& sp)
:option(sp.option)
{
Inner* temp = this->inner;
this->inner = sp.inner;
sp.inner = temp;
}
virtual ~SmartPtr() /// virtual??
{
if (isOnlyOne())
{
remove(false);
quit();
}
else
{
remove(false);
}
}
public:
SmartPtr<T>& operator=(T* ptr) {
if (inner && ptr) {
inner->ptr = ptr;
}
else if (!inner && ptr) {
inner = new Inner();
inner->ptr = ptr;
inner->count = 1;
}
return* this;
}
SmartPtr<T>& operator=(const SmartPtr<T>& _sp)
{
// temp link
SmartPtr<T> tempLink(_sp);
initFromOther(tempLink);
return *this;
}
T& operator*()
{
// wizard::assertNotnullptr( ptr );
return (*inner->ptr);
}
const T& operator*()const
{
// wizard::assertNotnullptr( ptr );
return (*inner->ptr);
}
public:
bool isOnlyOne()const
{
return this->inner && this->inner->count == 1;
}
bool isNULL()const
{
return nullptr == this->inner || nullptr == this->inner->ptr;
}
bool empty()const
{
return nullptr == this->inner;
}
/// remove return suceess?
bool remove()
{
return remove(true);
}
bool remove(const bool bremove) // make private and delete =true, and make public remove() call remove( true ); - 2012.3.5 todo...
{
if (empty()) { return false; }
if (!bremove && isOnlyOne()) { return false; } /// 2013.08.13 false means "no change"??
if (this->inner->ptr && bremove)
{
delete this->inner->ptr; this->inner->ptr = nullptr; // no dynamic.
quit();
}
else
{
quit();
}
return true;
}
public:
operator T*() {
if (this->inner) {
return this->inner->ptr;
}
return nullptr;
}
const T* operator->()const
{
//wizard::assertNotnullptr( ptr );
return inner->ptr;
}
T* operator->()
{
//wizard::assertNotnullptr( ptr );
return inner->ptr;
}
T* operator&()
{
return inner->ptr;
}
const T* operator&()const
{
return inner->ptr;
}
///
public:
bool hasSameObject(const SmartPtr<T>& wsp) const
{
return this->inner == wsp.inner;
}
};
}
#endif