From c74c3d36bc74a18ad8bf7f33d90a7db8d095b11b Mon Sep 17 00:00:00 2001 From: baldurk Date: Thu, 3 Aug 2017 16:50:10 +0100 Subject: [PATCH] Improve ResourceId repr/str handling, and add an int() handling --- qrenderdoc/Code/pyrenderdoc/renderdoc.i | 35 ++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/qrenderdoc/Code/pyrenderdoc/renderdoc.i b/qrenderdoc/Code/pyrenderdoc/renderdoc.i index ea82a33c9..6ffbfafeb 100644 --- a/qrenderdoc/Code/pyrenderdoc/renderdoc.i +++ b/qrenderdoc/Code/pyrenderdoc/renderdoc.i @@ -71,11 +71,44 @@ CONTAINER_TYPEMAPS(rdctype::array) // add __str__ functions %feature("python:tp_str") ResourceId "resid_str"; +%feature("python:tp_repr") ResourceId "resid_str"; +%feature("python:nb_int") ResourceId "resid_int"; %wrapper %{ static PyObject *resid_str(PyObject *resid) { - return PyUnicode_FromFormat("", PyObject_GetAttrString(resid, "id")); + void *resptr = NULL; + unsigned long long *id = NULL; + int res = SWIG_ConvertPtr(resid, &resptr, SWIGTYPE_p_ResourceId, 0); + if (!SWIG_IsOK(res)) { + SWIG_exception_fail(SWIG_ArgError(res), "in method 'ResourceId.str', ResourceId is not correct type"); + } + + // cast as unsigned long long + id = (unsigned long long *)resptr; + static_assert(sizeof(unsigned long long) == sizeof(ResourceId), "Wrong size"); + + return PyUnicode_FromFormat("", *id); +fail: + return NULL; +} + +static PyObject *resid_int(PyObject *resid) +{ + void *resptr = NULL; + unsigned long long *id = NULL; + int res = SWIG_ConvertPtr(resid, &resptr, SWIGTYPE_p_ResourceId, 0); + if (!SWIG_IsOK(res)) { + SWIG_exception_fail(SWIG_ArgError(res), "in method 'ResourceId.str', ResourceId is not correct type"); + } + + // cast as unsigned long long + id = (unsigned long long *)resptr; + static_assert(sizeof(unsigned long long) == sizeof(ResourceId), "Wrong size"); + + return PyLong_FromUnsignedLongLong(*id); +fail: + return NULL; } %}