6 * This file contains the SmartPtr definition.
8 * Based on CONFIG_USE_BOOST_POINTERS we can alternate using a custom SmartPtr class and boost::shared_ptr.
9 * Currently we use boost::shared_ptr since the custom pointer doesn't provide static_cast.
15 /** Statement is only executed because debug mode is enabled. */
18 /** Statement is not executed because debug mode is disabled. */
22 /** Statement is only executed because debug mode is enabled. */
25 /** Statement is not executed because debug mode is disabled. */
29 /** When defined this enables the use of boost::shared_pointer instead of a homebrow SmartPtr. */
30 #define CONFIG_USE_BOOST_POINTERS
31 #ifdef CONFIG_USE_BOOST_POINTERS
33 #include <boost/shared_ptr.hpp>
34 #include <boost/enable_shared_from_this.hpp>
36 /** Define SmartPtr as the type to use for smart pointers. */
37 #define SmartPtr boost::shared_ptr
39 /** Define boost::checked_delete as the method used to delete objects. */
40 #define SmartPtrDelete(target) void boost::checked_delete<target>(target* x);
44 /** Define SmartPtr as the class that will be used to delete objects. */
45 #define SmartPtrDelete(target) class SmartPtr<target>
48 * The reference counting class
56 * Construct a reference counting class for row pointer data
62 DEB( printf("SmartPtrCount ctor...\n"));
68 virtual ~SmartPtrCount()
70 DEB( printf("SmartPtrCount dtor...\n"));
74 * Increase reference counting by 1
76 void IncRef() { m_refCount
++ ; }
79 * Decrease reference counting by 1
81 void DecRef() { m_refCount
-- ; }
83 * Return the current reference counting
84 * \return current reference counting
86 int GetRefCount() { return m_refCount
; }
90 * A smart pointer class that provides a reference counting and auto delete memory.
92 * This class is similar to std::auto_ptr, with 2 exceptions:
93 * - This class uses reference counting
94 * - We dont provide a release() function (because of the reference counting)
95 * It is recommended to use this class instead of using raw pointer wherever possible.
97 * \note smart pointer to NULL is valid.
103 SmartPtrCount
* m_ref
;
107 * Construct smart pointer from ptr
112 DEB( printf("SmartPtr T* ctor...\n"));
113 // create a fresh copy
118 * Default constructor
123 DEB( printf("SmartPtr ctor...\n"));
124 m_ref
= new SmartPtrCount();
129 * \param rhs right hand side
131 SmartPtr(const SmartPtr
& rhs
)
135 DEB( printf("SmartPtr SmartPtr ctor...\n"));
140 SmartPtr(SmartPtr
<Y
> const& rhs
)
141 : m_data(rhs
.m_data
),
144 DEB( printf("SmartPtr SmartPtr<Y> ctor...\n"));
149 * Assignment operator
150 * \param rhs right hand side
151 * \return reference to this
153 SmartPtr
& operator=(const SmartPtr
& rhs
)
155 DEB2( printf("SmartPtr = operator...\n"));
157 // increase the reference count
158 if( m_ref
== rhs
.m_ref
)
160 DEB2( printf("m_ref = rhs.m_ref\n") );
164 // Delete previous reference
167 // TODO: What to do here?
170 DEB2( printf("!rhs.m_ref\n") );
174 DEB2( printf("no returns so far\n") );
187 DEB( printf("SmartPtr dtor...\n"));
192 * Replace the current pointer with ptr
193 * if the current ptr is not NULL, it will be freed (reference counting free) before assingning the new ptr
194 * \param ptr new pointer
196 void reset(T
* ptr
= 0)
203 * Return pointer the row data pointer
204 * \return pointer to the row data pointer
212 * Overload the '->' operator
213 * \return pointer to the row data pointer
215 T
* operator->() const
221 * Dereference operator
222 * \return dereference the row data
230 * Test for NULL operator
231 * \return true if the internal row data or the reference counting class are NULL false otherwise
233 bool operator!() const
235 return (!m_ref
|| !m_data
);
239 * test for bool operation
240 * \return true of the internal raw data exist and it is not null
242 operator bool() const
244 return m_ref
&& m_data
;
249 printf("%d: %p (%p)\n", m_ref
->GetRefCount(), m_data
, this);
253 void DeleteRefCount()
255 DEB2( printf("DeleteRefCount()\n") );
256 // decrease the ref count (or delete pointer if it is 1)
259 DEB2( printf("m_ref\n") );
260 if( m_ref
->GetRefCount() <= 1 )
262 DEB2( printf("m_ref->GetRefCount() <= 1\n") );
270 DEB2( printf("m_ref->GetRefCount() = %d\n", m_ref
->GetRefCount()) );
276 DEB2( printf("!m_ref\n") );
280 void CreateFresh(T
* ptr
)
282 m_ref
= new SmartPtrCount();
286 template<class Y
> friend class SmartPtr
;
289 #endif // CONFIG_USE_BOOST_POINTERS
290 #endif // SMART_PTR_H