diff options
author | Marcelo Lira <marcelo.lira@openbossa.org> | 2011-07-16 17:15:09 -0300 |
---|---|---|
committer | Hugo Parente Lima <hugo.pl@gmail.com> | 2012-03-08 16:17:05 -0300 |
commit | 849c246c1e459ddeb545b5a4520365fbb803bf55 (patch) | |
tree | 96d061b6f20170085b3553f74254afb84a0cf837 /libshiboken | |
parent | 5d61dc4c2d080c1e843d8958169c924c8b31f4de (diff) |
Added a function to initialize a new wrapper type and add it to a module or enclosing class.
The class register writer was updated to use the new
ObjectType::introduceWrapperType().
Types are now created and registered via the introduceWrapperType() function.
I also did a little refactoring on CppGenerator::writeClassRegister.
Diffstat (limited to 'libshiboken')
-rw-r--r-- | libshiboken/basewrapper.cpp | 34 | ||||
-rw-r--r-- | libshiboken/basewrapper.h | 24 |
2 files changed, 55 insertions, 3 deletions
diff --git a/libshiboken/basewrapper.cpp b/libshiboken/basewrapper.cpp index 6503616ab..0e2f2c89a 100644 --- a/libshiboken/basewrapper.cpp +++ b/libshiboken/basewrapper.cpp @@ -626,6 +626,38 @@ void initPrivateData(SbkObjectType* self) memset(self->d, 0, sizeof(SbkObjectTypePrivate)); } +bool introduceWrapperType(PyObject* enclosingObject, + const char* typeName, const char* originalName, + SbkObjectType* type, ObjectDestructor cppObjDtor, + SbkObjectType* baseType, PyObject* baseTypes, + bool isInnerClass) +{ + initPrivateData(type); + setOriginalName(type, originalName); + setDestructorFunction(type, cppObjDtor); + + if (baseType) { + type->super.ht_type.tp_base = (PyTypeObject*)baseType; + if (baseTypes) { + for (int i = 0; i < PySequence_Fast_GET_SIZE(baseTypes); ++i) + BindingManager::instance().addClassInheritance((SbkObjectType*)PySequence_Fast_GET_ITEM(baseTypes, i), type); + type->super.ht_type.tp_bases = baseTypes; + } else { + BindingManager::instance().addClassInheritance(baseType, type); + } + } + + if (PyType_Ready((PyTypeObject*)type) < 0) + return false; + + if (isInnerClass) + return PyDict_SetItemString(enclosingObject, typeName, (PyObject*)type) == 0; + + //PyModule_AddObject steals type's reference. + Py_INCREF((PyObject*)type); + return PyModule_AddObject(enclosingObject, typeName, (PyObject*)type) == 0; +} + void setSubTypeInitHook(SbkObjectType* self, SubTypeInitHook func) { self->d->subtype_init = func; @@ -1185,5 +1217,3 @@ void clearReferences(SbkObject* self) } // namespace Object } // namespace Shiboken - - diff --git a/libshiboken/basewrapper.h b/libshiboken/basewrapper.h index 4d08b7cd4..fdaca4ba6 100644 --- a/libshiboken/basewrapper.h +++ b/libshiboken/basewrapper.h @@ -168,6 +168,29 @@ LIBSHIBOKEN_API void setDestructorFunction(SbkObjectType* self, ObjectDes LIBSHIBOKEN_API void initPrivateData(SbkObjectType* self); /** + * Initializes a Shiboken wrapper type and adds it to the module, + * or to the enclosing class if the type is an inner class. + * This function also calls initPrivateData and setDestructorFunction. + * \param enclosingObject The module or enclosing class to where the new \p type will be added. + * \param typeName Name by which the type will be known in Python. + * \param originalName Original C++ name of the type. + * \param type The new type to be initialized and added to the module. + * \param cppObjDtor Memory deallocation function for the C++ object held by \p type. + * Should not be used if the underlying C++ class has a private destructor. + * \param baseType Base type from whom the new \p type inherits. + * \param baseTypes Other base types from whom the new \p type inherits. + * \param isInnerClass Tells if the new \p type is an inner class (the default is that it isn't). + * If false then the \p enclosingObject is a module, otherwise it is another + * wrapper type. + * \returns true if the initialization went fine, false otherwise. + */ +LIBSHIBOKEN_API bool introduceWrapperType(PyObject* enclosingObject, + const char* typeName, const char* originalName, + SbkObjectType* type, ObjectDestructor cppObjDtor = 0, + SbkObjectType* baseType = 0, PyObject* baseTypes = 0, + bool isInnerClass = false); + +/** * Set the subtype init hook for a type. * * This hook will be invoked every time the user creates a sub-type inherited from a Shiboken based type. @@ -365,4 +388,3 @@ LIBSHIBOKEN_API void removeReference(SbkObject* self, const char* key, Py } // namespace Shiboken #endif // BASEWRAPPER_H - |