diff options
author | mano-wii <germano.costa@ig.com.br> | 2018-10-06 07:15:15 +0300 |
---|---|---|
committer | mano-wii <germano.costa@ig.com.br> | 2018-10-06 07:15:15 +0300 |
commit | 495a7128cb230a26bc72cb735e33be5324bf3baa (patch) | |
tree | 6cee7ac416017617b102c56e8747ef8ee01436e3 | |
parent | 0f5533441390d049d2b85e86abdd57b5a874e530 (diff) |
Cleanup: use the naming convention in py_capi_utils
And use inline functions instead of preprocessor directives.
-rw-r--r-- | source/blender/python/generic/bgl.c | 6 | ||||
-rw-r--r-- | source/blender/python/generic/py_capi_utils.h | 74 | ||||
-rw-r--r-- | source/blender/python/gpu/gpu_py_element.c | 12 |
3 files changed, 75 insertions, 17 deletions
diff --git a/source/blender/python/generic/bgl.c b/source/blender/python/generic/bgl.c index 58bfea5fab8..a89525d942f 100644 --- a/source/blender/python/generic/bgl.c +++ b/source/blender/python/generic/bgl.c @@ -480,14 +480,14 @@ int BGL_typeSize(int type) static int gl_buffer_type_from_py_buffer(Py_buffer *pybuffer) { - const char format = FORMAT_STR_GET(pybuffer->format); + const char format = PyC_Formatstr_get(pybuffer->format); Py_ssize_t itemsize = pybuffer->itemsize; - if (FORMAT_STR_IS_FLOAT(format)) { + if (PyC_Formatstr_is_float(format)) { if (itemsize == 4) return GL_FLOAT; if (itemsize == 8) return GL_DOUBLE; } - if (FORMAT_STR_IS_BYTE(format) || FORMAT_STR_IS_INT(format)) { + if (PyC_Formatstr_is_byte(format) || PyC_Formatstr_is_int(format)) { if (itemsize == 1) return GL_BYTE; if (itemsize == 2) return GL_SHORT; if (itemsize == 4) return GL_INT; diff --git a/source/blender/python/generic/py_capi_utils.h b/source/blender/python/generic/py_capi_utils.h index 8ceb4a76a53..523712d0cb0 100644 --- a/source/blender/python/generic/py_capi_utils.h +++ b/source/blender/python/generic/py_capi_utils.h @@ -132,10 +132,74 @@ Py_LOCAL_INLINE(int64_t) PyC_Long_AsI64(PyObject *value) { return (int64_t)PyLo Py_LOCAL_INLINE(uint64_t) PyC_Long_AsU64(PyObject *value) { return (uint64_t)PyLong_AsUnsignedLongLong(value); } /* utils for format string in `struct` module style syntax */ -#define FORMAT_STR_GET(typestr) ELEM(typestr[0], '!', '<', '=', '>', '@') ? typestr[1] : typestr[0] -#define FORMAT_STR_IS_FLOAT(format) ELEM(format, 'f', 'd', 'e') -#define FORMAT_STR_IS_INT(format) ELEM(format, 'i', 'I', 'l', 'L', 'h', 'H', 'b', 'B', 'q', 'Q', 'n', 'N', 'P') -#define FORMAT_STR_IS_BYTE(format) ELEM(format, 'c', 's', 'p') -#define FORMAT_STR_IS_BOOL(format) ELEM(format, '?') +Py_LOCAL_INLINE(char) PyC_Formatstr_get(char *typestr) +{ + switch (typestr[0]) { + case '!': + case '<': + case '=': + case '>': + case '@': + return typestr[1]; + default: + return typestr[0]; + } +} + +Py_LOCAL_INLINE(bool) PyC_Formatstr_is_float(char format) +{ + switch (format) { + case 'f': + case 'd': + case 'e': + return true; + default: + return false; + } +} + +Py_LOCAL_INLINE(bool) PyC_Formatstr_is_int(char format) +{ + switch (format) { + case 'i': + case 'I': + case 'l': + case 'L': + case 'h': + case 'H': + case 'b': + case 'B': + case 'q': + case 'Q': + case 'n': + case 'N': + case 'P': + return true; + default: + return false; + } +} + +Py_LOCAL_INLINE(bool) PyC_Formatstr_is_byte(char format) +{ + switch (format) { + case 'c': + case 's': + case 'p': + return true; + default: + return false; + } +} + +Py_LOCAL_INLINE(bool) PyC_Formatstr_is_bool(char format) +{ + switch (format) { + case '?': + return true; + default: + return false; + } +} #endif /* __PY_CAPI_UTILS_H__ */ diff --git a/source/blender/python/gpu/gpu_py_element.c b/source/blender/python/gpu/gpu_py_element.c index 0c4cd1d815b..379cd0836ca 100644 --- a/source/blender/python/gpu/gpu_py_element.c +++ b/source/blender/python/gpu/gpu_py_element.c @@ -91,17 +91,11 @@ static PyObject *bpygpu_IndexBuf_new(PyTypeObject *UNUSED(type), PyObject *args, return NULL; } - bool format_error = pybuffer.itemsize != 4; + if (pybuffer.itemsize != 4 || + PyC_Formatstr_is_float(PyC_Formatstr_get(pybuffer.format))) { - char format = FORMAT_STR_GET(pybuffer.format); - if (FORMAT_STR_IS_FLOAT(format)) { - format_error = true; - } - } - - if (format_error) { PyErr_Format(PyExc_ValueError, - "Each index must be an integer value with 4 bytes in size"); + "Each index must be an 4-bytes integer value"); return NULL; } |