GPU Python: Use 'PyC_ParseStringEnum' to parse items
Currently the GPU module for python has different ways to handle enums. - Organizing items in `PyC_StringEnumItems` arrays and parsing them with `PyC_ParseStringEnum`. - Using dedicated functions for each type of enum (`bpygpu_ParsePrimType`, `pygpu_ParseVertCompType` and `pygpu_ParseVertFetchMode`). Although apparently more efficient (especially `pygpu_ParseVertCompType` which transforms strings into integers for simple comparison), these dedicated functions duplicate functionality, increase the complexity of the code and consequently make it less readable. Reviewed By: campbellbarton Differential Revision: https://developer.blender.org/D10456
This commit is contained in:
@@ -31,7 +31,6 @@
|
||||
#include "../generic/python_utildefines.h"
|
||||
|
||||
#include "GPU_init_exit.h"
|
||||
#include "GPU_primitive.h"
|
||||
|
||||
#include "gpu_py_matrix.h"
|
||||
#include "gpu_py_select.h"
|
||||
@@ -58,50 +57,6 @@ bool bpygpu_is_init_or_error(void)
|
||||
|
||||
/** \} */
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
/** \name Primitive Type Utils
|
||||
* \{ */
|
||||
|
||||
int bpygpu_ParsePrimType(PyObject *o, void *p)
|
||||
{
|
||||
Py_ssize_t mode_id_len;
|
||||
const char *mode_id = PyUnicode_AsUTF8AndSize(o, &mode_id_len);
|
||||
if (mode_id == NULL) {
|
||||
PyErr_Format(PyExc_ValueError, "expected a string, got %s", Py_TYPE(o)->tp_name);
|
||||
return 0;
|
||||
}
|
||||
#define MATCH_ID(id) \
|
||||
if (mode_id_len == strlen(STRINGIFY(id))) { \
|
||||
if (STREQ(mode_id, STRINGIFY(id))) { \
|
||||
mode = GPU_PRIM_##id; \
|
||||
goto success; \
|
||||
} \
|
||||
} \
|
||||
((void)0)
|
||||
|
||||
GPUPrimType mode;
|
||||
MATCH_ID(POINTS);
|
||||
MATCH_ID(LINES);
|
||||
MATCH_ID(TRIS);
|
||||
MATCH_ID(LINE_STRIP);
|
||||
MATCH_ID(LINE_LOOP);
|
||||
MATCH_ID(TRI_STRIP);
|
||||
MATCH_ID(TRI_FAN);
|
||||
MATCH_ID(LINES_ADJ);
|
||||
MATCH_ID(TRIS_ADJ);
|
||||
MATCH_ID(LINE_STRIP_ADJ);
|
||||
|
||||
#undef MATCH_ID
|
||||
PyErr_Format(PyExc_ValueError, "unknown type literal: '%s'", mode_id);
|
||||
return 0;
|
||||
|
||||
success:
|
||||
(*(GPUPrimType *)p) = mode;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/** \} */
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
/** \name GPU Module
|
||||
* \{ */
|
||||
|
||||
Reference in New Issue
Block a user