JointXMLnode.h
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014 Jose Luis Blanco Claraco (University of Almeria) |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under GNU General Public License version 3 |
7  | See <http://www.gnu.org/licenses/> |
8  +-------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <rapidxml.hpp>
12 #include <stdexcept>
13 
14 namespace mvsim
15 {
17 template <typename Ch = char>
19 {
20  public:
21  typedef std::vector<const rapidxml::xml_node<Ch>*> TListNodes;
22 
23  private:
24  TListNodes m_nodes;
25 
26  public:
27  void add(const rapidxml::xml_node<Ch>* node) { m_nodes.push_back(node); }
28  const rapidxml::xml_node<Ch>* first_node(const char* name)
29  {
30  const rapidxml::xml_node<Ch>* ret = NULL;
31  for (typename TListNodes::const_iterator it = m_nodes.begin();
32  it != m_nodes.end(); ++it)
33  {
34  ret = (*it)->first_node(name);
35  if (ret != NULL) return ret;
36  }
37  return ret;
38  }
39 
40  TListNodes& getListOfNodes() { return m_nodes; }
41  // Iterators-like interface ----------------------
42  class iterator
43  {
44  public:
45  template <typename Ch_>
46  friend class JointXMLnode;
47 
48  // ++it
50  {
51  if (!current)
52  throw std::runtime_error("++ called on end() iterator!?");
53  current = current->next_sibling();
54  JointXMLnode<Ch>::TListNodes& lst = parent.getListOfNodes();
55  while (!current && lst_idx < lst.size())
56  {
57  lst_idx++;
58  if (lst_idx < lst.size())
59  current = lst[lst_idx]->first_node();
60  else
61  current = NULL;
62  }
63  return *this;
64  }
65 
67  {
68  if (!current)
69  throw std::runtime_error("-> called on end() iterator!?");
70  return current;
71  }
72 
74  {
75  if (!current)
76  throw std::runtime_error("* called on end() iterator!?");
77  return current;
78  }
79 
80  bool operator==(const iterator& it) const
81  {
82  return (this->current == it.current) &&
83  (this->lst_idx == it.lst_idx) &&
84  (&this->parent == &it.parent);
85  }
86  bool operator!=(const iterator& it) const { return !(*this == it); }
87  private:
88  // begin():
90  {
91  JointXMLnode<Ch>::TListNodes& lst = parent.getListOfNodes();
92  while (!current && lst_idx < lst.size())
93  {
94  current = lst[lst_idx]->first_node();
95  if (!current) lst_idx++;
96  }
97  }
98  // end()
99  iterator(JointXMLnode<Ch>& pa, size_t idx)
100  : parent(pa), lst_idx(idx), current(NULL)
101  {
102  }
103 
105  size_t lst_idx; // => lst.size() means this is "end()"
107 
108  }; // end class iterator
109 
110  iterator begin() { return iterator(*this); }
111  iterator end() { return iterator(*this, m_nodes.size()); }
112 };
113 }
This file contains rapidxml parser and DOM implementation.
iterator(JointXMLnode< Ch > &pa)
Definition: JointXMLnode.h:89
rapidxml::xml_node< Ch > * operator*() const
Definition: JointXMLnode.h:73
std::vector< const rapidxml::xml_node< Ch > * > TListNodes
Definition: JointXMLnode.h:21
xml_node< Ch > * first_node(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const
Definition: rapidxml.hpp:936
rapidxml::xml_node< Ch > * operator->() const
Definition: JointXMLnode.h:66
TListNodes m_nodes
Definition: JointXMLnode.h:24
const rapidxml::xml_node< Ch > * first_node(const char *name)
Definition: JointXMLnode.h:28
TListNodes & getListOfNodes()
Definition: JointXMLnode.h:40
rapidxml::xml_node< Ch > * current
Definition: JointXMLnode.h:106
iterator(JointXMLnode< Ch > &pa, size_t idx)
Definition: JointXMLnode.h:99
bool operator!=(const iterator &it) const
Definition: JointXMLnode.h:86
JointXMLnode< Ch > & parent
Definition: JointXMLnode.h:104
void add(const rapidxml::xml_node< Ch > *node)
Definition: JointXMLnode.h:27
bool operator==(const iterator &it) const
Definition: JointXMLnode.h:80


mvsim
Author(s):
autogenerated on Thu Jun 6 2019 19:36:40