Skip to content

Commit 1499403

Browse files
author
xupengjie1
committed
Fea, WeakPtr抽象到GetWeakPtrRef、FromWeakPtr、FreeWeakPtrRef
1 parent 19ce40b commit 1499403

File tree

1 file changed

+66
-46
lines changed

1 file changed

+66
-46
lines changed

include/YY/Base/Memory/WeakPtr.h

Lines changed: 66 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,61 @@ namespace YY
2020
{
2121
};
2222

23+
template<typename _Type>
24+
static _Ret_maybenull_ YY::WeakPtrRef<_Type>* __YYAPI GetWeakPtrRef(_In_opt_ _Type* _pWeakRefPtr) noexcept
25+
{
26+
if (!_pWeakRefPtr)
27+
{
28+
return nullptr;
29+
}
30+
31+
_pWeakRefPtr->AddWeakRef();
32+
return reinterpret_cast<YY::WeakPtrRef<_Type>*>(_pWeakRefPtr);
33+
}
34+
35+
template<typename _Type>
36+
static YY::RefPtr<_Type> __YYAPI FromWeakPtr(_In_opt_ YY::WeakPtrRef<_Type>* _pWeakRefPtr) noexcept
37+
{
38+
auto _pPtr = reinterpret_cast<_Type*>(_pWeakRefPtr);
39+
if (_pPtr == nullptr || _pPtr->TryAddRef() == false)
40+
{
41+
return nullptr;
42+
}
43+
44+
return YY::RefPtr<_Type>::FromPtr(_pPtr);
45+
}
46+
47+
template<typename _Type>
48+
static void __YYAPI FreeWeakPtrRef(_In_opt_ YY::WeakPtrRef<_Type>* _pWeakRefPtr) noexcept
49+
{
50+
auto _pPtr = reinterpret_cast<_Type*>(_pWeakRefPtr);
51+
if (_pPtr)
52+
{
53+
_pPtr->ReleaseWeak();
54+
}
55+
}
56+
2357
template<typename _Type>
2458
class WeakPtr
2559
{
2660
private:
27-
_Type* p;
61+
WeakPtrRef<_Type>* p = nullptr;
2862

2963
public:
30-
constexpr WeakPtr() noexcept
31-
: p(nullptr)
32-
{
33-
}
64+
constexpr WeakPtr() noexcept = default;
3465

3566
WeakPtr(_In_opt_ _Type* _pOther) noexcept
36-
: p(_pOther)
67+
: p(GetWeakPtrRef(_pOther))
3768
{
38-
if (p)
39-
p->AddWeakRef();
4069
}
4170

4271
constexpr WeakPtr(_In_opt_ WeakPtrRef<_Type>* _pOther) noexcept
43-
: p(reinterpret_cast<_Type*>(_pOther))
72+
: p(_pOther)
4473
{
4574
}
4675

4776
WeakPtr(_In_ const WeakPtr& _pOther) noexcept
48-
: WeakPtr(_pOther.p)
77+
: p(GetWeakPtrRef(reinterpret_cast<_Type*>(_pOther.p)))
4978
{
5079
}
5180

@@ -57,83 +86,74 @@ namespace YY
5786

5887
~WeakPtr()
5988
{
60-
if (p)
61-
p->ReleaseWeak();
89+
FreeWeakPtrRef(p);
6290
}
6391

6492
inline void __YYAPI Attach(_In_opt_ WeakPtrRef<_Type>* _pOther) noexcept
6593
{
66-
if (p)
67-
p->ReleaseWeak();
68-
p = reinterpret_cast<_Type*>(_pOther);
94+
FreeWeakPtrRef(p);
95+
p = _pOther;
6996
}
7097

7198
inline _Ret_maybenull_ WeakPtrRef<_Type>* __YYAPI Detach() noexcept
7299
{
73100
auto _p = p;
74101
p = nullptr;
75-
return reinterpret_cast<WeakPtrRef<_Type>*>(_p);
102+
return _p;
76103
}
77104

78105
_Ret_maybenull_ RefPtr<_Type> __YYAPI Get() const noexcept
79106
{
80-
RefPtr<_Type> _pTmp;
81-
82-
if (p && p->TryAddRef())
83-
{
84-
_pTmp.Attach(p);
85-
}
86-
return _pTmp;
107+
return FromWeakPtr(p);
87108
}
88109

89-
bool operator==(_In_opt_ _Type* _pOther) const noexcept
110+
bool __YYAPI operator==(_In_opt_ _Type* _pOther) const noexcept
90111
{
91-
return p == _pOther;
112+
return reinterpret_cast<void*>(p) == reinterpret_cast<void*>(_pOther);
92113
}
93114

94-
bool operator==(_In_opt_ const WeakPtr& _pOther) const noexcept
115+
bool __YYAPI operator==(_In_opt_ const WeakPtr& _pOther) const noexcept
95116
{
96117
return p == _pOther.p;
97118
}
98119

99-
WeakPtr& operator=(std::nullptr_t) noexcept
120+
bool __YYAPI operator!=(_In_opt_ _Type* _pOther) const noexcept
100121
{
101-
if (p)
102-
p->ReleaseWeak();
122+
return reinterpret_cast<void*>(p) != reinterpret_cast<void*>(_pOther);
123+
}
124+
125+
bool __YYAPI operator!=(_In_opt_ const WeakPtr& _pOther) const noexcept
126+
{
127+
return p != _pOther.p;
128+
}
103129

130+
WeakPtr& __YYAPI operator=(std::nullptr_t) noexcept
131+
{
132+
FreeWeakPtrRef(p);
104133
p = nullptr;
105134
return *this;
106135
}
107136

108-
WeakPtr& operator=(_In_opt_ _Type* _pOther) noexcept
137+
WeakPtr& __YYAPI operator=(_In_opt_ _Type* _pOther) noexcept
109138
{
110-
if (p != _pOther)
139+
if (reinterpret_cast<void*>(p) != reinterpret_cast<void*>(_pOther))
111140
{
112-
if (p)
113-
p->ReleaseWeak();
114-
115-
p = _pOther;
116-
117-
if (p)
118-
p->AddWeakRef();
141+
FreeWeakPtrRef(p);
142+
p = GetWeakPtrRef(_pOther);
119143
}
120144
return *this;
121145
}
122146

123-
WeakPtr& operator=(const WeakPtr& _pOther) noexcept
147+
WeakPtr& __YYAPI operator=(const WeakPtr& _pOther) noexcept
124148
{
125149
return this->operator=(_pOther.p);
126150
}
127151

128-
WeakPtr& operator=(const WeakPtr&& _pOther) noexcept
152+
WeakPtr& __YYAPI operator=(WeakPtr&& _pOther) noexcept
129153
{
130-
if (&_pOther != this)
154+
if (p != _pOther.p)
131155
{
132-
if (p)
133-
p->ReleaseWeak();
134-
135-
p = _pOther.p;
136-
_pOther.p = nullptr;
156+
Attach(_pOther.Detach());
137157
}
138158

139159
return *this;

0 commit comments

Comments
 (0)