summaryrefslogtreecommitdiff
path: root/thirdparty/thekla_atlas/nvcore/Ptr.h
blob: b43039274bdd15b335fb39055621d51376ee274e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// This code is in the public domain -- Ignacio Castaño <castano@gmail.com>

#ifndef NV_CORE_PTR_H
#define NV_CORE_PTR_H

#include "nvcore.h"
#include "Debug.h"

#include "RefCounted.h"

namespace nv
{
    class WeakProxy;

    /** Simple auto pointer template class.
    *
    * This is very similar to the standard auto_ptr class, but with some 
    * additional limitations to make its use less error prone:
    * - Copy constructor and assignment operator are disabled.
    * - reset method is removed.
    * 
    * The semantics of the standard auto_ptr are not clear and change depending
    * on the std implementation. For a discussion of the problems of auto_ptr read:
    * http://www.awprofessional.com/content/images/020163371X/autoptrupdate\auto_ptr_update.html
    */
    template <class T>
    class AutoPtr
    {
        NV_FORBID_COPY(AutoPtr);
        NV_FORBID_HEAPALLOC();
    public:

        /// Ctor.
        AutoPtr(T * p = NULL) : m_ptr(p) { }

        template <class Q>
        AutoPtr(Q * p) : m_ptr(static_cast<T *>(p)) { }

        /// Dtor. Deletes owned pointer.
        ~AutoPtr() {
            delete m_ptr;
            m_ptr = NULL;
        }

        /// Delete owned pointer and assign new one.
        void operator=( T * p ) {
            if (p != m_ptr)
            {
                delete m_ptr;
                m_ptr = p;
            }
        }

        template <class Q>
        void operator=( Q * p ) {
            if (p != m_ptr)
            {
                delete m_ptr;
                m_ptr = static_cast<T *>(p);
            }
        }

        /// Member access.
        T * operator -> () const {
            nvDebugCheck(m_ptr != NULL);
            return m_ptr;
        }

        /// Get reference.
        T & operator*() const {
            nvDebugCheck(m_ptr != NULL);
            return *m_ptr;
        }

        /// Get pointer.
        T * ptr() const { return m_ptr; }

        /// Relinquish ownership of the underlying pointer and returns that pointer.
        T * release() {
            T * tmp = m_ptr;
            m_ptr = NULL;
            return tmp;
        }

        /// Const pointer equal comparation.
        friend bool operator == (const AutoPtr<T> & ap, const T * const p) {
            return (ap.ptr() == p);
        }

        /// Const pointer nequal comparation.
        friend bool operator != (const AutoPtr<T> & ap, const T * const p) {
            return (ap.ptr() != p);
        }

        /// Const pointer equal comparation.
        friend bool operator == (const T * const p, const AutoPtr<T> & ap) {
            return (ap.ptr() == p);
        }

        /// Const pointer nequal comparation.
        friend bool operator != (const T * const p, const AutoPtr<T> & ap) {
            return (ap.ptr() != p);
        }

    private:
        T * m_ptr;
    };


    /// Smart pointer template class.
    template <class BaseClass>
    class SmartPtr {
    public:

        // BaseClass must implement addRef() and release().
        typedef SmartPtr<BaseClass> ThisType;

        /// Default ctor.
        SmartPtr() : m_ptr(NULL) 
        {
        }

        /// Other type assignment.
        template <class OtherBase>
        SmartPtr( const SmartPtr<OtherBase> & tc )
        {
            m_ptr = static_cast<BaseClass *>( tc.ptr() );
            if (m_ptr) {
                m_ptr->addRef();
            }
        }

        /// Copy ctor.
        SmartPtr( const ThisType & bc )
        {
            m_ptr = bc.ptr();
            if (m_ptr) {
                m_ptr->addRef();
            }
        }

        /// Copy cast ctor. SmartPtr(NULL) is valid.
        explicit SmartPtr( BaseClass * bc )
        {
            m_ptr = bc;
            if (m_ptr) {
                m_ptr->addRef();
            }
        }

