Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/PythonQt/src/PythonQtObjectPtr.h
3206 views
1
#ifndef _PYTHONQTOBJECTPTR_H
2
#define _PYTHONQTOBJECTPTR_H
3
4
/*
5
*
6
* Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.
7
*
8
* This library is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Lesser General Public
10
* License as published by the Free Software Foundation; either
11
* version 2.1 of the License, or (at your option) any later version.
12
*
13
* This library is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* Lesser General Public License for more details.
17
*
18
* Further, this software is distributed without any warranty that it is
19
* free of the rightful claim of any third person regarding infringement
20
* or the like. Any license provided herein, whether implied or
21
* otherwise, applies only to this software file. Patent licenses, if
22
* any, provided herein do not apply to combinations of this program with
23
* other software, or any other product whatsoever.
24
*
25
* You should have received a copy of the GNU Lesser General Public
26
* License along with this library; if not, write to the Free Software
27
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28
*
29
* Contact information: MeVis Research GmbH, Universitaetsallee 29,
30
* 28359 Bremen, Germany or:
31
*
32
* http://www.mevis.de
33
*
34
*/
35
36
//----------------------------------------------------------------------------------
37
/*!
38
// \file PythonQtObjectPtr.h
39
// \author Florian Link
40
// \author Last changed by $Author: florian $
41
// \date 2006-05
42
*/
43
//----------------------------------------------------------------------------------
44
45
#include <Python.h>
46
#include "PythonQtSystem.h"
47
48
//! a smart pointer that stores a PyObject pointer and that handles reference counting automatically
49
class PYTHONQT_EXPORT PythonQtObjectPtr
50
{
51
public:
52
PythonQtObjectPtr():_object(NULL) {}
53
54
PythonQtObjectPtr(const PythonQtObjectPtr &p):_object(NULL) {
55
setObject(p.object());
56
}
57
58
PythonQtObjectPtr(PyObject* o) {
59
_object = o;
60
if (o) Py_INCREF(_object);
61
}
62
63
~PythonQtObjectPtr() { if (_object) Py_DECREF(_object); }
64
65
PythonQtObjectPtr &operator=(const PythonQtObjectPtr &p) {
66
setObject(p.object());
67
return *this;
68
}
69
70
PythonQtObjectPtr &operator=(PyObject* o) {
71
setObject(o);
72
return *this;
73
}
74
75
bool operator==( const PythonQtObjectPtr &p ) const {
76
return object() == p.object();
77
}
78
79
bool operator!= ( const PythonQtObjectPtr& p ) const {
80
return !( *this == p );
81
}
82
83
bool operator==( PyObject* p ) const {
84
return object() == p;
85
}
86
87
bool operator!= ( PyObject* p ) const {
88
return object() != p;
89
}
90
91
bool isNull() const { return !object(); }
92
93
PyObject* operator->() const { return object(); }
94
95
PyObject& operator*() const { return *( object() ); }
96
97
operator PyObject*() const { return object(); }
98
99
//! sets the object and passes the ownership (stealing the reference, in Python slang)
100
void setNewRef(PyObject* o) {
101
if (o != _object) {
102
if (_object) Py_DECREF(_object);
103
_object = o;
104
}
105
}
106
107
PyObject* object() const {
108
return _object;
109
}
110
111
//! evaluates the given script code in the context of this object and returns the result value
112
QVariant evalScript(const QString& script, int start = Py_file_input);
113
114
//! evaluates the given code and returns the result value (use Py_Compile etc. to create pycode from string)
115
//! If pycode is NULL, a python error is printed.
116
QVariant evalCode(PyObject* pycode);
117
118
//! evaluates the given code in the context
119
void evalFile(const QString& filename);
120
121
//! add the given \c object to the \c module as a variable with \c name (it can be removed via clearVariable)
122
void addObject(const QString& name, QObject* object);
123
124
//! add the given variable to the module
125
void addVariable(const QString& name, const QVariant& v);
126
127
//! remove the given variable
128
void removeVariable(const QString& name);
129
130
//! get the variable with the \c name of the \c module, returns an invalid QVariant on error
131
QVariant getVariable(const QString& name);
132
133
//! call the given python object (in the scope of the current object), returns the result converted to a QVariant
134
QVariant call(const QString& callable, const QVariantList& args);
135
136
137
protected:
138
139
void setObject(PyObject* o) {
140
if (o != _object) {
141
if (_object) Py_DECREF(_object);
142
_object = o;
143
if (_object) Py_INCREF(_object);
144
}
145
}
146
147
private:
148
PyObject* _object;
149
};
150
151
152
// register it to the meta type system
153
Q_DECLARE_METATYPE(PythonQtObjectPtr)
154
155
#endif
156
157