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 
36 #include "PythonQtPythonInclude.h"
37 #include "PythonQt.h"
38 #include <QMetaObject>
39 #include <QMetaMethod>
40 #include <QHash>
41 #include <QByteArray>
42 #include <QList>
43 
44 class PythonQtSlotInfo;
45 class PythonQtClassInfo;
46 
48 {
51 
52  const QMetaObject* _dynamicMetaObject;
54 };
55 
57  enum Type {
59  };
60 
61  PythonQtMemberInfo():_type(Invalid),_slot(nullptr),_pythonType(nullptr),_enumValue(nullptr) { }
62 
64 
66 
67  PythonQtMemberInfo(const QMetaProperty& prop);
68 
70 
71  // TODO: this could be a union...
75  QMetaProperty _property;
76 };
77 
79 
82 
83 public:
86 
88  struct ParentClassInfo {
89  ParentClassInfo(PythonQtClassInfo* parent, int upcastingOffset=0):_parent(parent),_upcastingOffset(upcastingOffset)
90  {};
91 
94  };
95 
96 
98  void setupQObject(const QMetaObject* meta);
99 
101  void setupCPPObject(const QByteArray& classname);
102 
104  void setTypeSlots(int typeSlots) { _typeSlots = typeSlots; }
106  int typeSlots() const { return _typeSlots; }
107 
109  PythonQtMemberInfo member(const char* member);
110 
113 
116 
119 
122 
125 
128 
130  const QByteArray& className() const;
131 
133  QByteArray unscopedClassName() const;
134 
136  bool isQObject() { return _isQObject; }
137 
139  bool isCPPWrapper() { return !_isQObject; }
140 
142  const QMetaObject* metaObject() { return _meta; }
143 
145  void setMetaObject(const QMetaObject* meta);
146 
148  bool inherits(const char* classname);
149 
152 
156  void* castTo(void* ptr, const char* classname);
157 
159  QString help();
160 
162  QStringList propertyList();
163 
165  QStringList memberList();
166 
168  int metaTypeId() { return _metaTypeId; }
169 
172 
174  QObject* decorator();
175 
177  void addParentClass(const ParentClassInfo& info) { _parentClasses.append(info); }
178 
180  void setPythonQtClassWrapper(PyObject* obj) { _pythonQtClassWrapper = obj; }
181 
183  PyObject* pythonQtClassWrapper() { return _pythonQtClassWrapper; }
184 
187  _shellSetInstanceWrapperCB = cb;
188  }
189 
192  return _shellSetInstanceWrapperCB;
193  }
194 
196  void addPolymorphicHandler(PythonQtPolymorphicHandlerCB* cb) { _polymorphicHandlers.append(cb); }
197 
199  void* castDownIfPossible(void* ptr, PythonQtClassInfo** resultClassInfo);
200 
202  static PyObject* findEnumWrapper(const QByteArray& name, PythonQtClassInfo* localScope, bool* isLocalEnum = nullptr);
203 
206 
208  const QList<PythonQtClassInfo*>& nestedClasses() { return _nestedClasses; }
209 
214  PyObject* copyObject(void* cppObject);
215 
218 
221 
226 
229  PyObject* getPythonTypeForProperty(const QString& name);
230 
233 
239 
242  static QByteArray escapeReservedNames(const QByteArray& name);
243 
245  static void addGlobalNamespaceWrapper(PythonQtClassInfo* namespaceWrapper);
246 
247 private:
248  void updateRefCountingCBs();
249 
250  void createEnumWrappers(const QObject* decoratorProvider);
251  void createEnumWrappers(const QMetaObject* meta);
252  PyObject* findEnumWrapper(const char* name);
253 
255  void clearCachedMembers();
256 
257  void* recursiveCastDownIfPossible(void* ptr, const char** resultClassName);
258 
259  PythonQtSlotInfo* findDecoratorSlotsFromDecoratorProvider(const char* memberName, PythonQtSlotInfo* inputInfo, bool &found, QHash<QByteArray, PythonQtMemberInfo>& memberCache, int upcastingOffset);
260  void listDecoratorSlotsFromDecoratorProvider(QStringList& list, bool metaOnly);
261  PythonQtSlotInfo* recursiveFindDecoratorSlotsFromDecoratorProvider(const char* memberName, PythonQtSlotInfo* inputInfo, bool &found, QHash<QByteArray, PythonQtMemberInfo>& memberCache, int upcastingOffset);
262 
263  void recursiveCollectClassInfos(QList<PythonQtClassInfo*>& classInfoObjects);
264  void recursiveCollectDecoratorObjects(QList<QObject*>& decoratorObjects);
265 
266  bool lookForPropertyAndCache(const char* memberName);
267  bool lookForMethodAndCache(const char* memberName);
268  bool lookForEnumAndCache(const QMetaObject* m, const char* memberName);
269 
270  PythonQtSlotInfo* findDecoratorSlots(const char* memberName, PythonQtSlotInfo* tail, bool &found, QHash<QByteArray, PythonQtMemberInfo>& memberCache, int upcastingOffset);
271  int findCharOffset(const char* sigStart, char someChar);
272 
273  QHash<QByteArray, PythonQtMemberInfo> _cachedMembers;
274 
275  PythonQtSlotInfo* _constructors;
276  PythonQtSlotInfo* _destructor;
277 
278  PythonQtVoidPtrCB* _refCallback;
279  PythonQtVoidPtrCB* _unrefCallback;
280 
281  QList<PythonQtSlotInfo*> _decoratorSlots;
282 
283  QList<PythonQtObjectPtr> _enumWrappers;
284 
285  const QMetaObject* _meta;
286 
287  QByteArray _wrappedClassName;
288  QList<ParentClassInfo> _parentClasses;
289 
290  QList<PythonQtPolymorphicHandlerCB*> _polymorphicHandlers;
291 
292  QList<PythonQtClassInfo*> _nestedClasses;
293 
294  QObject* _decoratorProvider;
295  PythonQtQObjectCreatorFunctionCB* _decoratorProviderCB;
296 
297  PyObject* _pythonQtClassWrapper;
298 
299  PythonQtShellSetInstanceWrapperCB* _shellSetInstanceWrapperCB;
300 
301  int _metaTypeId;
302  int _typeSlots;
303 
304  bool _isQObject;
305  bool _enumsCreated;
306  bool _richCompareDetectionDone;
307  bool _searchPolymorphicHandlerOnParent;
308  bool _searchRefCountCB;
309 
310  static QList<PythonQtClassInfo*> _globalNamespaceWrappers;
311 
312  static QSet<QByteArray> _reservedNames;
313 };
314 
315 //---------------------------------------------------------------
316 
317 
318 #endif
struct _object PyObject
#define PYTHONQT_EXPORT
QObject * PythonQtQObjectCreatorFunctionCB()
callback to create a QObject lazily
Definition: PythonQt.h:144
void PythonQtShellSetInstanceWrapperCB(void *object, PythonQtInstanceWrapper *wrapper)
Definition: PythonQt.h:79
void PythonQtVoidPtrCB(void *object)
Definition: PythonQt.h:73
void * PythonQtPolymorphicHandlerCB(const void *ptr, const char **class_name)
Definition: PythonQt.h:76
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.
bool supportsRichCompare()
void addNestedClass(PythonQtClassInfo *info)
add a nested class, so that it can be shown as outer class member
PythonQtClassInfo * getClassInfoForProperty(const QString &name)
Returns the class info for given property, if available.
bool isCPPWrapper()
returns if the class is a CPP wrapper
PyObject * copyObject(void *cppObject)
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())
void setDecoratorProvider(PythonQtQObjectCreatorFunctionCB *cb)
set an additional decorator provider that offers additional decorator slots for this class
PythonQtSlotInfo * constructors()
get access to the constructor slot (which may be overloaded if there are multiple constructors)
int typeSlots() const
get the type capabilities
PythonQtShellSetInstanceWrapperCB * shellSetInstanceWrapperCB()
get the shell set instance wrapper cb
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
PythonQtSlotInfo * destructor()
get access to the destructor slot
void addPolymorphicHandler(PythonQtPolymorphicHandlerCB *cb)
add a handler for polymorphic downcasting
PythonQtVoidPtrCB * referenceCountingRefCB()
Returns the ref counting CB, if there is any.
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
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,...
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
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
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.
PythonQtSlotInfo * getCopyConstructor()
Get the copy constructor for this class.
void setTypeSlots(int typeSlots)
set the type capabilities
void * castTo(void *ptr, const char *classname)
const QList< PythonQtClassInfo * > & nestedClasses()
get nested classes
void setPythonQtClassWrapper(PyObject *obj)
set the associated PythonQtClassWrapper (which handles instance creation of this type)
PyObject * pythonQtClassWrapper()
get 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
QObject * decorator()
get the decorator qobject instance
static void addGlobalNamespaceWrapper(PythonQtClassInfo *namespaceWrapper)
Add a wrapper that contains global enums.
const QMetaObject * metaObject()
get the meta object
PyObject * getPythonTypeForProperty(const QString &name)
void * castDownIfPossible(void *ptr, PythonQtClassInfo **resultClassInfo)
cast the pointer down in the class hierarchy if a polymorphic handler allows to do that
const QByteArray & className() const
get the classname (either of the QObject or of the wrapped CPP object)
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)