librf

counted_ptr.h
1 #pragma once
2
3 namespace resumef
4 {
8 template <typename T>
9 struct counted_ptr
10 {
14 counted_ptr() noexcept = default;
15
19 counted_ptr(const counted_ptr& cp) : _p(cp._p)
20 {
21 _lock();
22 }
23
27 counted_ptr(T* p) : _p(p)
28 {
29 _lock();
30 }
31
35 counted_ptr(counted_ptr&& cp) noexcept
36 {
37 std::swap(_p, cp._p);
38 }
39
43 counted_ptr& operator=(const counted_ptr& cp)
44 {
45 if (&cp != this)
46 {
47 counted_ptr t = cp;
48 std::swap(_p, t._p);
49 }
50 return *this;
51 }
52
56 counted_ptr& operator=(counted_ptr&& cp) noexcept
57 {
58 if (&cp != this)
59 std::swap(_p, cp._p);
60 return *this;
61 }
62
66 ~counted_ptr()
67 {
68 _unlock();
69 }
70
74 T* operator->() const noexcept
75 {
76 return _p;
77 }
78
82 T* get() const noexcept
83 {
84 return _p;
85 }
86
90 void reset()
91 {
92 _unlock();
93 }
94 private:
95 void _unlock()
96 {
97 if (_p != nullptr)
98 {
99 auto t = _p;
100 _p = nullptr;
101 t->unlock();
102 }
103 }
104 void _lock(T* p)
105 {
106 if (p != nullptr)
107 p->lock();
108 _p = p;
109 }
110 void _lock()
111 {
112 if (_p != nullptr)
113 _p->lock();
114 }
115 T* _p = nullptr;
116 };
117
118 template <typename T, typename U>
119 inline bool operator == (const counted_ptr<T>& _Left, const counted_ptr<U>& _Right)
120 {
121 return _Left.get() == _Right.get();
122 }
123 template <typename T>
124 inline bool operator == (const counted_ptr<T>& _Left, std::nullptr_t)
125 {
126 return _Left.get() == nullptr;
127 }
128 template <typename T>
129 inline bool operator == (std::nullptr_t, const counted_ptr<T>& _Left)
130 {
131 return _Left.get() == nullptr;
132 }
133 template <typename T>
134 inline bool operator != (const counted_ptr<T>& _Left, std::nullptr_t)
135 {
136 return _Left.get() != nullptr;
137 }
138 template <typename T>
139 inline bool operator != (std::nullptr_t, const counted_ptr<T>& _Left)
140 {
141 return _Left.get() != nullptr;
142 }
143 }
144
resumef::counted_ptr::counted_ptr
counted_ptr(T *p)
通过裸指针构造一个计数指针。
Definition: counted_ptr.h:27
resumef::counted_ptr::operator=
counted_ptr & operator=(counted_ptr &&cp) noexcept
移动赋值函数。
Definition: counted_ptr.h:56
resumef::counted_ptr::counted_ptr
counted_ptr() noexcept=default
构造一个无内容的计数指针。
resumef::counted_ptr::counted_ptr
counted_ptr(counted_ptr &&cp) noexcept
移动构造函数。
Definition: counted_ptr.h:35
resumef::counted_ptr::get
T * get() const noexcept
获得管理的state指针。
Definition: counted_ptr.h:82
resumef::counted_ptr
专用与state的智能计数指针，通过管理state内嵌的引用计数来管理state的生存期。
Definition: counted_ptr.h:9
resumef::counted_ptr::reset
void reset()
重置为空指针。
Definition: counted_ptr.h:90
resumef::counted_ptr::operator->
T * operator->() const noexcept
重载指针操作符。
Definition: counted_ptr.h:74
resumef::counted_ptr::operator=
counted_ptr & operator=(const counted_ptr &cp)
拷贝赋值函数。
Definition: counted_ptr.h:43
resumef::counted_ptr::~counted_ptr
~counted_ptr()
析构函数中自动做一个计数减一操作。计数减为0，则删除state对象。
Definition: counted_ptr.h:66
resumef::counted_ptr::counted_ptr
counted_ptr(const counted_ptr &cp)
拷贝构造函数。
Definition: counted_ptr.h:19