diff options
author | Hugo Lima <hugo.lima@openbossa.org> | 2010-03-02 14:34:01 -0300 |
---|---|---|
committer | Hugo Lima <hugo.lima@openbossa.org> | 2010-03-02 17:18:20 -0300 |
commit | 3a4263a603b8caa4f93dcd534971bf7ffaf65e9f (patch) | |
tree | fc98bf94b965e9f5f35c7150463b4966d7620e49 /libshiboken | |
parent | ac7573693042bc8f9d4d94958d89d5b15add39f2 (diff) |
Use polymorphic-id-expression to discover the correct type of a C++ object.
Diffstat (limited to 'libshiboken')
-rw-r--r-- | libshiboken/basewrapper.cpp | 35 | ||||
-rw-r--r-- | libshiboken/basewrapper.h | 18 | ||||
-rw-r--r-- | libshiboken/conversions.h | 2 |
3 files changed, 35 insertions, 20 deletions
diff --git a/libshiboken/basewrapper.cpp b/libshiboken/basewrapper.cpp index fe9b73b1d..0deaefbe6 100644 --- a/libshiboken/basewrapper.cpp +++ b/libshiboken/basewrapper.cpp @@ -125,12 +125,8 @@ PyObject* SbkBaseWrapper_New(SbkBaseWrapperType* instanceType, bool isExactType) { // Try to find the exact type of cptr. - if (!isExactType && instanceType->type_name_func) { - const char* typeName = instanceType->type_name_func(cptr); - TypeResolver* typeResolver = TypeResolver::get(typeName); - if (typeResolver) - instanceType = reinterpret_cast<SbkBaseWrapperType*>(typeResolver->pythonType()); - } + if (!isExactType && instanceType->type_discovery) + instanceType = instanceType->type_discovery->getType(cptr, instanceType); SbkBaseWrapper* self = reinterpret_cast<SbkBaseWrapper*>(SbkBaseWrapper_TpNew(reinterpret_cast<PyTypeObject*>(instanceType), 0, 0)); self->cptr = const_cast<void*>(cptr); @@ -230,16 +226,12 @@ SbkBaseWrapperType_TpNew(PyTypeObject* metatype, PyObject* args, PyObject* kwds) newType->mi_offsets = parentType->mi_offsets; newType->mi_init = parentType->mi_init; newType->mi_specialcast = parentType->mi_specialcast; - newType->type_name_func = parentType->type_name_func; + newType->type_discovery = parentType->type_discovery; + newType->obj_copier = parentType->obj_copier; return reinterpret_cast<PyObject*>(newType); } -extern "C" -{ - -struct SbkBaseWrapperType_Type; - PyTypeObject SbkBaseWrapperType_Type = { PyObject_HEAD_INIT(0) /*ob_size*/ 0, @@ -289,8 +281,6 @@ PyTypeObject SbkBaseWrapperType_Type = { /*tp_weaklist*/ 0 }; -} // extern "C" - static PyObject* SbkBaseWrapper_get_dict(SbkBaseWrapper* obj) { if (!obj->ob_dict) @@ -418,7 +408,22 @@ void setErrorAboutWrongArguments(PyObject* args, const char* funcName, const cha } -} // namespace Shiboken +SbkBaseWrapperType* TypeDiscovery::getType(const void* cptr, SbkBaseWrapperType* instanceType) const +{ + TypeDiscoveryFuncList::const_reverse_iterator it = m_discoveryFunctions.rbegin(); + for (; it != m_discoveryFunctions.rend(); ++it) { + SbkBaseWrapperType* type = (*it)(const_cast<void*>(cptr), instanceType); + if (type) + return type; + } + return instanceType; +} + +void TypeDiscovery::addTypeDiscoveryFunction(Shiboken::TypeDiscoveryFunc func) +{ + m_discoveryFunctions.push_back(func); +} +} // namespace Shiboken diff --git a/libshiboken/basewrapper.h b/libshiboken/basewrapper.h index ca876fccc..72b8d86f2 100644 --- a/libshiboken/basewrapper.h +++ b/libshiboken/basewrapper.h @@ -75,13 +75,22 @@ struct SbkBaseWrapperType; * part of a multiple inheritance hierarchy. * The implementation of this function is auto generated by the generator and you don't need to care about it. */ -typedef void* (*SpecialCastFunction)(PyObject*, SbkBaseWrapperType*); -typedef const char* (*TypeNameFunction)(const void*); +typedef void* (*SpecialCastFunction)(void*, SbkBaseWrapperType*); typedef void* (*ObjectCopierFunction)(const void*); +typedef SbkBaseWrapperType* (*TypeDiscoveryFunc)(void*, SbkBaseWrapperType*); +typedef std::list<TypeDiscoveryFunc> TypeDiscoveryFuncList; LIBSHIBOKEN_API PyAPI_DATA(PyTypeObject) SbkBaseWrapperType_Type; LIBSHIBOKEN_API PyAPI_DATA(SbkBaseWrapperType) SbkBaseWrapper_Type; +class LIBSHIBOKEN_API TypeDiscovery { +public: + SbkBaseWrapperType* getType(const void* cptr, SbkBaseWrapperType* instanceType) const; + void addTypeDiscoveryFunction(TypeDiscoveryFunc func); +private: + TypeDiscoveryFuncList m_discoveryFunctions; +}; + /// PyTypeObject extended with C++ multiple inheritance information. struct LIBSHIBOKEN_API SbkBaseWrapperType { @@ -90,7 +99,7 @@ struct LIBSHIBOKEN_API SbkBaseWrapperType MultipleInheritanceInitFunction mi_init; /// Special cast function, null if this class doesn't have multiple inheritance. SpecialCastFunction mi_specialcast; - TypeNameFunction type_name_func; + TypeDiscovery* type_discovery; ObjectCopierFunction obj_copier; }; @@ -256,7 +265,8 @@ void SbkBaseWrapper_Dealloc(PyObject* self) if (SbkBaseWrapper_hasParentInfo(self)) destroyParentInfo(reinterpret_cast<SbkBaseWrapper*>(self)); SbkBaseWrapper_clearReferences(reinterpret_cast<SbkBaseWrapper*>(self)); - Py_TYPE(reinterpret_cast<SbkBaseWrapper*>(self))->tp_free(self); + + Py_TYPE(self)->tp_free(self); } LIBSHIBOKEN_API PyAPI_FUNC(void) SbkBaseWrapper_Dealloc_PrivateDtor(PyObject* self); diff --git a/libshiboken/conversions.h b/libshiboken/conversions.h index 5b559308e..3426f9439 100644 --- a/libshiboken/conversions.h +++ b/libshiboken/conversions.h @@ -160,7 +160,7 @@ struct ConverterBase<T*> : ConverterBase<T> return 0; SbkBaseWrapperType* shiboType = reinterpret_cast<SbkBaseWrapperType*>(pyobj->ob_type); if (shiboType->mi_specialcast) - return (T*) shiboType->mi_specialcast(pyobj, reinterpret_cast<SbkBaseWrapperType*>(SbkType<T>())); + return (T*) shiboType->mi_specialcast(SbkBaseWrapper_cptr(pyobj), reinterpret_cast<SbkBaseWrapperType*>(SbkType<T>())); return (T*) SbkBaseWrapper_cptr(pyobj); } }; |