@@ -20,32 +20,61 @@ namespace YY
20
20
{
21
21
};
22
22
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
+
23
57
template <typename _Type>
24
58
class WeakPtr
25
59
{
26
60
private:
27
- _Type* p;
61
+ WeakPtrRef< _Type> * p = nullptr ;
28
62
29
63
public:
30
- constexpr WeakPtr () noexcept
31
- : p(nullptr )
32
- {
33
- }
64
+ constexpr WeakPtr () noexcept = default;
34
65
35
66
WeakPtr (_In_opt_ _Type* _pOther) noexcept
36
- : p(_pOther)
67
+ : p(GetWeakPtrRef( _pOther) )
37
68
{
38
- if (p)
39
- p->AddWeakRef ();
40
69
}
41
70
42
71
constexpr WeakPtr (_In_opt_ WeakPtrRef<_Type>* _pOther) noexcept
43
- : p(reinterpret_cast <_Type*>( _pOther) )
72
+ : p(_pOther)
44
73
{
45
74
}
46
75
47
76
WeakPtr (_In_ const WeakPtr& _pOther) noexcept
48
- : WeakPtr( _pOther.p)
77
+ : p(GetWeakPtrRef( reinterpret_cast <_Type*>( _pOther.p)) )
49
78
{
50
79
}
51
80
@@ -57,83 +86,74 @@ namespace YY
57
86
58
87
~WeakPtr ()
59
88
{
60
- if (p)
61
- p->ReleaseWeak ();
89
+ FreeWeakPtrRef (p);
62
90
}
63
91
64
92
inline void __YYAPI Attach (_In_opt_ WeakPtrRef<_Type>* _pOther) noexcept
65
93
{
66
- if (p)
67
- p->ReleaseWeak ();
68
- p = reinterpret_cast <_Type*>(_pOther);
94
+ FreeWeakPtrRef (p);
95
+ p = _pOther;
69
96
}
70
97
71
98
inline _Ret_maybenull_ WeakPtrRef<_Type>* __YYAPI Detach () noexcept
72
99
{
73
100
auto _p = p;
74
101
p = nullptr ;
75
- return reinterpret_cast <WeakPtrRef<_Type>*>(_p) ;
102
+ return _p ;
76
103
}
77
104
78
105
_Ret_maybenull_ RefPtr<_Type> __YYAPI Get () const noexcept
79
106
{
80
- RefPtr<_Type> _pTmp;
81
-
82
- if (p && p->TryAddRef ())
83
- {
84
- _pTmp.Attach (p);
85
- }
86
- return _pTmp;
107
+ return FromWeakPtr (p);
87
108
}
88
109
89
- bool operator ==(_In_opt_ _Type* _pOther) const noexcept
110
+ bool __YYAPI operator ==(_In_opt_ _Type* _pOther) const noexcept
90
111
{
91
- return p == _pOther;
112
+ return reinterpret_cast < void *>(p) == reinterpret_cast < void *>( _pOther) ;
92
113
}
93
114
94
- bool operator ==(_In_opt_ const WeakPtr& _pOther) const noexcept
115
+ bool __YYAPI operator ==(_In_opt_ const WeakPtr& _pOther) const noexcept
95
116
{
96
117
return p == _pOther.p ;
97
118
}
98
119
99
- WeakPtr& operator =(std:: nullptr_t ) noexcept
120
+ bool __YYAPI operator !=(_In_opt_ _Type* _pOther) const noexcept
100
121
{
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
+ }
103
129
130
+ WeakPtr& __YYAPI operator =(std::nullptr_t ) noexcept
131
+ {
132
+ FreeWeakPtrRef (p);
104
133
p = nullptr ;
105
134
return *this ;
106
135
}
107
136
108
- WeakPtr& operator =(_In_opt_ _Type* _pOther) noexcept
137
+ WeakPtr& __YYAPI operator =(_In_opt_ _Type* _pOther) noexcept
109
138
{
110
- if (p != _pOther)
139
+ if (reinterpret_cast < void *>(p) != reinterpret_cast < void *>( _pOther) )
111
140
{
112
- if (p)
113
- p->ReleaseWeak ();
114
-
115
- p = _pOther;
116
-
117
- if (p)
118
- p->AddWeakRef ();
141
+ FreeWeakPtrRef (p);
142
+ p = GetWeakPtrRef (_pOther);
119
143
}
120
144
return *this ;
121
145
}
122
146
123
- WeakPtr& operator =(const WeakPtr& _pOther) noexcept
147
+ WeakPtr& __YYAPI operator =(const WeakPtr& _pOther) noexcept
124
148
{
125
149
return this ->operator =(_pOther.p );
126
150
}
127
151
128
- WeakPtr& operator =(const WeakPtr&& _pOther) noexcept
152
+ WeakPtr& __YYAPI operator =(WeakPtr&& _pOther) noexcept
129
153
{
130
- if (&_pOther != this )
154
+ if (p != _pOther. p )
131
155
{
132
- if (p)
133
- p->ReleaseWeak ();
134
-
135
- p = _pOther.p ;
136
- _pOther.p = nullptr ;
156
+ Attach (_pOther.Detach ());
137
157
}
138
158
139
159
return *this ;
0 commit comments