From 2420b80c9eccd12dd005f13204183d9ce45f31bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Mon, 18 Nov 2019 20:27:46 +0100 Subject: [PATCH] plugins: python renderer: Add GdsPoint object type definition to gds_render module. This is still playing around as I don't really know what I'm doing at the moment. --- .../python-renderer/gds-render-module.h | 5 ++ .../python-renderer/src/gds-render-module.c | 70 +++++++++++++++++-- 2 files changed, 69 insertions(+), 6 deletions(-) diff --git a/plugins/python-renderer/include/python-renderer/gds-render-module.h b/plugins/python-renderer/include/python-renderer/gds-render-module.h index b602b40..d88f9ed 100644 --- a/plugins/python-renderer/include/python-renderer/gds-render-module.h +++ b/plugins/python-renderer/include/python-renderer/gds-render-module.h @@ -33,6 +33,11 @@ #include +/** + * @brief The gds_render python module name as string + */ +#define GDS_RENDER_MOD_NAME "gds_render" + /** * @brief Set up the gds_render python module * @return New module reference diff --git a/plugins/python-renderer/src/gds-render-module.c b/plugins/python-renderer/src/gds-render-module.c index e1e17cc..07b995b 100644 --- a/plugins/python-renderer/src/gds-render-module.c +++ b/plugins/python-renderer/src/gds-render-module.c @@ -24,6 +24,22 @@ */ #include +#include + +typedef struct { + PyObject_HEAD + /* TODO: Fill in type specific fields */ +} GdsPointObject; + +static PyTypeObject GdsPointType = { + PyVarObject_HEAD_INIT(NULL, 0) + .tp_name = GDS_RENDER_MOD_NAME ".GdsPoint", + .tp_doc = "2D Gds Database Point", + .tp_basicsize = sizeof(GdsPointObject), + .tp_itemsize = 0, + .tp_flags = Py_TPFLAGS_DEFAULT, + .tp_new = PyType_GenericNew, +}; static PyObject *test(PyObject *self, PyObject *args) { @@ -32,7 +48,7 @@ static PyObject *test(PyObject *self, PyObject *args) if(!PyArg_ParseTuple(args, "l:get_number", &i)) return NULL; - i *= 2; + i *= i; return PyLong_FromLong(i); } @@ -42,15 +58,57 @@ static PyMethodDef gds_render_methods[] = { {NULL, NULL, 0, NULL} }; -static PyModuleDef gds_render_module = { - PyModuleDef_HEAD_INIT, "gds_render", NULL, -1, gds_render_methods, +static PyModuleDef gds_render_module_def = { + PyModuleDef_HEAD_INIT, + .m_name = GDS_RENDER_MOD_NAME, + .m_doc = "GDS Render Base App Module", + .m_size = -1, + .m_methods = gds_render_methods, NULL, NULL, NULL, NULL }; +static int gds_render_module_check_types_ready() +{ + if (PyType_Ready(&GdsPointType) < 0) + return -1; + + return 0; +} + +static int gds_render_module_add_types(PyObject *module) +{ + int err; + + Py_INCREF(&GdsPointType); + err = PyModule_AddObject(module, "GdsPoint", (PyObject *)&GdsPointType); + if (err < 0) { + goto decref_gds_point; + } + + return 0; + +decref_gds_point: + Py_DECREF(&GdsPointType); + return -1; +} + PyObject *init_gds_render_module(void) { - PyObject *this_module; + int err; + PyObject *gds_render_module; - this_module = PyModule_Create(&gds_render_module); - return this_module; + if (gds_render_module_check_types_ready()) + return NULL; + + gds_render_module = PyModule_Create(&gds_render_module_def); + + err = gds_render_module_add_types(gds_render_module); + if (err) { + Py_DECREF(gds_render_module); + gds_render_module = NULL; + goto return_module; + } + +return_module: + return gds_render_module; }