PythonQt
PythonQtMethodInfo.h
Go to the documentation of this file.
1#ifndef _PYTHONQTMETHODINFO_H
2#define _PYTHONQTMETHODINFO_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#include "PythonQtSystem.h"
47
48#include <QByteArray>
49#include <QHash>
50#include <QList>
51#include <QMetaMethod>
52
54struct _object;
55typedef struct _object PyObject;
56
59{
60public:
61 enum ParameterType { Unknown = -1, Variant = -2 };
62
66 QByteArray innerName; // if the type is a template, this stores the inner name
67 PyObject* enumWrapper; // if it is an enum, a pointer to the enum wrapper
68 int typeId; // a mixture from QMetaType and ParameterType
69 char pointerCount; // the number of pointer indirections
70 char innerNamePointerCount; // the number of pointer indirections in the inner name
71 bool isConst;
73 bool isQList;
77 };
78
79 PythonQtMethodInfo() { _shouldAllowThreads = true; };
82 PythonQtMethodInfo(const QByteArray& typeName, const QList<QByteArray>& args);
84 {
85 _parameters = other._parameters;
86 _shouldAllowThreads = other._shouldAllowThreads;
87 }
88
91 static const PythonQtMethodInfo* getCachedMethodInfo(const QMetaMethod& method, PythonQtClassInfo* classInfo);
92
94 static const PythonQtMethodInfo* getCachedMethodInfoFromArgumentList(int numArgs, const char** args);
95
98
100 int parameterCount() const { return _parameters.size(); };
101
103 static int nameToType(const char* name);
104
106 const QList<ParameterInfo>& parameters() const { return _parameters; }
107
109 static void addParameterTypeAlias(const QByteArray& alias, const QByteArray& name);
110
112 static void fillParameterInfo(ParameterInfo& type, const QByteArray& name, PythonQtClassInfo* classInfo = nullptr);
113
116
118 static int getInnerTemplateMetaType(const QByteArray& typeName);
119
122
125
129 bool shouldAllowThreads() const { return _shouldAllowThreads; }
130
131protected:
133
136
139
141
144};
145
148{
149public:
150 enum Type { MemberSlot, InstanceDecorator, ClassDecorator };
151
154 {
155 _meta = info._meta;
156 _parameters = info._parameters;
157 _shouldAllowThreads = info._shouldAllowThreads;
158 _slotIndex = info._slotIndex;
159 _next = nullptr;
160 _decorator = info._decorator;
161 _type = info._type;
162 _upcastingOffset = 0;
163 }
164
165 PythonQtSlotInfo(PythonQtClassInfo* classInfo, const QMetaMethod& meta, int slotIndex, QObject* decorator = nullptr,
166 Type type = MemberSlot)
168 {
169 const PythonQtMethodInfo* info = getCachedMethodInfo(meta, classInfo);
170 _meta = meta;
171 _parameters = info->parameters();
172 _shouldAllowThreads = info->shouldAllowThreads();
173 _slotIndex = slotIndex;
174 _next = nullptr;
175 _decorator = decorator;
176 _type = type;
177 _upcastingOffset = 0;
178 }
179
180public:
183
185
186 const QMetaMethod* metaMethod() const { return &_meta; }
187
188 void setUpcastingOffset(int upcastingOffset) { _upcastingOffset = upcastingOffset; }
189
190 int upcastingOffset() const { return _upcastingOffset; }
191
193 int slotIndex() const { return _slotIndex; }
194
196 PythonQtSlotInfo* nextInfo() const { return _next; }
197
200
202 bool isInstanceDecorator() const { return _decorator != nullptr && _type == InstanceDecorator; }
203
205 bool isClassDecorator() const { return _decorator != nullptr && _type == ClassDecorator; }
206
207 QObject* decorator() const { return _decorator; }
208
211
214
217
222
226
228 static void invokeQtMethod(QObject* obj, PythonQtSlotInfo* slot, void** argList);
229
232
235
236private:
237 int _slotIndex;
238 PythonQtSlotInfo* _next;
239 QObject* _decorator;
240 Type _type;
241 QMetaMethod _meta;
242 int _upcastingOffset;
243
244 static bool _globalShouldAllowThreads;
245};
246
247#endif
PyObject * PythonQtConvertPairToPython(const void *inPair, int metaTypeId)
struct _object PyObject
#define PYTHONQT_EXPORT
a class that stores all required information about a Qt object (and an optional associated C++ class ...
stores information about a specific signal/slot/method
static const PythonQtMethodInfo * getCachedMethodInfoFromArgumentList(int numArgs, const char **args)
get the cached method info using the passed in list of return value and arguments,...
static QByteArray getInnerTemplateTypeName(const QByteArray &typeName)
returns the inner type name of a simple template of the form SomeObject<InnerType>
static const PythonQtMethodInfo * getCachedMethodInfo(const QMetaMethod &method, PythonQtClassInfo *classInfo)
static void addParameterTypeAlias(const QByteArray &alias, const QByteArray &name)
add an alias for a typename, e.g. QObjectList and QList<QObject*>.
static QHash< QByteArray, QByteArray > _parameterNameAliases
static int getInnerTemplateMetaType(const QByteArray &typeName)
returns the inner type id of a simple template of the form SomeObject<InnerType>
static const ParameterInfo & getParameterInfoForMetaType(int type)
returns a parameter info for the given metatype (and creates and caches one if it is not yet present)
const QList< ParameterInfo > & parameters() const
get the parameter infos
static QHash< QByteArray, int > _parameterTypeDict
static QByteArray getInnerListTypeName(const QByteArray &typeName)
returns the inner type name of a simple template or the typename without appended "List".
static void cleanupCachedMethodInfos()
cleanup the cache
PythonQtMethodInfo(const QByteArray &typeName, const QList< QByteArray > &args)
PythonQtMethodInfo(const PythonQtMethodInfo &other)
QList< ParameterInfo > _parameters
static QHash< int, ParameterInfo > _cachedParameterInfos
int parameterCount() const
returns the number of parameters including the return value
static int nameToType(const char *name)
returns the id for the given type (using an internal dictionary)
static void fillParameterInfo(ParameterInfo &type, const QByteArray &name, PythonQtClassInfo *classInfo=nullptr)
fill the parameter info for the given type name
static QHash< QByteArray, PythonQtMethodInfo * > _cachedSignatures
stores the cached signatures of methods to speedup mapping from Qt to Python types
bool shouldAllowThreads() const
PythonQtMethodInfo(const QMetaMethod &meta, PythonQtClassInfo *classInfo)
stores information about a slot, including a next pointer to overloaded slots
PythonQtSlotInfo(PythonQtClassInfo *classInfo, const QMetaMethod &meta, int slotIndex, QObject *decorator=nullptr, Type type=MemberSlot)
QByteArray getImplementingClassName() const
QStringList overloads(bool skipReturnValue=false) const
int slotIndex() const
get the index of the slot (needed for qt_metacall)
QString fullSignature(bool skipReturnValue=false, int optionalArgsIndex=-1) const
get the full signature including return type
static bool getGlobalShouldAllowThreads()
Returns if calling slots should release the GIL to allow Python threads while being inside of C++.
QByteArray slotName(bool removeDecorators=false) const
get the short slot name
PythonQtSlotInfo * nextInfo() const
get next overloaded slot (which has the same name)
QList< ParameterInfo > arguments() const
get the parameter infos for the arguments, without return type and instance decorator.
static void invokeQtMethod(QObject *obj, PythonQtSlotInfo *slot, void **argList)
Invoke the given slot on obj, save/restore thread state if needed.
bool isInstanceDecorator() const
returns if the slot is a decorator slot
void setNextInfo(PythonQtSlotInfo *next)
set the next overloaded slot
QByteArray signature() const
get the Qt signature of the slot
bool isClassDecorator() const
returns if the slot is a constructor slot
static void setGlobalShouldAllowThreads(bool flag)
Sets if calling slots should release the GIL to allow other Python threads while being inside of C++.
void setUpcastingOffset(int upcastingOffset)
QObject * decorator() const
void deleteOverloadsAndThis()
PythonQtSlotInfo(const PythonQtSlotInfo &info)
const QMetaMethod * metaMethod() const
int upcastingOffset() const
stores various informations about a parameter/type name