itom  3.0.0
pythonSharedPointerGuard.h
1 /* ********************************************************************
2  itom software
3  URL: http://www.uni-stuttgart.de/ito
4  Copyright (C) 2016, 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.h"
32  #define _DEBUG
33  #else
34  #include "Python.h"
35  #endif
36 #endif
37 
38 #include "../../common/sharedStructures.h"
39 #include "../global.h"
40 
41 #include <qhash.h>
42 #include <qsharedpointer.h>
43 
44 namespace ito
45 {
46 
48 {
49 public:
50  static PyObject *tParamToPyObject(ito::ParamBase &param);
51 
52  template<typename _Tp> static void deleter(_Tp *sharedPointerData)
53  {
54  QHash<void*, PyObject*>::iterator i = m_hashTable.find((void*)sharedPointerData);
55  if(i != m_hashTable.end())
56  {
57  Py_XDECREF(i.value());
58  m_hashTable.erase(i);
59  }
60  }
61 
62  template<typename _Tp> static QSharedPointer<_Tp> createPythonSharedPointer(_Tp *sharedPointerData, PyObject *pyObjOwner)
63  {
64  Py_XINCREF(pyObjOwner);
65  m_hashTable.insert((void*)sharedPointerData, pyObjOwner);
66  return QSharedPointer<_Tp>(sharedPointerData, deleter<_Tp>);
67  }
68 
69 private:
70  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)
71 
72 };
73 
74 } //namespace ito
75 
76 #endif
Definition: pythonSharedPointerGuard.h:47
Definition: apiFunctionsGraph.cpp:39
Definition: param.h:67