itom 2.0.0
D:/git-itom/sources/itom/Qitom/python/pythonSharedPointerGuard.h
00001 /* ********************************************************************
00002     itom software
00003     URL: http://www.uni-stuttgart.de/ito
00004     Copyright (C) 2013, Institut fuer Technische Optik (ITO),
00005     Universitaet Stuttgart, Germany
00006 
00007     This file is part of itom.
00008   
00009     itom is free software; you can redistribute it and/or modify it
00010     under the terms of the GNU Library General Public Licence as published by
00011     the Free Software Foundation; either version 2 of the Licence, or (at
00012     your option) any later version.
00013 
00014     itom is distributed in the hope that it will be useful, but
00015     WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library
00017     General Public Licence for more details.
00018 
00019     You should have received a copy of the GNU Library General Public License
00020     along with itom. If not, see <http://www.gnu.org/licenses/>.
00021 *********************************************************************** */
00022 
00023 #ifndef PYTHONSHAREDPOINTERGUARD_H
00024 #define PYTHONSHAREDPOINTERGUARD_H
00025 
00026 //python
00027 #ifndef Q_MOC_RUN
00028     // see http://vtk.org/gitweb?p=VTK.git;a=commitdiff;h=7f3f750596a105d48ea84ebfe1b1c4ca03e0bab3
00029     #if (defined _DEBUG) && (defined WIN32)
00030         #undef _DEBUG
00031         #include "Python.h"
00032         #define _DEBUG
00033     #else
00034         #include "Python.h"
00035     #endif
00036 #endif
00037 
00038 #include "../../common/sharedStructures.h"
00039 #include "../global.h"
00040 
00041 #include <qhash.h>
00042 #include <qsharedpointer.h>
00043 
00044 namespace ito
00045 {
00046 
00047 class PythonSharedPointerGuard
00048 {
00049 public:
00050     static PyObject *tParamToPyObject(ito::ParamBase &param);
00051 
00052     template<typename _Tp> static void deleter(_Tp *sharedPointerData)
00053     {
00054         QHash<void*, PyObject*>::iterator i = m_hashTable.find((void*)sharedPointerData);
00055         if(i != m_hashTable.end())
00056         {
00057             Py_XDECREF(i.value());
00058             m_hashTable.erase(i);
00059         }
00060     }
00061 
00062     template<typename _Tp> static QSharedPointer<_Tp> createPythonSharedPointer(_Tp *sharedPointerData, PyObject *pyObjOwner)
00063     {
00064         Py_XINCREF(pyObjOwner);
00065         m_hashTable.insert((void*)sharedPointerData, pyObjOwner);
00066         return QSharedPointer<_Tp>(sharedPointerData, deleter<_Tp>);
00067     }
00068 
00069 private:
00070     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)
00071 
00072 };
00073 
00074 } //namespace ito
00075 
00076 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends