~ubuntu-branches/ubuntu/hardy/codeblocks/hardy-backports

« back to all changes in this revision

Viewing changes to src/include/wxFlatNotebook/include/wx/wxFlatNotebook/fnb_smart_ptr.h

  • Committer: Bazaar Package Importer
  • Author(s): Michael Casadevall
  • Date: 2008-07-17 04:39:23 UTC
  • Revision ID: james.westby@ubuntu.com-20080717043923-gmsy5cwkdjswghkm
Tags: upstream-8.02
ImportĀ upstreamĀ versionĀ 8.02

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef FNB_SMART_PTR_H
 
2
#define FNB_SMART_PTR_H
 
3
 
 
4
 
 
5
/**
 
6
 * A smart pointer class that provides a reference counting and auto delete memory.
 
7
 *
 
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.
 
12
 *
 
13
 * \note smart pointer to NULL is valid.
 
14
 *
 
15
 * \ingroup CodeLite
 
16
 * \version 1.0
 
17
 * first version
 
18
 * \date 09-17-2006
 
19
 * \author Eran
 
20
 */
 
21
template <typename T>
 
22
class wxFNBSmartPtr
 
23
{
 
24
        typedef T* type_ptr;
 
25
 
 
26
        /**
 
27
     * The reference counting class
 
28
     *
 
29
         * \ingroup CodeLite
 
30
         * \version 1.0
 
31
         * first version
 
32
         *
 
33
         * \date 09-17-2006
 
34
         * \author Eran
 
35
         */
 
36
        class SmartPtrRef
 
37
        {
 
38
                type_ptr m_data;
 
39
                int m_refCount;
 
40
 
 
41
        public:
 
42
                /**
 
43
                 * Construct a reference counting class for row pointer data
 
44
                 * \param data pointer
 
45
                 */
 
46
                SmartPtrRef(type_ptr data)
 
47
                        : m_data( data )
 
48
                        , m_refCount( 1 )
 
49
                {
 
50
                }
 
51
 
 
52
                /**
 
53
                 * Destructor
 
54
                 */
 
55
                virtual ~SmartPtrRef()
 
56
                {
 
57
                        delete m_data;
 
58
                }
 
59
 
 
60
                /**
 
61
                 * \return Pointer to the row data 
 
62
                 */
 
63
                type_ptr GetData() { return m_data; }
 
64
 
 
65
                /**
 
66
                 * Increase reference counting by 1
 
67
                 */
 
68
                void IncRef() { m_refCount ++ ; }
 
69
 
 
70
 
 
71
                /**
 
72
                 * Decrease reference counting by 1
 
73
                 */
 
74
                void DecRef() { m_refCount -- ; }
 
75
                /**
 
76
                 * Return the current reference counting
 
77
                 * \return current reference counting
 
78
                 */
 
79
                int  GetRefCount() { return m_refCount; }
 
80
        };
 
81
 
 
82
        SmartPtrRef *m_ref;
 
83
 
 
84
public:
 
85
        /**
 
86
         * Construct smart pointer from ptr
 
87
         * \param ptr pointer
 
88
         */
 
89
        wxFNBSmartPtr(type_ptr ptr)
 
90
        {
 
91
                // create a fresh copy
 
92
                CreateFresh( ptr );
 
93
        }
 
94
        
 
95
        /**
 
96
         * Default constructor
 
97
         */
 
98
        wxFNBSmartPtr()
 
99
                : m_ref(NULL)
 
100
        {
 
101
        }
 
102
 
 
103
        /**
 
104
         * Copy constructor
 
105
         * \param rhs right hand side 
 
106
         */
 
107
        wxFNBSmartPtr(const wxFNBSmartPtr& rhs)
 
108
                : m_ref(NULL)
 
109
        {
 
110
                *this = rhs;
 
111
        }
 
112
 
 
113
        /**
 
114
         * Assignment operator
 
115
         * \param rhs right hand side 
 
116
         * \return reference to this
 
117
         */
 
118
        wxFNBSmartPtr& operator=(const wxFNBSmartPtr& rhs)
 
119
        {
 
120
                // increase the reference count
 
121
                if( m_ref == rhs.m_ref )
 
122
                        return *this;
 
123
 
 
124
                // Delete previous reference 
 
125
                DeleteRefCount();
 
126
 
 
127
                if( !rhs.m_ref )
 
128
                        return *this;
 
129
 
 
130
                m_ref = rhs.m_ref;
 
131
                m_ref->IncRef();
 
132
                return *this;
 
133
        }
 
134
 
 
135
        /**
 
136
         * Destructor
 
137
         */
 
138
        virtual ~wxFNBSmartPtr()
 
139
        {
 
140
                DeleteRefCount();
 
141
        }
 
142
 
 
143
        /**
 
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
 
147
         */
 
148
        void Reset(type_ptr ptr)
 
149
        {       
 
150
                DeleteRefCount();
 
151
                CreateFresh( ptr );
 
152
        }
 
153
 
 
154
        /**
 
155
         * Return pointer the row data pointer
 
156
         * \return pointer to the row data pointer
 
157
         */
 
158
        type_ptr Get()
 
159
        {
 
160
                return m_ref->GetData();
 
161
        }
 
162
 
 
163
        /**
 
164
         * Overload the '->' operator 
 
165
         * \return pointer to the row data pointer
 
166
         */
 
167
        type_ptr operator->() const 
 
168
        {
 
169
                return m_ref->GetData();
 
170
        }
 
171
 
 
172
        /**
 
173
         * Dereference operator
 
174
         * \return dereference the row data
 
175
         */
 
176
        T& operator*() const
 
177
        {
 
178
                return *(m_ref->GetData());
 
179
        }
 
180
 
 
181
        /**
 
182
         * Test for NULL operator
 
183
         * \return true if the internal row data or the reference counting class are NULL false otherwise
 
184
         */
 
185
        bool operator!() const
 
186
        {
 
187
                if( !m_ref )
 
188
                        return true;
 
189
 
 
190
                return m_ref->GetData() == NULL;
 
191
        }
 
192
 
 
193
private:
 
194
        void DeleteRefCount()
 
195
        {
 
196
                // decrease the ref count (or delete pointer if it is 1)
 
197
                if( m_ref )
 
198
                {
 
199
                        if( m_ref->GetRefCount() == 1 )
 
200
                        {
 
201
                                delete m_ref;
 
202
                                m_ref = NULL;
 
203
                        }
 
204
                        else
 
205
                                m_ref->DecRef();
 
206
                }
 
207
        };
 
208
 
 
209
        void CreateFresh(type_ptr ptr)
 
210
        {
 
211
                m_ref = new SmartPtrRef( ptr );
 
212
        }
 
213
};
 
214
 
 
215
#endif // FNB_SMART_PTR_H