aboutsummaryrefslogtreecommitdiffstats
path: root/libshiboken/sbkenum.cpp
diff options
context:
space:
mode:
authorMarcelo Lira <marcelo.lira@openbossa.org>2011-07-21 21:48:21 -0300
committerHugo Parente Lima <hugo.pl@gmail.com>2012-03-08 16:17:06 -0300
commit5afdf1fae25fdb7f8e090c24d6526dccbba18e21 (patch)
tree072235eddb5dda7ad2242e59eb7e043cfd5a6440 /libshiboken/sbkenum.cpp
parent808c7b33fe6aafc81c58ec4850c0e30dcfbd91e9 (diff)
Added functions to provide a cleaner enum and flags initialization.
The functions are: Shiboken::Enum::createGlobalEnum() Shiboken::Enum::createScopedEnum() Shiboken::Enum::createGlobalEnumItem() Shiboken::Enum::createScopedEnumItem() Also updated the generator to make use of them.
Diffstat (limited to 'libshiboken/sbkenum.cpp')
-rw-r--r--libshiboken/sbkenum.cpp75
1 files changed, 75 insertions, 0 deletions
diff --git a/libshiboken/sbkenum.cpp b/libshiboken/sbkenum.cpp
index e128a059a..e05f5cdad 100644
--- a/libshiboken/sbkenum.cpp
+++ b/libshiboken/sbkenum.cpp
@@ -21,6 +21,7 @@
*/
#include "sbkenum.h"
+#include "basewrapper.h"
#include <string.h>
#include <cstring>
#include <list>
@@ -175,6 +176,80 @@ PyObject* getEnumItemFromValue(PyTypeObject* enumType, long itemValue)
return 0;
}
+static PyTypeObject* createEnum(const char* fullName, const char* cppName, PyTypeObject* flagsType)
+{
+ PyTypeObject* enumType = newTypeWithName(fullName, cppName);
+ if (flagsType) {
+ if (PyType_Ready(flagsType) < 0)
+ return 0;
+ enumType->tp_as_number = flagsType->tp_as_number;
+ }
+ if (PyType_Ready(enumType) < 0)
+ return 0;
+ return enumType;
+}
+
+PyTypeObject* createGlobalEnum(PyObject* module, const char* name, const char* fullName, const char* cppName, PyTypeObject* flagsType)
+{
+ PyTypeObject* enumType = createEnum(fullName, cppName, flagsType);
+ if (enumType) {
+ if (PyModule_AddObject(module, name, (PyObject*)enumType) < 0)
+ return 0;
+ }
+ if (flagsType) {
+ if (PyModule_AddObject(module, flagsType->tp_name, (PyObject*)flagsType) < 0)
+ return 0;
+ }
+ return enumType;
+}
+
+PyTypeObject* createScopedEnum(SbkObjectType* scope, const char* name, const char* fullName, const char* cppName, PyTypeObject* flagsType)
+{
+ PyTypeObject* enumType = createEnum(fullName, cppName, flagsType);
+ if (enumType) {
+ if (PyDict_SetItemString(scope->super.ht_type.tp_dict, name, (PyObject*)enumType) < 0)
+ return 0;
+ }
+ if (flagsType) {
+ if (PyDict_SetItemString(scope->super.ht_type.tp_dict, flagsType->tp_name, (PyObject*)flagsType) < 0)
+ return 0;
+ }
+ return enumType;
+}
+
+static PyObject* createEnumItem(PyTypeObject* enumType, const char* itemName, long itemValue)
+{
+ PyObject* enumItem = newItem(enumType, itemValue, itemName);
+ if (PyDict_SetItemString(enumType->tp_dict, itemName, enumItem) < 0)
+ return 0;
+ Py_DECREF(enumItem);
+ return enumItem;
+}
+
+bool createGlobalEnumItem(PyTypeObject* enumType, PyObject* module, const char* itemName, long itemValue)
+{
+ PyObject* enumItem = createEnumItem(enumType, itemName, itemValue);
+ if (enumItem) {
+ if (PyModule_AddObject(module, itemName, enumItem) < 0)
+ return false;
+ Py_DECREF(enumItem);
+ return true;
+ }
+ return false;
+}
+
+bool createScopedEnumItem(PyTypeObject* enumType, SbkObjectType* scope, const char* itemName, long itemValue)
+{
+ PyObject* enumItem = createEnumItem(enumType, itemName, itemValue);
+ if (enumItem) {
+ if (PyDict_SetItemString(scope->super.ht_type.tp_dict, itemName, enumItem) < 0)
+ return false;
+ Py_DECREF(enumItem);
+ return true;
+ }
+ return false;
+}
+
PyObject* newItem(PyTypeObject* enumType, long itemValue, const char* itemName)
{
bool newValue = true;