Files
pvData/pvDataApp/property/pvEnumerated.cpp
Michael Davidsaver 629c8346d2 postPut in new array API
Call when appropriate (putFrom(), copyIn(), and replace()).
Not called by swap(), take(), reuse(), or shareData().
Users of the second set of methods are expected to call
one of the methods in the first set, or call postPut() directly.

Document when postPut is (not) called.
2013-05-23 17:51:52 -04:00

115 lines
2.8 KiB
C++

/* pvEnumerated.cpp */
/**
* Copyright - See the COPYRIGHT that is included with this distribution.
* EPICS pvData is distributed subject to a Software License Agreement found
* in file LICENSE that is included with this distribution.
*/
/**
* @author mrk
*/
#include <string>
#include <stdexcept>
#include <pv/pvType.h>
#include <pv/pvIntrospect.h>
#include <pv/pvData.h>
#include <pv/pvEnumerated.h>
namespace epics { namespace pvData {
using std::tr1::static_pointer_cast;
String PVEnumerated::notFound("No enumerated structure found");
String PVEnumerated::notAttached("Not attached to an enumerated structure");
bool PVEnumerated::attach(PVFieldPtr const & pvField)
{
if(pvField->getField()->getType()!=structure) {
pvField->message(notFound,errorMessage);
return false;
}
PVStructurePtr pvStructure = static_pointer_cast<PVStructure>(pvField);
pvIndex = pvStructure->getIntField("index");
if(pvIndex.get()==NULL) {
pvField->message(notFound,errorMessage);
return false;
}
PVScalarArrayPtr pvScalarArray = pvStructure->getScalarArrayField(
"choices",pvString);
if(pvScalarArray.get()==NULL) {
pvIndex.reset();
pvField->message(notFound,errorMessage);
return false;
}
pvChoices = static_pointer_cast<PVStringArray>(pvScalarArray);
return true;
}
void PVEnumerated::detach()
{
pvIndex.reset();
pvChoices.reset();
}
bool PVEnumerated::isAttached() {
if(pvIndex.get()==NULL) return false;
return true;
}
bool PVEnumerated::setIndex(int32 index)
{
if(pvIndex.get()==NULL ) {
throw std::logic_error(notAttached);
}
if(pvIndex->isImmutable()) return false;
pvIndex->put(index);
return true;
}
int32 PVEnumerated::getIndex()
{
if(pvIndex.get()==NULL ) {
throw std::logic_error(notAttached);
}
return pvIndex->get();
}
String PVEnumerated::getChoice()
{
if(pvIndex.get()==NULL ) {
throw std::logic_error(notAttached);
}
int index = pvIndex->get();
const PVStringArray::svector& data(pvChoices->viewUnsafe());
return data[index];
}
bool PVEnumerated::choicesMutable()
{
if(pvIndex.get()==NULL ) {
throw std::logic_error(notAttached);
}
return pvChoices->isImmutable();
}
int32 PVEnumerated::getNumberChoices()
{
if(pvIndex.get()==NULL ) {
throw std::logic_error(notAttached);
}
return pvChoices->getLength();
}
bool PVEnumerated:: setChoices(const StringArray & choices)
{
if(pvIndex.get()==NULL ) {
throw std::logic_error(notAttached);
}
if(pvChoices->isImmutable()) return false;
PVStringArray::svector data(choices.size());
std::copy(choices.begin(), choices.end(), data.begin());
pvChoices->replace(data);
return true;
}
}}