-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ex16_28_shared_ptr.h
63 lines (52 loc) · 1.5 KB
/
ex16_28_shared_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
#ifndef CP5_EX16_28_SHARED_PTR_H_
#define CP5_EX16_28_SHARED_PTR_H_
template <typename T> class SharedPtr {
using DelFuncPtr = void (*)(T*);
public:
SharedPtr(T* ptr = nullptr, DelFuncPtr del = nullptr)
: ptr_(ptr), count_ptr_(new size_t(ptr != nullptr)), del_(del)
{
}
~SharedPtr()
{
if (!ptr_) return;
if (--*count_ptr_ == 0) {
del_ ? del_(ptr_) : delete ptr_;
delete count_ptr_;
}
ptr_ = nullptr;
count_ptr_ = nullptr;
}
SharedPtr(const SharedPtr& sp) : ptr_(sp.ptr_), count_ptr_(sp.count_ptr_), del_(sp.del_)
{
++*count_ptr_;
}
SharedPtr& operator=(SharedPtr sp)
{
swap(sp);
return *this;
}
SharedPtr(const SharedPtr&& sp) noexcept : SharedPtr() { swap(sp); }
void reset(T* ptr = nullptr, DelFuncPtr del = nullptr)
{
SharedPtr tmp(ptr, del);
swap(tmp);
}
void swap(SharedPtr& r) noexcept
{
using std::swap;
swap(ptr_, r.ptr_);
swap(count_ptr_, r.count_ptr_);
swap(del_, r.del_);
}
T* get() const noexcept { return ptr_; }
T& operator*() const noexcept { return *get(); }
T* operator->() const noexcept { return get(); }
size_t use_count() const noexcept { return *count_ptr_; }
explicit operator bool() const noexcept { return ptr_ != nullptr; }
private:
T* ptr_ = nullptr;
size_t* count_ptr_ = nullptr;
DelFuncPtr del_ = nullptr;
};
#endif // CP5_EX16_28_SHARED_PTR_H_