soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
obj-ref-impl.hpp
1// IObjRef的实现类
2#ifndef __OBJ_REF_IMPL_HPP
3#define __OBJ_REF_IMPL_HPP
4#include <windows.h>
5#include <sdef.h>
6#include <objbase.h>
7#include <interface/obj-ref-i.h>
8
9SNSBEGIN
10
11/// @brief Template class implementing the IObjRef interface.
12/// @tparam T The base class that implements the IObjRef interface.
13template<class T>
14class TObjRefImpl : public T
15{
16public:
17 /// @brief Constructor that initializes the reference count to 1.
19 {
20 }
21
22 /// @brief Virtual destructor.
23 virtual ~TObjRefImpl()
24 {
25 }
26
27 /// @brief Increments the reference count.
28 /// @return The new reference count.
29 STDMETHOD_(long, AddRef)(THIS) override
30 {
31 return InterlockedIncrement(&m_cRef);
32 }
33
34 /// @brief Decrements the reference count and deletes the object if the count reaches zero.
35 /// @return The new reference count.
36 STDMETHOD_(long, Release)(THIS) override
37 {
38 long lRet = InterlockedDecrement(&m_cRef);
39 if (lRet == 0)
40 {
42 }
43 return lRet;
44 }
45
46 /// @brief Deletes the object.
47 STDMETHOD_(void, OnFinalRelease)(THIS) override
48 {
49 delete this;
50 }
51
52protected:
53 /// @brief Reference count.
54 LONG m_cRef;
55};
56
57/// @brief Template class extending TObjRefImpl with a specific final release behavior.
58/// @tparam T The base class that implements the IObjRef interface.
59/// @tparam T2 The derived class that should be deleted in OnFinalRelease.
60template<class T, class T2>
61class TObjRefImpl2 : public TObjRefImpl<T>
62{
63public:
64 /// @brief Deletes the object as the derived type T2.
65 STDMETHOD_(void, OnFinalRelease)(THIS) override
66 {
67 delete static_cast<T2*>(this);
68 }
69};
70
71/// @brief Smart pointer class for managing COM-style reference-counted objects.
72/// @tparam T The type of the object being managed.
73template <class T>
75{
76public:
77 /// @brief Default constructor that initializes the pointer to NULL.
79 {
80 p = NULL;
81 }
82
83 /// @brief Constructor that takes a pointer and optionally adds a reference.
84 /// @param lp The pointer to the object.
85 /// @param bAddRef Whether to add a reference to the object.
86 SAutoRefPtr(T* lp, BOOL bAddRef = TRUE)
87 {
88 p = lp;
89 if (p != NULL && bAddRef)
90 {
91 p->AddRef();
92 }
93 }
94
95 /// @brief Copy constructor that adds a reference to the object.
96 /// @param src The source smart pointer.
98 {
99 p = src.p;
100 if (p)
101 {
102 p->AddRef();
103 }
104 }
105
106 /// @brief Destructor that releases the object.
108 {
109 if (p)
110 {
111 p->Release();
112 }
113 }
114
115 /// @brief Overloaded operator-> to access members of the object.
116 /// @return The pointer to the object.
117 T* operator->() const
118 {
119 return p;
120 }
121
122 /// @brief Overloaded cast operator to T*.
123 /// @return The pointer to the object.
124 operator T*() const
125 {
126 return p;
127 }
128
129 /// @brief Overloaded dereference operator.
130 /// @return The reference to the object.
131 T& operator*() const
132 {
133 return *p;
134 }
135
136 /// @brief Overloaded address-of operator.
137 /// @return The address of the pointer.
139 {
140 SASSERT(p == NULL);
141 return &p;
142 }
143
144 /// @brief Overloaded not operator to check if the pointer is NULL.
145 /// @return TRUE if the pointer is NULL, FALSE otherwise.
146 bool operator!() const
147 {
148 return (p == NULL);
149 }
150
151 /// @brief Overloaded less-than operator.
152 /// @param pT The pointer to compare with.
153 /// @return TRUE if the current pointer is less than pT, FALSE otherwise.
154 bool operator<(T* pT) const
155 {
156 return p < pT;
157 }
158
159 /// @brief Overloaded not-equal operator.
160 /// @param pT The pointer to compare with.
161 /// @return TRUE if the current pointer is not equal to pT, FALSE otherwise.
162 bool operator!=(T* pT) const
163 {
164 return !operator==(pT);
165 }
166
167 /// @brief Overloaded equal operator.
168 /// @param pT The pointer to compare with.
169 /// @return TRUE if the current pointer is equal to pT, FALSE otherwise.
170 bool operator==(T* pT) const
171 {
172 return p == pT;
173 }
174
175 /// @brief Overloaded assignment operator from a raw pointer.
176 /// @param lp The pointer to assign.
177 /// @return The pointer to the object.
178 T* operator=(T* lp)
179 {
180 if (*this != lp)
181 {
182 if (p)
183 {
184 p->Release();
185 }
186 p = lp;
187 if (p)
188 {
189 p->AddRef();
190 }
191 }
192 return *this;
193 }
194
195 /// @brief Overloaded assignment operator from another SAutoRefPtr.
196 /// @param lp The source smart pointer.
197 /// @return The pointer to the object.
199 {
200 if (*this != lp)
201 {
202 if (p)
203 {
204 p->Release();
205 }
206 p = lp.p;
207 if (p)
208 {
209 p->AddRef();
210 }
211 }
212 return *this;
213 }
214
215 /// @brief Releases the object and sets the pointer to NULL.
216 void Release()
217 {
218 T* pTemp = p;
219 if (pTemp)
220 {
221 p = NULL;
222 pTemp->Release();
223 }
224 }
225
226 /// @brief Attaches to an existing object without adding a reference.
227 /// @param p2 The pointer to attach.
228 void Attach(T* p2)
229 {
230 if (p)
231 {
232 p->Release();
233 }
234 p = p2;
235 }
236
237 /// @brief Detaches the object without releasing it.
238 /// @return The pointer to the object.
240 {
241 T* pt = p;
242 p = NULL;
243 return pt;
244 }
245
246 /// @brief Copies the pointer to another location and adds a reference.
247 /// @param ppT The destination pointer.
248 /// @return S_OK if successful, E_POINTER if ppT is NULL.
249 HRESULT CopyTo(T** ppT)
250 {
251 if (ppT == NULL)
252 return E_POINTER;
253 *ppT = p;
254 if (p)
255 {
256 p->AddRef();
257 }
258 return S_OK;
259 }
260
261protected:
262 /// @brief Pointer to the managed object.
263 T* p;
264};
265
266/// @brief Macro for compatibility with CAutoRefPtr.
267#define CAutoRefPtr SAutoRefPtr
268
269SNSEND
270
271#endif //__OBJ_REF_IMPL_HPP
~SAutoRefPtr()
Destructor that releases the object.
SAutoRefPtr()
Default constructor that initializes the pointer to NULL.
HRESULT CopyTo(T **ppT)
Copies the pointer to another location and adds a reference.
void Release()
Releases the object and sets the pointer to NULL.
bool operator!=(T *pT) const
Overloaded not-equal operator.
T * operator=(T *lp)
Overloaded assignment operator from a raw pointer.
T * operator=(const SAutoRefPtr< T > &lp)
Overloaded assignment operator from another SAutoRefPtr.
bool operator<(T *pT) const
Overloaded less-than operator.
T * p
Pointer to the managed object.
T * Detach()
Detaches the object without releasing it.
T * operator->() const
Overloaded operator-> to access members of the object.
SAutoRefPtr(const SAutoRefPtr &src)
Copy constructor that adds a reference to the object.
bool operator!() const
Overloaded not operator to check if the pointer is NULL.
SAutoRefPtr(T *lp, BOOL bAddRef=TRUE)
Constructor that takes a pointer and optionally adds a reference.
T & operator*() const
Overloaded dereference operator.
bool operator==(T *pT) const
Overloaded equal operator.
void Attach(T *p2)
Attaches to an existing object without adding a reference.
T ** operator&()
Overloaded address-of operator.
Template class extending TObjRefImpl with a specific final release behavior.
void OnFinalRelease() override
Deletes the object as the derived type T2.
void OnFinalRelease() override
Deletes the object.
virtual ~TObjRefImpl()
Virtual destructor.
LONG m_cRef
Reference count.
long AddRef() override
Increments the reference count.
TObjRefImpl()
Constructor that initializes the reference count to 1.
long Release() override
Decrements the reference count and deletes the object if the count reaches zero.