1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // File : PyInterp_RefCounterObj.h
20 // Author : Anthony Geay (EDF R&D)
22 #ifndef PYINTERP_REFCOUNTEROBJ_H
23 #define PYINTERP_REFCOUNTEROBJ_H
28 class PYINTERP_EXPORT PyInterp_Auto
31 PyInterp_Auto(const PyInterp_Auto& other):_ptr(0) { referPtr(other._ptr); }
32 PyInterp_Auto(T *ptr=0):_ptr(ptr) { }
33 ~PyInterp_Auto() { destroyPtr(); }
34 bool isNull() const { return _ptr==0; }
35 bool isNotNull() const { return !isNull(); }
36 void nullify() { destroyPtr(); _ptr=0; }
37 bool operator==(const PyInterp_Auto& other) const { return _ptr==other._ptr; }
38 bool operator==(const T *other) const { return _ptr==other; }
39 PyInterp_Auto &operator=(const PyInterp_Auto<T>& other) { if(_ptr!=other._ptr) { destroyPtr(); referPtr(other._ptr); } return *this; }
40 PyInterp_Auto &operator=(T *ptr) { if(_ptr!=ptr) { destroyPtr(); _ptr=ptr; } return *this; }
41 void takeRef(T *ptr) { if(_ptr!=ptr) { destroyPtr(); _ptr=ptr; if(_ptr) _ptr->incrRef(); } }
42 T *operator->() { return _ptr ; }
43 const T *operator->() const { return _ptr; }
44 T& operator*() { return *_ptr; }
45 const T& operator*() const { return *_ptr; }
46 operator T *() { return _ptr; }
47 operator const T *() const { return _ptr; }
48 T *retn() { if(_ptr) _ptr->incrRef(); return _ptr; }
49 T *iAmATrollConstCast() const { return _ptr; }
51 void referPtr(T *ptr) { _ptr=ptr; if(_ptr) _ptr->incrRef(); }
52 void destroyPtr() { if(_ptr) _ptr->decrRef(); }
57 class PYINTERP_EXPORT PyInterp_RefCounterObj
60 PyInterp_RefCounterObj():_cnt(1) { }
61 PyInterp_RefCounterObj(const PyInterp_RefCounterObj& other):_cnt(1) { }
65 bool ret=((--_cnt)==0);
70 void incrRef() const { _cnt++; }
71 int getRCValue() const { return _cnt; }
72 // copies using operator= should not copy the ref counter of \a other
73 PyInterp_RefCounterObj& operator=(const PyInterp_RefCounterObj& other) { return *this; }
75 virtual ~PyInterp_RefCounterObj() { }
80 #endif // PYINTERP_REFCOUNTEROBJ_H