Get rid of more boilerplate

This commit is contained in:
Kovid Goyal 2016-11-02 11:47:45 +05:30
parent c0d5719eb4
commit 38e8462277
5 changed files with 27 additions and 18 deletions

View File

@ -134,3 +134,4 @@ copy(Cursor *self, PyObject UNUSED *args) {
return (PyObject*)ans;
}
INIT_TYPE(Cursor)

View File

@ -6,10 +6,9 @@
*/
#include "data-types.h"
extern PyTypeObject LineBuf_Type;
extern PyTypeObject Cursor_Type;
extern PyTypeObject Line_Type;
extern int init_LineBuf(PyObject *);
extern int init_Cursor(PyObject *);
extern int init_Line(PyObject *);
static PyMethodDef module_methods[] = {
{NULL, NULL, 0, NULL} /* Sentinel */
@ -28,19 +27,13 @@ PyInit_fast_data_types(void) {
PyObject *m;
if (PyType_Ready(&LineBuf_Type) < 0) return NULL;
if (PyType_Ready(&Cursor_Type) < 0) return NULL;
if (PyType_Ready(&Line_Type) < 0) return NULL;
m = PyModule_Create(&module);
if (m == NULL) return NULL;
if (m != NULL) {
Py_INCREF(&LineBuf_Type);
PyModule_AddObject(m, "LineBuf", (PyObject *)&LineBuf_Type);
Py_INCREF(&Cursor_Type);
PyModule_AddObject(m, "Cursor", (PyObject *)&Cursor_Type);
Py_INCREF(&Line_Type);
PyModule_AddObject(m, "Line", (PyObject *)&Line_Type);
if (!init_LineBuf(m)) return NULL;
if (!init_Line(m)) return NULL;
if (!init_Cursor(m)) return NULL;
}
return m;

View File

@ -47,6 +47,14 @@ typedef unsigned int index_type;
#define METHOD(name, arg_type) {#name, (PyCFunction)name, arg_type, name##_doc},
#define INIT_TYPE(type) \
int init_##type(PyObject *module) {\
if (PyType_Ready(&type##_Type) < 0) return 0; \
if (PyModule_AddObject(module, #type, (PyObject *)&type##_Type) != 0) return 0; \
Py_INCREF(&type##_Type); \
return 1; \
}
typedef struct {
PyObject_HEAD
@ -83,3 +91,5 @@ typedef struct {
uint32_t fg, bg, decoration_fg;
} Cursor;
Line *alloc_line();

View File

@ -6,7 +6,6 @@
*/
#include "data-types.h"
extern PyTypeObject Line_Type;
static inline void
clear_chars_to_space(LineBuf* linebuf, index_type y) {
@ -39,7 +38,7 @@ new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) {
self->buf = PyMem_Calloc(xnum * ynum, CELL_SIZE);
self->line_map = PyMem_Calloc(ynum, sizeof(index_type));
self->continued_map = PyMem_Calloc(ynum, sizeof(uint8_t));
self->line = (Line*)PyType_GenericAlloc(&Line_Type, 0);
self->line = alloc_line();
if (self->buf == NULL || self->line_map == NULL || self->continued_map == NULL || self->line == NULL) {
PyErr_NoMemory();
PyMem_Free(self->buf); PyMem_Free(self->line_map); PyMem_Free(self->continued_map); Py_XDECREF(self->line);
@ -93,7 +92,7 @@ static PyMethodDef methods[] = {
{NULL} /* Sentinel */
};
PyTypeObject LineBuf_Type = {
static PyTypeObject LineBuf_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "fast_data_types.LineBuf",
.tp_basicsize = sizeof(LineBuf),
@ -103,5 +102,7 @@ PyTypeObject LineBuf_Type = {
.tp_methods = methods,
.tp_new = new
};
INIT_TYPE(LineBuf)
// }}

View File

@ -203,7 +203,7 @@ static PyMethodDef methods[] = {
{NULL} /* Sentinel */
};
PyTypeObject Line_Type = {
static PyTypeObject Line_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "fast_data_types.Line",
.tp_basicsize = sizeof(Line),
@ -215,6 +215,10 @@ PyTypeObject Line_Type = {
.tp_methods = methods,
.tp_new = new
};
Line *alloc_line() {
return (Line*)PyType_GenericAlloc(&Line_Type, 0);
}
// }}
static PyObject*
@ -234,4 +238,4 @@ copy_char(Line* self, PyObject *args) {
Py_RETURN_NONE;
}
INIT_TYPE(Line)