From e6e1434fc1bc3f5108c3a654b70d2b73fe55fb17 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Fri, 26 Apr 2013 15:54:39 -0400 Subject: [PATCH] Add PrinterPlain --- pvDataApp/Makefile | 2 + pvDataApp/factory/printer.cpp | 138 ++++++++++++++++++++++++++++++++++ pvDataApp/pv/printer.h | 60 +++++++++++++++ 3 files changed, 200 insertions(+) create mode 100644 pvDataApp/factory/printer.cpp create mode 100644 pvDataApp/pv/printer.h diff --git a/pvDataApp/Makefile b/pvDataApp/Makefile index 88e54e7..bd628a4 100644 --- a/pvDataApp/Makefile +++ b/pvDataApp/Makefile @@ -25,6 +25,7 @@ INC += status.h INC += sharedPtr.h INC += localStaticLock.h INC += typeCast.h +INC += printer.h LIBSRCS += byteBuffer.cpp LIBSRCS += bitSet.cpp @@ -67,6 +68,7 @@ LIBSRCS += Convert.cpp LIBSRCS += Compare.cpp LIBSRCS += StandardField.cpp LIBSRCS += StandardPVField.cpp +LIBSRCS += printer.cpp SRC_DIRS += $(PVDATA)/property diff --git a/pvDataApp/factory/printer.cpp b/pvDataApp/factory/printer.cpp new file mode 100644 index 0000000..1b39ecc --- /dev/null +++ b/pvDataApp/factory/printer.cpp @@ -0,0 +1,138 @@ + +#include "pv/printer.h" + +namespace { + +void indentN(std::ostream& strm, size_t N) +{ + while(N--) + strm.put(' '); +} + +} + +namespace epics { namespace pvData { + +PrinterBase::PrinterBase() + :strm(NULL) +{} + +PrinterBase::~PrinterBase() {} + +void PrinterBase::setStream(std::ostream& s) +{ + strm = &s; +} + +void PrinterBase::clearStream() +{ + strm = NULL; +} + +void PrinterBase::print(const PVField& pv) +{ + if(!strm) + throw std::runtime_error("No stream set for PV Printer"); + impl_print(pv); +} + +void PrinterBase::beginStructure(const PVStructure&) {} +void PrinterBase::endStructure(const PVStructure&) {} + +void PrinterBase::beginStructureArray(const PVStructureArray&) {} +void PrinterBase::endStructureArray(const PVStructureArray&) {} + +void PrinterBase::encodeScalar(const PVScalar& pv) {} + +void PrinterBase::encodeArray(const PVScalarArray&) {} + +void PrinterBase::abortField(const PVField&) {} + +void PrinterBase::impl_print(const PVField& pv) +{ + try { + switch(pv.getField()->getType()) { + case scalar: encodeScalar(static_cast(pv)); return; + case scalarArray: encodeArray(static_cast(pv)); return; + case structure: { + const PVStructure &fld = static_cast(pv); + const PVFieldPtrArray& vals = fld.getPVFields(); + + beginStructure(fld); + for(size_t i=0, nfld=fld.getNumberFields(); iprint(*vals[i]); + endStructure(fld); + } + case structureArray: { + const PVStructureArray &fld = static_cast(pv); + const PVStructureArray::pointer vals = fld.get(); + + beginStructureArray(fld); + for(size_t i=0, nfld=fld.getLength(); iprint(*vals[i]); + endStructureArray(fld); + } + } + } catch(...) { + abortField(pv); + throw; + } +} + + +PrinterPlain::PrinterPlain() + :PrinterBase() + ,ilvl(0) +{} + +PrinterPlain::~PrinterPlain() {} + +void PrinterPlain::beginStructure(const PVStructure& pv) +{ + indentN(S(), ilvl); + S() << pv.getStructure()->getID() << " " << pv.getFieldName(); + String ename(pv.getExtendsStructureName()); + if(!ename.empty()) + S() << " extends " << ename; + S() << std::endl; + ilvl++; +} + +void PrinterPlain::endStructure(const PVStructure&) {ilvl--;} + +void PrinterPlain::beginStructureArray(const PVStructureArray& pv) +{ + indentN(S(), ilvl); + S() << pv.getStructureArray()->getID() << " " + << pv.getFieldName() << " "; + ilvl++; +} + +void PrinterPlain::endStructureArray(const PVStructureArray&) {ilvl--;} + +void PrinterPlain::encodeScalar(const PVScalar& pv) +{ + indentN(S(), ilvl); + S() << pv.getScalar()->getID() << " " + << pv.getFieldName() << " " + << pv.getAs() << std::endl; +} + +void PrinterPlain::encodeArray(const PVScalarArray& pv) +{ + indentN(S(), ilvl); + StringArray temp(pv.getLength()); // TODO: no copy + pv.getAs(&temp[0], temp.size()); + + S() << pv.getScalarArray()->getID() << " " + << pv.getFieldName() << " ["; + for(size_t i=0, len=pv.getLength(); i + +#include "pvData.h" + +namespace epics { namespace pvData { + +class PrinterBase +{ +public: + virtual void setStream(std::ostream&); + virtual void clearStream(); + + virtual void print(const PVField&); + +protected: + PrinterBase(); + virtual ~PrinterBase()=0; + + virtual void beginStructure(const PVStructure&); + virtual void endStructure(const PVStructure&); + + virtual void beginStructureArray(const PVStructureArray&); + virtual void endStructureArray(const PVStructureArray&); + + virtual void encodeScalar(const PVScalar&); + virtual void encodeArray(const PVScalarArray&); + + virtual void abortField(const PVField&); + + inline std::ostream& S() { return *strm; } + + void impl_print(const PVField&); +private: + std::ostream *strm; +}; + +class PrinterPlain : public PrinterBase +{ + size_t ilvl; +protected: + virtual void beginStructure(const PVStructure&); + virtual void endStructure(const PVStructure&); + + virtual void beginStructureArray(const PVStructureArray&); + virtual void endStructureArray(const PVStructureArray&); + + virtual void encodeScalar(const PVScalar&); + virtual void encodeArray(const PVScalarArray&); + +public: + PrinterPlain(); + virtual ~PrinterPlain(); +}; + +}} + +#endif // PRINTER_H