/
Exer16_28_unique_ptr.h
260 lines (260 loc) · 6.86 KB
/
Exer16_28_unique_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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#ifndef EXER16_28_UP_H
#define EXER16_28_UP_H
#include <cstddef>
#include <utility>
#include <functional>
#include <stdexcept>
class DefaultDeleter {
public:
// as with any function template, the type of T is deduced by the compiler
template <typename T>
void operator()(T *p) const
{ delete p; }
};
// delete dynamic array
class DefaultDeleterArray {
public:
// as with any function template, the type of T is deduced by the compiler
template <typename T>
void operator()(T *p) const
{ delete []p; }
};
template <typename T, typename D = DefaultDeleter> class unique_ptr;
template <typename T>
void swap(unique_ptr<T>&, unique_ptr<T>&);
template <typename T, typename D>
class unique_ptr {
friend void swap<T>(unique_ptr<T>&, unique_ptr<T>&);
public:
// constructor
unique_ptr() : p(nullptr), del(D()) {}
// forbid implicit conversion
template <typename U>
explicit unique_ptr(U *t) : p(t), del(D()) {}
unique_ptr(const D &d) : p(nullptr), del(d) {}
// copy-control operations(copy version is deleted)
unique_ptr(const unique_ptr&) = delete;
unique_ptr(unique_ptr &&up) noexcept : p(std::move(up.p)), del(std::move(up.del)) {}
unique_ptr& operator=(const unique_ptr&) = delete;
unique_ptr& operator=(unique_ptr&&) noexcept;
// generic copy control members, see effective C++, 3rd edition, item 45
template <typename U, typename DU>
unique_ptr(unique_ptr<U, DU> &&up) noexcept : p(std::move(up.p)), del(std::move(up.del)) {}
template <typename U, typename DU>
unique_ptr& operator=(unique_ptr<U, DU> &&up) noexcept;
// std::nullptr_t is not covered in textbook, see http://en.cppreference.com/w/cpp/types/nullptr_t
unique_ptr& operator=(std::nullptr_t);
~unique_ptr();
// overloaded operators
T& operator*() const;
T* operator->() const;
explicit operator bool() const { return p; }
// other operations
T* get() const { return p; }
void swap(unique_ptr&);
T* release();
void reset();
void reset(T*);
void reset(std::nullptr_t);
private:
void free() const;
T *p;
D del;
};
template <typename T, typename D>
inline void unique_ptr<T, D>::free() const
{
if(p) {
del(p);
}
}
// move assignment
template <typename T, typename D>
unique_ptr<T, D>& unique_ptr<T, D>::operator=(unique_ptr &&rhs) noexcept
{
p = rhs.p;
del = rhs.del;
rhs.p = nullptr;
return *this;
}
template <typename T, typename D>
template <typename U, typename DU>
unique_ptr<T, D>& unique_ptr<T, D>::operator=(unique_ptr<U, DU> &&rhs) noexcept
{
p = rhs.p;
del = rhs.del;
rhs.p = nullptr;
return *this;
}
template <typename T, typename D>
unique_ptr<T, D>& unique_ptr<T, D>::operator=(std::nullptr_t)
{
free();
p = nullptr;
return *this;
}
template <typename T, typename D>
unique_ptr<T, D>::~unique_ptr()
{
free();
p = nullptr;
}
// overloaded operators
template <typename T, typename D>
inline T& unique_ptr<T, D>::operator*() const
{
if(!p)
throw std::runtime_error("dereference null pointer");
return *p;
}
template <typename T, typename D>
inline T* unique_ptr<T, D>::operator->() const
{
return &this->operator*();
}
// other operations
template <typename T>
void swap(unique_ptr<T> &lhs, unique_ptr<T> &rhs)
{
lhs.swap(rhs);
}
// non-member version calls member version, if we use member version call
// non-member version, we have to use ::swap for a hidden name
template <typename T, typename D>
inline void unique_ptr<T, D>::swap(unique_ptr &rhs)
{
using std::swap;
swap(this->p, rhs.p);
swap(this->del, rhs.del);
}
template <typename T, typename D>
T* unique_ptr<T, D>::release()
{
auto ret = p;
p = nullptr;
return ret;
}
template <typename T, typename D>
void unique_ptr<T, D>::reset()
{
free();
p = nullptr;
}
template <typename T, typename D>
void unique_ptr<T, D>::reset(T *t)
{
free();
p = t;
}
template <typename T, typename D>
void unique_ptr<T, D>::reset(std::nullptr_t)
{
free();
p = nullptr;
}
// dynamic array version
template <typename T, typename D> class unique_ptr<T[], D>;
template <typename T>
void swap(unique_ptr<T[]>&, unique_ptr<T[]>&);
template <typename T, typename D = DefaultDeleterArray>
class unique_ptr<T[], D> {
friend void swap<T>(unique_ptr<T>&, unique_ptr<T>&);
public:
// constructor
unique_ptr() : p(nullptr), del(D()) {}
// forbid implicit conversion
explicit unique_ptr(T *t) : p(t), del(D()) {}
unique_ptr(const D &d) : p(nullptr), del(d) {}
// copy-control operations(copy version is deleted)
unique_ptr(const unique_ptr&) = delete;
unique_ptr(unique_ptr &&up) noexcept : p(std::move(up.p)), del(std::move(del)) {}
unique_ptr& operator=(const unique_ptr&) = delete;
unique_ptr& operator=(unique_ptr&&) noexcept;
// std::nullptr_t is not covered in textbook, see http://en.cppreference.com/w/cpp/types/nullptr_t
unique_ptr& operator=(std::nullptr_t);
~unique_ptr();
// overloaded operators
T& operator[](std::size_t n) const { return p[n]; }
explicit operator bool() const { return p; }
// other operations
T* get() const { return p; }
void swap(unique_ptr&);
void release();
void reset();
void reset(T*);
void reset(std::nullptr_t);
private:
void free() const;
T *p;
D del;
};
template <typename T, typename D>
inline void unique_ptr<T[], D>::free() const
{
if(p) {
del(p);
}
}
// constructor
template <typename T, typename D>
unique_ptr<T[], D>& unique_ptr<T[], D>::operator=(unique_ptr &&rhs) noexcept
{
p = rhs.p;
del = rhs.del;
rhs.p = nullptr;
}
template <typename T, typename D>
unique_ptr<T[], D>& unique_ptr<T[], D>::operator=(std::nullptr_t)
{
free();
p = nullptr;
return *this;
}
template <typename T, typename D>
unique_ptr<T[], D>::~unique_ptr()
{
free();
}
// other operations
template <typename T>
void swap(unique_ptr<T[]> &lhs, unique_ptr<T> &rhs)
{
lhs.swap(rhs);
}
// non-member version calls member version, if we use member version call
// non-member version, we have to use ::swap for a hidden name
template <typename T, typename D>
inline void unique_ptr<T[], D>::swap(unique_ptr &rhs)
{
using std::swap;
swap(this->p, rhs.p);
swap(this->del, rhs.del);
}
template <typename T, typename D>
void unique_ptr<T[], D>::release()
{
free();
p = nullptr;
}
template <typename T, typename D>
void unique_ptr<T[], D>::reset()
{
free();
p = nullptr;
}
template <typename T, typename D>
void unique_ptr<T[], D>::reset(T *t)
{
free();
p = t;
}
template <typename T, typename D>
void unique_ptr<T[], D>::reset(std::nullptr_t)
{
free();
p = nullptr;
}
#endif
// Note: because release and reset might change the value of one object, they can't
// be applied to const unique_ptr. Because all of this functions call a private
// function free() to free resource, we can not write p = nullptr in free.