summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHugo Lima <hugo.lima@openbossa.org>2010-04-01 18:10:37 -0300
committerHugo Parente Lima <hugo.pl@gmail.com>2010-04-05 18:39:52 -0300
commit319e5f2467e8682865f1206ae4ce4b3e2bdff147 (patch)
tree32ec9d4d6ba9c0b4e509db1e24043386cdc496fe
parent9a5e72d08f908183d7bf8abcb3546cf509f9362d (diff)
downloadshiboken-319e5f2467e8682865f1206ae4ce4b3e2bdff147.tar.gz
shiboken-319e5f2467e8682865f1206ae4ce4b3e2bdff147.tar.xz
shiboken-319e5f2467e8682865f1206ae4ce4b3e2bdff147.zip
Added enum meta type, this will ease you to known if a PyObject is a Shiboken enum.
-rw-r--r--cppgenerator.cpp2
-rw-r--r--libshiboken/basewrapper.cpp3
-rw-r--r--libshiboken/pyenum.cpp49
-rw-r--r--libshiboken/pyenum.h7
4 files changed, 60 insertions, 1 deletions
diff --git a/cppgenerator.cpp b/cppgenerator.cpp
index 388d45e9..c1881a44 100644
--- a/cppgenerator.cpp
+++ b/cppgenerator.cpp
@@ -2441,7 +2441,7 @@ void CppGenerator::writeEnumDefinition(QTextStream& s, const AbstractMetaEnum* c
s << "static PyObject* " << newFunc << "(PyTypeObject*, PyObject*, PyObject*);" << endl << endl;
s << "static PyTypeObject " << cpythonName << "_Type = {" << endl;
- s << INDENT << "PyObject_HEAD_INIT(&PyType_Type)" << endl;
+ s << INDENT << "PyObject_HEAD_INIT(&Shiboken::SbkEnumType_Type)" << endl;
s << INDENT << "/*ob_size*/ 0," << endl;
s << INDENT << "/*tp_name*/ \"" << cppEnum->name() << "\"," << endl;
s << INDENT << "/*tp_basicsize*/ sizeof(Shiboken::SbkEnumObject)," << endl;
diff --git a/libshiboken/basewrapper.cpp b/libshiboken/basewrapper.cpp
index 36e14b10..92b6b1d3 100644
--- a/libshiboken/basewrapper.cpp
+++ b/libshiboken/basewrapper.cpp
@@ -496,6 +496,9 @@ void initShiboken()
PyEval_InitThreads();
#endif
+ if (PyType_Ready(&SbkEnumType_Type) < 0)
+ Py_FatalError("[libshiboken] Failed to initialise Shiboken.SbkEnumType metatype.");
+
if (PyType_Ready(&SbkBaseWrapperType_Type) < 0)
Py_FatalError("[libshiboken] Failed to initialise Shiboken.BaseWrapperType metatype.");
diff --git a/libshiboken/pyenum.cpp b/libshiboken/pyenum.cpp
index b7bacc9e..7b9d05d0 100644
--- a/libshiboken/pyenum.cpp
+++ b/libshiboken/pyenum.cpp
@@ -37,6 +37,55 @@
namespace Shiboken
{
+PyTypeObject SbkEnumType_Type = {
+ PyObject_HEAD_INIT(0)
+ /*ob_size*/ 0,
+ /*tp_name*/ "Shiboken.EnumType",
+ /*tp_basicsize*/ sizeof(PyTypeObject),
+ /*tp_itemsize*/ 0,
+ /*tp_dealloc*/ 0,
+ /*tp_print*/ 0,
+ /*tp_getattr*/ 0,
+ /*tp_setattr*/ 0,
+ /*tp_compare*/ 0,
+ /*tp_repr*/ 0,
+ /*tp_as_number*/ 0,
+ /*tp_as_sequence*/ 0,
+ /*tp_as_mapping*/ 0,
+ /*tp_hash*/ 0,
+ /*tp_call*/ 0,
+ /*tp_str*/ 0,
+ /*tp_getattro*/ 0,
+ /*tp_setattro*/ 0,
+ /*tp_as_buffer*/ 0,
+ /*tp_flags*/ Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
+ /*tp_doc*/ 0,
+ /*tp_traverse*/ 0,
+ /*tp_clear*/ 0,
+ /*tp_richcompare*/ 0,
+ /*tp_weaklistoffset*/ 0,
+ /*tp_iter*/ 0,
+ /*tp_iternext*/ 0,
+ /*tp_methods*/ 0,
+ /*tp_members*/ 0,
+ /*tp_getset*/ 0,
+ /*tp_base*/ &PyType_Type,
+ /*tp_dict*/ 0,
+ /*tp_descr_get*/ 0,
+ /*tp_descr_set*/ 0,
+ /*tp_dictoffset*/ 0,
+ /*tp_init*/ 0,
+ /*tp_alloc*/ 0,
+ /*tp_new*/ PyType_GenericNew,
+ /*tp_free*/ 0,
+ /*tp_is_gc*/ 0,
+ /*tp_bases*/ 0,
+ /*tp_mro*/ 0,
+ /*tp_cache*/ 0,
+ /*tp_subclasses*/ 0,
+ /*tp_weaklist*/ 0
+};
+
PyObject*
SbkEnumObject_New(PyTypeObject *type, long item_value, PyObject* item_name)
{
diff --git a/libshiboken/pyenum.h b/libshiboken/pyenum.h
index cb8bb48f..a29a7f1c 100644
--- a/libshiboken/pyenum.h
+++ b/libshiboken/pyenum.h
@@ -50,11 +50,18 @@ typedef struct {
PyObject* ob_name;
} SbkEnumObject;
+LIBSHIBOKEN_API PyAPI_DATA(PyTypeObject) SbkEnumType_Type;
+
LIBSHIBOKEN_API PyAPI_FUNC(PyObject*) SbkEnumObject_repr(PyObject* self);
LIBSHIBOKEN_API PyAPI_FUNC(PyObject*) SbkEnumObject_name(PyObject* self);
} // extern "C"
+inline bool isShibokenEnum(PyObject* pyObj)
+{
+ return pyObj->ob_type->ob_type == &SbkEnumType_Type;
+}
+
LIBSHIBOKEN_API PyObject* SbkEnumObject_New(PyTypeObject *instanceType,
long item_value,
const char* item_name);