Guiliani  Version 2.5 revision 7293 (documentation build 13)
SharedPtr.h
1/*
2* Copyright (C) TES Electronic Solutions GmbH,
3* All Rights Reserved.
4* Contact: info@guiliani.de
5*
6* This file is part of the Guiliani HMI framework
7* for the development of graphical user interfaces on embedded systems.
8*/
9
10#ifndef SHARED_PTR_H
11#define SHARED_PTR_H
12
72namespace NSmartPtr
73{
114 template<class C>
116 {
117 public:
119 inline SharedPtr();
120
124 inline SharedPtr(C* rawPtr);
125
129 inline SharedPtr(const SharedPtr& rhs) :
130 m_RawPtr(rhs.m_RawPtr)
131 {
132 if (m_RawPtr)
133 {
134 m_RawPtr->AddRef();
135 }
136 }
137
142 template< class C2 >
143 inline SharedPtr(const SharedPtr<C2>& otherSharedPtr) :
144 m_RawPtr(otherSharedPtr.RawPtr())
145 {
146 if (m_RawPtr)
147 {
148 m_RawPtr->AddRef();
149 }
150 }
151
153 inline ~SharedPtr();
154
156 inline C* operator->() const;
157
159 inline C& operator*() const;
160
166 {
167 Assign(rhs.RawPtr());
168
169 return *this;
170 }
171
176 template<class C2>
177 inline SharedPtr<C>& operator=(const SharedPtr<C2>& otherSharedPtr)
178 {
179 Assign(otherSharedPtr.RawPtr());
180
181 return *this;
182 }
183
189 template<class C2>
190 inline SharedPtr& operator=(C2* rawPtr)
191 {
192 Assign(rawPtr);
193
194 return *this;
195 }
196
200 inline bool operator!() const;
201
205 inline operator bool() const;
206
210 inline C* RawPtr() const;
211
212 private:
214 C * m_RawPtr;
215
216 inline void Assign(C* rawPtr);
217 };
218
225 template<class C1, class C2>
226 inline bool operator!=(
229 {
230 return (a.RawPtr() != b.RawPtr());
231 }
232
239 template<class C1, class C2>
240 inline bool operator==(
243 {
244 return (a.RawPtr() == b.RawPtr());
245 }
246
251 template< class DEST_TYPE, class SOURCE_TYPE >
254 {
255 return static_cast< DEST_TYPE* >(source.RawPtr());
256 }
257
259 // inline methods
261
262 template<class C>
264 m_RawPtr(0)
265 {
266 }
267
268 template<class C>
269 inline SharedPtr<C>::SharedPtr(C* rawPtr) :
270 m_RawPtr(rawPtr)
271 {
272 if (rawPtr)
273 {
274 m_RawPtr->AddRef();
275 }
276 }
277
278 template<class C>
280 {
281 if (m_RawPtr)
282 {
283 m_RawPtr->Release();
284 }
285 }
286
287 template<class C>
288 inline C* SharedPtr<C>::operator->() const
289 {
290 return m_RawPtr;
291 }
292
293 template<class C>
294 inline C& SharedPtr<C>::operator*() const
295 {
296 return *m_RawPtr;
297 }
298
299 template<class C>
301 {
302 return (m_RawPtr == NULL);
303 }
304
305 template<class C>
307 {
308 return (m_RawPtr != NULL);
309 }
310
311 template<class C>
313 {
314 return m_RawPtr;
315 }
316
317 template<class C>
318 inline void SharedPtr<C>::Assign(C* rawPtr)
319 {
320 // call AddRef before Release, because rawPtr may be equal m_RawPtr
321 if (rawPtr)
322 {
323 rawPtr->AddRef();
324 }
325 if (m_RawPtr)
326 {
327 m_RawPtr->Release();
328 }
329 m_RawPtr = rawPtr;
330 }
331
332} // namespace NSmartPtr
333
334#endif // SMART_PTR_H
The reference counted pointer class used with RefCounted.
Definition: SharedPtr.h:116
SharedPtr()
Creates a shared NULL pointer.
Definition: SharedPtr.h:263
bool operator!() const
Definition: SharedPtr.h:300
SharedPtr(C *rawPtr)
Definition: SharedPtr.h:269
C * RawPtr() const
Definition: SharedPtr.h:312
~SharedPtr()
Destroys the SharedPtr.
Definition: SharedPtr.h:279
C & operator*() const
Definition: SharedPtr.h:294
SharedPtr(const SharedPtr< C2 > &otherSharedPtr)
Definition: SharedPtr.h:143
SharedPtr & operator=(const SharedPtr &rhs)
Definition: SharedPtr.h:165
C * operator->() const
Definition: SharedPtr.h:288
SharedPtr< C > & operator=(const SharedPtr< C2 > &otherSharedPtr)
Definition: SharedPtr.h:177
SharedPtr & operator=(C2 *rawPtr)
Definition: SharedPtr.h:190
SharedPtr(const SharedPtr &rhs)
Definition: SharedPtr.h:129
bool operator==(const NSmartPtr::SharedPtr< C1 > &a, const NSmartPtr::SharedPtr< C2 > &b)
Definition: SharedPtr.h:240
bool operator!=(const NSmartPtr::SharedPtr< C1 > &a, const NSmartPtr::SharedPtr< C2 > &b)
Definition: SharedPtr.h:226
Contains pointer class templates.
Definition: GUIRefCntPtr.h:17
NSmartPtr::SharedPtr< DEST_TYPE > StaticCast(const NSmartPtr::SharedPtr< SOURCE_TYPE > &source)
Definition: SharedPtr.h:253