This is Unofficial EPICS BASE Doxygen Site
nturi.cpp
Go to the documentation of this file.
1 /* nturi.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 <algorithm>
8 #include "validator.h"
9 
10 #define epicsExportSharedSymbols
11 #include <pv/nturi.h>
12 #include <pv/ntutils.h>
13 
14 using namespace std;
15 using namespace epics::pvData;
16 
17 namespace epics { namespace nt {
18 
19 static NTFieldPtr ntField = NTField::get();
20 
21 namespace detail {
22 
23 
24 NTURIBuilder::shared_pointer NTURIBuilder::addQueryString(std::string const & name)
25 {
26  if (std::find(queryFieldNames.begin(), queryFieldNames.end(), name) != queryFieldNames.end())
27  throw std::runtime_error("duplicate query field name");
28 
29  queryFieldNames.push_back(name);
30  queryTypes.push_back(pvString);
31 
32  return shared_from_this();
33 }
34 
35 NTURIBuilder::shared_pointer NTURIBuilder::addQueryDouble(std::string const & name)
36 {
37  if (std::find(queryFieldNames.begin(), queryFieldNames.end(), name) != queryFieldNames.end())
38  throw std::runtime_error("duplicate query field name");
39 
40  queryFieldNames.push_back(name);
41  queryTypes.push_back(pvDouble);
42 
43  return shared_from_this();
44 }
45 
46 NTURIBuilder::shared_pointer NTURIBuilder::addQueryInt(std::string const & name)
47 {
48  if (std::find(queryFieldNames.begin(), queryFieldNames.end(), name) != queryFieldNames.end())
49  throw std::runtime_error("duplicate query field name");
50 
51  queryFieldNames.push_back(name);
52  queryTypes.push_back(pvInt);
53 
54  return shared_from_this();
55 }
56 
57 StructureConstPtr NTURIBuilder::createStructure()
58 {
59  FieldBuilderPtr builder = getFieldCreate()->
60  createFieldBuilder()->
61  setId(NTURI::URI)->
62  add("scheme", pvString);
63 
64  if (authority)
65  builder->add("authority", pvString);
66 
67  builder->add("path", pvString);
68 
69  if (!queryFieldNames.empty())
70  {
71  FieldBuilderPtr nestedBuilder = builder->
72  addNestedStructure("query");
73 
74  vector<string>::size_type len = queryFieldNames.size();
75  for (vector<string>::size_type i = 0; i < len; i++)
76  nestedBuilder->add(queryFieldNames[i], queryTypes[i]);
77 
78  builder = nestedBuilder->endNested();
79  }
80 
81  size_t extraCount = extraFieldNames.size();
82  for (size_t i = 0; i< extraCount; i++)
83  builder->add(extraFieldNames[i], extraFields[i]);
84 
85  StructureConstPtr s = builder->createStructure();
86 
87  reset();
88  return s;
89 }
90 
91 NTURIBuilder::shared_pointer NTURIBuilder::addAuthority()
92 {
93  authority = true;
94  return shared_from_this();
95 }
96 
97 PVStructurePtr NTURIBuilder::createPVStructure()
98 {
99  return getPVDataCreate()->createPVStructure(createStructure());
100 }
101 
102 NTURIPtr NTURIBuilder::create()
103 {
104  return NTURIPtr(new NTURI(createPVStructure()));
105 }
106 
107 NTURIBuilder::NTURIBuilder()
108 {
109  reset();
110 }
111 
112 void NTURIBuilder::reset()
113 {
114  queryFieldNames.clear();
115  queryTypes.clear();
116  authority = false;
117 }
118 
119 NTURIBuilder::shared_pointer NTURIBuilder::add(string const & name, FieldConstPtr const & field)
120 {
121  extraFields.push_back(field);
122  extraFieldNames.push_back(name);
123  return shared_from_this();
124 }
125 
126 }
127 
128 const std::string NTURI::URI("epics:nt/NTURI:1.0");
129 
130 NTURI::shared_pointer NTURI::wrap(PVStructurePtr const & pvStructure)
131 {
132  if(!isCompatible(pvStructure)) return shared_pointer();
133  return wrapUnsafe(pvStructure);
134 }
135 
136 NTURI::shared_pointer NTURI::wrapUnsafe(PVStructurePtr const & pvStructure)
137 {
138  return shared_pointer(new NTURI(pvStructure));
139 }
140 
141 bool NTURI::is_a(StructureConstPtr const & structure)
142 {
143  return NTUtils::is_a(structure->getID(), URI);
144 }
145 
146 bool NTURI::is_a(PVStructurePtr const & pvStructure)
147 {
148  return is_a(pvStructure->getStructure());
149 }
150 
151 bool NTURI::isCompatible(StructureConstPtr const & structure)
152 {
153  if (!structure)
154  return false;
155 
156  Result result(structure);
157 
158  result
159  .is<Structure>()
160  .has<Scalar>("scheme")
161  .has<Scalar>("path")
162  .maybeHas<Scalar>("authority")
163  .maybeHas<Structure>("query");
164 
165  StructureConstPtr query(structure->getField<Structure>("query"));
166  if (query) {
167  Result r(query);
168  StringArray const & names(query->getFieldNames());
169  StringArray::const_iterator it;
170 
171  for (it = names.begin(); it != names.end(); ++it)
172  r.has<ScalarArray>(*it);
173 
174  result |= r;
175  }
176 
177  return result.valid();
178 }
179 
180 
181 bool NTURI::isCompatible(PVStructurePtr const & pvStructure)
182 {
183  if(!pvStructure) return false;
184 
185  return isCompatible(pvStructure->getStructure());
186 }
187 
188 bool NTURI::isValid()
189 {
190  return true;
191 }
192 
193 NTURIBuilderPtr NTURI::createBuilder()
194 {
196 }
197 
198 
199 PVStructurePtr NTURI::getPVStructure() const
200 {
201  return pvNTURI;
202 }
203 
204 
205 PVStringPtr NTURI::getScheme() const
206 {
207  return pvNTURI->getSubField<PVString>("scheme");
208 }
209 
210 PVStringPtr NTURI::getAuthority() const
211 {
212  return pvNTURI->getSubField<PVString>("authority");
213 }
214 
215 PVStringPtr NTURI::getPath() const
216 {
217  return pvNTURI->getSubField<PVString>("path");
218 }
219 
220 PVStructurePtr NTURI::getQuery() const
221 {
222  return pvNTURI->getSubField<PVStructure>("query");
223 }
224 
225 StringArray const & NTURI::getQueryNames() const
226 {
227  return pvNTURI->getSubField<PVStructure>("query")->getStructure()->getFieldNames();
228 }
229 
230 PVFieldPtr NTURI::getQueryField(std::string const & name) const
231 {
232  return pvNTURI->getSubField("query." + name);
233 }
234 
235 NTURI::NTURI(PVStructurePtr const & pvStructure) :
236  pvNTURI(pvStructure)
237 {}
238 
239 
240 }}
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
Convenience Class for NTURI.
Definition: nturi.h:136
pvac::PutEvent result
Definition: clientSync.cpp:117
Result & is(void)
Definition: validator.h:89
int i
Definition: scan.c:967
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
Interface for in-line creating of NTURI.
Definition: nturi.h:40
std::tr1::shared_ptr< NTURI > NTURIPtr
Definition: nturi.h:28
PVString is special case, since it implements SerializableArray.
Definition: pvData.h:521
pvData
Definition: monitor.h:428
This class implements introspection object for a structure.
Definition: pvIntrospect.h:697
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
Result & has(const std::string &name)
Definition: validator.h:132
std::tr1::shared_ptr< PVString > PVStringPtr
Definition: pvData.h:540
bool valid(void) const
Definition: validator.h:77
std::vector< std::string > StringArray
Definition: pvType.h:110
std::tr1::shared_ptr< PVField > PVFieldPtr
Definition: pvData.h:66
Definition: caget.c:48
std::tr1::shared_ptr< detail::NTURIBuilder > NTURIBuilderPtr
Definition: nturi.h:127
FORCE_INLINE const PVDataCreatePtr & getPVDataCreate()
Definition: pvData.h:1648