diff --git a/src/Makefile b/src/Makefile index 6b96e13..f7e58bf 100644 --- a/src/Makefile +++ b/src/Makefile @@ -6,6 +6,7 @@ SRC = $(TOP)/src/ SRC_DIRS += $(SRC)/nt INC += nt.h +INC += ntutils.h INC += ntfield.h INC += ntscalar.h INC += ntscalarArray.h @@ -14,6 +15,7 @@ INC += nttable.h INC += ntmultiChannel.h INC += ntndarray.h +LIBSRCS += ntutils.cpp LIBSRCS += ntfield.cpp LIBSRCS += ntscalar.cpp LIBSRCS += ntscalarArray.cpp diff --git a/src/nt/nt.h b/src/nt/nt.h index 3544cfe..7c6ddff 100644 --- a/src/nt/nt.h +++ b/src/nt/nt.h @@ -7,6 +7,9 @@ #ifndef NT_H #define NT_H +/* + * Include all the normative types here. + */ #include #include #include diff --git a/src/nt/ntfield.cpp b/src/nt/ntfield.cpp index b364651..a8c29b4 100644 --- a/src/nt/ntfield.cpp +++ b/src/nt/ntfield.cpp @@ -8,9 +8,10 @@ #include #include +using namespace epics::pvData; using std::tr1::static_pointer_cast; -namespace epics { namespace pvData { +namespace epics { namespace nt { NTFieldPtr NTField::get() { diff --git a/src/nt/ntfield.h b/src/nt/ntfield.h index da70787..d3a3f58 100644 --- a/src/nt/ntfield.h +++ b/src/nt/ntfield.h @@ -17,7 +17,7 @@ #include #include -namespace epics { namespace pvData { +namespace epics { namespace nt { /** * Convenience Class for introspection fields of a Normative Type * @author mrk @@ -47,83 +47,83 @@ public: * @param field The field to test. * @return (false,true) if field (is not,is) an enumerated structure. */ - bool isEnumerated(FieldConstPtr const & field); + bool isEnumerated(epics::pvData::FieldConstPtr const & field); /** * Is field a timeStamp structure. * @param field The field to test. * @return (false,true) if field (is not,is) a timeStamp structure. */ - bool isTimeStamp(FieldConstPtr const & field); + bool isTimeStamp(epics::pvData::FieldConstPtr const & field); /** * Is field an alarm structure. * @param field The field to test. * @return (false,true) if field (is not,is) an alarm structure. */ - bool isAlarm(FieldConstPtr const & field); + bool isAlarm(epics::pvData::FieldConstPtr const & field); /** * Is field a display structure. * @param field The field to test. * @return (false,true) if field (is not,is) a display structure. */ - bool isDisplay(FieldConstPtr const & field); + bool isDisplay(epics::pvData::FieldConstPtr const & field); /** * Is field an alarmLimit structure. * @param field The field to test. * @return (false,true) if field (is not,is) an alarmLimit structure. */ - bool isAlarmLimit(FieldConstPtr const & field); + bool isAlarmLimit(epics::pvData::FieldConstPtr const & field); /** * Is field a control structure. * @param field The field to test. * @return (false,true) if field (is not,is) a control structure. */ - bool isControl(FieldConstPtr const & field); + bool isControl(epics::pvData::FieldConstPtr const & field); /** * Create an enumerated structure. * @return an enumerated structure. */ - StructureConstPtr createEnumerated(); + epics::pvData::StructureConstPtr createEnumerated(); /** * Create a timeStamp structure. * @return a timeStamp structure. */ - StructureConstPtr createTimeStamp(); + epics::pvData::StructureConstPtr createTimeStamp(); /** * Create an alarm structure. * @return an alarm structure. */ - StructureConstPtr createAlarm(); + epics::pvData::StructureConstPtr createAlarm(); /** * Create a display structure. * @return a displayalarm structure. */ - StructureConstPtr createDisplay(); + epics::pvData::StructureConstPtr createDisplay(); /** * Create a control structure. * @return a control structure. */ - StructureConstPtr createControl(); + epics::pvData::StructureConstPtr createControl(); /** * Create an array of enumerated structures. * @return an array of enumerated structures. */ - StructureArrayConstPtr createEnumeratedArray(); + epics::pvData::StructureArrayConstPtr createEnumeratedArray(); /** * Create an array of timeStamp structures. * @return an array of timeStamp structures. */ - StructureArrayConstPtr createTimeStampArray(); + epics::pvData::StructureArrayConstPtr createTimeStampArray(); /** * Create an array of alarm structures. * @return an array of alarm structures. */ - StructureArrayConstPtr createAlarmArray(); + epics::pvData::StructureArrayConstPtr createAlarmArray(); private: NTField(); - FieldCreatePtr fieldCreate; - StandardFieldPtr standardField; + epics::pvData::FieldCreatePtr fieldCreate; + epics::pvData::StandardFieldPtr standardField; }; /** @@ -148,54 +148,54 @@ public: * @param choices The array of choices. * @return an enumerated PVStructure.. */ - PVStructurePtr createEnumerated( - StringArray const & choices); + epics::pvData::PVStructurePtr createEnumerated( + epics::pvData::StringArray const & choices); /** * Create a timeStamp PVStructure. * @return a timeStamp PVStructure.. */ - PVStructurePtr createTimeStamp(); + epics::pvData::PVStructurePtr createTimeStamp(); /** * Create an alarm PVStructure. * @return an alarm PVStructure.. */ - PVStructurePtr createAlarm(); + epics::pvData::PVStructurePtr createAlarm(); /** * Create a display PVStructure. * @return a display PVStructure.. */ - PVStructurePtr createDisplay(); + epics::pvData::PVStructurePtr createDisplay(); /** * Create an alarmLimit PVStructure. * @return an alarmLimit PVStructure.. */ - PVStructurePtr createAlarmLimit(); + epics::pvData::PVStructurePtr createAlarmLimit(); /** * Create a control PVStructure. * @return a control PVStructure.. */ - PVStructurePtr createControl(); + epics::pvData::PVStructurePtr createControl(); /** * Create an enumerated PVStructureArray. * @return an enumerated PVStructureArray.. */ - PVStructureArrayPtr createEnumeratedArray(); + epics::pvData::PVStructureArrayPtr createEnumeratedArray(); /** * Create a timeStamp PVStructureArray. * @return a timeStamp PVStructureArray */ - PVStructureArrayPtr createTimeStampArray(); + epics::pvData::PVStructureArrayPtr createTimeStampArray(); /** * Create an alarm PVStructureArray. * @return an alarm PVStructureArray.. */ - PVStructureArrayPtr createAlarmArray(); + epics::pvData::PVStructureArrayPtr createAlarmArray(); private: PVNTField(); - PVDataCreatePtr pvDataCreate; - StandardFieldPtr standardField; - StandardPVFieldPtr standardPVField; + epics::pvData::PVDataCreatePtr pvDataCreate; + epics::pvData::StandardFieldPtr standardField; + epics::pvData::StandardPVFieldPtr standardPVField; NTFieldPtr ntstructureField; }; diff --git a/src/nt/ntmultiChannel.cpp b/src/nt/ntmultiChannel.cpp index 0fc53e2..b625741 100644 --- a/src/nt/ntmultiChannel.cpp +++ b/src/nt/ntmultiChannel.cpp @@ -6,6 +6,7 @@ */ #include +#include #include using namespace std; @@ -210,7 +211,7 @@ NTMultiChannel::shared_pointer NTMultiChannel::wrapUnsafe(PVStructurePtr const & bool NTMultiChannel::is_a(StructureConstPtr const &structure) { - return structure->getID() == URI; + return NTUtils::is_a(structure->getID(), URI); } bool NTMultiChannel::isCompatible(PVStructurePtr const &pvStructure) diff --git a/src/nt/ntnameValue.cpp b/src/nt/ntnameValue.cpp index e80b558..384dafd 100644 --- a/src/nt/ntnameValue.cpp +++ b/src/nt/ntnameValue.cpp @@ -6,6 +6,7 @@ */ #include +#include using namespace std; using namespace epics::pvData; @@ -123,7 +124,7 @@ NTNameValue::shared_pointer NTNameValue::wrapUnsafe(PVStructurePtr const & struc bool NTNameValue::is_a(StructureConstPtr const & structure) { - return structure->getID() == URI; + return NTUtils::is_a(structure->getID(), URI); } bool NTNameValue::isCompatible(PVStructurePtr const & pvStructure) diff --git a/src/nt/ntndarray.cpp b/src/nt/ntndarray.cpp index 14b5016..b6c7d65 100644 --- a/src/nt/ntndarray.cpp +++ b/src/nt/ntndarray.cpp @@ -8,6 +8,7 @@ #include #include +#include using namespace std; using namespace epics::pvData; @@ -217,7 +218,7 @@ NTNDArray::shared_pointer NTNDArray::wrapUnsafe(PVStructurePtr const & structure bool NTNDArray::is_a(StructureConstPtr const & structure) { - return structure->getID() == URI; + return NTUtils::is_a(structure->getID(), URI); } bool NTNDArray::isCompatible(PVStructurePtr const & pvStructure) diff --git a/src/nt/ntscalar.cpp b/src/nt/ntscalar.cpp index e28b729..0f9d624 100644 --- a/src/nt/ntscalar.cpp +++ b/src/nt/ntscalar.cpp @@ -6,6 +6,7 @@ */ #include +#include using namespace std; using namespace epics::pvData; @@ -143,7 +144,7 @@ NTScalar::shared_pointer NTScalar::wrapUnsafe(PVStructurePtr const & structure) bool NTScalar::is_a(StructureConstPtr const & structure) { - return structure->getID() == URI; + return NTUtils::is_a(structure->getID(), URI); } bool NTScalar::isCompatible(PVStructurePtr const & pvStructure) diff --git a/src/nt/ntscalarArray.cpp b/src/nt/ntscalarArray.cpp index 6e6addf..e510eb3 100644 --- a/src/nt/ntscalarArray.cpp +++ b/src/nt/ntscalarArray.cpp @@ -6,6 +6,7 @@ */ #include +#include using namespace std; using namespace epics::pvData; @@ -140,7 +141,7 @@ NTScalarArray::shared_pointer NTScalarArray::wrapUnsafe(PVStructurePtr const & s bool NTScalarArray::is_a(StructureConstPtr const & structure) { - return structure->getID() == URI; + return NTUtils::is_a(structure->getID(), URI); } bool NTScalarArray::isCompatible(PVStructurePtr const & pvStructure) diff --git a/src/nt/nttable.cpp b/src/nt/nttable.cpp index dc96906..240d6b9 100644 --- a/src/nt/nttable.cpp +++ b/src/nt/nttable.cpp @@ -8,6 +8,7 @@ #include #include +#include using namespace std; using namespace epics::pvData; @@ -138,7 +139,7 @@ NTTable::shared_pointer NTTable::wrapUnsafe(PVStructurePtr const & structure) bool NTTable::is_a(StructureConstPtr const & structure) { - return structure->getID() == URI; + return NTUtils::is_a(structure->getID(), URI); } bool NTTable::isCompatible(PVStructurePtr const & pvStructure) diff --git a/src/nt/ntutils.cpp b/src/nt/ntutils.cpp new file mode 100644 index 0000000..a736082 --- /dev/null +++ b/src/nt/ntutils.cpp @@ -0,0 +1,24 @@ +/* ntutils.cpp */ +/** + * Copyright - See the COPYRIGHT that is included with this distribution. + * EPICS pvDataCPP is distributed subject to a Software License Agreement found + * in file LICENSE that is included with this distribution. + */ + +#include + +using namespace std; + +namespace epics { namespace nt { + +bool NTUtils::is_a(const std::string &u1, const std::string &u2) +{ + // remove minor for the u2 + size_t pos = u2.find_last_of('.'); + std::string su2 = (pos == string::npos) ? u2 : u2.substr(0, pos); + + // "starts with comparison" + return su2.size() <= u1.size() && u1.compare(0, su2.size(), su2) == 0; +} + +}} diff --git a/src/nt/ntutils.h b/src/nt/ntutils.h new file mode 100644 index 0000000..3e99336 --- /dev/null +++ b/src/nt/ntutils.h @@ -0,0 +1,38 @@ +/* ntutils.h */ +/** + * Copyright - See the COPYRIGHT that is included with this distribution. + * EPICS pvDataCPP is distributed subject to a Software License Agreement found + * in file LICENSE that is included with this distribution. + */ +#ifndef NTUTILS_H +#define NTUTILS_H + +#include + +namespace epics { namespace nt { + +/** + * Utility methods for NT types. + * @author mse + */ +class NTUtils { +public: + + /** + * Checks whether NT types are compatible by checking their IDs, + * i.e. their names and major version must match. + * @param u1 the first uri. + * @param u2 the second uri. + * @return true of URIs are compatible, false otherwise. + */ + static bool is_a(const std::string &u1, const std::string &u2); + +private: + // disable object creation + NTUtils() {} +}; + +}} + +#endif /* NTUTILS_H */ + diff --git a/test/nt/Makefile b/test/nt/Makefile index c1f47ea..3b0b4b6 100644 --- a/test/nt/Makefile +++ b/test/nt/Makefile @@ -32,6 +32,10 @@ TESTPROD_HOST += ntndarrayTest ntndarrayTest_SRCS = ntndarrayTest.cpp TESTS += ntndarrayTest +TESTPROD_HOST += ntutilsTest +ntndarrayTest_SRCS = ntutilsTest.cpp +TESTS += ntutilsTest + TESTSCRIPTS_HOST += $(TESTS:%=%.t) include $(TOP)/configure/RULES diff --git a/test/nt/ntfieldTest.cpp b/test/nt/ntfieldTest.cpp index f394533..dccee87 100644 --- a/test/nt/ntfieldTest.cpp +++ b/test/nt/ntfieldTest.cpp @@ -15,6 +15,7 @@ #include +using namespace epics::nt; using namespace epics::pvData; using std::string; using std::cout; diff --git a/test/nt/ntutilsTest.cpp b/test/nt/ntutilsTest.cpp new file mode 100644 index 0000000..5180657 --- /dev/null +++ b/test/nt/ntutilsTest.cpp @@ -0,0 +1,36 @@ +/** + * Copyright - See the COPYRIGHT that is included with this distribution. + * EPICS pvDataCPP is distributed subject to a Software License Agreement found + * in file LICENSE that is included with this distribution. + */ + +#include +#include + +#include + + +using namespace epics::nt; + +void test_is_a() +{ + testDiag("test_is_a"); + + testOk1(NTUtils::is_a("epics:nt/NTTable:1.0", "epics:nt/NTTable:1.0")); + testOk1(NTUtils::is_a("epics:nt/NTTable:2.0", "epics:nt/NTTable:2.0")); + testOk1(NTUtils::is_a("epics:nt/NTTable:1.0", "epics:nt/NTTable:1.1")); + testOk1(NTUtils::is_a("epics:nt/NTTable:1.1", "epics:nt/NTTable:1.0")); + + testOk1(!NTUtils::is_a("epics:nt/NTTable:1.0", "epics:nt/NTTable:2.0")); + testOk1(!NTUtils::is_a("epics:nt/NTTable:2.0", "epics:nt/NTTable:1.0")); + testOk1(!NTUtils::is_a("epics:nt/NTTable:1.3", "epics:nt/NTTable:2.3")); + testOk1(!NTUtils::is_a("epics:nt/NTTable:1.0", "epics:nt/NTMatrix:1.0")); +} + +MAIN(testNTUtils) { + testPlan(8); + test_is_a(); + return testDone(); +} + +