ntnameValue: extra fields can be added now

This commit is contained in:
Matej Sekoranja
2014-09-04 12:30:02 +02:00
parent 89396a7455
commit 63eb1aa703
3 changed files with 58 additions and 1 deletions

View File

@ -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");

View File

@ -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;
};

View File

@ -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<const Scalar>(structure->getField("function")).get() != 0 &&
dynamic_pointer_cast<const Scalar>(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();
}