Salome HOME
Copyright update 2022
[modules/gui.git] / tools / PyInterp / src / PyInterp_RefCounterObj.h
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 //  File   : PyInterp_RefCounterObj.h
20 //  Author : Anthony Geay (EDF R&D)
21
22 #ifndef PYINTERP_REFCOUNTEROBJ_H
23 #define PYINTERP_REFCOUNTEROBJ_H
24
25 #include "PyInterp.h"
26
27 template<class T>
28 class PyInterp_Auto
29 {
30 public:
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; }
50  private:
51   void referPtr(T *ptr) { _ptr=ptr; if(_ptr) _ptr->incrRef(); }
52   void destroyPtr() { if(_ptr) _ptr->decrRef(); }
53  private:
54   T *_ptr;
55 };
56
57 class PYINTERP_EXPORT PyInterp_RefCounterObj
58 {
59  protected:
60   PyInterp_RefCounterObj():_cnt(1) { }
61   PyInterp_RefCounterObj(const PyInterp_RefCounterObj& /*other*/):_cnt(1) { }
62  public:
63   bool decrRef() const
64   {
65     bool ret=((--_cnt)==0);
66     if(ret)
67       delete this;
68     return ret;
69   }
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; }
74  protected:
75   virtual ~PyInterp_RefCounterObj() { }
76  private:
77   mutable int _cnt;
78 };
79
80 #endif // PYINTERP_REFCOUNTEROBJ_H