~hikiko/nux/arb-srgba-shader

« back to all changes in this revision

Viewing changes to NuxCore/SmartPtr/NSmartPtr.h

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-01 19:25:37 UTC
  • Revision ID: neil.patel@canonical.com-20100901192537-mfz7rm6q262pewg6
Import and build NuxCore

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef NSMARTPTR_H
 
2
#define NSMARTPTR_H
 
3
 
 
4
NAMESPACE_BEGIN
 
5
 
 
6
template < class T >
 
7
class NSmartPtr
 
8
{
 
9
public:
 
10
    NSmartPtr();
 
11
    NSmartPtr(T* t);
 
12
    NSmartPtr(const NSmartPtr& rp);
 
13
    ~NSmartPtr();
 
14
    NSmartPtr& operator = (const NSmartPtr<T>& rp);
 
15
    NSmartPtr& operator = (const T* rp);
 
16
 
 
17
    bool operator == (const NSmartPtr<T>& rp) const;
 
18
    bool operator == (const T* ptr) const;
 
19
    bool operator != (const NSmartPtr<T>& rp) const;
 
20
    bool operator != (const T* ptr) const;
 
21
    bool operator < (const NSmartPtr<T>& rp) const;
 
22
    bool operator < (const T* ptr) const;
 
23
    bool operator > (const NSmartPtr<T>& rp) const;
 
24
    bool operator > (const T* ptr) const;
 
25
 
 
26
    T& operator* ();
 
27
    const T& operator* () const;
 
28
    T* operator-> ();
 
29
    const T* operator-> () const;
 
30
 
 
31
    bool operator! () const;
 
32
    bool IsValid () const;
 
33
    T* GetPtr();
 
34
    const T* GetPtr() const; 
 
35
 
 
36
private:
 
37
    void CheckReferenceCount();
 
38
    T* m_ptr;
 
39
};
 
40
 
 
41
template<class T> 
 
42
inline NSmartPtr<T>::NSmartPtr() 
 
43
:   m_ptr(NULL) 
 
44
{
 
45
}
 
46
 
 
47
template<class T> inline 
 
48
NSmartPtr<T>::NSmartPtr(T* t) 
 
49
:   m_ptr(t)              
 
50
 
51
    if(m_ptr) 
 
52
    {
 
53
        m_ptr->Increment(); 
 
54
    }
 
55
}
 
56
 
 
57
template<class T> 
 
58
inline NSmartPtr<T>::NSmartPtr(const NSmartPtr& rp) 
 
59
:   m_ptr(rp.m_ptr)  
 
60
 
61
    if(m_ptr) 
 
62
    {
 
63
        m_ptr->Increment();
 
64
    }
 
65
}
 
66
 
 
67
template<class T> 
 
68
inline NSmartPtr<T>::~NSmartPtr()                           
 
69
 
70
    if(m_ptr) 
 
71
    {
 
72
        m_ptr->Decrement();
 
73
        CheckReferenceCount();
 
74
    }
 
75
}
 
76
 
 
77
template<class T> 
 
78
inline void NSmartPtr<T>::CheckReferenceCount()
 
79
{
 
80
    if(m_ptr->GetRefCount() == 0)
 
81
    {
 
82
        delete m_ptr;
 
83
        m_ptr = 0;
 
84
    }
 
85
}
 
86
 
 
87
template<class T> 
 
88
inline NSmartPtr<T>& NSmartPtr<T>::operator = (const NSmartPtr& rp)
 
89
{
 
90
    // if the value to assign to the pointer is already set, just return this element
 
91
    if(rp.m_ptr == m_ptr) 
 
92
    {
 
93
        return *this;
 
94
    }
 
95
 
 
96
    // decrement ref count of RefCounted previously pointed to by this pointer
 
97
    if(m_ptr) 
 
98
    {
 
99
        m_ptr->Decrement();
 
100
        CheckReferenceCount();
 
101
    }
 
102
 
 
103
    // copy new pointer value
 
104
    m_ptr = rp.m_ptr;
 
105
 
 
106
    // increment ref count of RefCounted now pointed to by this pointer
 
107
    if(m_ptr) 
 
108
    {
 
109
        m_ptr->Increment();
 
110
    }
 
111
 
 
112
    return *this;
 
113
}
 
114
 
 
115
template<class T> 
 
116
inline bool NSmartPtr<T>::operator == (const NSmartPtr& rp) const
 
117
{
 
118
    return (m_ptr == rp.m_ptr);
 
119
}
 
120
 
 
121
template<class T> 
 
122
inline bool NSmartPtr<T>::operator == (const T* ptr) const
 
123
{
 
124
    return (m_ptr == ptr);
 
125
}
 
126
 
 
127
template<class T> 
 
128
inline bool NSmartPtr<T>::operator != (const NSmartPtr& rp) const
 
129
{
 
130
    return (m_ptr != rp.m_ptr);
 
131
}
 
132
 
 
133
template<class T> 
 
134
inline bool NSmartPtr<T>::operator != (const T* ptr) const
 
135
{
 
136
    return (m_ptr != ptr);
 
137
}
 
138
 
 
139
template<class T> 
 
140
inline bool NSmartPtr<T>::operator < (const NSmartPtr& rp) const
 
141
{
 
142
    return (m_ptr < rp.m_ptr);
 
143
}
 
144
 
 
145
template<class T> 
 
146
inline bool NSmartPtr<T>::operator < (const T* ptr) const
 
147
{
 
148
    return (m_ptr < ptr);
 
149
}
 
150
 
 
151
template<class T> 
 
152
inline bool NSmartPtr<T>::operator > (const NSmartPtr& rp) const
 
153
{
 
154
    return (m_ptr > rp.m_ptr);
 
155
}
 
156
 
 
157
template<class T> 
 
158
inline bool NSmartPtr<T>::operator > (const T* ptr) const
 
159
{
 
160
    return (m_ptr > ptr);
 
161
}
 
162
 
 
163
template<class T> 
 
164
inline T& NSmartPtr<T>::operator * () 
 
165
 
166
    return *m_ptr; 
 
167
}
 
168
 
 
169
template<class T> 
 
170
inline const T& NSmartPtr<T>::operator * () const 
 
171
 
172
    return *m_ptr; 
 
173
}
 
174
 
 
175
template<class T>
 
176
inline T* NSmartPtr<T>::operator -> () 
 
177
 
178
    return m_ptr; 
 
179
}
 
180
 
 
181
template<class T> 
 
182
inline const T* NSmartPtr<T>::operator -> () const   
 
183
 
184
    return m_ptr; 
 
185
}
 
186
 
 
187
template<class T> 
 
188
inline bool NSmartPtr<T>::operator ! () const   
 
189
 
190
    return (m_ptr == NULL); 
 
191
}
 
192
 
 
193
template<class T> 
 
194
inline bool NSmartPtr<T>::IsValid() const       
 
195
 
196
    return (m_ptr != NULL); 
 
197
}
 
198
 
 
199
template<class T> 
 
200
inline T* NSmartPtr<T>::GetPtr() 
 
201
 
202
    return m_ptr; 
 
203
}
 
204
 
 
205
template<class T> 
 
206
inline const T* NSmartPtr<T>::GetPtr() const 
 
207
 
208
    return m_ptr; 
 
209
}
 
210
 
 
211
NAMESPACE_END
 
212
 
 
213
#endif // NSMARTPTR_H
 
214
 
 
215