From 92c68dfcbcdd0e83d598607669c44e5103687769 Mon Sep 17 00:00:00 2001 From: jrowlandls Date: Fri, 20 Jul 2012 10:25:01 +0100 Subject: [PATCH] pvData ctypes: added context handler and handle destructor to test example --- testApp/capi/testc.cpp | 6 ++++++ testApp/capi/testc.py | 44 ++++++++++++++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/testApp/capi/testc.cpp b/testApp/capi/testc.cpp index 7b54272..0f6292c 100644 --- a/testApp/capi/testc.cpp +++ b/testApp/capi/testc.cpp @@ -76,6 +76,12 @@ extern "C" } } + void destroy(void * handle) + { + StructureHandle * sh = (StructureHandle *)handle; + delete sh; + } + }; diff --git a/testApp/capi/testc.py b/testApp/capi/testc.py index 8dc9c6d..18a8826 100755 --- a/testApp/capi/testc.py +++ b/testApp/capi/testc.py @@ -3,6 +3,8 @@ import os from ctypes import * +# cytpes API + libname = "../../lib/%s/libtestc.so" % os.environ["EPICS_HOST_ARCH"] lib = CDLL(libname) lib.createTest.restype = c_void_p @@ -18,16 +20,38 @@ lib.getScalarType.argtypes = [c_void_p, c_char_p] lib.getElementType.restype = c_int lib.getElementType.argtypes = [c_void_p, c_char_p] +# Python API + type_enum = ["scalar", "scalarArray", "structure", "structureArray"] -struct = lib.createTest() -l = lib.getNumberFields(struct) -print "pvData structure has %d fields" % l +class Structure: + def getNumberFields(self): + return lib.getNumberFields(self.handle) + def getFieldName(self, n): + return lib.getFieldName(self.handle, n) + def getFieldType(self, n): + return lib.getFieldType(self.handle, n) + def getScalarType(self, n): + return lib.getScalarType(self.handle, n) + def getElementType(self, n): + return lib.getElementType(self.handle, n) + def __enter__(self): + return self + def __exit__(self, a, b, c): + print "destroying handle" + lib.destroy(self.handle) -for n in range(l): - name = lib.getFieldName(struct, n) - type_ = lib.getFieldType(struct, name) - sc_type = lib.getScalarType(struct, name) - el_type = lib.getElementType(struct, name) - print name, type_enum[type_], sc_type, el_type - +class TestStructure(Structure): + def __init__(self): + self.handle = lib.createTest() + +with TestStructure() as s: + l = s.getNumberFields() + print "pvData structure has %d fields" % l + for n in range(l): + name = s.getFieldName(n) + type_ = s.getFieldType(name) + sc_type = s.getScalarType(name) + el_type = s.getElementType(name) + print name, type_enum[type_], sc_type, el_type +