From 63eb1aa7032e2da7903e913e6cfb0a4838a5024d Mon Sep 17 00:00:00 2001 From: Matej Sekoranja Date: Thu, 4 Sep 2014 12:30:02 +0200 Subject: [PATCH] ntnameValue: extra fields can be added now --- src/nt/ntnameValue.cpp | 13 +++++++++++++ src/nt/ntnameValue.h | 12 ++++++++++++ test/nt/ntnameValueTest.cpp | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/nt/ntnameValue.cpp b/src/nt/ntnameValue.cpp index bb46d0d..1ab39d7 100644 --- a/src/nt/ntnameValue.cpp +++ b/src/nt/ntnameValue.cpp @@ -46,6 +46,10 @@ StructureConstPtr NTNameValueBuilder::createStructure() if (timeStamp) builder->add("timeStamp", ntField->createTimeStamp()); + size_t extraCount = extraFieldNames.size(); + for (size_t i = 0; i< extraCount; i++) + builder->add(extraFieldNames[i], extraFields[i]); + StructureConstPtr s = builder->createStructure(); reset(); @@ -91,8 +95,17 @@ void NTNameValueBuilder::reset() descriptor = false; alarm = false; timeStamp = false; + extraFieldNames.clear(); + extraFields.clear(); } +NTNameValueBuilder::shared_pointer NTNameValueBuilder::add(string const & name, FieldConstPtr const & field) +{ + extraFields.push_back(field); extraFieldNames.push_back(name); + return shared_from_this(); +} + + } const std::string NTNameValue::URI("uri:ev4:nt/2012/pwd:NTNameValue"); diff --git a/src/nt/ntnameValue.h b/src/nt/ntnameValue.h index 0ccc780..6430c1a 100644 --- a/src/nt/ntnameValue.h +++ b/src/nt/ntnameValue.h @@ -74,6 +74,14 @@ namespace detail { */ NTNameValuePtr create(); + /** + * Add extra {@code Field} to the type. + * @param name name of the field. + * @param field a field to add. + * @return this instance of a {@code NTNameValueBuilder}. + */ + shared_pointer add(std::string const & name, epics::pvData::FieldConstPtr const & field); + private: NTNameValueBuilder(); @@ -86,6 +94,10 @@ namespace detail { bool alarm; bool timeStamp; + // NOTE: this preserves order, however it does not handle duplicates + epics::pvData::StringArray extraFieldNames; + epics::pvData::FieldConstPtrArray extraFields; + friend class ::epics::nt::NTNameValue; }; diff --git a/test/nt/ntnameValueTest.cpp b/test/nt/ntnameValueTest.cpp index ea7d9c9..fba6c96 100644 --- a/test/nt/ntnameValueTest.cpp +++ b/test/nt/ntnameValueTest.cpp @@ -204,11 +204,43 @@ void test_narrow() testOk(ptr.get() != 0, "narrow_unsafe OK"); } +void test_extra() +{ + testDiag("test_extra"); + + NTNameValueBuilderPtr builder = NTNameValue::createBuilder(); + testOk(builder.get() != 0, "Got builder"); + + StructureConstPtr structure = builder-> + value(pvDouble)-> + addTimeStamp()-> + add("function", getFieldCreate()->createScalar(pvString))-> + createStructure(); + testOk1(structure.get() != 0); + if (!structure) + return; + + testOk1(NTNameValue::is_a(structure)); + testOk1(structure->getID() == NTNameValue::URI); + testOk1(structure->getNumberFields() == 4); + testOk1(structure->getField("names").get() != 0); + testOk1(structure->getField("values").get() != 0); + testOk1(structure->getField("timeStamp").get() != 0); + testOk1(structure->getField("function").get() != 0); + + testOk(dynamic_pointer_cast(structure->getField("function")).get() != 0 && + dynamic_pointer_cast(structure->getField("function"))->getScalarType() == pvString, "function type"); + + std::cout << *structure << std::endl; +} + + MAIN(testNTNameValue) { - testPlan(37); + testPlan(47); test_builder(); test_ntnameValue(); test_narrow(); + test_extra(); return testDone(); }