1
#ifndef FNB_SMART_PTR_H
2
#define FNB_SMART_PTR_H
6
* A smart pointer class that provides a reference counting and auto delete memory.
8
* This class is similar to std::auto_ptr, with 2 exceptions:
9
* - This class uses reference counting
10
* - We dont provide a release() function (because of the reference counting)
11
* It is recommended to use this class instead of using raw pointer wherever possible.
13
* \note smart pointer to NULL is valid.
27
* The reference counting class
43
* Construct a reference counting class for row pointer data
46
SmartPtrRef(type_ptr data)
55
virtual ~SmartPtrRef()
61
* \return Pointer to the row data
63
type_ptr GetData() { return m_data; }
66
* Increase reference counting by 1
68
void IncRef() { m_refCount ++ ; }
72
* Decrease reference counting by 1
74
void DecRef() { m_refCount -- ; }
76
* Return the current reference counting
77
* \return current reference counting
79
int GetRefCount() { return m_refCount; }
86
* Construct smart pointer from ptr
89
wxFNBSmartPtr(type_ptr ptr)
91
// create a fresh copy
105
* \param rhs right hand side
107
wxFNBSmartPtr(const wxFNBSmartPtr& rhs)
114
* Assignment operator
115
* \param rhs right hand side
116
* \return reference to this
118
wxFNBSmartPtr& operator=(const wxFNBSmartPtr& rhs)
120
// increase the reference count
121
if( m_ref == rhs.m_ref )
124
// Delete previous reference
138
virtual ~wxFNBSmartPtr()
144
* Replace the current pointer with ptr
145
* if the current ptr is not NULL, it will be freed (reference counting free) before assingning the new ptr
146
* \param ptr new pointer
148
void Reset(type_ptr ptr)
155
* Return pointer the row data pointer
156
* \return pointer to the row data pointer
160
return m_ref->GetData();
164
* Overload the '->' operator
165
* \return pointer to the row data pointer
167
type_ptr operator->() const
169
return m_ref->GetData();
173
* Dereference operator
174
* \return dereference the row data
178
return *(m_ref->GetData());
182
* Test for NULL operator
183
* \return true if the internal row data or the reference counting class are NULL false otherwise
185
bool operator!() const
190
return m_ref->GetData() == NULL;
194
void DeleteRefCount()
196
// decrease the ref count (or delete pointer if it is 1)
199
if( m_ref->GetRefCount() == 1 )
209
void CreateFresh(type_ptr ptr)
211
m_ref = new SmartPtrRef( ptr );
215
#endif // FNB_SMART_PTR_H