GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • src

This view

  • Original page
  • New address

GeezerVison is showing mevislab.github.io — PythonQt: PythonQtObjectPtr.h Source File

PythonQt
  • src
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
45#include "PythonQtPythonInclude.h"
46
47#include "PythonQtSystem.h"
48#include "PythonQtThreadSupport.h"
49#include <QVariant>
50#include <QVariantList>
51#include <QVariantMap>
52
53class PythonQtSafeObjectPtr;
54
56class PYTHONQT_EXPORT PythonQtObjectPtr
57{
58public:
59 PythonQtObjectPtr()
60 : _object(nullptr)
61 {
62 }
63
64 PythonQtObjectPtr(const PythonQtObjectPtr& p)
65 : _object(nullptr)
66 {
67 setObject(p.object());
68 }
69
71 PythonQtObjectPtr(PythonQtObjectPtr&& p) noexcept
72 : _object(p.takeObject())
73 {
74 }
75
77 PythonQtObjectPtr(PythonQtSafeObjectPtr&& p);
78
80 PythonQtObjectPtr(const QVariant& variant)
81 : _object(nullptr)
82 {
83 fromVariant(variant);
84 }
85
86 PythonQtObjectPtr(PyObject* o);
87
88 ~PythonQtObjectPtr();
89
91 bool fromVariant(const QVariant& variant);
92
94 QVariant toVariant();
95
97 QVariant toLocalVariant();
98
99 PythonQtObjectPtr& operator=(const PythonQtObjectPtr& p)
100 {
101 setObject(p.object());
102 return *this;
103 }
104
106 PythonQtObjectPtr& operator=(PythonQtObjectPtr&& p) noexcept
107 {
108 if (_object) {
109 setObject(nullptr);
110 }
111 _object = p.takeObject();
112 return *this;
113 }
114
116 PythonQtObjectPtr& operator=(PythonQtSafeObjectPtr&& p);
117
118 PythonQtObjectPtr& operator=(PyObject* o)
119 {
120 setObject(o);
121 return *this;
122 }
123
124 PythonQtObjectPtr& operator=(const QVariant& variant)
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
147 void setNewRef(PyObject* o);
148
149 PyObject* object() const { return _object; }
150
152 QVariant evalScript(const QString& script, int start = Py_file_input);
153
156 QVariant evalCode(PyObject* pycode);
157
159 void evalFile(const QString& filename);
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
171 QVariant getVariable(const QString& name);
172
174 QVariant call(const QString& callable, const QVariantList& args = QVariantList(),
175 const QVariantMap& kwargs = QVariantMap());
176
178 QVariant call(const QVariantList& args = QVariantList(), const QVariantMap& kwargs = QVariantMap());
179
182 PyObject* takeObject()
183 {
184 PyObject* o = _object;
185 _object = nullptr;
186 return o;
187 }
188
189protected:
190
191 void setObject(PyObject* o);
192
193private:
194 PyObject* _object;
195};
196
198class PYTHONQT_EXPORT PythonQtSafeObjectPtr
199{
200public:
201 PythonQtSafeObjectPtr()
202 : _object(nullptr)
203 {
204 }
205
206 PythonQtSafeObjectPtr(const PythonQtSafeObjectPtr& p)
207 : _object(nullptr)
208 {
209 setObject(p.object());
210 }
211
212 PythonQtSafeObjectPtr(const PythonQtObjectPtr& p)
213 : _object(nullptr)
214 {
215 setObject(p.object());
216 }
217
219 PythonQtSafeObjectPtr(PythonQtSafeObjectPtr&& p) noexcept
220 : _object(p._object)
221 {
222 p._object = nullptr;
223 }
224
226 PythonQtSafeObjectPtr(PythonQtObjectPtr&& p)
227 : _object(p.takeObject())
228 {
229 }
230
231 PythonQtSafeObjectPtr(PyObject* o);
232
233 ~PythonQtSafeObjectPtr();
234
235 PythonQtSafeObjectPtr& operator=(const PythonQtSafeObjectPtr& p)
236 {
237 setObject(p.object());
238 return *this;
239 }
240
241 PythonQtSafeObjectPtr& operator=(const PythonQtObjectPtr& p)
242 {
243 setObjectUnsafe(p.object());
244 return *this;
245 }
246
248 PythonQtSafeObjectPtr& operator=(PythonQtSafeObjectPtr&& p) noexcept
249 {
250 if (_object) {
251 setObject(nullptr);
252 }
253 _object = p._object;
254 p._object = nullptr;
255 return *this;
256 }
257
259 PythonQtSafeObjectPtr& operator=(PythonQtObjectPtr&& p)
260 {
261 if (_object) {
262 setObjectUnsafe(nullptr);
263 }
264 _object = p.takeObject();
265 return *this;
266 }
267
268 PythonQtSafeObjectPtr& operator=(PyObject* o)
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
294 PyObject* takeObject()
295 {
296 PyObject* o = _object;
297 _object = nullptr;
298 return o;
299 }
300
301protected:
302 void setObject(PyObject* o);
303 void setObjectUnsafe(PyObject* o);
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
314Q_DECLARE_METATYPE(PythonQtSafeObjectPtr)
315
316#endif
PyObject
struct _object PyObject
Definition PythonQtMethodInfo.h:55
PythonQtPythonInclude.h
PythonQtSystem.h
PYTHONQT_EXPORT
#define PYTHONQT_EXPORT
Definition PythonQtSystem.h:55
PythonQtThreadSupport.h
PythonQtObjectPtr
a smart pointer that stores a PyObject pointer and that handles reference counting automatically
Definition PythonQtObjectPtr.h:57
PythonQtObjectPtr::operator->
PyObject * operator->() const
Definition PythonQtObjectPtr.h:140
PythonQtObjectPtr::call
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=
PythonQtObjectPtr & operator=(const PythonQtObjectPtr &p)
Definition PythonQtObjectPtr.h:99
PythonQtObjectPtr::PythonQtObjectPtr
PythonQtObjectPtr(PythonQtObjectPtr &&p) noexcept
rvalue copy constructor, does not need any incref/decref.
Definition PythonQtObjectPtr.h:71
PythonQtObjectPtr::setObject
void setObject(PyObject *o)
PythonQtObjectPtr::addVariable
void addVariable(const QString &name, const QVariant &v)
add the given variable to the module
PythonQtObjectPtr::operator=
PythonQtObjectPtr & operator=(PyObject *o)
Definition PythonQtObjectPtr.h:118
PythonQtObjectPtr::evalFile
void evalFile(const QString &filename)
evaluates the given code in the context
PythonQtObjectPtr::operator=
PythonQtObjectPtr & operator=(const QVariant &variant)
Definition PythonQtObjectPtr.h:124
PythonQtObjectPtr::takeObject
PyObject * takeObject()
Definition PythonQtObjectPtr.h:182
PythonQtObjectPtr::PythonQtObjectPtr
PythonQtObjectPtr(PyObject *o)
PythonQtObjectPtr::PythonQtObjectPtr
PythonQtObjectPtr(const PythonQtObjectPtr &p)
Definition PythonQtObjectPtr.h:64
PythonQtObjectPtr::PythonQtObjectPtr
PythonQtObjectPtr(PythonQtSafeObjectPtr &&p)
rvalue copy constructor, does not need any incref/decref.
PythonQtObjectPtr::operator*
PyObject & operator*() const
Definition PythonQtObjectPtr.h:142
PythonQtObjectPtr::toLocalVariant
QVariant toLocalVariant()
Returns a PythonQtObjectPtr as a QVariant. Only use this when you know that the variant is only used ...
PythonQtObjectPtr::operator=
PythonQtObjectPtr & operator=(PythonQtObjectPtr &&p) noexcept
rvalue assignment operator that steals the reference from p
Definition PythonQtObjectPtr.h:106
PythonQtObjectPtr::isNull
bool isNull() const
Definition PythonQtObjectPtr.h:138
PythonQtObjectPtr::operator==
bool operator==(PyObject *p) const
Definition PythonQtObjectPtr.h:134
PythonQtObjectPtr::operator=
PythonQtObjectPtr & operator=(PythonQtSafeObjectPtr &&p)
rvalue assignment operator that steals the reference from p
PythonQtObjectPtr::~PythonQtObjectPtr
~PythonQtObjectPtr()
PythonQtObjectPtr::evalScript
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
PythonQtObjectPtr::removeVariable
void removeVariable(const QString &name)
remove the given variable
PythonQtObjectPtr::PythonQtObjectPtr
PythonQtObjectPtr(const QVariant &variant)
If the given variant holds a PythonQtObjectPtr, extract the value from it and hold onto the reference...
Definition PythonQtObjectPtr.h:80
PythonQtObjectPtr::operator!=
bool operator!=(PyObject *p) const
Definition PythonQtObjectPtr.h:136
PythonQtObjectPtr::operator!=
bool operator!=(const PythonQtObjectPtr &p) const
Definition PythonQtObjectPtr.h:132
PythonQtObjectPtr::toVariant
QVariant toVariant()
Returns a PythonQtSafeObjectPtr as a QVariant. It does not return a PythonQtObjectPtr,...
PythonQtObjectPtr::addObject
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)
PythonQtObjectPtr::fromVariant
bool fromVariant(const QVariant &variant)
If the given variant holds a PythonQtObjectPtr, extract the value from it and hold onto the reference...
PythonQtObjectPtr::getVariable
QVariant getVariable(const QString &name)
get the variable with the name of the module, returns an invalid QVariant on error
PythonQtObjectPtr::object
PyObject * object() const
Definition PythonQtObjectPtr.h:149
PythonQtObjectPtr::call
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 ...
PythonQtObjectPtr::operator==
bool operator==(const PythonQtObjectPtr &p) const
Definition PythonQtObjectPtr.h:130
PythonQtObjectPtr::evalCode
QVariant evalCode(PyObject *pycode)
PythonQtObjectPtr::setNewRef
void setNewRef(PyObject *o)
sets the object and passes the ownership (stealing the reference, in Python slang)
PythonQtObjectPtr::PythonQtObjectPtr
PythonQtObjectPtr()
Definition PythonQtObjectPtr.h:59
PythonQtSafeObjectPtr
a smart pointer that stores a PyObject pointer and that handles reference counting automatically
Definition PythonQtObjectPtr.h:199
PythonQtSafeObjectPtr::setObject
void setObject(PyObject *o)
PythonQtSafeObjectPtr::operator=
PythonQtSafeObjectPtr & operator=(PythonQtSafeObjectPtr &&p) noexcept
rvalue assignment operator that steals the reference from p
Definition PythonQtObjectPtr.h:248
PythonQtSafeObjectPtr::operator->
PyObject * operator->() const
Definition PythonQtObjectPtr.h:284
PythonQtSafeObjectPtr::setObjectUnsafe
void setObjectUnsafe(PyObject *o)
PythonQtSafeObjectPtr::PythonQtSafeObjectPtr
PythonQtSafeObjectPtr(PythonQtSafeObjectPtr &&p) noexcept
rvalue copy constructor, does not need any incref/decref.
Definition PythonQtObjectPtr.h:219
PythonQtSafeObjectPtr::PythonQtSafeObjectPtr
PythonQtSafeObjectPtr(const PythonQtSafeObjectPtr &p)
Definition PythonQtObjectPtr.h:206
PythonQtSafeObjectPtr::PythonQtSafeObjectPtr
PythonQtSafeObjectPtr()
Definition PythonQtObjectPtr.h:201
PythonQtSafeObjectPtr::operator=
PythonQtSafeObjectPtr & operator=(const PythonQtObjectPtr &p)
Definition PythonQtObjectPtr.h:241
PythonQtSafeObjectPtr::operator!=
bool operator!=(PyObject *p) const
Definition PythonQtObjectPtr.h:280
PythonQtSafeObjectPtr::takeObject
PyObject * takeObject()
Definition PythonQtObjectPtr.h:294
PythonQtSafeObjectPtr::isNull
bool isNull() const
Definition PythonQtObjectPtr.h:282
PythonQtSafeObjectPtr::PythonQtSafeObjectPtr
PythonQtSafeObjectPtr(PyObject *o)
PythonQtSafeObjectPtr::PythonQtSafeObjectPtr
PythonQtSafeObjectPtr(PythonQtObjectPtr &&p)
rvalue copy constructor, does not need any incref/decref.
Definition PythonQtObjectPtr.h:226
PythonQtSafeObjectPtr::object
PyObject * object() const
Definition PythonQtObjectPtr.h:290
PythonQtSafeObjectPtr::operator*
PyObject & operator*() const
Definition PythonQtObjectPtr.h:286
PythonQtSafeObjectPtr::operator==
bool operator==(PyObject *p) const
Definition PythonQtObjectPtr.h:278
PythonQtSafeObjectPtr::PythonQtSafeObjectPtr
PythonQtSafeObjectPtr(const PythonQtObjectPtr &p)
Definition PythonQtObjectPtr.h:212
PythonQtSafeObjectPtr::operator=
PythonQtSafeObjectPtr & operator=(PythonQtObjectPtr &&p)
rvalue assignment operator that steals the reference from p
Definition PythonQtObjectPtr.h:259
PythonQtSafeObjectPtr::~PythonQtSafeObjectPtr
~PythonQtSafeObjectPtr()
PythonQtSafeObjectPtr::operator=
PythonQtSafeObjectPtr & operator=(PyObject *o)
Definition PythonQtObjectPtr.h:268
PythonQtSafeObjectPtr::operator=
PythonQtSafeObjectPtr & operator=(const PythonQtSafeObjectPtr &p)
Definition PythonQtObjectPtr.h:235
PythonQtSafeObjectPtr::operator!=
bool operator!=(const PythonQtSafeObjectPtr &p) const
Definition PythonQtObjectPtr.h:276
PythonQtSafeObjectPtr::operator==
bool operator==(const PythonQtSafeObjectPtr &p) const
Definition PythonQtObjectPtr.h:274

Generated on for PythonQt by doxygen 1.15.0
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.