From eb17353f3cc83d0bc9c151f918b060eacb0fecac Mon Sep 17 00:00:00 2001 From: doomchild Date: Mon, 5 Apr 2010 16:12:37 -0500 Subject: [PATCH] added getter for memory info --- dfhack/python/DF_API.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/dfhack/python/DF_API.cpp b/dfhack/python/DF_API.cpp index b84d72804..f252af5dd 100644 --- a/dfhack/python/DF_API.cpp +++ b/dfhack/python/DF_API.cpp @@ -29,6 +29,7 @@ distribution. #include #include "DFTypes.h" #include "DFHackAPI.h" +#include "MemInfo.cpp" using namespace std; using namespace DFHack; @@ -36,6 +37,7 @@ using namespace DFHack; struct DF_API { PyObject_HEAD + PyObject* mem_info; DFHack::API* api_Ptr; }; @@ -59,6 +61,8 @@ static int DF_API_init(DF_API* self, PyObject* args, PyObject* kwds) if(self->api_Ptr == NULL) { + self->mem_info = NULL; + if(!PyArg_ParseTuple(args, "s", &memFileString)) return -1; @@ -82,6 +86,8 @@ static void DF_API_dealloc(DF_API* self) self->api_Ptr = NULL; } + Py_XDECREF(self->mem_info); + self->ob_type->tp_free((PyObject*)self); } } @@ -122,10 +128,40 @@ static PyObject* DF_API_getIsSuspended(DF_API* self, void* closure) Py_RETURN_FALSE; } +static PyObject* DF_API_getMemoryInfo(DF_API* self, void* closure) +{ + if(self->mem_info != NULL) + return self->mem_info; + + try + { + if(self->api_Ptr != NULL) + { + DFHack::memory_info* mem; + + mem = self->api_Ptr->getMemoryInfo(); + + self->mem_info = _PyObject_New(&DF_MemInfo_type); + delete ((DF_MemInfo*)(self->mem_info))->mem_Ptr; + ((DF_MemInfo*)(self->mem_info))->mem_Ptr = mem; + + return self->mem_info; + } + } + catch(...) + { + PyErr_SetString(PyExc_ValueError, "Error trying to read memory info"); + return NULL; + } + + Py_RETURN_NONE; +} + static PyGetSetDef DF_API_getterSetters[] = { {"is_attached", (getter)DF_API_getIsAttached, NULL, "is_attached", NULL}, {"is_suspended", (getter)DF_API_getIsSuspended, NULL, "is_suspended", NULL}, + {"memory_info", (getter)DF_API_getMemoryInfo, NULL, "memory_info", NULL}, {NULL} // Sentinel };