itom  4.1.0
pythonSharedPointerGuard.h
1 /* ********************************************************************
2  itom software
3  URL: http://www.uni-stuttgart.de/ito
4  Copyright (C) 2020, Institut fuer Technische Optik (ITO),
5  Universitaet Stuttgart, Germany
6 
7  This file is part of itom.
8 
9  itom is free software; you can redistribute it and/or modify it
10  under the terms of the GNU Library General Public Licence as published by
11  the Free Software Foundation; either version 2 of the Licence, or (at
12  your option) any later version.
13 
14  itom is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
17  General Public Licence for more details.
18 
19  You should have received a copy of the GNU Library General Public License
20  along with itom. If not, see <http://www.gnu.org/licenses/>.
21 *********************************************************************** */
22 
23 #ifndef PYTHONSHAREDPOINTERGUARD_H
24 #define PYTHONSHAREDPOINTERGUARD_H
25 
26 //python
27 #ifndef Q_MOC_RUN
28  // see http://vtk.org/gitweb?p=VTK.git;a=commitdiff;h=7f3f750596a105d48ea84ebfe1b1c4ca03e0bab3
29  #if (defined _DEBUG) && (defined WIN32)
30  #undef _DEBUG
31  #include "python/pythonWrapper.h"
32  #define _DEBUG
33  #else
34  #include "python/pythonWrapper.h"
35  #endif
36 #endif
37 
38 #include "../../common/sharedStructures.h"
39 #include "../global.h"
40 #include "patchlevel.h"
41 
42 #include <qhash.h>
43 #include <qsharedpointer.h>
44 #include <qdebug.h>
45 
46 namespace ito
47 {
48 
50 {
51 public:
52  static PyObject *tParamToPyObject(ito::ParamBase &param);
53 
54  static void safeDecrefPyObject2Async(PyObject* obj);
55 
56  template<typename _Tp> static void deleter(_Tp *sharedPointerData)
57  {
58  QHash<void*, PyObject*>::iterator i = m_hashTable.find((void*)sharedPointerData);
59  if (i != m_hashTable.end())
60  {
61  if (i.value())
62  {
63  if (PyGILState_Check())
64  {
65  Py_DECREF(i.value());
66  }
67  else
68  {
69  safeDecrefPyObject2Async(i.value());
70  }
71  }
72 
73  m_hashTable.erase(i);
74  }
75  }
76 
77  template<typename _Tp> static QSharedPointer<_Tp> createPythonSharedPointer(_Tp *sharedPointerData, PyObject *pyObjOwner)
78  {
79  Py_XINCREF(pyObjOwner);
80  m_hashTable.insert((void*)sharedPointerData, pyObjOwner);
81  return QSharedPointer<_Tp>(sharedPointerData, deleter<_Tp>);
82  }
83 
84 private:
85  static QHash<void* /*sharedPointerData*/, PyObject* /*pyObjOwner*/> m_hashTable; //elements in this hash-table are pyObjects (type byteArray, unicode...), whose inlying char*-pointer is given to a QSharedPointer<char>. Before giving it to the shared pointer, the reference of the pyObject is incremented. If the deleter of the sharedPointer is called, it does not delete the char-array, but decrements the PyObject and deletes it from this hash-table (used in getVal)
86 
87 };
88 
89 } //namespace ito
90 
91 #endif
Definition: pythonSharedPointerGuard.h:49
Definition: apiFunctionsGraph.cpp:39
Definition: param.h:67