248 lines
6.7 KiB
C
248 lines
6.7 KiB
C
#include "channel.h"
|
|
#include "Python.h"
|
|
#include "structmember.h"
|
|
|
|
// class ChannelMask
|
|
|
|
typedef struct {
|
|
PyObject_HEAD;
|
|
|
|
IntSet mask;
|
|
} ChannelMask;
|
|
|
|
// ChannelMask __init__
|
|
static PyObject *ChannelMask_new(PyTypeObject *type, PyObject *args, PyObject *keywords);
|
|
static int ChannelMask_init(ChannelMask *self, PyObject *args, PyObject *kwargs);
|
|
static void ChannelMask_dealloc(ChannelMask *self);
|
|
|
|
// ChannelMask property
|
|
static PyObject *ChannelMask_len(ChannelMask *self, void *ignore);
|
|
|
|
static PyGetSetDef ChannelMaskGetSet[] = { //
|
|
// size: int [G]
|
|
{"size", (getter)ChannelMask_len, NULL, "", NULL},
|
|
|
|
{NULL}};
|
|
|
|
// ChannelMask methods
|
|
static PyObject *ChannelMask_clear(ChannelMask *self);
|
|
static PyObject *ChannelMask_contain_channel(ChannelMask *self, PyObject *args, PyObject *kwargs);
|
|
static PyObject *ChannelMask_add_channel(ChannelMask *self, PyObject *args, PyObject *kwargs);
|
|
static PyObject *ChannelMask_set_channel(ChannelMask *self, PyObject *args, PyObject *kwargs);
|
|
static PyObject *ChannelMask_del_channel(ChannelMask *self, PyObject *args, PyObject *kwargs);
|
|
static PyObject *ChannelMask_channels(ChannelMask *self);
|
|
static PyObject* ChannelMask_iter(ChannelMask *self);
|
|
|
|
static PyMethodDef ChannelMaskMethods[] = { //
|
|
{"clear", (PyCFunction)ChannelMask_clear, METH_NOARGS, "clear"},
|
|
|
|
{"contain_channel", (PyCFunction)ChannelMask_contain_channel, METH_VARARGS | METH_KEYWORDS, "contain"},
|
|
|
|
{"add_channel", (PyCFunction)ChannelMask_add_channel, METH_VARARGS | METH_KEYWORDS, "add"},
|
|
|
|
{"set_channel", (PyCFunction)ChannelMask_set_channel, METH_VARARGS | METH_KEYWORDS, "set"},
|
|
|
|
{"del_channel", (PyCFunction)ChannelMask_del_channel, METH_VARARGS | METH_KEYWORDS, "del"},
|
|
|
|
{"channels", (PyCFunction)ChannelMask_channels, METH_NOARGS, "channels"},
|
|
{NULL}};
|
|
|
|
static PyTypeObject ChannelMaskType = {
|
|
PyVarObject_HEAD_INIT(NULL, 0) //,
|
|
.tp_name = "ChannelMask",
|
|
.tp_doc = "Channel Mask",
|
|
.tp_basicsize = sizeof(ChannelMask),
|
|
.tp_itemsize = 0,
|
|
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
|
.tp_new = ChannelMask_new,
|
|
.tp_init = (initproc)ChannelMask_init,
|
|
.tp_dealloc = (destructor)ChannelMask_dealloc,
|
|
.tp_getset = ChannelMaskGetSet,
|
|
.tp_methods = ChannelMaskMethods,
|
|
.tp_iter = ChannelMask_iter,
|
|
};
|
|
|
|
// function implement
|
|
|
|
static PyObject *ChannelMask_new(PyTypeObject *type, PyObject *args, PyObject *keywords) {
|
|
ChannelMask *self = (ChannelMask *)type->tp_alloc(type, 0);
|
|
|
|
if (self != NULL) {
|
|
int_set_init(&(self->mask));
|
|
}
|
|
|
|
return (PyObject *)self;
|
|
}
|
|
|
|
static int ChannelMask_init(ChannelMask *self, PyObject *args, PyObject *kwargs) {
|
|
static char *keywords[] = {"channel", NULL};
|
|
|
|
PyObject *channel = NULL;
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", keywords, &channel)) return -1;
|
|
|
|
if (channel != NULL) {
|
|
int_set_add_list(&(self->mask), channel);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void ChannelMask_dealloc(ChannelMask *self) {
|
|
int_set_clear(&(self->mask));
|
|
Py_TYPE(self)->tp_free((PyObject *)self);
|
|
}
|
|
|
|
static PyObject *ChannelMask_len(ChannelMask *self, void *ignore) {
|
|
return PyLong_FromLong(self->mask.size);
|
|
}
|
|
|
|
static PyObject *ChannelMask_clear(ChannelMask *self) {
|
|
int_set_clear(&(self->mask));
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
static PyObject *ChannelMask_contain_channel(ChannelMask *self, PyObject *args, PyObject *kwargs) {
|
|
static char *keywords[] = {"channel", NULL};
|
|
|
|
unsigned int channel;
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "I", keywords, &channel)) return NULL;
|
|
|
|
int result = int_set_contain(&(self->mask), channel);
|
|
|
|
if (PyErr_Occurred()) return NULL;
|
|
|
|
if (result) {
|
|
Py_RETURN_TRUE;
|
|
} else {
|
|
Py_RETURN_FALSE;
|
|
}
|
|
}
|
|
|
|
static PyObject *ChannelMask_add_channel(ChannelMask *self, PyObject *args, PyObject *kwargs) {
|
|
static char *keywords[] = {"channel", NULL};
|
|
|
|
PyObject *channel;
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", keywords, &channel)) return NULL;
|
|
|
|
int result;
|
|
|
|
if (PyLong_CheckExact(channel)) {
|
|
long value = PyLong_AsLong(channel);
|
|
|
|
if (value == -1 && PyErr_Occurred()) {
|
|
return NULL;
|
|
} else if (value < 0) {
|
|
PyErr_SetString(PyExc_ValueError, "negative channel number");
|
|
return NULL;
|
|
}
|
|
|
|
result = int_set_add(&(self->mask), value);
|
|
} else {
|
|
// iterable
|
|
result = int_set_add_list(&(self->mask), channel);
|
|
}
|
|
|
|
if (PyErr_Occurred()) return NULL;
|
|
|
|
if (result) {
|
|
Py_RETURN_TRUE;
|
|
} else {
|
|
Py_RETURN_FALSE;
|
|
}
|
|
}
|
|
|
|
static PyObject *ChannelMask_set_channel(ChannelMask *self, PyObject *args, PyObject *kwargs) {
|
|
static char *keywords[] = {"channel", NULL};
|
|
|
|
PyObject *channel;
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", keywords, &channel)) return NULL;
|
|
|
|
int_set_clear(&(self->mask));
|
|
|
|
int result;
|
|
|
|
if (PyLong_CheckExact(channel)) {
|
|
long value = PyLong_AsLong(channel);
|
|
|
|
if (value == -1 && PyErr_Occurred()) {
|
|
return NULL;
|
|
} else if (value < 0) {
|
|
PyErr_SetString(PyExc_ValueError, "negative channel number");
|
|
return NULL;
|
|
}
|
|
|
|
result = int_set_add(&(self->mask), value);
|
|
} else {
|
|
// iterable
|
|
result = int_set_add_list(&(self->mask), channel);
|
|
}
|
|
|
|
if (PyErr_Occurred()) return NULL;
|
|
|
|
if (result) {
|
|
Py_RETURN_TRUE;
|
|
} else {
|
|
Py_RETURN_FALSE;
|
|
}
|
|
}
|
|
|
|
static PyObject *ChannelMask_del_channel(ChannelMask *self, PyObject *args, PyObject *kwargs) {
|
|
static char *keywords[] = {"channel", NULL};
|
|
|
|
unsigned int channel;
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "I", keywords, &channel)) return NULL;
|
|
|
|
int result = int_set_del(&(self->mask), channel);
|
|
|
|
if (PyErr_Occurred()) return NULL;
|
|
|
|
if (result) {
|
|
Py_RETURN_TRUE;
|
|
} else {
|
|
Py_RETURN_FALSE;
|
|
}
|
|
}
|
|
|
|
static PyObject *ChannelMask_channels(ChannelMask *self) {
|
|
return int_set_list(&(self->mask));
|
|
}
|
|
|
|
static PyObject* ChannelMask_iter(ChannelMask *self) {
|
|
return PyObject_GetIter(int_set_list(&(self->mask)));
|
|
}
|
|
|
|
// module init
|
|
|
|
static PyMethodDef moduleMethods[] = { //
|
|
{NULL, NULL, 0, NULL}};
|
|
|
|
static struct PyModuleDef ChannelModule = { //
|
|
PyModuleDef_HEAD_INIT,
|
|
// module name
|
|
"channel",
|
|
// module doc
|
|
NULL,
|
|
-1,
|
|
moduleMethods};
|
|
|
|
// module export
|
|
|
|
PyMODINIT_FUNC PyInit_channel(void) {
|
|
PyObject *module;
|
|
|
|
module = PyModule_Create(&ChannelModule);
|
|
if (module == NULL) return NULL;
|
|
|
|
if (PyType_Ready(&ChannelMaskType) < 0) return NULL;
|
|
|
|
Py_INCREF(&ChannelMaskType);
|
|
PyModule_AddObject(module, "ChannelMask", (PyObject *)&ChannelMaskType);
|
|
|
|
return module;
|
|
}
|