From 9f7d2431089459a24124f1bb01157217b4ab8965 Mon Sep 17 00:00:00 2001 From: Matej Sekoranja Date: Wed, 22 Aug 2012 00:02:47 +0200 Subject: [PATCH] LUT for NT formatters --- testApp/remote/eget.cpp | 47 +++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/testApp/remote/eget.cpp b/testApp/remote/eget.cpp index 2f4801c..d9f729f 100644 --- a/testApp/remote/eget.cpp +++ b/testApp/remote/eget.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -162,7 +163,6 @@ char *url_encode(char *str) { - void formatNTAny(std::ostream& o, PVStructurePtr const & pvStruct) { PVFieldPtr value = pvStruct->getSubField("value"); @@ -262,32 +262,42 @@ void formatNTTable(std::ostream& o, PVStructurePtr const & pvStruct) } +typedef void(*NTFormatterFunc)(std::ostream& o, PVStructurePtr const & pvStruct); +typedef map NTFormatterLUTMap; +NTFormatterLUTMap ntFormatterLUT; + +void initializeNTFormatterLUT() +{ + ntFormatterLUT["NTTable"] = formatNTTable; + ntFormatterLUT["NTScalar"] = formatNTScalar; + ntFormatterLUT["NTScalarArray"] = formatNTScalarArray; + ntFormatterLUT["NTAny"] = formatNTAny; + + // bug in StandardPV + ntFormatterLUT["scalar_t"] = formatNTScalar; + ntFormatterLUT["scalarArray_t"] = formatNTScalarArray; +} + void toNTString(std::ostream& o, PVFieldPtr const & pv) { + static bool lutInitialized = false; + if (!lutInitialized) + { + initializeNTFormatterLUT(); + lutInitialized = true; + } + Type type = pv->getField()->getType(); if (type==structure) { PVStructurePtr pvStruct = static_pointer_cast(pv); { - String value = pvStruct->getField()->getID(); + String id = pvStruct->getField()->getID(); - if (value == "NTTable") + NTFormatterLUTMap::const_iterator formatter = ntFormatterLUT.find(id); + if (formatter != ntFormatterLUT.end()) { - formatNTTable(std::cout, pvStruct); - } -// else if (value == "NTScalar") - else if (value == "scalar_t") - { - formatNTScalar(std::cout, pvStruct); - } - // else if (value == "NTScalarArray") - else if (value == "scalarArray_t") - { - formatNTScalarArray(std::cout, pvStruct); - } - else if (value == "NTAny") - { - formatNTAny(std::cout, pvStruct); + (formatter->second)(o, pvStruct); } else { @@ -301,6 +311,7 @@ void toNTString(std::ostream& o, PVFieldPtr const & pv) } } + // no ID, just dump String buffer; pv->toString(&buffer); o << buffer;