Salome HOME
Merge branch 'V7_dev'
[modules/yacs.git] / src / runtime / AutoGIL.hxx
index 640d59070e30362956aa0447d8110460d3ff40da..70b220b68569d184f8c54827e9be7470ae00df14 100644 (file)
@@ -35,6 +35,24 @@ namespace YACS
     private:
       PyGILState_STATE _gstate;
     };
+
+    class AutoPyRef
+    {
+    public:
+      AutoPyRef(PyObject *pyobj=0):_pyobj(pyobj) { }
+      ~AutoPyRef() { release(); }
+      AutoPyRef(const AutoPyRef& other):_pyobj(other._pyobj) { if(_pyobj) Py_XINCREF(_pyobj); }
+      AutoPyRef& operator=(const AutoPyRef& other) { if(_pyobj==other._pyobj) return *this; release(); _pyobj=other._pyobj; Py_XINCREF(_pyobj); return *this; }
+      operator PyObject *() { return _pyobj; }
+      void set(PyObject *pyobj) { if(pyobj==_pyobj) return ; release(); _pyobj=pyobj; }
+      PyObject *get() { return _pyobj; }
+      bool isNull() const { return _pyobj==0; }
+      PyObject *retn() { if(_pyobj) Py_XINCREF(_pyobj); return _pyobj; }
+    private:
+      void release() { if(_pyobj) Py_XDECREF(_pyobj); _pyobj=0; }
+    private:
+      PyObject *_pyobj;
+    };
   }
 }