This is Unofficial EPICS BASE Doxygen Site
ntattribute.cpp
Go to the documentation of this file.
1 /* ntattribute.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/ntattribute.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 
23 StructureConstPtr NTAttributeBuilder::createStructure()
24 {
25  FieldBuilderPtr builder =
26  getFieldCreate()->createFieldBuilder()->
27  setId(NTAttribute::URI)->
28  add("name", pvString)->
29  add("value", getFieldCreate()->createVariantUnion());
30 
31  if (tags)
32  builder->addArray("tags", pvString);
33 
34  if (descriptor)
35  builder->add("descriptor", pvString);
36 
37  if (alarm)
38  builder->add("alarm", ntField->createAlarm());
39 
40  if (timeStamp)
41  builder->add("timeStamp", ntField->createTimeStamp());
42 
43  size_t extraCount = extraFieldNames.size();
44  for (size_t i = 0; i< extraCount; i++)
45  builder->add(extraFieldNames[i], extraFields[i]);
46 
47 
48  StructureConstPtr s = builder->createStructure();
49 
50  reset();
51  return s;
52 }
53 
54 NTAttributeBuilder::shared_pointer NTAttributeBuilder::addTags()
55 {
56  tags = true;
57  return shared_from_this();
58 }
59 
60 NTAttributeBuilder::shared_pointer NTAttributeBuilder::addDescriptor()
61 {
62  descriptor = true;
63  return shared_from_this();
64 }
65 
66 NTAttributeBuilder::shared_pointer NTAttributeBuilder::addAlarm()
67 {
68  alarm = true;
69  return shared_from_this();
70 }
71 
72 NTAttributeBuilder::shared_pointer NTAttributeBuilder::addTimeStamp()
73 {
74  timeStamp = true;
75  return shared_from_this();
76 }
77 
78 PVStructurePtr NTAttributeBuilder::createPVStructure()
79 {
80  return getPVDataCreate()->createPVStructure(createStructure());
81 }
82 
83 NTAttributePtr NTAttributeBuilder::create()
84 {
85  return NTAttributePtr(new NTAttribute(createPVStructure()));
86 }
87 
88 NTAttributeBuilder::NTAttributeBuilder()
89 {
90  reset();
91 }
92 
93 void NTAttributeBuilder::reset()
94 {
95  descriptor = false;
96  alarm = false;
97  timeStamp = false;
98  extraFieldNames.clear();
99  extraFields.clear();
100 }
101 
102 NTAttributeBuilder::shared_pointer NTAttributeBuilder::add(string const & name, FieldConstPtr const & field)
103 {
104  extraFields.push_back(field); extraFieldNames.push_back(name);
105  return shared_from_this();
106 }
107 
108 }
109 
110 const std::string NTAttribute::URI("epics:nt/NTAttribute:1.0");
111 
112 NTAttribute::shared_pointer NTAttribute::wrap(PVStructurePtr const & pvStructure)
113 {
114  if(!isCompatible(pvStructure)) return shared_pointer();
115  return wrapUnsafe(pvStructure);
116 }
117 
118 NTAttribute::shared_pointer NTAttribute::wrapUnsafe(PVStructurePtr const & pvStructure)
119 {
120  return shared_pointer(new NTAttribute(pvStructure));
121 }
122 
123 bool NTAttribute::is_a(StructureConstPtr const & structure)
124 {
125  return NTUtils::is_a(structure->getID(), URI);
126 }
127 
128 bool NTAttribute::is_a(PVStructurePtr const & pvStructure)
129 {
130  return is_a(pvStructure->getStructure());
131 }
132 
133 bool NTAttribute::isCompatible(StructureConstPtr const & structure)
134 {
135  if (!structure)
136  return false;
137 
138  Result result(structure);
139 
140  return result
141  .is<Structure>()
142  .has<Scalar>("name")
143  .has<Union>("value")
144  .maybeHas<ScalarArray>("tags")
145  .maybeHas<Scalar>("descriptor")
146  .maybeHas<&NTField::isAlarm, Structure>("alarm")
147  .maybeHas<&NTField::isTimeStamp, Structure>("timeStamp")
148  .valid();
149 }
150 
151 bool NTAttribute::isCompatible(PVStructurePtr const & pvStructure)
152 {
153  if(!pvStructure) return false;
154 
155  return isCompatible(pvStructure->getStructure());
156 }
157 
158 bool NTAttribute::isValid()
159 {
160  return true;
161 }
162 
163 NTAttributeBuilderPtr NTAttribute::createBuilder()
164 {
166 }
167 
168 bool NTAttribute::attachTimeStamp(PVTimeStamp &pvTimeStamp) const
169 {
170  PVStructurePtr ts = getTimeStamp();
171  if (ts)
172  return pvTimeStamp.attach(ts);
173  else
174  return false;
175 }
176 
177 bool NTAttribute::attachAlarm(PVAlarm &pvAlarm) const
178 {
179  PVStructurePtr al = getAlarm();
180  if (al)
181  return pvAlarm.attach(al);
182  else
183  return false;
184 }
185 
186 PVStructurePtr NTAttribute::getPVStructure() const
187 {
188  return pvNTAttribute;
189 }
190 
191 PVStringPtr NTAttribute::getDescriptor() const
192 {
193  return pvNTAttribute->getSubField<PVString>("descriptor");
194 }
195 
196 PVStructurePtr NTAttribute::getTimeStamp() const
197 {
198  return pvNTAttribute->getSubField<PVStructure>("timeStamp");
199 }
200 
201 PVStructurePtr NTAttribute::getAlarm() const
202 {
203  return pvNTAttribute->getSubField<PVStructure>("alarm");
204 }
205 
206 
207 PVStringPtr NTAttribute::getName() const
208 {
209  return pvNTAttribute->getSubField<PVString>("name");
210 }
211 
212 PVUnionPtr NTAttribute::getValue() const
213 {
214  return pvValue;
215 }
216 
217 PVStringArrayPtr NTAttribute::getTags() const
218 {
219  return pvNTAttribute->getSubField<PVStringArray>("tags");
220 }
221 
222 NTAttribute::NTAttribute(PVStructurePtr const & pvStructure) :
223  pvNTAttribute(pvStructure), pvValue(pvNTAttribute->getSubField<PVUnion>("value"))
224 {
225 }
226 
227 
228 }}
FORCE_INLINE std::tr1::shared_ptr< PVField > getSubField(A a)
Definition: pvData.h:744
This class implements introspection object for Scalar.
Definition: pvIntrospect.h:397
This class implements introspection object for a union.
Definition: pvIntrospect.h:866
pvac::PutEvent result
Definition: clientSync.cpp:117
Result & is(void)
Definition: validator.h:89
int i
Definition: scan.c:967
Interface for in-line creating of NTAttribute.
Definition: ntattribute.h:37
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< PVStringArray > PVStringArrayPtr
Definition: pvData.h:1464
std::tr1::shared_ptr< FieldBuilder > FieldBuilderPtr
std::tr1::shared_ptr< detail::NTAttributeBuilder > NTAttributeBuilderPtr
Definition: ntattribute.h:115
PVString is special case, since it implements SerializableArray.
Definition: pvData.h:521
std::tr1::shared_ptr< PVUnion > PVUnionPtr
Definition: pvData.h:107
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
PVUnion has a single subfield.
Definition: pvData.h:940
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()
std::tr1::shared_ptr< PVStructure > PVStructurePtr
Definition: pvData.h:87
std::tr1::shared_ptr< PVString > PVStringPtr
Definition: pvData.h:540
Definition: caget.c:48
Convenience Class for NTAttribute.
Definition: ntattribute.h:124
std::tr1::shared_ptr< NTAttribute > NTAttributePtr
Definition: ntattribute.h:25
FORCE_INLINE const PVDataCreatePtr & getPVDataCreate()
Definition: pvData.h:1648
bool attach(PVFieldPtr const &pvField)
Definition: pvTimeStamp.cpp:26