Branch.h
Go to the documentation of this file.
1 #ifndef _utl_Branch_h_
2 #define _utl_Branch_h_
3 
4 #include <utl/TimeStamp.h>
5 #include <utl/TabulatedFunction.h>
6 #include <utl/AugerException.h>
7 
8 #include <xercesc/dom/DOM.hpp>
9 #include <boost/shared_ptr.hpp>
10 
11 // stuff we want to be able to GetData()
12 #include <vector>
13 #include <list>
14 #include <map>
15 
16 #include <sstream>
17 
18 
19 namespace utl {
20 
21  class Reader;
22  class Deprecator;
23 
24  class Function;
25 
26  typedef std::map<std::string, std::string> AttributeMap;
27 
28 
40  class BranchOwner {
41  public:
42  BranchOwner(xercesc::DOMDocument* const doc) : fDocument(doc) { }
43  ~BranchOwner() { if (fDocument) fDocument->release(); }
44 
45  xercesc::DOMDocument* GetDocument() const { return fDocument; }
46 
47  private:
48  BranchOwner(const BranchOwner&);
50 
51  xercesc::DOMDocument* fDocument = nullptr;
52  };
53 
54 
107  class Branch {
108 
109  public:
110  Branch() = default;
111 
112  Branch(const Branch& branch) = default;
113 
114  Branch& operator=(const Branch& b);
115 
116  private:
117  Branch(xercesc::DOMDocument* const doc, xercesc::DOMNode* const docBranch)
118  : fOwner(new BranchOwner(doc)), fDOMNode(docBranch) { }
119 
120  Branch(const boost::shared_ptr<BranchOwner>& doc, xercesc::DOMNode* const docBranch)
121  : fOwner(doc), fDOMNode(docBranch) { }
122 
123  bool HasTopBranch() const { return bool(*this) && GetDOMNode()->getOwnerDocument(); }
124 
125  Branch GetTopBranch() const;
126 
127  public:
128  Branch GetParent() const;
129 
131 
132  Branch GetFirstChild() const;
133 
135 
136  Branch GetChild(const std::string& childName) const;
137 
139 
147  Branch GetChild(const std::string& childName, const std::string& attributes) const;
148 
150 
162  Branch GetChild(const std::string& childName, AttributeMap attributeMap) const;
163 
164  unsigned int GetNChildren() const;
165 
167 
168  Branch GetNextSibling() const;
169 
172 
174 
175  Branch GetPreviousSibling() const;
176 
179 
181 
182  Branch GetSibling(const std::string& childName) const;
183 
185 
186  Branch GetSibling(const std::string& childName, const std::string& multiID) const;
187 
189 
190  Branch GetSibling(const std::string& childName, AttributeMap& attributeMap) const;
191 
193 
195  AttributeMap GetAttributes() const;
196 
198  // for cases that do not need unit conversion
199 
200  // bool
201  void GetData(bool& b) const;
202  void GetData(std::vector<bool>& b) const;
203  void GetData(std::list<bool>& b) const;
204  // string
205  void GetData(std::string& s) const;
206  void GetData(std::vector<std::string>& s) const;
207  void GetData(std::list<std::string>& s) const;
208  // char*
209  void GetData(char*& c) const;
210  // TimeStamp
211  void GetData(utl::TimeStamp& t) const;
212  void GetData(std::vector<utl::TimeStamp>& vt) const;
213  // TabulatedFunction
214  void GetData(utl::TabulatedFunction& tf) const;
215 
216  void GetData(utl::Function& tf) const;
217 
219 
220  template<typename T>
221  void GetData(T& a) const { CastData(a); a *= static_cast<T>(GetUnit()); }
222 
224 
244  template<typename T, class A, template<typename, typename> class W>
245  void
246  GetData(W<T, A>& a)
247  const
248  {
249  // a = W<T, A>(); <-- at some point we should make sure that the argument is clear'd
250  CastData(a);
251  T u = static_cast<T>(GetUnit());
252  for (auto& x : a)
253  x *= u;
254  }
255 
257  template<typename T1, typename T2>
258  void
259  GetData(std::pair<T1, T2>& p)
260  const
261  {
262  std::istringstream is(GetDataString());
263  if (is >> p.first >> std::ws >> p.second) {
264  p.first *= static_cast<T1>(GetUnit());
265  p.second *= static_cast<T2>(GetUnit());
266  } else
267  throw XMLParseException("Parsing pair failed!");
268  }
269 
270  template<typename T>
271  T Get() const { T t; GetData(t); return t; }
272 
274  Branch Clone() const;
275 
277  std::string String() const;
278 
279  void SetWarning(const std::string& wrn) { fWarning = wrn; }
280 
281  explicit operator bool() const { return bool(fOwner) && GetDOMNode(); }
282 
284  std::string GetName() const;
285 
287  std::string GetDataString() const;
288 
289  bool operator==(const Branch& b) const { return b.GetDOMNode() == GetDOMNode(); }
290 
291  bool operator!=(const Branch& b) const { return !operator==(b); }
292 
293  xercesc::DOMNode* GetDOMNode() const { return fDOMNode; }
294 
295  private:
302  template<typename T>
303  void
304  CastData(T& dataT)
305  const
306  {
307  std::istringstream is(GetDataString());
308  is >> dataT;
309  }
310 
311  template<typename T, class A, template<typename, typename> class W>
312  void
313  CastData(W<T, A>& v)
314  const
315  {
316  const std::string dataString = GetDataString();
317  std::istringstream is(dataString);
318  T value;
319  while (is >> value)
320  v.push_back(value);
321  }
322 
327  double GetUnit() const;
328  std::vector<std::string> GetListOfVariables() const;
329 
330  inline void ZeroBranchCheck() const;
331 
332  // this here to guaranteee that the pointed data is still allocated when
333  // the Reader goes out of scope.
334  boost::shared_ptr<BranchOwner> fOwner;
335 
336  mutable xercesc::DOMNode* fDOMNode = nullptr;
337 
338  std::string fWarning;
339 
340  friend class Reader;
341 
342  };
343 
344 }
345 
346 
347 #endif
Branch GetTopBranch() const
Definition: Branch.cc:63
void SetWarning(const std::string &wrn)
Definition: Branch.h:279
BranchOwner(xercesc::DOMDocument *const doc)
Definition: Branch.h:42
Branch GetParent() const
Definition: Branch.cc:79
else throw XMLParseException("Parsing pair failed!")
bool HasTopBranch() const
Definition: Branch.h:123
std::string String() const
Dump the branch into a string.
Definition: Branch.cc:593
Class to hold collection (x,y) points and provide interpolation between them.
xercesc::DOMNode * GetDOMNode() const
Definition: Branch.h:293
xercesc::DOMDocument * fDocument
Definition: Branch.h:51
std::map< std::string, std::string > AttributeMap
Definition: Branch.h:24
std::vector< std::string > GetListOfVariables() const
Get the list of variables of a function.
Definition: Branch.cc:463
Branch(xercesc::DOMDocument *const doc, xercesc::DOMNode *const docBranch)
Definition: Branch.h:117
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
Definition: Branch.cc:211
A TimeStamp holds GPS second and nanosecond for some event.
Definition: TimeStamp.h:110
T Get() const
Definition: Branch.h:271
AttributeMap GetAttributes() const
Get a map&lt;string, string&gt; containing all the attributes of this Branch.
Definition: Branch.cc:267
Branch GetNextSibling() const
Get next sibling of this branch.
Definition: Branch.cc:284
Branch & operator=(const Branch &b)
Definition: Branch.cc:494
Utility for parsing XML files.
Definition: Reader.h:25
Class representing a document branch.
Definition: Branch.h:107
bool operator==(const Branch &b) const
Definition: Branch.h:289
xercesc::DOMNode * fDOMNode
Definition: Branch.h:336
constexpr double s
Definition: AugerUnits.h:163
Class to handle memory related to Xerces.
Definition: Branch.h:40
Branch & operator--()
Syntactic sugar for for-loops.
Definition: Branch.h:178
BranchOwner & operator=(const BranchOwner &)
Branch(const boost::shared_ptr< BranchOwner > &doc, xercesc::DOMNode *const docBranch)
Definition: Branch.h:120
std::istringstream is(dataString)
is dataT
Definition: Branch.h:308
unsigned int GetNChildren() const
Definition: Branch.cc:255
void ZeroBranchCheck() const
Definition: Branch.cc:49
void GetData(bool &b) const
Overloads of the GetData member template function.
Definition: Branch.cc:644
std::string GetName() const
function to get the Branch name
Definition: Branch.cc:374
bool operator!=(const Branch &b) const
Definition: Branch.h:291
Evaluate functions given in a string. The real work is done by the ExpressionParser class...
Definition: Function.h:27
std::string GetDataString() const
function to get the data inside an element as one big string
Definition: Branch.cc:390
Branch Clone() const
returns a clone of this branch.
Definition: Branch.cc:578
Branch()=default
std::string fWarning
Definition: Branch.h:338
void GetData(T &a) const
Get data in the current branch into an atomic type.
Definition: Branch.h:221
Branch GetFirstChild() const
Get first child of this Branch.
Definition: Branch.cc:98
Branch GetPreviousSibling() const
Get previous sibling of this branch.
Definition: Branch.cc:305
Branch GetSibling(const std::string &childName) const
Get sibling by name.
Definition: Branch.cc:347
boost::shared_ptr< BranchOwner > fOwner
Definition: Branch.h:334
Branch & operator++()
Syntactic sugar for for-loops.
Definition: Branch.h:171
xercesc::DOMDocument * GetDocument() const
Definition: Branch.h:45
double GetUnit() const
Get the unit of the token.
Definition: Branch.cc:427

, generated on Tue Sep 26 2023.