PythonQt
PythonQtClassInfo.h
Go to the documentation of this file.
1#ifndef _PYTHONQTCLASSINFO_H
2#define _PYTHONQTCLASSINFO_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
37#include "PythonQt.h"
38#include <QMetaObject>
39#include <QMetaMethod>
40#include <QHash>
41#include <QByteArray>
42#include <QList>
43
46
58
61
63 : _type(Invalid)
64 , _slot(nullptr)
65 , _pythonType(nullptr)
66 , _enumValue(nullptr)
67 {
68 }
69
71
73
74 PythonQtMemberInfo(const QMetaProperty& prop);
75
77
78 // TODO: this could be a union...
82 QMetaProperty _property;
83};
84
86
89{
90
91public:
94
97 ParentClassInfo(PythonQtClassInfo* parent, int upcastingOffset = 0)
98 : _parent(parent)
99 , _upcastingOffset(upcastingOffset) {};
100
103 };
104
106 void setupQObject(const QMetaObject* meta);
107
109 void setupCPPObject(const QByteArray& classname);
110
112 void setTypeSlots(int typeSlots) { _typeSlots = typeSlots; }
114 int typeSlots() const { return _typeSlots; }
115
117 PythonQtMemberInfo member(const char* member);
118
121
124
127
130
133
136
138 const QByteArray& className() const;
139
141 QByteArray unscopedClassName() const;
142
144 bool isQObject() { return _isQObject; }
145
147 bool isCPPWrapper() { return !_isQObject; }
148
150 const QMetaObject* metaObject() { return _meta; }
151
153 void setMetaObject(const QMetaObject* meta);
154
156 bool inherits(const char* classname);
157
160
164 void* castTo(void* ptr, const char* classname);
165
167 QString help();
168
170 QStringList propertyList();
171
173 QStringList memberList();
174
176 int metaTypeId() { return _metaTypeId; }
177
180
182 QObject* decorator();
183
185 void addParentClass(const ParentClassInfo& info) { _parentClasses.append(info); }
186
188 void setPythonQtClassWrapper(PyObject* obj) { _pythonQtClassWrapper = obj; }
189
191 PyObject* pythonQtClassWrapper() { return _pythonQtClassWrapper; }
192
194 void setShellSetInstanceWrapperCB(PythonQtShellSetInstanceWrapperCB* cb) { _shellSetInstanceWrapperCB = cb; }
195
197 PythonQtShellSetInstanceWrapperCB* shellSetInstanceWrapperCB() { return _shellSetInstanceWrapperCB; }
198
200 void addPolymorphicHandler(PythonQtPolymorphicHandlerCB* cb) { _polymorphicHandlers.append(cb); }
201
203 void* castDownIfPossible(void* ptr, PythonQtClassInfo** resultClassInfo);
204
206 static PyObject* findEnumWrapper(const QByteArray& name, PythonQtClassInfo* localScope, bool* isLocalEnum = nullptr);
207
210
212 const QList<PythonQtClassInfo*>& nestedClasses() { return _nestedClasses; }
213
218 PyObject* copyObject(void* cppObject);
219
222
225
230
233 PyObject* getPythonTypeForProperty(const QString& name);
234
237
243
246 static QByteArray escapeReservedNames(const QByteArray& name);
247
249 static void addGlobalNamespaceWrapper(PythonQtClassInfo* namespaceWrapper);
250
254
255private:
256 void updateRefCountingCBs();
257
258 void createEnumWrappers(const QObject* decoratorProvider);
259 void createEnumWrappers(const QMetaObject* meta);
260 PyObject* findEnumWrapper(const char* name);
261
263 void clearCachedMembers();
264
265 void* recursiveCastDownIfPossible(void* ptr, const char** resultClassName);
266
267 PythonQtSlotInfo* findDecoratorSlotsFromDecoratorProvider(const char* memberName, PythonQtSlotInfo* inputInfo,
268 bool& found, QHash<QByteArray, PythonQtMemberInfo>& memberCache, int upcastingOffset);
269 void listDecoratorSlotsFromDecoratorProvider(QStringList& list, bool metaOnly);
270 PythonQtSlotInfo* recursiveFindDecoratorSlotsFromDecoratorProvider(const char* memberName,
271 PythonQtSlotInfo* inputInfo, bool& found, QHash<QByteArray, PythonQtMemberInfo>& memberCache, int upcastingOffset);
272
273 void recursiveCollectClassInfos(QList<PythonQtClassInfo*>& classInfoObjects);
274 void recursiveCollectDecoratorObjects(QList<QObject*>& decoratorObjects);
275
276 bool lookForPropertyAndCache(const char* memberName);
277 bool lookForMethodAndCache(const char* memberName);
278 bool lookForEnumAndCache(const QMetaObject* m, const char* memberName);
279
280 PythonQtSlotInfo* findDecoratorSlots(const char* memberName, PythonQtSlotInfo* tail, bool& found,
281 QHash<QByteArray, PythonQtMemberInfo>& memberCache, int upcastingOffset);
282 int findCharOffset(const char* sigStart, char someChar);
283
284 QHash<QByteArray, PythonQtMemberInfo> _cachedMembers;
285
286 PythonQtSlotInfo* _constructors;
287 PythonQtSlotInfo* _destructor;
288
289 PythonQtVoidPtrCB* _refCallback;
290 PythonQtVoidPtrCB* _unrefCallback;
291
292 QList<PythonQtSlotInfo*> _decoratorSlots;
293
294 QList<PythonQtObjectPtr> _enumWrappers;
295
296 const QMetaObject* _meta;
297
298 QByteArray _wrappedClassName;
299 QList<ParentClassInfo> _parentClasses;
300
301 QList<PythonQtPolymorphicHandlerCB*> _polymorphicHandlers;
302
303 QList<PythonQtClassInfo*> _nestedClasses;
304
305 QObject* _decoratorProvider;
306 PythonQtQObjectCreatorFunctionCB* _decoratorProviderCB;
307
308 PyObject* _pythonQtClassWrapper;
309
310 PythonQtShellSetInstanceWrapperCB* _shellSetInstanceWrapperCB;
311
312 int _metaTypeId;
313 int _typeSlots;
314
315 bool _isQObject;
316 bool _enumsCreated;
317 bool _richCompareDetectionDone;
318 bool _searchPolymorphicHandlerOnParent;
319 bool _searchRefCountCB;
320
321 static QList<PythonQtClassInfo*> _globalNamespaceWrappers;
322
323 static QSet<QByteArray> _reservedNames;
324};
325
326//---------------------------------------------------------------
327
328#endif
struct _object PyObject
#define PYTHONQT_EXPORT
void PythonQtShellSetInstanceWrapperCB(void *object, PythonQtInstanceWrapper *wrapper)
Definition PythonQt.h:78
void * PythonQtPolymorphicHandlerCB(const void *ptr, const char **class_name)
Definition PythonQt.h:75
void PythonQtVoidPtrCB(void *object)
Definition PythonQt.h:72
QObject * PythonQtQObjectCreatorFunctionCB()
callback to create a QObject lazily
Definition PythonQt.h:155
a class that stores all required information about a Qt object (and an optional associated C++ class ...
void setShellSetInstanceWrapperCB(PythonQtShellSetInstanceWrapperCB *cb)
set the shell set instance wrapper cb
void setReferenceCounting(PythonQtVoidPtrCB *refCB, PythonQtVoidPtrCB *unrefCB)
Sets reference counting callbacks for this class and all its subclasses.
const QMetaObject * metaObject()
get the meta object
bool supportsRichCompare()
void addNestedClass(PythonQtClassInfo *info)
add a nested class, so that it can be shown as outer class member
bool isCPPWrapper()
returns if the class is a CPP wrapper
bool inherits(const char *classname)
returns if this class inherits from the given classname
void addDecoratorSlot(PythonQtSlotInfo *info)
add a decorator slot, ownership is passed to classinfo
void clearNotFoundCachedMembers()
clear all members that where cached as "NotFound"
QStringList memberList()
get list of all members (excluding properties, which can be listed with propertyList())
PythonQtSlotInfo * destructor()
get access to the destructor slot
void setDecoratorProvider(PythonQtQObjectCreatorFunctionCB *cb)
set an additional decorator provider that offers additional decorator slots for this class
const QList< PythonQtClassInfo * > & nestedClasses()
get nested classes
int typeSlots() const
get the type capabilities
int metaTypeId()
get the meta type id of this class (only valid for isCPPWrapper() == true)
void addParentClass(const ParentClassInfo &info)
add the parent class info of a CPP object
QString help()
get help string for the metaobject
void addPolymorphicHandler(PythonQtPolymorphicHandlerCB *cb)
add a handler for polymorphic downcasting
void * castTo(void *ptr, const char *classname)
PyObject * getPythonTypeForProperty(const QString &name)
static QByteArray escapeReservedNames(const QByteArray &name)
bool isQObject()
returns if the QObject
bool inherits(PythonQtClassInfo *info)
returns if this class inherits from the given classinfo
PythonQtVoidPtrCB * referenceCountingRefCB()
Returns the ref counting CB, if there is any.
PythonQtSlotInfo * getCopyConstructor()
Get the copy constructor for this class.
PythonQtShellSetInstanceWrapperCB * shellSetInstanceWrapperCB()
get the shell set instance wrapper cb
QStringList propertyList()
get list of all properties (on QObjects only, otherwise the list is empty)
void setupCPPObject(const QByteArray &classname)
setup as a CPP (non-QObject), taking the classname
PythonQtClassInfo * getClassInfoForProperty(const QString &name)
Returns the class info for given property, if available.
void setDestructor(PythonQtSlotInfo *info)
set a destructor, ownership is passed to classinfo
PythonQtMemberInfo member(const char *member)
get the Python method definition for a given slot name (without return type and signature)
void addConstructor(PythonQtSlotInfo *info)
add a constructor, ownership is passed to classinfo
PyObject * pythonQtClassWrapper()
get the associated PythonQtClassWrapper (which handles instance creation of this type)
void * castDownIfPossible(void *ptr, PythonQtClassInfo **resultClassInfo)
cast the pointer down in the class hierarchy if a polymorphic handler allows to do that
PyObject * copyObject(void *cppObject)
void setMetaObject(const QMetaObject *meta)
set the meta object, this will reset the caching
QByteArray unscopedClassName() const
get the unscoped classname (without ParentClass::) for nested classes
PythonQtVoidPtrCB * referenceCountingUnrefCB()
Returns the unref counting CB, if there is any.
void setTypeSlots(int typeSlots)
set the type capabilities
PythonQtSlotInfo * constructors()
get access to the constructor slot (which may be overloaded if there are multiple constructors)
static void clearGlobalNamespaceWrappers()
void setPythonQtClassWrapper(PyObject *obj)
set the associated PythonQtClassWrapper (which handles instance creation of this type)
void setupQObject(const QMetaObject *meta)
setup as a QObject, taking the meta object as meta information about the QObject
const QByteArray & className() const
get the classname (either of the QObject or of the wrapped CPP object)
static void addGlobalNamespaceWrapper(PythonQtClassInfo *namespaceWrapper)
Add a wrapper that contains global enums.
static PyObject * findEnumWrapper(const QByteArray &name, PythonQtClassInfo *localScope, bool *isLocalEnum=nullptr)
returns if the localScope has an enum of that type name or if the enum contains a :: scope,...
QObject * decorator()
get the decorator qobject instance
a smart pointer that stores a PyObject pointer and that handles reference counting automatically
stores information about a slot, including a next pointer to overloaded slots
store information about parent classes
ParentClassInfo(PythonQtClassInfo *parent, int upcastingOffset=0)
const QMetaObject * _dynamicMetaObject
PythonQtClassInfo * _classInfo
PythonQtObjectPtr _enumValue
QMetaProperty _property
PythonQtSlotInfo * _slot
PythonQtMemberInfo(const QMetaProperty &prop)
PythonQtMemberInfo(const PythonQtObjectPtr &enumValue)
PythonQtMemberInfo(PythonQtSlotInfo *info)