soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SSharedPtr.hpp
1#ifndef _SSHAREDPTR_H_
2#define _SSHAREDPTR_H_
3
5
6SNSBEGIN
7
8/**
9 * @class PtrDisposer
10 * @brief Base class for disposing of a pointer.
11 * @tparam T The type of the pointer to be disposed.
12 * @details This class provides a virtual interface for deleting a pointer.
13 */
14template <class T>
16{
17public:
18 /**
19 * @brief Virtual destructor.
20 */
21 virtual ~PtrDisposer() {}
22
23 /**
24 * @brief Deletes the pointer.
25 * @param ptr Pointer to be deleted.
26 */
27 virtual void dispose(T *ptr) = 0;
28};
29
30/**
31 * @class DefaultPtrDisposer
32 * @brief Default implementation of PtrDisposer.
33 * @tparam T The type of the pointer to be disposed.
34 * @details This class provides a default implementation for deleting a pointer using the `delete` operator.
35 */
36template <class T>
38{
39public:
40 /**
41 * @brief Deletes the pointer.
42 * @param ptr Pointer to be deleted.
43 */
44 virtual void dispose(T *ptr)
45 {
46 if (ptr)
47 {
48 delete ptr;
49 }
50 }
51};
52
53/**
54 * @class SSharedCount
55 * @brief Manages the reference count for a shared pointer.
56 * @tparam T The type of the pointer.
57 * @tparam Disposer The disposer class used to delete the pointer.
58 * @details This class maintains the reference count and ensures that the pointer is deleted when the reference count reaches zero.
59 */
60template <class T, class Disposer = DefaultPtrDisposer<T>>
62{
63public:
64 /**
65 * @brief Constructor.
66 * @param ptr Pointer to be managed.
67 */
68 SSharedCount(T *ptr) : _ptr(ptr), _refCount(1), _cs(), _disposer(new Disposer())
69 {
70 }
71
72 /**
73 * @brief Destructor.
74 * @details Deletes the managed pointer using the disposer.
75 */
77 {
78 _disposer->dispose(_ptr);
79 delete _disposer;
80 }
81
82 /**
83 * @brief Increments the reference count.
84 */
86 {
87 SAutoLock autoLock(_cs);
88 ++_refCount;
89 }
90
91 /**
92 * @brief Decrements the reference count.
93 * @details Deletes the managed pointer if the reference count reaches zero.
94 */
96 {
97 bool shouldDelete = false;
98
99 {
100 SAutoLock autoLock(_cs);
101
102 if (_refCount > 0)
103 {
104 --_refCount;
105 }
106
107 if (_refCount == 0)
108 {
109 shouldDelete = true;
110 }
111 }
112
113 if (shouldDelete)
114 {
115 delete this;
116 }
117 }
118
119private:
120 T *_ptr; /**< Pointer to the managed object. */
121 unsigned int _refCount; /**< Reference count. */
122 SCriticalSection _cs; /**< Critical section for thread-safe operations. */
123 PtrDisposer<T> *_disposer; /**< Pointer to the disposer. */
124};
125
126/**
127 * @class SSharedPtr
128 * @brief A smart pointer class that manages the lifetime of an object using reference counting.
129 * @tparam T The type of the pointer.
130 * @tparam Disposer The disposer class used to delete the pointer.
131 * @details This class automatically deletes the managed object when the last reference to it is destroyed.
132 */
133template <class T, class Disposer = DefaultPtrDisposer<T>>
135{
136public:
137 /**
138 * @brief Default constructor.
139 * @details Initializes the shared pointer with a null pointer.
140 */
141 SSharedPtr() : _ptr(NULL), _sc(new SSharedCount<T, Disposer>(NULL))
142 {
143 }
144
145 /**
146 * @brief Constructor.
147 * @param ptr Pointer to be managed.
148 */
149 SSharedPtr(T *ptr) : _ptr(ptr), _sc(new SSharedCount<T, Disposer>(ptr))
150 {
151 }
152
153 /**
154 * @brief Copy constructor.
155 * @param obj Another SSharedPtr object.
156 * @details Increments the reference count of the managed pointer.
157 */
158 SSharedPtr(const SSharedPtr<T, Disposer> &obj) : _ptr(obj._ptr), _sc(obj._sc)
159 {
160 obj._sc->incRefCount();
161 }
162
163 /**
164 * @brief Destructor.
165 * @details Decrements the reference count of the managed pointer.
166 */
168 {
169 _sc->decRefCount();
170 }
171
172 /**
173 * @brief Assignment operator.
174 * @param obj Another SSharedPtr object.
175 * @return Reference to the current object.
176 * @details Decrements the reference count of the original pointer and increments the reference count of the new pointer.
177 */
179 {
180 if (this != &obj)
181 {
182 obj._sc->incRefCount();
183 _sc->decRefCount();
184 _sc = obj._sc;
185 _ptr = obj._ptr;
186 }
187
188 return *this;
189 }
190
191 /**
192 * @brief Returns the object pointer.
193 * @return Const pointer to the managed object.
194 */
195 const T *operator->() const
196 {
197 return _ptr;
198 }
199
200 /**
201 * @brief Returns the object pointer.
202 * @return Pointer to the managed object.
203 */
205 {
206 return _ptr;
207 }
208
209 /**
210 * @brief Returns the object pointer.
211 * @return Const pointer to the managed object.
212 */
213 const T *ptr() const
214 {
215 return _ptr;
216 }
217
218 /**
219 * @brief Returns the object pointer.
220 * @return Pointer to the managed object.
221 */
222 T *ptr()
223 {
224 return _ptr;
225 }
226
227 /**
228 * @brief Returns the object reference.
229 * @return Const reference to the managed object.
230 */
231 const T &operator*() const
232 {
233 return *_ptr;
234 }
235
236 /**
237 * @brief Returns the object reference.
238 * @return Reference to the managed object.
239 */
241 {
242 return *_ptr;
243 }
244
245 /**
246 * @brief Compares the pointer with another pointer.
247 * @param ptr Pointer to compare with.
248 * @return TRUE if the pointers are equal, FALSE otherwise.
249 */
250 bool operator==(T *ptr) const
251 {
252 return _ptr == ptr;
253 }
254
255 /**
256 * @brief Compares the pointer with another SSharedPtr object.
257 * @param obj SSharedPtr object to compare with.
258 * @return TRUE if the pointers are equal, FALSE otherwise.
259 */
260 bool operator==(const SSharedPtr<T, Disposer> &obj) const
261 {
262 return _ptr == obj._ptr;
263 }
264
265private:
266 T *_ptr; /**< Pointer to the managed object. */
267 SSharedCount<T, Disposer> *_sc; /**< Pointer to the shared count object. */
268};
269
270SNSEND
271
272#endif // _SSHAREDPTR_H_
Header file for SCriticalSection and SAutoLock classes.
Default implementation of PtrDisposer.
virtual void dispose(T *ptr)
Deletes the pointer.
Base class for disposing of a pointer.
virtual void dispose(T *ptr)=0
Deletes the pointer.
virtual ~PtrDisposer()
Virtual destructor.
Auto-lock class for managing critical sections.
Wrapper class for a critical section.
Manages the reference count for a shared pointer.
~SSharedCount()
Destructor.
void incRefCount()
Increments the reference count.
SSharedCount(T *ptr)
Constructor.
void decRefCount()
Decrements the reference count.
SSharedPtr()
Default constructor.
SSharedPtr(const SSharedPtr< T, Disposer > &obj)
Copy constructor.
T * ptr()
Returns the object pointer.
const T & operator*() const
Returns the object reference.
SSharedPtr & operator=(const SSharedPtr< T, Disposer > &obj)
Assignment operator.
~SSharedPtr()
Destructor.
bool operator==(const SSharedPtr< T, Disposer > &obj) const
Compares the pointer with another SSharedPtr object.
T & operator*()
Returns the object reference.
bool operator==(T *ptr) const
Compares the pointer with another pointer.
T * operator->()
Returns the object pointer.
const T * ptr() const
Returns the object pointer.
SSharedPtr(T *ptr)
Constructor.
const T * operator->() const
Returns the object pointer.