This is Unofficial EPICS BASE Doxygen Site
ntmatrix.cpp
Go to the documentation of this file.
1 /* ntmatrix.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  */
6 
7 #include "validator.h"
8 
9 #define epicsExportSharedSymbols
10 #include <pv/ntmatrix.h>
11 #include <pv/ntutils.h>
12 
13 using namespace std;
14 using namespace epics::pvData;
15 
16 namespace epics { namespace nt {
17 
18 static NTFieldPtr ntField = NTField::get();
19 
20 namespace detail {
21 
22 StructureConstPtr NTMatrixBuilder::createStructure()
23 {
24  FieldBuilderPtr builder =
25  getFieldCreate()->createFieldBuilder()->
26  setId(NTMatrix::URI)->
27  addArray("value", pvDouble);
28 
29  if (dim)
30  builder->addArray("dim", pvInt);
31 
32  if (descriptor)
33  builder->add("descriptor", pvString);
34 
35  if (alarm)
36  builder->add("alarm", ntField->createAlarm());
37 
38  if (timeStamp)
39  builder->add("timeStamp", ntField->createTimeStamp());
40 
41  if (display)
42  builder->add("display", ntField->createDisplay());
43 
44  size_t extraCount = extraFieldNames.size();
45  for (size_t i = 0; i< extraCount; i++)
46  builder->add(extraFieldNames[i], extraFields[i]);
47 
48 
49  StructureConstPtr s = builder->createStructure();
50 
51  reset();
52  return s;
53 }
54 
55 NTMatrixBuilder::shared_pointer NTMatrixBuilder::addDim()
56 {
57  dim = true;
58  return shared_from_this();
59 }
60 
61 NTMatrixBuilder::shared_pointer NTMatrixBuilder::addDescriptor()
62 {
63  descriptor = true;
64  return shared_from_this();
65 }
66 
67 NTMatrixBuilder::shared_pointer NTMatrixBuilder::addAlarm()
68 {
69  alarm = true;
70  return shared_from_this();
71 }
72 
73 NTMatrixBuilder::shared_pointer NTMatrixBuilder::addTimeStamp()
74 {
75  timeStamp = true;
76  return shared_from_this();
77 }
78 
79 NTMatrixBuilder::shared_pointer NTMatrixBuilder::addDisplay()
80 {
81  display = true;
82  return shared_from_this();
83 }
84 
85 PVStructurePtr NTMatrixBuilder::createPVStructure()
86 {
87  return getPVDataCreate()->createPVStructure(createStructure());
88 }
89 
90 NTMatrixPtr NTMatrixBuilder::create()
91 {
92  return NTMatrixPtr(new NTMatrix(createPVStructure()));
93 }
94 
95 NTMatrixBuilder::NTMatrixBuilder()
96 {
97  reset();
98 }
99 
100 void NTMatrixBuilder::reset()
101 {
102  dim = false;
103  descriptor = false;
104  alarm = false;
105  timeStamp = false;
106  display = false;
107  extraFieldNames.clear();
108  extraFields.clear();
109 }
110 
111 NTMatrixBuilder::shared_pointer NTMatrixBuilder::add(string const & name, FieldConstPtr const & field)
112 {
113  extraFields.push_back(field); extraFieldNames.push_back(name);
114  return shared_from_this();
115 }
116 
117 }
118 
119 const std::string NTMatrix::URI("epics:nt/NTMatrix:1.0");
120 
121 NTMatrix::shared_pointer NTMatrix::wrap(PVStructurePtr const & pvStructure)
122 {
123  if(!isCompatible(pvStructure)) return shared_pointer();
124  return wrapUnsafe(pvStructure);
125 }
126 
127 NTMatrix::shared_pointer NTMatrix::wrapUnsafe(PVStructurePtr const & pvStructure)
128 {
129  return shared_pointer(new NTMatrix(pvStructure));
130 }
131 
132 bool NTMatrix::is_a(StructureConstPtr const & structure)
133 {
134  return NTUtils::is_a(structure->getID(), URI);
135 }
136 
137 bool NTMatrix::is_a(PVStructurePtr const & pvStructure)
138 {
139  return is_a(pvStructure->getStructure());
140 }
141 
142 bool NTMatrix::isCompatible(StructureConstPtr const & structure)
143 {
144  if (!structure)
145  return false;
146 
147  Result result(structure);
148 
149  return result
150  .is<Structure>()
151  .has<ScalarArray>("value")
152  .maybeHas<ScalarArray>("dim")
153  .maybeHas<Scalar>("descriptor")
154  .maybeHas<&NTField::isAlarm, Structure>("alarm")
155  .maybeHas<&NTField::isTimeStamp, Structure>("timeStamp")
156  .maybeHas<&NTField::isDisplay, Structure>("display")
157  .valid();
158 }
159 
160 bool NTMatrix::isCompatible(PVStructurePtr const & pvStructure)
161 {
162  if(!pvStructure) return false;
163 
164  return isCompatible(pvStructure->getStructure());
165 }
166 
167 bool NTMatrix::isValid()
168 {
169  int valueLength = getValue()->getLength();
170  if (valueLength == 0)
171  return false;
172 
173  PVIntArrayPtr pvDim = getDim();
174  if (pvDim.get())
175  {
176  int length = pvDim->getLength();
177  if (length != 1 && length !=2)
178  return false;
179 
180  PVIntArray::const_svector data = pvDim->view();
181  int expectedLength = 1;
182  for (PVIntArray::const_svector::const_iterator it = data.begin();
183  it != data.end(); ++it)
184  {
185  expectedLength *= *it;
186  }
187  if (expectedLength != valueLength)
188  return false;
189  }
190  return true;
191 }
192 
193 NTMatrixBuilderPtr NTMatrix::createBuilder()
194 {
196 }
197 
198 bool NTMatrix::attachTimeStamp(PVTimeStamp &pvTimeStamp) const
199 {
200  PVStructurePtr ts = getTimeStamp();
201  if (ts)
202  return pvTimeStamp.attach(ts);
203  else
204  return false;
205 }
206 
207 bool NTMatrix::attachAlarm(PVAlarm &pvAlarm) const
208 {
209  PVStructurePtr al = getAlarm();
210  if (al)
211  return pvAlarm.attach(al);
212  else
213  return false;
214 }
215 
216 bool NTMatrix::attachDisplay(PVDisplay &pvDisplay) const
217 {
218  PVStructurePtr dp = getDisplay();
219  if (dp)
220  return pvDisplay.attach(dp);
221  else
222  return false;
223 }
224 
225 PVStructurePtr NTMatrix::getPVStructure() const
226 {
227  return pvNTMatrix;
228 }
229 
230 PVStringPtr NTMatrix::getDescriptor() const
231 {
232  return pvNTMatrix->getSubField<PVString>("descriptor");
233 }
234 
235 PVStructurePtr NTMatrix::getTimeStamp() const
236 {
237  return pvNTMatrix->getSubField<PVStructure>("timeStamp");
238 }
239 
240 PVStructurePtr NTMatrix::getAlarm() const
241 {
242  return pvNTMatrix->getSubField<PVStructure>("alarm");
243 }
244 
245 PVStructurePtr NTMatrix::getDisplay() const
246 {
247  return pvNTMatrix->getSubField<PVStructure>("display");
248 }
249 
250 PVDoubleArrayPtr NTMatrix::getValue() const
251 {
252  return pvValue;
253 }
254 
255 PVIntArrayPtr NTMatrix::getDim() const
256 {
257  return pvNTMatrix->getSubField<PVIntArray>("dim");
258 }
259 
260 NTMatrix::NTMatrix(PVStructurePtr const & pvStructure) :
261  pvNTMatrix(pvStructure),
262  pvValue(pvNTMatrix->getSubField<PVDoubleArray>("value"))
263 {}
264 
265 
266 }}
FORCE_INLINE std::tr1::shared_ptr< PVField > getSubField(A a)
Definition: pvData.h:744
bool attach(PVFieldPtr const &pvField)
Definition: pvDisplay.cpp:26
pvac::PutEvent result
Definition: clientSync.cpp:117
std::tr1::shared_ptr< PVIntArray > PVIntArrayPtr
Definition: pvData.h:1440
Result & is(void)
Definition: validator.h:89
Interface for in-line creating of NTMatrix.
Definition: ntmatrix.h:40
int i
Definition: scan.c:967
Convenience Class for NTMatrix.
Definition: ntmatrix.h:134
bool attach(PVFieldPtr const &pvField)
Definition: pvAlarm.cpp:26
Validation methods for NT types.
Definition: validator.h:23
Definition: memory.hpp:41
TODO only here because of the Lockable.
Definition: ntaggregate.cpp:16
std::tr1::shared_ptr< const Structure > StructureConstPtr
Definition: pvIntrospect.h:162
std::tr1::shared_ptr< NTField > NTFieldPtr
Definition: ntfield.h:35
std::tr1::shared_ptr< FieldBuilder > FieldBuilderPtr
PVString is special case, since it implements SerializableArray.
Definition: pvData.h:521
std::tr1::shared_ptr< NTMatrix > NTMatrixPtr
Definition: ntmatrix.h:28
pvData
Definition: monitor.h:428
This class implements introspection object for a structure.
Definition: pvIntrospect.h:697
template class for all extensions of PVArray.
Definition: pvData.h:55
Methods for accessing a timeStamp structure.
Definition: pvTimeStamp.h:38
Methods for accessing an alarm structure.
Definition: pvAlarm.h:37
Data interface for a structure,.
Definition: pvData.h:712
std::tr1::shared_ptr< const Field > FieldConstPtr
Definition: pvIntrospect.h:137
FORCE_INLINE const FieldCreatePtr & getFieldCreate()
This class implements introspection object for scalar array.
Definition: pvIntrospect.h:497
std::tr1::shared_ptr< PVStructure > PVStructurePtr
Definition: pvData.h:87
std::tr1::shared_ptr< PVString > PVStringPtr
Definition: pvData.h:540
std::tr1::shared_ptr< detail::NTMatrixBuilder > NTMatrixBuilderPtr
Definition: ntmatrix.h:125
Definition: caget.c:48
Methods for accessing an display structure.
Definition: pvDisplay.h:39
std::tr1::shared_ptr< PVDoubleArray > PVDoubleArrayPtr
Definition: pvData.h:1461
FORCE_INLINE const PVDataCreatePtr & getPVDataCreate()
Definition: pvData.h:1648
bool attach(PVFieldPtr const &pvField)
Definition: pvTimeStamp.cpp:26