NTNameValue refactored

This commit is contained in:
Matej Sekoranja
2014-08-21 21:49:46 +02:00
parent c0cb48e97e
commit 5b46b9ebed
4 changed files with 446 additions and 200 deletions

View File

@@ -7,104 +7,157 @@
#include <pv/ntnameValue.h>
namespace epics { namespace pvData {
using namespace std;
using namespace epics::pvData;
using std::tr1::static_pointer_cast;
namespace epics { namespace nt {
bool NTNameValue::isNTNameValue(PVStructurePtr const & pvStructure)
namespace detail {
static NTFieldPtr ntField = NTField::get();
NTNameValueBuilder::shared_pointer NTNameValueBuilder::value(
epics::pvData::ScalarType scalarType
)
{
PVFieldPtr pvField = pvStructure->getSubField("names");
if(pvField.get()==NULL) return false;
FieldConstPtr field = pvField->getField();
if(field->getType()!=scalarArray) return false;
ScalarArrayConstPtr pscalarArray =
static_pointer_cast<const ScalarArray>(field);
if(pscalarArray->getElementType()!=pvString) return false;
pvField = pvStructure->getSubField("values");
if(pvField==0) return false;
field = pvField->getField();
if(field->getType()!=scalarArray) return false;
pscalarArray = static_pointer_cast<const ScalarArray>(field);
if(pscalarArray->getElementType()!=pvString) return false;
return true;
valueType = scalarType;
valueTypeSet = true;
return shared_from_this();
}
NTNameValuePtr NTNameValue::create(
bool hasFunction,bool hasTimeStamp, bool hasAlarm)
StructureConstPtr NTNameValueBuilder::createStructure()
{
StandardFieldPtr standardField = getStandardField();
size_t nfields = 2;
if(hasFunction) nfields++;
if(hasTimeStamp) nfields++;
if(hasAlarm) nfields++;
FieldCreatePtr fieldCreate = getFieldCreate();
PVDataCreatePtr pvDataCreate = getPVDataCreate();
FieldConstPtrArray fields;
StringArray names;
fields.resize(nfields);
names.resize(nfields);
names[0] = "names";
fields[0] = fieldCreate->createScalarArray(pvString);
names[1] = "values";
fields[1] = fieldCreate->createScalarArray(pvString);
size_t ind = 2;
if(hasFunction) {
names[ind] = "function";
fields[ind++] = fieldCreate->createScalar(pvString);
}
if(hasTimeStamp) {
names[ind] = "timeStamp";
fields[ind++] = standardField->timeStamp();
}
if(hasAlarm) {
names[ind] = "alarm";
fields[ind++] = standardField->alarm();
}
StructureConstPtr st = fieldCreate->createStructure(names,fields);
PVStructurePtr pvStructure = pvDataCreate->createPVStructure(st);
return NTNameValuePtr(new NTNameValue(pvStructure));
if (!valueTypeSet)
throw std::runtime_error("value type not set");
FieldBuilderPtr builder =
getFieldCreate()->createFieldBuilder()->
setId(NTNameValue::URI)->
addArray("names", pvString)->
addArray("values", valueType);
if (descriptor)
builder->add("descriptor", pvString);
if (alarm)
builder->add("alarm", ntField->createAlarm());
if (timeStamp)
builder->add("timeStamp", ntField->createTimeStamp());
StructureConstPtr s = builder->createStructure();
reset();
return s;
}
NTNameValuePtr NTNameValue::create(
PVStructurePtr const & pvStructure)
NTNameValueBuilder::shared_pointer NTNameValueBuilder::addDescriptor()
{
return NTNameValuePtr(new NTNameValue(pvStructure));
descriptor = true;
return shared_from_this();
}
NTNameValue::NTNameValue(PVStructure::shared_pointer const & pvStructure)
: pvNTNameValue(pvStructure)
NTNameValueBuilder::shared_pointer NTNameValueBuilder::addAlarm()
{
NTFieldPtr ntfield = NTField::get();
PVScalarArrayPtr pvArray =
pvStructure->getScalarArrayField("names",pvString);
pvNames = static_pointer_cast<PVStringArray>(pvArray);
pvArray = pvStructure->getScalarArrayField("values",pvString);
pvValues = static_pointer_cast<PVStringArray>(pvArray);
PVFieldPtr pvField = pvStructure->getSubField("function");
if(pvField.get()!=NULL) {
pvFunction = pvStructure->getStringField("function");
}
pvField = pvStructure->getSubField("timeStamp");
if(pvField.get()!=NULL && ntfield->isTimeStamp(pvField->getField())) {
pvTimeStamp = static_pointer_cast<PVStructure>(pvField);
}
pvField = pvStructure->getSubField("alarm");
if(pvField.get()!=NULL && ntfield->isAlarm(pvField->getField())) {
pvAlarm = static_pointer_cast<PVStructure>(pvField);
}
alarm = true;
return shared_from_this();
}
void NTNameValue::attachTimeStamp(PVTimeStamp &pv)
NTNameValueBuilder::shared_pointer NTNameValueBuilder::addTimeStamp()
{
if(pvTimeStamp.get()==NULL) return;
pv.attach(pvTimeStamp);
timeStamp = true;
return shared_from_this();
}
void NTNameValue::attachAlarm(PVAlarm &pv)
PVStructurePtr NTNameValueBuilder::createPVStructure()
{
if(pvAlarm.get()==NULL) return;
pv.attach(pvAlarm);
return getPVDataCreate()->createPVStructure(createStructure());
}
NTNameValuePtr NTNameValueBuilder::create()
{
return NTNameValuePtr(new NTNameValue(createPVStructure()));
}
NTNameValueBuilder::NTNameValueBuilder()
{
reset();
}
void NTNameValueBuilder::reset()
{
valueTypeSet = false;
descriptor = false;
alarm = false;
timeStamp = false;
}
}
const std::string NTNameValue::URI("uri:ev4:nt/2012/pwd:NTNameValue");
bool NTNameValue::is_a(StructureConstPtr const & structure)
{
return structure->getID() == URI;
}
NTNameValueBuilderPtr NTNameValue::createBuilder()
{
return NTNameValueBuilderPtr(new detail::NTNameValueBuilder());
}
bool NTNameValue::attachTimeStamp(PVTimeStamp &pvTimeStamp) const
{
PVStructurePtr ts = getTimeStamp();
if (ts)
return pvTimeStamp.attach(ts);
else
return false;
}
bool NTNameValue::attachAlarm(PVAlarm &pvAlarm) const
{
PVStructurePtr al = getAlarm();
if (al)
return pvAlarm.attach(al);
else
return false;
}
PVStructurePtr NTNameValue::getPVStructure() const
{
return pvNTNameValue;
}
PVStringPtr NTNameValue::getDescriptor() const
{
return pvNTNameValue->getSubField<PVString>("descriptor");
}
PVStructurePtr NTNameValue::getTimeStamp() const
{
return pvNTNameValue->getSubField<PVStructure>("timeStamp");
}
PVStructurePtr NTNameValue::getAlarm() const
{
return pvNTNameValue->getSubField<PVStructure>("alarm");
}
PVStringArrayPtr NTNameValue::getNames() const
{
return pvNTNameValue->getSubField<PVStringArray>("names");
}
PVFieldPtr NTNameValue::getValues() const
{
return pvNTNameValue->getSubField("values");
}
NTNameValue::NTNameValue(PVStructurePtr const & pvStructure) :
pvNTNameValue(pvStructure)
{}
}}

View File

@@ -9,92 +9,191 @@
#include <pv/ntfield.h>
namespace epics { namespace pvData {
/**
* Convenience Class for NTNameValue
* @author mrk
*
*/
namespace epics { namespace nt {
class NTNameValue;
typedef std::tr1::shared_ptr<NTNameValue> NTNameValuePtr;
class NTNameValue
namespace detail {
/**
* Interface for in-line creating of NTNameValue.
* One instance can be used to create multiple instances.
* An instance of this object must not be used concurrently (an object has a state).
* @author mse
*/
class epicsShareClass NTNameValueBuilder :
public std::tr1::enable_shared_from_this<NTNameValueBuilder>
{
public:
POINTER_DEFINITIONS(NTNameValueBuilder);
/**
* Set a value array {@code Scalar} type.
* @param scalarType value array scalar array.
* @return this instance of a {@code NTTableBuilder}.
*/
shared_pointer value(epics::pvData::ScalarType scalarType);
/**
* Add descriptor field to the NTNameValue.
* @return this instance of a {@code NTNameValueBuilder}.
*/
shared_pointer addDescriptor();
/**
* Add alarm structure to the NTNameValue.
* @return this instance of a {@code NTNameValueBuilder}.
*/
shared_pointer addAlarm();
/**
* Add timeStamp structure to the NTNameValue.
* @return this instance of a {@code NTNameValueBuilder}.
*/
shared_pointer addTimeStamp();
/**
* Create a {@code Structure} that represents NTNameValue.
* This resets this instance state and allows new instance to be created.
* @return a new instance of a {@code Structure}.
*/
epics::pvData::StructureConstPtr createStructure();
/**
* Create a {@code PVStructure} that represents NTNameValue.
* This resets this instance state and allows new {@code instance to be created.
* @return a new instance of a {@code PVStructure}
*/
epics::pvData::PVStructurePtr createPVStructure();
/**
* Create a {@code NTNameValue} instance.
* This resets this instance state and allows new {@code instance to be created.
* @return a new instance of a {@code NTNameValue}
*/
NTNameValuePtr create();
private:
NTNameValueBuilder();
void reset();
bool valueTypeSet;
epics::pvData::ScalarType valueType;
bool descriptor;
bool alarm;
bool timeStamp;
friend class ::epics::nt::NTNameValue;
};
}
typedef std::tr1::shared_ptr<detail::NTNameValueBuilder> NTNameValueBuilderPtr;
/**
* Convenience Class for NTNameValue
* @author mrk
*/
class NTNameValue
{
public:
POINTER_DEFINITIONS(NTNameValue);
static const std::string URI;
/**
* Is the pvStructure an NTNameValue.
* @param pvStructure The pvStructure to test.
* Is the structure an NTNameValue.
* @param structure The structure to test.
* @return (false,true) if (is not, is) an NTNameValue.
*/
static bool isNTNameValue(PVStructurePtr const & pvStructure);
static bool is_a(epics::pvData::StructureConstPtr const & structure);
/**
* Create an NTNameValue pvStructure.
* @param hasFunction Create a PVString field named function.
* @param hasTimeStamp Create a timeStamp structure field.
* @param hasAlarm Create an alarm structure field.
* @return NTNameValuePtr
* Create a NTNameValue builder instance.
* @return builder instance.
*/
static NTNameValuePtr create(
bool hasFunction,bool hasTimeStamp, bool hasAlarm);
static NTNameValuePtr create(
PVStructurePtr const & pvStructure);
static NTNameValueBuilderPtr createBuilder();
/**
* Destructor
* Destructor.
*/
~NTNameValue() {}
/**
* Get the function field.
* @return The pvString or null if no function field.
*/
PVStringPtr getFunction() {return pvFunction;}
/**
* Attach a pvTimeStamp.
* @param pvTimeStamp The pvTimeStamp that will be attached.
* Does nothing if no timeStamp
*/
void attachTimeStamp(PVTimeStamp &pvTimeStamp);
/**
* Attach a pvTimeStamp.
* @param pvTimeStamp The pvTimeStamp that will be attached.
* Does nothing if no timeStamp.
* @return true if the operation was successfull (i.e. this instance has a timeStamp field), otherwise false.
*/
bool attachTimeStamp(epics::pvData::PVTimeStamp &pvTimeStamp) const;
/**
* Attach an pvAlarm.
* @param pvAlarm The pvAlarm that will be attached.
* Does nothing if no alarm
* Does nothing if no alarm.
* @return true if the operation was successfull (i.e. this instance has a timeStamp field), otherwise false.
*/
void attachAlarm(PVAlarm &pvAlarm);
bool attachAlarm(epics::pvData::PVAlarm &pvAlarm) const;
/**
* Get the pvStructure.
* @return PVStructurePtr.
*/
PVStructurePtr getPVStructure(){return pvNTNameValue;}
epics::pvData::PVStructurePtr getPVStructure() const;
/**
* Get the descriptor field.
* @return The pvString or null if no function field.
*/
epics::pvData::PVStringPtr getDescriptor() const;
/**
* Get the timeStamp.
* @return PVStructurePtr which may be null.
*/
PVStructurePtr getTimeStamp(){return pvTimeStamp;}
epics::pvData::PVStructurePtr getTimeStamp() const;
/**
* Get the alarm.
* @return PVStructurePtr which may be null.
*/
PVStructurePtr getAlarm() {return pvAlarm;}
epics::pvData::PVStructurePtr getAlarm() const;
/**
* Get the string array on names.
* @return The array of names.
* Get the names array field.
* @return The PVStringArray for the names.
*/
PVStringArrayPtr getNames() {return pvNames;}
epics::pvData::PVStringArrayPtr getNames() const;
/**
* Get the string array on values.
* @return The array of values.
* Get the value array field.
* @return The PVField for the values.
*/
PVStringArrayPtr getValues() {return pvValues;}
epics::pvData::PVFieldPtr getValues() const;
/**
* Get the value array field of a specified type (e.g. PVDoubleArray).
* @return The <PVT> array for the values.
*/
template<typename PVT>
std::tr1::shared_ptr<PVT> getValues() const
{
epics::pvData::PVFieldPtr pvField = getValues();
if (pvField.get())
return std::tr1::dynamic_pointer_cast<PVT>(pvField);
else
return std::tr1::shared_ptr<PVT>();
}
private:
NTNameValue(PVStructurePtr const & pvStructure);
PVStructurePtr pvNTNameValue;
PVStringPtr pvFunction;
PVStructurePtr pvTimeStamp;
PVStructurePtr pvAlarm;
PVStringArrayPtr pvNames;
PVStringArrayPtr pvValues;
NTNameValue(epics::pvData::PVStructurePtr const & pvStructure);
epics::pvData::PVStructurePtr pvNTNameValue;
friend class detail::NTNameValueBuilder;
};
}}