Debian Codegraph @

CodeLite/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 #include <stdlib.h>
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> class SmartPtr
45 {
46 /**
47 * The reference counting class
48 *
49 * \ingroup CodeLite
50 * \version 1.0
51 * first version
52 *
53 * \date 09-17-2006
54 * \author Eran
55 */
56 class SmartPtrRef
57 {
58 T* m_data;
59 int m_refCount;
60
61 public:
62 /**
63 * Construct a reference counting class for row pointer data
64 * \param data pointer
65 */
66 SmartPtrRef(T* data)
67 : m_data(data)
68 , m_refCount(1)
69 {
70 }
71
72 /**
73 * Destructor
74 */
75 virtual ~SmartPtrRef() { delete m_data; }
76
77 /**
78 * \return Pointer to the row data
79 */
80 T* GetData() { return m_data; }
81
82 const T* GetData() const { return m_data; }
83 /**
84 * Increase reference counting by 1
85 */
86 void IncRef() { m_refCount++; }
87
88 /**
89 * Decrease reference counting by 1
90 */
91 void DecRef() { m_refCount--; }
92 /**
93 * Return the current reference counting
94 * \return current reference counting
95 */
96 int GetRefCount() { return m_refCount; }
97 };
98
99 SmartPtrRef* m_ref;
100
101 public:
102 /**
103 * Construct smart pointer from ptr
104 * \param ptr pointer
105 */
106 SmartPtr(T* ptr)
107 {
108 // create a fresh copy
109 CreateFresh(ptr);
110 }
111
112 /**
113 * Default constructor
114 */
115 SmartPtr()
116 : m_ref(NULL)
117 {
118 }
119
120 /**
121 * Copy constructor
122 * \param rhs right hand side
123 */
124 SmartPtr(const SmartPtr& rhs)
125 : m_ref(NULL)
126 {
127 *this = rhs;
128 }
129
130 /**
131 * Assignment operator
132 * \param rhs right hand side
133 * \return reference to this
134 */
135 SmartPtr& operator=(const SmartPtr& rhs)
136 {
137 // increase the reference count
138 if(m_ref == rhs.m_ref) return *this;
139
140 // Delete previous reference
141 DeleteRefCount();
142
143 if(!rhs.m_ref) return *this;
144
145 m_ref = rhs.m_ref;
146 if(m_ref) {
147 m_ref->IncRef();
148 }
149 return *this;
150 }
151
152 /**
153 * Destructor
154 */
155 virtual ~SmartPtr() { DeleteRefCount(); }
156
157 /**
158 * Replace the current pointer with ptr
159 * if the current ptr is not NULL, it will be freed (reference counting free) before assingning the new ptr
160 * \param ptr new pointer
161 */
162 void Reset(T* ptr)
163 {
164 DeleteRefCount();
165 CreateFresh(ptr);
166 }
167
168 /**
169 * Return pointer the row data pointer
170 * \return pointer to the row data pointer
171 */
172 T* Get() { return m_ref->GetData(); }
173
174 const T* Get() const { return m_ref->GetData(); }
175 /**
176 * Overload the '->' operator
177 * \return pointer to the row data pointer
178 */
179 T* operator->() const { return m_ref->GetData(); }
180
181 /**
182 * Dereference operator
183 * \return dereference the row data
184 */
185 T& operator*() const { return *(m_ref->GetData()); }
186
187 /**
188 * Test for NULL operator
189 * \return true if the internal row data or the reference counting class are NULL false otherwise
190 */
191 bool operator!() const
192 {
193 if(!m_ref) return true;
194
195 return m_ref->GetData() == NULL;
196 }
197
198 /**
199 * test for bool operation
200 * \return true of the internal raw data exist and it is not null
201 */
202 operator bool() const { return m_ref && m_ref->GetData(); }
203
204 private:
205 void DeleteRefCount()
206 {
207 // decrease the ref count (or delete pointer if it is 1)
208 if(m_ref) {
209 if(m_ref->GetRefCount() == 1) {
210 delete m_ref;
211 m_ref = NULL;
212 } else {
213 m_ref->DecRef();
214 }
215 }
216 };
217
218 void CreateFresh(T* ptr) { m_ref = new SmartPtrRef(ptr); }
219 };
220
221 #endif // CODELITE_SMART_PTR_H