Creates QFlags types at runtime.

This commit is contained in:
Hugo Parente Lima 2011-10-14 14:00:18 -03:00
commit b50186e9f3
2 changed files with 42 additions and 2 deletions

View file

@ -21,8 +21,20 @@
*/ */
#include "pysideqflags.h" #include "pysideqflags.h"
#include <sbkenum.h>
extern "C" { extern "C" {
struct SbkConverter;
/**
* Type of all QFlags
*/
struct PySideQFlagsType
{
PyHeapTypeObject super;
SbkConverter* converter;
};
#define PYSIDE_QFLAGS(X) reinterpret_cast<PySideQFlagsObject*>(X) #define PYSIDE_QFLAGS(X) reinterpret_cast<PySideQFlagsObject*>(X)
PyObject* PySideQFlagsNew(PyTypeObject *type, PyObject *args, PyObject *kwds) PyObject* PySideQFlagsNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
@ -89,6 +101,24 @@ namespace PySide
{ {
namespace QFlags namespace QFlags
{ {
PyTypeObject* create(const char* name, PyNumberMethods* numberMethods)
{
PyTypeObject* type = reinterpret_cast<PyTypeObject*>(new PySideQFlagsType);
::memset(type, 0, sizeof(PySideQFlagsType));
Py_TYPE(type) = &PyType_Type;
type->tp_basicsize = sizeof(PySideQFlagsObject);
type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES;
type->tp_name = name;
type->tp_new = &PySideQFlagsNew;
type->tp_as_number = numberMethods;
type->tp_richcompare = &PySideQFlagsRichCompare;
if (PyType_Ready(type) < 0)
return 0;
return type;
}
PySideQFlagsObject* newObject(long value, PyTypeObject* type) PySideQFlagsObject* newObject(long value, PyTypeObject* type)
{ {
PySideQFlagsObject* qflags = PyObject_New(PySideQFlagsObject, type); PySideQFlagsObject* qflags = PyObject_New(PySideQFlagsObject, type);

View file

@ -24,7 +24,7 @@
#define PYSIDE_QFLAGS_H #define PYSIDE_QFLAGS_H
#include <sbkpython.h> #include <sbkpython.h>
#include <pysidemacros.h> #include "pysidemacros.h"
extern "C" extern "C"
@ -43,7 +43,17 @@ namespace PySide
{ {
namespace QFlags namespace QFlags
{ {
/**
* Creates a new QFlags type.
*/
PYSIDE_API PyTypeObject* create(const char* name, PyNumberMethods* numberMethods);
/**
* Creates a new QFlags instance of type \p type and value \p value.
*/
PYSIDE_API PySideQFlagsObject* newObject(long value, PyTypeObject* type); PYSIDE_API PySideQFlagsObject* newObject(long value, PyTypeObject* type);
/**
* Returns the value held by a QFlag.
*/
PYSIDE_API long getValue(PySideQFlagsObject* self); PYSIDE_API long getValue(PySideQFlagsObject* self);
} }
} }