Code_TYMPAN  4.4.0
Industrial site acoustic simulation
smartptr.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2014> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  */
15 
16 /*
17  *
18  */
19 
20 #ifndef TY_SMARTPTR
21 #define TY_SMARTPTR
22 
23 #if _MSC_VER > 1000
24  #pragma once
25 #endif // _MSC_VER > 1000
26 
32 #ifdef _MSC_VER
33  #pragma warning(disable : 4284)
34 #endif
35 
42 #ifdef _MSC_VER
43  #pragma warning(disable : 4786)
44 #endif
45 
51 class IRefCount
52 {
53 public:
55  virtual ~IRefCount() {}
56 
62  virtual int incRef()
63  {
64  return m_refCount++;
65  };
66 
74  virtual int decRef()
75  {
76  --m_refCount;
77  if (!m_refCount)
78  {
79  delete this;
80  return 0;
81  }
82  return m_refCount;
83  };
84 
90  int getRefCount() const
91  {
92  return m_refCount;
93  }
94 
95 protected:
98 };
99 
105 template <class T> class SmartPtr
106 {
107 public:
109  {
110  _pObj = 0;
111  }
112  SmartPtr(T* pObj)
113  {
114  _pObj = pObj;
115  if (_pObj != 0)
116  {
117  _pObj->incRef();
118  }
119  }
123  template <class U> SmartPtr(const SmartPtr<U>& ptr)
124  {
125  _pObj = ptr._pObj;
126  if (_pObj != 0)
127  {
128  _pObj->incRef();
129  }
130  }
134  SmartPtr(const SmartPtr& ptr)
135  {
136  _pObj = ptr._pObj;
137  if (_pObj != 0)
138  {
139  _pObj->incRef();
140  }
141  }
142 
144  {
145  if (_pObj != 0)
146  {
147  if (!_pObj->decRef())
148  {
149  _pObj = 0;
150  }
151  }
152  }
153 
157  operator T*() const
158  {
159  return _pObj;
160  }
162  {
163  return *_pObj;
164  }
165 
169  T* operator->() const
170  {
171  return _pObj;
172  }
173 
177  template <class U> SmartPtr& operator=(const SmartPtr<U>& ptr)
178  {
179  if (_pObj != ptr._pObj)
180  {
181  if (_pObj != 0)
182  {
183  _pObj->decRef();
184  }
185 
186  _pObj = ptr._pObj;
187 
188  if (_pObj != 0)
189  {
190  _pObj->incRef();
191  }
192  }
193 
194  return *this;
195  }
196 
201  {
202  *this = ptr._pObj;
203  return *this;
204  }
205 
209  SmartPtr& operator=(T* pObj)
210  {
211  if (pObj != 0)
212  {
213  pObj->incRef();
214  }
215 
216  if (_pObj != 0)
217  {
218  _pObj->decRef();
219  }
220 
221  _pObj = pObj;
222  return *this;
223  };
224 
228  bool operator==(const SmartPtr& ptr) const
229  {
230  return _pObj == ptr._pObj;
231  }
232 
236  bool operator==(T* pObj) const
237  {
238  return _pObj == pObj;
239  }
240 
244  bool operator!=(const SmartPtr& ptr) const
245  {
246  return _pObj != ptr._pObj;
247  }
248 
252  bool operator!=(T* pObj) const
253  {
254  return _pObj != pObj;
255  }
256 
260  bool operator>(const SmartPtr& ptr) const
261  {
262  return _pObj > ptr._pObj;
263  }
267  bool operator>(T* pObj) const
268  {
269  return _pObj > pObj;
270  }
271 
275  bool operator<(const SmartPtr& ptr) const
276  {
277  return _pObj < ptr._pObj;
278  }
282  bool operator<(T* pObj) const
283  {
284  return _pObj < pObj;
285  }
286 
292  {
293  return _pObj;
294  }
299  const T* getRealPointer() const
300  {
301  return _pObj;
302  }
303 
304  // Members
305 public:
307  T* _pObj;
308 };
309 
310 #endif // TY_SMARTPTR
virtual int incRef()
Definition: smartptr.h:62
virtual ~IRefCount()
Definition: smartptr.h:55
int m_refCount
The reference counter.
Definition: smartptr.h:97
virtual int decRef()
Definition: smartptr.h:74
IRefCount()
Definition: smartptr.h:54
int getRefCount() const
Definition: smartptr.h:90
const T * getRealPointer() const
Definition: smartptr.h:299
bool operator>(T *pObj) const
Definition: smartptr.h:267
SmartPtr & operator=(const SmartPtr< U > &ptr)
Definition: smartptr.h:177
T * getRealPointer()
Definition: smartptr.h:291
SmartPtr(const SmartPtr< U > &ptr)
Definition: smartptr.h:123
T & operator*()
Definition: smartptr.h:161
bool operator!=(const SmartPtr &ptr) const
Definition: smartptr.h:244
bool operator!=(T *pObj) const
Definition: smartptr.h:252
SmartPtr(const SmartPtr &ptr)
Definition: smartptr.h:134
bool operator==(T *pObj) const
Definition: smartptr.h:236
bool operator<(const SmartPtr &ptr) const
Definition: smartptr.h:275
SmartPtr()
Definition: smartptr.h:108
~SmartPtr()
Definition: smartptr.h:143
SmartPtr & operator=(T *pObj)
Definition: smartptr.h:209
T * _pObj
The real pointer, must derived IRefCount.
Definition: smartptr.h:307
bool operator==(const SmartPtr &ptr) const
Definition: smartptr.h:228
T * operator->() const
Definition: smartptr.h:169
SmartPtr(T *pObj)
Definition: smartptr.h:112
SmartPtr & operator=(const SmartPtr &ptr)
Definition: smartptr.h:200
bool operator<(T *pObj) const
Definition: smartptr.h:282
bool operator>(const SmartPtr &ptr) const
Definition: smartptr.h:260