        /// Dtor.
        ~SmartPtr()
        {
            set(NULL);
        }


        /// -> operator.
        BaseClass * operator -> () const
        {
            nvCheck( m_ptr != NULL );
            return m_ptr;
        }

        /// * operator.
        BaseClass & operator*() const
        {
            nvCheck( m_ptr != NULL );
            return *m_ptr;
        }

        /// Get pointer.
        BaseClass * ptr() const
        {
            return m_ptr;
        }

        /// Other type assignment.
        template <class OtherBase>
        void operator = ( const SmartPtr<OtherBase> & tc )
        {
            set( static_cast<BaseClass *>(tc.ptr()) );
        }

        /// This type assignment.
        void operator = ( const ThisType & bc )
        {
            set( bc.ptr() );
        }

        /// Pointer assignment.
        void operator = ( BaseClass * bc )
        {
            set( bc );
        }


        /// Other type equal comparation.
        template <class OtherBase>
        bool operator == ( const SmartPtr<OtherBase> & other ) const
        {
            return m_ptr == other.ptr();
        }

        /// This type equal comparation.
        bool operator == ( const ThisType & bc ) const
        {
            return m_ptr == bc.ptr();
        }

        /// Const pointer equal comparation.
        bool operator == ( const BaseClass * const bc ) const
        {
            return m_ptr == bc;
        }

        /// Other type not equal comparation.
        template <class OtherBase>
        bool operator != ( const SmartPtr<OtherBase> & other ) const
        {
            return m_ptr != other.ptr();
        }

        /// Other type not equal comparation.
        bool operator != ( const ThisType & bc ) const
        {
            return m_ptr != bc.ptr();
        }

        /// Const pointer not equal comparation.
        bool operator != (const BaseClass * const bc) const
        {
            return m_ptr != bc;
        }

        /// This type lower than comparation.
        bool operator < (const ThisType & p) const
        {
            return m_ptr < p.ptr();
        }

        bool isValid() const {
            return isValidPtr(m_ptr);
        }

    private:

        // Set this pointer.
        void set( BaseClass * p )
        {
            if (p) p->addRef();
            if (m_ptr) m_ptr->release();
            m_ptr = p;
        }

    private:

        BaseClass * m_ptr;

    };


    /// Smart pointer template class.
    template <class T>
    class WeakPtr {
    public:

        WeakPtr() {}

        WeakPtr(T * p)  { operator=(p); }
        WeakPtr(const SmartPtr<T> & p) { operator=(p.ptr()); }

        // Default constructor and assignment from weak_ptr<T> are OK.

        void operator=(T * p)
        {
            if (p) {
                m_proxy = p->getWeakProxy();
                nvDebugCheck(m_proxy != NULL);
                nvDebugCheck(m_proxy->ptr() == p);
            }
            else {
                m_proxy = NULL;
            }
        }

        void operator=(const SmartPtr<T> & ptr) { operator=(ptr.ptr()); }

        bool operator==(const SmartPtr<T> & p) const { return ptr() == p.ptr(); }
        bool operator!=(const SmartPtr<T> & p) const { return ptr() != p.ptr(); }

        bool operator==(const WeakPtr<T> & p) const { return ptr() == p.ptr(); }
        bool operator!=(const WeakPtr<T> & p) const { return ptr() != p.ptr(); }

        bool operator==(T * p) const { return ptr() == p; }
        bool operator!=(T * p) const { return ptr() != p; }

        T * operator->() const
        {
            T * p = ptr();
            nvDebugCheck(p != NULL);
            return p;
        }

        T * ptr() const
        {
            if (m_proxy != NULL) {
                return static_cast<T *>(m_proxy->ptr());
            }
            return NULL;
        }

    private:

        mutable SmartPtr<WeakProxy> m_proxy;

    };


} // nv namespace

#endif // NV_CORE_PTR_H