PythonQt
PythonQtObjectPtr.h
Go to the documentation of this file.
1#ifndef _PYTHONQTOBJECTPTR_H
2#define _PYTHONQTOBJECTPTR_H
3
4/*
5 *
6 * Copyright (C) 2010 MeVis Medical Solutions AG 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 Medical Solutions AG, Universitaetsallee 29,
30 * 28359 Bremen, Germany or:
31 *
32 * http://www.mevis.de
33 *
34 */
35
36//----------------------------------------------------------------------------------
43//----------------------------------------------------------------------------------
44
46
47#include "PythonQtSystem.h"
49#include <QVariant>
50#include <QVariantList>
51#include <QVariantMap>
52
54
57{
58public:
60 : _object(nullptr)
61 {
62 }
63
65 : _object(nullptr)
66 {
67 setObject(p.object());
68 }
69
72 : _object(p.takeObject())
73 {
74 }
75
78
81 : _object(nullptr)
82 {
83 fromVariant(variant);
84 }
85
87
89
92
95
98
100 {
101 setObject(p.object());
102 return *this;
103 }
104
107 {
108 if (_object) {
109 setObject(nullptr);
110 }
111 _object = p.takeObject();
112 return *this;
113 }
114
117
119 {
120 setObject(o);
121 return *this;
122 }
123
125 {
126 fromVariant(variant);
127 return *this;
128 }
129
130 bool operator==(const PythonQtObjectPtr& p) const { return object() == p.object(); }
131
132 bool operator!=(const PythonQtObjectPtr& p) const { return !(*this == p); }
133
134 bool operator==(PyObject* p) const { return object() == p; }
135
136 bool operator!=(PyObject* p) const { return object() != p; }
137
138 bool isNull() const { return !object(); }
139
140 PyObject* operator->() const { return object(); }
141
142 PyObject& operator*() const { return *(object()); }
143
144 operator PyObject*() const { return object(); }
145
148
149 PyObject* object() const { return _object; }
150
153
157
160
162 void addObject(const QString& name, QObject* object);
163
165 void addVariable(const QString& name, const QVariant& v);
166
168 void removeVariable(const QString& name);
169
172
175 const QVariantMap& kwargs = QVariantMap());
176
179
183 {
184 PyObject* o = _object;
185 _object = nullptr;
186 return o;
187 }
188
189protected:
190
192
193private:
194 PyObject* _object;
195};
196
199{
200public:
202 : _object(nullptr)
203 {
204 }
205
207 : _object(nullptr)
208 {
209 setObject(p.object());
210 }
211
213 : _object(nullptr)
214 {
215 setObject(p.object());
216 }
217
220 : _object(p._object)
221 {
222 p._object = nullptr;
223 }
224
227 : _object(p.takeObject())
228 {
229 }
230
232
234
236 {
237 setObject(p.object());
238 return *this;
239 }
240
242 {
243 setObjectUnsafe(p.object());
244 return *this;
245 }
246
249 {
250 if (_object) {
251 setObject(nullptr);
252 }
253 _object = p._object;
254 p._object = nullptr;
255 return *this;
256 }
257
260 {
261 if (_object) {
262 setObjectUnsafe(nullptr);
263 }
264 _object = p.takeObject();
265 return *this;
266 }
267
269 {
270 setObject(o);
271 return *this;
272 }
273
274 bool operator==(const PythonQtSafeObjectPtr& p) const { return object() == p.object(); }
275
276 bool operator!=(const PythonQtSafeObjectPtr& p) const { return !(*this == p); }
277
278 bool operator==(PyObject* p) const { return object() == p; }
279
280 bool operator!=(PyObject* p) const { return object() != p; }
281
282 bool isNull() const { return !object(); }
283
284 PyObject* operator->() const { return object(); }
285
286 PyObject& operator*() const { return *(object()); }
287
288 operator PyObject*() const { return object(); }
289
290 PyObject* object() const { return _object; }
291
295 {
296 PyObject* o = _object;
297 _object = nullptr;
298 return o;
299 }
300
301protected:
304
305private:
306 PyObject* _object;
307};
308
309// We don't want QVariant to take PythonQtObjectPtr via QVariant::fromValue, because it is unsafe when using multi-threading/GIL
310// A QVariant can still be obtained with PythonQtObjectPtr::toLocalVariant().
311//Q_DECLARE_METATYPE(PythonQtObjectPtr)
312
313// register PythonQtSafeObjectPtr to the meta type system
315
316#endif
PyObject * PythonQtConvertPairToPython(const void *inPair, int metaTypeId)
struct _object PyObject
#define PYTHONQT_EXPORT
a smart pointer that stores a PyObject pointer and that handles reference counting automatically
PyObject * operator->() const
QVariant call(const QVariantList &args=QVariantList(), const QVariantMap &kwargs=QVariantMap())
call the contained python object directly, returns the result converted to a QVariant
PythonQtObjectPtr & operator=(const PythonQtObjectPtr &p)
PythonQtObjectPtr(PythonQtObjectPtr &&p) noexcept
rvalue copy constructor, does not need any incref/decref.
void setObject(PyObject *o)
void addVariable(const QString &name, const QVariant &v)
add the given variable to the module
PythonQtObjectPtr & operator=(PyObject *o)
void evalFile(const QString &filename)
evaluates the given code in the context
PythonQtObjectPtr & operator=(const QVariant &variant)
PythonQtObjectPtr(PyObject *o)
PythonQtObjectPtr(const PythonQtObjectPtr &p)
PythonQtObjectPtr(PythonQtSafeObjectPtr &&p)
rvalue copy constructor, does not need any incref/decref.
PyObject & operator*() const
QVariant toLocalVariant()
Returns a PythonQtObjectPtr as a QVariant. Only use this when you know that the variant is only used ...
PythonQtObjectPtr & operator=(PythonQtObjectPtr &&p) noexcept
rvalue assignment operator that steals the reference from p
bool operator==(PyObject *p) const
PythonQtObjectPtr & operator=(PythonQtSafeObjectPtr &&p)
rvalue assignment operator that steals the reference from p
QVariant evalScript(const QString &script, int start=Py_file_input)
evaluates the given script code in the context of this object and returns the result value
void removeVariable(const QString &name)
remove the given variable
PythonQtObjectPtr(const QVariant &variant)
If the given variant holds a PythonQtObjectPtr, extract the value from it and hold onto the reference...
bool operator!=(PyObject *p) const
bool operator!=(const PythonQtObjectPtr &p) const
QVariant toVariant()
Returns a PythonQtSafeObjectPtr as a QVariant. It does not return a PythonQtObjectPtr,...
void addObject(const QString &name, QObject *object)
add the given object to the module as a variable with name (it can be removed via clearVariable)
bool fromVariant(const QVariant &variant)
If the given variant holds a PythonQtObjectPtr, extract the value from it and hold onto the reference...
QVariant getVariable(const QString &name)
get the variable with the name of the module, returns an invalid QVariant on error
PyObject * object() const
QVariant call(const QString &callable, const QVariantList &args=QVariantList(), const QVariantMap &kwargs=QVariantMap())
call the given python object (in the scope of the current object), returns the result converted to a ...
bool operator==(const PythonQtObjectPtr &p) const
QVariant evalCode(PyObject *pycode)
void setNewRef(PyObject *o)
sets the object and passes the ownership (stealing the reference, in Python slang)
a smart pointer that stores a PyObject pointer and that handles reference counting automatically
void setObject(PyObject *o)
PythonQtSafeObjectPtr & operator=(PythonQtSafeObjectPtr &&p) noexcept
rvalue assignment operator that steals the reference from p
PyObject * operator->() const
void setObjectUnsafe(PyObject *o)
PythonQtSafeObjectPtr(PythonQtSafeObjectPtr &&p) noexcept
rvalue copy constructor, does not need any incref/decref.
PythonQtSafeObjectPtr(const PythonQtSafeObjectPtr &p)
PythonQtSafeObjectPtr & operator=(const PythonQtObjectPtr &p)
bool operator!=(PyObject *p) const
PythonQtSafeObjectPtr(PyObject *o)
PythonQtSafeObjectPtr(PythonQtObjectPtr &&p)
rvalue copy constructor, does not need any incref/decref.
PyObject * object() const
PyObject & operator*() const
bool operator==(PyObject *p) const
PythonQtSafeObjectPtr(const PythonQtObjectPtr &p)
PythonQtSafeObjectPtr & operator=(PythonQtObjectPtr &&p)
rvalue assignment operator that steals the reference from p
PythonQtSafeObjectPtr & operator=(PyObject *o)
PythonQtSafeObjectPtr & operator=(const PythonQtSafeObjectPtr &p)
bool operator!=(const PythonQtSafeObjectPtr &p) const
bool operator==(const PythonQtSafeObjectPtr &p) const