From dabcbb8815ed6b0b9a042c7b07d7c0f9298fceaf Mon Sep 17 00:00:00 2001 From: Sasawat Prankprakma Date: Mon, 22 Apr 2024 14:12:16 +0900 Subject: [PATCH] Refactor ARRAYASLIST to support both float and int types --- .../_GeneralModuleFiles/swig_conly_data.i | 189 ++++-------------- .../msgAutoSource/msgInterfacePy.i.in | 2 +- 2 files changed, 36 insertions(+), 155 deletions(-) diff --git a/src/architecture/_GeneralModuleFiles/swig_conly_data.i b/src/architecture/_GeneralModuleFiles/swig_conly_data.i index 52f90596b2..290040346f 100644 --- a/src/architecture/_GeneralModuleFiles/swig_conly_data.i +++ b/src/architecture/_GeneralModuleFiles/swig_conly_data.i @@ -20,69 +20,8 @@ %module swig_conly_data %include "stdint.i" -%define ARRAYASLIST(type) -%typemap(in) type [ANY] (type temp[$1_dim0]) { - int i; - void *blankPtr = 0 ; - int resOut = 0 ; - if (!PySequence_Check($input)) { - PyErr_SetString(PyExc_ValueError,"Expected a sequence"); - return NULL; - } - if (PySequence_Length($input) > $1_dim0) { - printf("Value: %d\n", $1_dim0); - PyErr_SetString(PyExc_ValueError,"Size mismatch. Expected $1_dim0 elements"); - return NULL; - } - memset(temp, 0x0, $1_dim0*sizeof(type)); - for (i = 0; i < PySequence_Length($input); i++) { - PyObject *o = PySequence_GetItem($input,i); - if (PyNumber_Check(o)) { - temp[i] = (type)PyFloat_AsDouble(o); - } else { - resOut = SWIG_ConvertPtr(o, &blankPtr,$1_descriptor, 0 | 0 ); - if (!SWIG_IsOK(resOut)) { - SWIG_exception_fail(SWIG_ArgError(resOut), "Could not convert that type into a pointer for some reason. This is an ugly SWIG failure. Good luck.\n"); - return NULL; - } - memcpy(&(temp[i]), blankPtr, sizeof(type)); - } - } - $1 = temp; -} -%typemap(memberin) type [ANY] { - int i; - for (i = 0; i < $1_dim0; i++) { - memcpy(&($1[i]), &($input[i]), sizeof(type)); - } -} - -%typemap(out) type [ANY] { - int i; - $result = PyList_New(0); - PyObject *locOutObj = 0; - for (i = 0; i < $1_dim0; i++) { - locOutObj = SWIG_NewPointerObj(SWIG_as_voidptr(&($1[i])), $1_descriptor, 0 | 0 ); - - if(PyNumber_Check(locOutObj)){ - PyObject *outObject = PyFloat_FromDouble((double) $1[i]); - PyList_Append($result,outObject); - Py_DECREF(outObject); - Py_DECREF(locOutObj); - } - else - { - PyList_SetItem($result, i, locOutObj); - } - } -} - -%enddef -ARRAYASLIST(double) -ARRAYASLIST(float) - -%define ARRAYINTASLIST(type, fromfunc, asfunc) +%define ARRAYASLIST(type, fromfunc, asfunc) %typemap(in) type [ANY] (type temp[$1_dim0]) { int i; void *blankPtr = 0 ; @@ -141,84 +80,24 @@ ARRAYASLIST(float) } %enddef -ARRAYINTASLIST(int, PyLong_FromLong, PyLong_AsLong) -ARRAYINTASLIST(unsigned int, PyLong_FromUnsignedLong, PyLong_AsUnsignedLong) -ARRAYINTASLIST(int32_t, PyLong_FromLong, PyLong_AsLong) -ARRAYINTASLIST(uint32_t, PyLong_FromUnsignedLong, PyLong_AsUnsignedLong) -ARRAYINTASLIST(long long, PyLong_FromLongLong, PyLong_AsLongLong) -ARRAYINTASLIST(unsigned long long, PyLong_FromUnsignedLongLong, PyLong_AsUnsignedLongLong) -ARRAYINTASLIST(int64_t, PyLong_FromLongLong, PyLong_AsLongLong) -ARRAYINTASLIST(uint64_t, PyLong_FromUnsignedLongLong, PyLong_AsUnsignedLongLong) -ARRAYINTASLIST(short, PyLong_FromLong, PyLong_AsLong) -ARRAYINTASLIST(unsigned short, PyLong_FromUnsignedLong, PyLong_AsUnsignedLong) -ARRAYINTASLIST(int16_t, PyLong_FromLong, PyLong_AsLong) -ARRAYINTASLIST(uint16_t, PyLong_FromUnsignedLong, PyLong_AsUnsignedLong) -ARRAYINTASLIST(size_t, PyLong_FromSize_t, PyLong_AsSize_t) -ARRAYINTASLIST(ssize_t, PyLong_FromSsize_t, PyLong_AsSsize_t) - -%define ARRAY2ASLIST(type) - -%typemap(in) type [ANY][ANY] (type temp[$1_dim0][$1_dim1]) { - Py_ssize_t i; - if(!PySequence_Check($input)) { - PyErr_SetString(PyExc_ValueError,"Expected a list of lists! Does not appear to be that."); - return NULL; - } - Py_ssize_t rowLength = 0; - for(i=0; i