From 038567280bfb8652979714224992052890ef4b89 Mon Sep 17 00:00:00 2001 From: jrowlandls Date: Thu, 19 Jul 2012 14:22:37 +0100 Subject: [PATCH] pvData: added C api test --- testApp/Makefile | 1 + testApp/capi/Makefile | 9 +++++ testApp/capi/testc.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++ testApp/capi/testc.py | 33 +++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 testApp/capi/Makefile create mode 100644 testApp/capi/testc.cpp create mode 100755 testApp/capi/testc.py diff --git a/testApp/Makefile b/testApp/Makefile index b59c2a7..2cb742e 100644 --- a/testApp/Makefile +++ b/testApp/Makefile @@ -4,5 +4,6 @@ DIRS += misc DIRS += pv DIRS += property DIRS += monitor +DIRS += capi include $(TOP)/configure/RULES_DIRS diff --git a/testApp/capi/Makefile b/testApp/capi/Makefile new file mode 100644 index 0000000..328d98a --- /dev/null +++ b/testApp/capi/Makefile @@ -0,0 +1,9 @@ +TOP=../.. + +include $(TOP)/configure/CONFIG + +LIBRARY_HOST += testc +testc_SRCS += testc.cpp +testc_LIBS += pvData Com + +include $(TOP)/configure/RULES diff --git a/testApp/capi/testc.cpp b/testApp/capi/testc.cpp new file mode 100644 index 0000000..7b54272 --- /dev/null +++ b/testApp/capi/testc.cpp @@ -0,0 +1,81 @@ +#include +#include +#include + +using namespace epics::pvData; + +struct StructureHandle +{ + StructureConstPtr s; + StructureHandle(StructureConstPtr s) : s(s) {} +}; + +extern "C" +{ + void * createTest() + { + FieldCreatePtr fieldCreate = getFieldCreate(); + PVDataCreatePtr pvDataCreate = getPVDataCreate(); + FieldConstPtrArray fields; + StringArray fieldNames; + fields.push_back(fieldCreate->createScalar(pvString)); + fields.push_back(fieldCreate->createScalarArray(pvDouble)); + fieldNames.push_back("name"); + fieldNames.push_back("value"); + StructureConstPtr s = fieldCreate->createStructure(fieldNames, fields); + StructureHandle * sh = new StructureHandle(s); + return (void *)sh; + } + + int getNumberFields(void * handle) + { + StructureHandle * sh = (StructureHandle *)handle; + return sh->s->getNumberFields(); + } + + const char * getFieldName(void * handle, int n) + { + StructureHandle * sh = (StructureHandle *)handle; + return sh->s->getFieldNames()[n].c_str(); + } + + int getFieldType(void * handle, const char * name) + { + StructureHandle * sh = (StructureHandle *)handle; + /* api missing const for getField(size_t) so have to use name */ + return sh->s->getField(name)->getType(); + } + + int getScalarType(void * handle, const char * name) + { + StructureHandle * sh = (StructureHandle *)handle; + FieldConstPtr f = sh->s->getField(name); + if(f->getType() == scalar) + { + ScalarConstPtr sp = std::tr1::static_pointer_cast(f); + return sp->getScalarType(); + } + else + { + return -1; + } + } + + int getElementType(void * handle, const char * name) + { + StructureHandle * sh = (StructureHandle *)handle; + FieldConstPtr f = sh->s->getField(name); + if(f->getType() == scalarArray) + { + ScalarArrayConstPtr sp = std::tr1::static_pointer_cast(f); + return sp->getElementType(); + } + else + { + return -1; + } + } + +}; + + diff --git a/testApp/capi/testc.py b/testApp/capi/testc.py new file mode 100755 index 0000000..8dc9c6d --- /dev/null +++ b/testApp/capi/testc.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +import os +from ctypes import * + +libname = "../../lib/%s/libtestc.so" % os.environ["EPICS_HOST_ARCH"] +lib = CDLL(libname) +lib.createTest.restype = c_void_p +lib.createTest.argtypes = [] +lib.getNumberFields.restype = c_int +lib.getNumberFields.argtypes = [c_void_p] +lib.getFieldName.restype = c_char_p +lib.getFieldName.argtypes = [c_void_p, c_int] +lib.getFieldType.restype = c_int +lib.getFieldType.argtypes = [c_void_p, c_char_p] +lib.getScalarType.restype = c_int +lib.getScalarType.argtypes = [c_void_p, c_char_p] +lib.getElementType.restype = c_int +lib.getElementType.argtypes = [c_void_p, c_char_p] + +type_enum = ["scalar", "scalarArray", "structure", "structureArray"] +struct = lib.createTest() + +l = lib.getNumberFields(struct) +print "pvData structure has %d fields" % l + +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 +