Salome HOME
Agressive Windows porting
[tools/py2cpp.git] / src / PyPtr.hxx
1 // Copyright (C) 2019-2021 EDF R&D
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 #ifndef PY2CPP_PYPTR_HXX
20 #define PY2CPP_PYPTR_HXX
21 #include "py2cppExports.hxx"
22 #include <Python.h>
23 #include <memory>
24 #include <string>
25
26 namespace py2cpp
27 {
28 class PY2CPP_EXPORT PyPtrDeleter
29 {
30 public:
31   void operator()(PyObject * po){Py_DECREF(po);}
32 };
33
34 typedef std::unique_ptr<PyObject, PyPtrDeleter> _PyPtr;
35
36 class PY2CPP_EXPORT PyPtr: public _PyPtr
37 {
38 public:
39   PyPtr();
40   PyPtr(std::nullptr_t copy);
41   PyPtr(PyObject* pyObj);
42   PyPtr(const PyPtr& copy);
43   PyPtr(PyPtr&& move);
44   PyPtr& operator=(const PyPtr& copy);
45   PyPtr& operator=(PyPtr&& move);
46   PyPtr& operator=(std::nullptr_t copy);
47
48   PyPtr getAttr(const std::string& attribute)const;
49   void setAttr(const std::string& attribute, const PyPtr& value)const;
50   std::string repr()const;
51 };
52
53 class PY2CPP_EXPORT AutoGIL
54 {
55 public:
56   AutoGIL():_gstate(PyGILState_Ensure()) { }
57   ~AutoGIL() { PyGILState_Release(_gstate); }
58 private:
59   PyGILState_STATE _gstate;
60 };
61
62 }
63
64 #endif // PY2CPP_PYPTR_HXX