TPointerBase.h
Go to the documentation of this file.
1 #ifndef istd_TPointerBase_included
2 #define istd_TPointerBase_included
3 
4 
5 namespace istd
6 {
7 
8 
13 template <class Type>
15 {
16 public:
17  static void Delete(Type* ptr)
18  {
19  Q_ASSERT(ptr != NULL);
20  if (ptr != NULL){
21  delete ptr;
22  }
23  }
24 };
25 
26 
31 template <class Type>
33 {
34 public:
35  static void Delete(Type* ptr)
36  {
37  Q_ASSERT(ptr != NULL);
38  if (ptr != NULL){
39  delete[] ptr;
40  }
41  }
42 };
43 
44 
48 template <class Type>
50 {
51 public:
55  TPointerBase(Type* ptr = NULL);
56 
60  void SetPtr(Type* ptr);
61 
65  void Reset();
66 
70  Type* GetPtr() const;
75  bool IsValid() const;
76 
80  void Swap(TPointerBase& ptr);
81 
85  Type& operator*() const;
86 
87  template <class CastedType>
88  CastedType Cast() const
89  {
90  return dynamic_cast<CastedType>(GetPtr());
91  }
92 
93  // operators
94  Type* operator->() const;
95  bool operator==(const TPointerBase<Type>& ptr) const;
96  bool operator!=(const TPointerBase<Type>& ptr) const;
97  bool operator<(const TPointerBase<Type>& ptr) const;
98  bool operator>(const TPointerBase<Type>& ptr) const;
99  bool operator<=(const TPointerBase<Type>& ptr) const;
100  bool operator>=(const TPointerBase<Type>& ptr) const;
101  bool operator==(const Type* ptr) const;
102  bool operator!=(const Type* ptr) const;
103  bool operator<(const Type* ptr) const;
104  bool operator>(const Type* ptr) const;
105  bool operator<=(const Type* ptr) const;
106  bool operator>=(const Type* ptr) const;
107 
108 protected:
109  // blocked operators
111 
112  Type*& GetPtrRef();
113 
114 private:
115  Type* m_ptr;
116 };
117 
118 
119 // inline methods
120 
121 template <class Type>
123 {
124  m_ptr = NULL;
125 }
126 
127 
128 template <class Type>
129 inline Type* TPointerBase<Type>::GetPtr() const
130 {
131  return m_ptr;
132 }
133 
134 
135 template <class Type>
136 inline bool TPointerBase<Type>::IsValid() const
137 {
138  return (m_ptr != NULL);
139 }
140 
141 
142 template <class Type>
144 {
145  qSwap(m_ptr, ptr.m_ptr);
146 }
147 
148 
149 template <class Type>
150 inline Type& TPointerBase<Type>::operator*() const
151 {
152  Q_ASSERT(m_ptr != NULL);
153 
154  return *m_ptr;
155 }
156 
157 
158 template <class Type>
159 inline Type* TPointerBase<Type>::operator->() const
160 {
161  return m_ptr;
162 }
163 
164 
165 template <class Type>
167 {
168  return (m_ptr == ptr.m_ptr);
169 }
170 
171 
172 template <class Type>
174 {
175  return (m_ptr != ptr.m_ptr);
176 }
177 
178 
179 template <class Type>
181 {
182  return m_ptr < ptr.m_ptr;
183 }
184 
185 
186 template <class Type>
188 {
189  return m_ptr > ptr.m_ptr;
190 }
191 
192 
193 template <class Type>
195 {
196  return m_ptr <= ptr.m_ptr;
197 }
198 
199 
200 template <class Type>
202 {
203  return m_ptr >= ptr.m_ptr;
204 }
205 
206 
207 template <class Type>
208 inline bool TPointerBase<Type>::operator==(const Type* ptr) const
209 {
210  return (m_ptr == ptr);
211 }
212 
213 
214 template <class Type>
215 inline bool TPointerBase<Type>::operator!=(const Type* ptr) const
216 {
217  return (m_ptr != ptr);
218 }
219 
220 
221 template <class Type>
222 bool TPointerBase<Type>::operator<(const Type* ptr) const
223 {
224  return (m_ptr < ptr);
225 }
226 
227 
228 template <class Type>
229 bool TPointerBase<Type>::operator>(const Type* ptr) const
230 {
231  return (m_ptr > ptr);
232 }
233 
234 
235 template <class Type>
236 bool TPointerBase<Type>::operator<=(const Type* ptr) const
237 {
238  return (m_ptr <= ptr);
239 }
240 
241 
242 template <class Type>
243 bool TPointerBase<Type>::operator>=(const Type* ptr) const
244 {
245  return (m_ptr >= ptr);
246 }
247 
248 
249 // protected inline methods
250 
251 template <class Type>
253 {
254  m_ptr = ptr;
255 }
256 
257 
258 template <class Type>
259 inline void TPointerBase<Type>::SetPtr(Type* ptr)
260 {
261  m_ptr = ptr;
262 }
263 
264 
265 template <class Type>
267 {
268  m_ptr = ptr.m_ptr;
269 
270  return *this;
271 }
272 
273 
274 template <class Type>
276 {
277  return m_ptr;
278 }
279 
280 
281 } // namespace istd
282 
283 
284 #endif // !istd_TPointerBase_included
285 
286 
void Reset()
Set internal pointer value to NULL.
Definition: TPointerBase.h:122
Implementation of pointer wrapper.
Definition: TPointerBase.h:49
bool operator<=(const TPointerBase< Type > &ptr) const
Definition: TPointerBase.h:194
static void Delete(Type *ptr)
Definition: TPointerBase.h:17
bool operator<(const TPointerBase< Type > &ptr) const
Definition: TPointerBase.h:180
bool IsValid() const
Check if internal pointer not NULL.
Definition: TPointerBase.h:136
Type & operator*() const
Get an access to object pointed at.
Definition: TPointerBase.h:150
bool operator>=(const TPointerBase< Type > &ptr) const
Definition: TPointerBase.h:201
TPointerBase< Type > & operator=(const TPointerBase< Type > ptr)
Definition: TPointerBase.h:266
bool operator!=(const TPointerBase< Type > &ptr) const
Definition: TPointerBase.h:173
bool operator>(const TPointerBase< Type > &ptr) const
Definition: TPointerBase.h:187
bool operator==(const TPointerBase< Type > &ptr) const
Definition: TPointerBase.h:166
TPointerBase(Type *ptr=NULL)
Construct and assign internal pointer.
Definition: TPointerBase.h:252
Type * GetPtr() const
Return access to internal stored pointer.
Definition: TPointerBase.h:129
#define NULL
Definition: istd.h:64
void Swap(TPointerBase &ptr)
Swap two pointers.
Definition: TPointerBase.h:143
static void Delete(Type *ptr)
Definition: TPointerBase.h:35
Type * operator->() const
Definition: TPointerBase.h:159
CastedType Cast() const
Definition: TPointerBase.h:88
void SetPtr(Type *ptr)
Set value of internal stored pointer.
Definition: TPointerBase.h:259

© 2007-2017 Witold Gantzke and Kirill Lepskiy