summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHugo Parente Lima <hugo.pl@gmail.com>2010-12-03 16:53:26 -0200
committerHugo Parente Lima <hugo.pl@gmail.com>2010-12-03 16:53:26 -0200
commit07f1fe9dc97e05247043b3e579d94d323a2a48dc (patch)
tree40023477986a1ef6ea874464aa6ccc3b7139e38d
parenta5146ad246af19703671451451564e613e509463 (diff)
downloadshiboken-07f1fe9dc97e05247043b3e579d94d323a2a48dc.tar.gz
shiboken-07f1fe9dc97e05247043b3e579d94d323a2a48dc.tar.xz
shiboken-07f1fe9dc97e05247043b3e579d94d323a2a48dc.zip
Add subtype init hook.
The hook is invoked every time the user creates a sub-type inherited from a Shiboken based type.
-rw-r--r--libshiboken/basewrapper.cpp22
-rw-r--r--libshiboken/basewrapper.h14
-rw-r--r--libshiboken/basewrapper_p.h1
3 files changed, 34 insertions, 3 deletions
diff --git a/libshiboken/basewrapper.cpp b/libshiboken/basewrapper.cpp
index 09c47bda..b3fc3801 100644
--- a/libshiboken/basewrapper.cpp
+++ b/libshiboken/basewrapper.cpp
@@ -238,6 +238,13 @@ PyObject* SbkObjectTypeTpNew(PyTypeObject* metatype, PyObject* args, PyObject* k
d->d_func = 0;
d->is_user_type = 1;
newType->d = d;
+
+ std::list<SbkObjectType*>::const_iterator it = bases.begin();
+ for (; it != bases.end(); ++it) {
+ if ((*it)->d->subtype_init)
+ (*it)->d->subtype_init(newType, args, kwds);
+ }
+
return reinterpret_cast<PyObject*>(newType);
}
@@ -548,9 +555,20 @@ void initPrivateData(SbkObjectType* self)
memset(self->d, 0, sizeof(SbkObjectTypePrivate));
}
-void* getTypeUserData(SbkObjectType* type)
+void setSubTypeInitHook(SbkObjectType* self, SubTypeInitHook func)
+{
+ self->d->subtype_init = func;
+}
+
+void* getTypeUserData(SbkObjectType* self)
+{
+ return self->d->user_data;
+}
+
+void setTypeUserData(SbkObjectType* self, void* userData, DeleteUserDataFunc d_func)
{
- return type->d->user_data;
+ self->d->user_data = userData;
+ self->d->d_func = d_func;
}
} // namespace ObjectType
diff --git a/libshiboken/basewrapper.h b/libshiboken/basewrapper.h
index 0f836079..46bc641d 100644
--- a/libshiboken/basewrapper.h
+++ b/libshiboken/basewrapper.h
@@ -72,6 +72,8 @@ typedef void (*DeleteUserDataFunc)(void*);
typedef void (*ObjectDestructor)(void*);
+typedef void (*SubTypeInitHook)(SbkObjectType*, PyObject*, PyObject*);
+
extern LIBSHIBOKEN_API PyTypeObject SbkObjectType_Type;
extern LIBSHIBOKEN_API SbkObjectType SbkObject_Type;
@@ -150,9 +152,19 @@ LIBSHIBOKEN_API void setDestructorFunction(SbkObjectType* self, ObjectDes
LIBSHIBOKEN_API void initPrivateData(SbkObjectType* self);
/**
+ * 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.
+ * The hook gets 3 params, they are: The new type being created, args and kwds. The last two are the very
+ * same got from tp_new.
+ */
+LIBSHIBOKEN_API void setSubTypeInitHook(SbkObjectType* self, SubTypeInitHook func);
+
+/**
* Get the user data previously set by Shiboken::Object::setTypeUserData
*/
-LIBSHIBOKEN_API void* getTypeUserData(SbkObjectType* type);
+LIBSHIBOKEN_API void* getTypeUserData(SbkObjectType* self);
+LIBSHIBOKEN_API void setTypeUserData(SbkObjectType* self, void* userData, DeleteUserDataFunc d_func);
}
namespace Object {
diff --git a/libshiboken/basewrapper_p.h b/libshiboken/basewrapper_p.h
index 05208848..f4ab6e65 100644
--- a/libshiboken/basewrapper_p.h
+++ b/libshiboken/basewrapper_p.h
@@ -104,6 +104,7 @@ struct SbkObjectTypePrivate
/// Type user data
void *user_data;
DeleteUserDataFunc d_func;
+ void (*subtype_init)(SbkObjectType*, PyObject*, PyObject*);
};