This is Unofficial EPICS BASE Doxygen Site
pvEnumerated.cpp
Go to the documentation of this file.
1 /* pvEnumerated.cpp */
2 /*
3  * Copyright information and license terms for this software can be
4  * found in the file LICENSE that is included with the distribution
5  */
9 #include <string>
10 #include <stdexcept>
11 
12 #define epicsExportSharedSymbols
13 #include <pv/pvType.h>
14 #include <pv/pvIntrospect.h>
15 #include <pv/pvData.h>
16 #include <pv/pvEnumerated.h>
17 
19 using std::string;
20 
21 namespace epics { namespace pvData {
22 
23 string PVEnumerated::notFound("No enumerated structure found");
24 string PVEnumerated::notAttached("Not attached to an enumerated structure");
25 
26 bool PVEnumerated::attach(PVFieldPtr const & pvField)
27 {
28  if(pvField->getField()->getType()!=structure) return false;
29  PVStructurePtr pvStructure = static_pointer_cast<PVStructure>(pvField);
30  pvIndex = pvStructure->getSubField<PVInt>("index");
31  if(pvIndex.get()==NULL) return false;
32  PVStringArrayPtr pvStringArray = pvStructure->getSubField<PVStringArray>("choices");
33  if(pvStringArray.get()==NULL) {
34  pvIndex.reset();
35  return false;
36  }
37  pvChoices = pvStringArray;
38  return true;
39 }
40 
42 {
43  pvIndex.reset();
44  pvChoices.reset();
45 }
46 
48  if(pvIndex.get()==NULL) return false;
49  return true;
50 }
51 
53 {
54  if(pvIndex.get()==NULL ) {
55  throw std::logic_error(notAttached);
56  }
57  if(pvIndex->isImmutable()) return false;
58  pvIndex->put(index);
59  return true;
60 }
61 
63 {
64  if(pvIndex.get()==NULL ) {
65  throw std::logic_error(notAttached);
66  }
67  return pvIndex->get();
68 }
69 
71 {
72  if(pvIndex.get()==NULL ) {
73  throw std::logic_error(notAttached);
74  }
75  size_t index = pvIndex->get();
76  const PVStringArray::const_svector& data(pvChoices->view());
77  if(/*index<0 ||*/ index>=data.size()) {
78  string nullString;
79  return nullString;
80  }
81  return data[index];
82 }
83 
85 {
86  if(pvIndex.get()==NULL ) {
87  throw std::logic_error(notAttached);
88  }
89  return pvChoices->isImmutable();
90 }
91 
93 {
94  if(pvIndex.get()==NULL ) {
95  throw std::logic_error(notAttached);
96  }
97  return static_cast<int32>(pvChoices->getLength());
98 }
99 
101 {
102  if(pvIndex.get()==NULL ) {
103  throw std::logic_error(notAttached);
104  }
105  if(pvChoices->isImmutable()) return false;
106  PVStringArray::svector data(choices.size());
107  std::copy(choices.begin(), choices.end(), data.begin());
108  pvChoices->replace(freeze(data));
109  return true;
110 }
111 
112 
113 }}
bool setChoices(const StringArray &choices)
shared_ptr< T > static_pointer_cast(shared_ptr< U > const &r) BOOST_NOEXCEPT
Definition: shared_ptr.hpp:788
bool attach(PVFieldPtr const &pvField)
TODO only here because of the Lockable.
Definition: ntaggregate.cpp:16
#define NULL
Definition: catime.c:38
std::tr1::shared_ptr< PVStringArray > PVStringArrayPtr
Definition: pvData.h:1464
void copy(PVValueArray< T > &pvFrom, size_t fromOffset, size_t fromStride, PVValueArray< T > &pvTo, size_t toOffset, size_t toStride, size_t count)
Copy a subarray from one scalar array to another.
template class for all extensions of PVArray.
Definition: pvData.h:55
bool setIndex(int32 index)
Data interface for a structure,.
Definition: pvData.h:712
std::tr1::shared_ptr< PVStructure > PVStructurePtr
Definition: pvData.h:87
Class that holds the data for each possible scalar type.
Definition: pvData.h:54
std::vector< std::string > StringArray
Definition: pvType.h:110
std::tr1::shared_ptr< PVField > PVFieldPtr
Definition: pvData.h:66
int32_t int32
Definition: pvType.h:83