cutter/src/common/PythonManager.h

59 lines
1.2 KiB
C
Raw Normal View History

2018-06-23 16:59:23 +00:00
#ifndef PYTHONMANAGER_H
#define PYTHONMANAGER_H
2019-02-13 21:53:52 +00:00
#ifdef CUTTER_ENABLE_PYTHON
2021-01-24 14:50:13 +00:00
# include <QObject>
2018-06-23 16:59:23 +00:00
typedef struct _ts PyThreadState;
typedef struct _object PyObject;
2021-01-24 14:50:13 +00:00
class PythonManager : public QObject
2018-06-23 16:59:23 +00:00
{
Q_OBJECT
2018-06-23 16:59:23 +00:00
public:
static PythonManager *getInstance();
PythonManager();
~PythonManager();
void setPythonHome(const QString &pythonHome) { customPythonHome = pythonHome; }
2018-06-23 16:59:23 +00:00
void initPythonHome();
void initialize();
2019-02-03 13:00:40 +00:00
void shutdown();
2018-06-23 16:59:23 +00:00
void addPythonPath(char *path);
2018-06-24 09:51:16 +00:00
void restoreThread();
void saveThread();
2018-06-23 16:59:23 +00:00
2019-03-06 20:30:39 +00:00
/**
* @brief RAII Helper class to call restoreThread() and saveThread() automatically
2019-02-09 13:05:06 +00:00
*
* As long as an object of this class is in scope, the Python thread will remain restored.
*/
class ThreadHolder
{
public:
2021-01-24 14:50:13 +00:00
ThreadHolder() { getInstance()->restoreThread(); }
~ThreadHolder() { getInstance()->saveThread(); }
2019-02-09 13:05:06 +00:00
};
signals:
void willShutDown();
2018-06-23 16:59:23 +00:00
private:
QString customPythonHome;
wchar_t *pythonHome = nullptr;
PyThreadState *pyThreadState = nullptr;
2019-02-09 13:05:06 +00:00
int pyThreadStateCounter = 0;
2018-06-23 16:59:23 +00:00
};
2021-01-24 14:50:13 +00:00
# define Python() (PythonManager::getInstance())
2018-06-23 16:59:23 +00:00
2019-02-13 21:53:52 +00:00
#endif // CUTTER_ENABLE_PYTHON
2018-06-23 16:59:23 +00:00
#endif // PYTHONMANAGER_H