Debian Codegraph @

wxcrafter/wxcLib/smart_ptr.h

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2008 by Eran Ifrah
5 // file name : smart_ptr.h
6 //
7 // -------------------------------------------------------------------------
8 // A
9 // _____ _ _ _ _
10 // / __ \ | | | | (_) |
11 // | / \/ ___ __| | ___| | _| |_ ___
12 // | | / _ \ / _ |/ _ \ | | | __/ _ )
13 // | \__/\ (_) | (_| | __/ |___| | || __/
14 // \____/\___/ \__,_|\___\_____/_|\__\___|
15 //
16 // F i l e
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 //////////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////////////////
25 #ifndef CODELITE_SMART_PTR_H
26 #define CODELITE_SMART_PTR_H
27
28 /**
29 * A smart pointer class that provides a reference counting and auto delete memory.
30 *
31 * This class is similar to std::auto_ptr, with 2 exceptions:
32 * - This class uses reference counting
33 * - We dont provide a release() function (because of the reference counting)
34 * It is recommended to use this class instead of using raw pointer wherever possible.
35 *
36 * \note smart pointer to NULL is valid.
37 *
38 * \ingroup CodeLite
39 * \version 1.0
40 * first version
41 * \date 09-17-2006
42 * \author Eran
43 */
44 template <class T>
45 class SmartPtr
46 {
47 /**
48 * The reference counting class
49 *
50 * \ingroup CodeLite
51 * \version 1.0
52 * first version
53 *
54 * \date 09-17-2006
55 * \author Eran
56 */
57 class SmartPtrRef
58 {
59 T* m_data;
60 int m_refCount;
61
62 public:
63 /**
64 * Construct a reference counting class for row pointer data
65 * \param data pointer
66 */
67 SmartPtrRef(T* data)
68 : m_data( data )
69 , m_refCount( 1 )
70 {
71 }
72
73 /**
74 * Destructor
75 */
76 virtual ~SmartPtrRef()
77 {
78 delete m_data;
79 }
80
81 /**
82 * \return Pointer to the row data
83 */
84 T* GetData() { return m_data; }
85
86 const T* GetData() const { return m_data; }
87 /**
88 * Increase reference counting by 1
89 */
90 void IncRef() { m_refCount ++ ; }
91
92
93 /**
94 * Decrease reference counting by 1
95 */
96 void DecRef() { m_refCount -- ; }
97 /**
98 * Return the current reference counting
99 * \return current reference counting
100 */
101 int GetRefCount() { return m_refCount; }
102 };
103
104 SmartPtrRef *m_ref;
105
106 public:
107 /**
108 * Construct smart pointer from ptr
109 * \param ptr pointer
110 */
111 SmartPtr(T* ptr)
112 {
113 // create a fresh copy
114 CreateFresh( ptr );
115 }
116
117 /**
118 * Default constructor
119 */
120 SmartPtr()
121 : m_ref(NULL)
122 {
123 }
124
125 /**
126 * Copy constructor
127 * \param rhs right hand side
128 */
129 SmartPtr(const SmartPtr& rhs)
130 : m_ref(NULL)
131 {
132 *this = rhs;
133 }
134
135 /**
136 * Assignment operator
137 * \param rhs right hand side
138 * \return reference to this
139 */
140 SmartPtr& operator=(const SmartPtr& rhs)
141 {
142 // increase the reference count
143 if( m_ref == rhs.m_ref )
144 return *this;
145
146 // Delete previous reference
147 DeleteRefCount();
148
149 if( !rhs.m_ref )
150 return *this;
151
152 m_ref = rhs.m_ref;
153 if (m_ref) {
154 m_ref->IncRef();
155 }
156 return *this;
157 }
158
159 /**
160 * Destructor
161 */
162 virtual ~SmartPtr()
163 {
164 DeleteRefCount();
165 }
166
167 /**
168 * Replace the current pointer with ptr
169 * if the current ptr is not NULL, it will be freed (reference counting free) before assingning the new ptr
170 * \param ptr new pointer
171 */
172 void reset(T* ptr)
173 {
174 DeleteRefCount();
175 CreateFresh( ptr );
176 }
177
178 /**
179 * Return pointer the row data pointer
180 * \return pointer to the row data pointer
181 */
182 T* get()
183 {
184 return m_ref->GetData();
185 }
186
187 const T* get() const
188 {
189 return m_ref->GetData();
190 }
191 /**
192 * Overload the '->' operator
193 * \return pointer to the row data pointer
194 */
195 T* operator->() const
196 {
197 return m_ref->GetData();
198 }
199
200 /**
201 * Dereference operator
202 * \return dereference the row data
203 */
204 T& operator*() const
205 {
206 return *(m_ref->GetData());
207 }
208
209 /**
210 * Test for NULL operator
211 * \return true if the internal row data or the reference counting class are NULL false otherwise
212 */
213 bool operator!() const
214 {
215 if( !m_ref )
216 return true;
217
218 return m_ref->GetData() == NULL;
219 }
220
221 /**
222 * test for bool operation
223 * \return true of the internal raw data exist and it is not null
224 */
225 operator bool() const
226 {
227 return m_ref && m_ref->GetData();
228 }
229
230 private:
231 void DeleteRefCount()
232 {
233 // decrease the ref count (or delete pointer if it is 1)
234 if( m_ref )
235 {
236 if( m_ref->GetRefCount() == 1 )
237 {
238 delete m_ref;
239 m_ref = NULL;
240 }
241 else
242 m_ref->DecRef();
243 }
244 };
245
246 void CreateFresh(T* ptr)
247 {
248 m_ref = new SmartPtrRef( ptr );
249 }
250 };
251
252
253 #endif // CODELITE_SMART_PTR_H