JointXMLnode.h
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2020 Jose Luis Blanco Claraco |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under 3-clause BSD License |
7  | See COPYING |
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); }
29  {
30  const rapidxml::xml_node<Ch>* ret = nullptr;
31  for (const auto& node : m_nodes)
32  {
33  ret = node->first_node(name);
34  if (ret != nullptr) return ret;
35  }
36  return ret;
37  }
38 
39  TListNodes& getListOfNodes() { return m_nodes; }
40  // Iterators-like interface ----------------------
41  class iterator
42  {
43  public:
44  template <typename Ch_>
45  friend class JointXMLnode;
46 
47  // ++it
49  {
50  if (!current)
51  throw std::runtime_error("++ called on end() iterator!?");
52  current = current->next_sibling();
53  JointXMLnode<Ch>::TListNodes& lst = parent.getListOfNodes();
54  while (!current && lst_idx < lst.size())
55  {
56  lst_idx++;
57  if (lst_idx < lst.size())
58  current = lst[lst_idx]->first_node();
59  else
60  current = nullptr;
61  }
62  return *this;
63  }
64 
66  {
67  if (!current)
68  throw std::runtime_error("-> called on end() iterator!?");
69  return current;
70  }
71 
73  {
74  if (!current)
75  throw std::runtime_error("* called on end() iterator!?");
76  return current;
77  }
78 
79  bool operator==(const iterator& it) const
80  {
81  return (this->current == it.current) &&
82  (this->lst_idx == it.lst_idx) &&
83  (&this->parent == &it.parent);
84  }
85  bool operator!=(const iterator& it) const { return !(*this == it); }
86 
87  private:
88  // begin():
90  : parent(pa), lst_idx(0), current(nullptr)
91  {
92  JointXMLnode<Ch>::TListNodes& lst = parent.getListOfNodes();
93  while (!current && lst_idx < lst.size())
94  {
95  current = lst[lst_idx]->first_node();
96  if (!current) lst_idx++;
97  }
98  }
99  // end()
100  iterator(JointXMLnode<Ch>& pa, size_t idx)
101  : parent(pa), lst_idx(idx), current(nullptr)
102  {
103  }
104 
106  size_t lst_idx; // => lst.size() means this is "end()"
108 
109  }; // end class iterator
110 
111  iterator begin() { return iterator(*this); }
112  iterator end() { return iterator(*this, m_nodes.size()); }
113 };
114 } // namespace mvsim
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:72
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:65
TListNodes m_nodes
Definition: JointXMLnode.h:24
GLuint const GLchar * name
const rapidxml::xml_node< Ch > * first_node(const char *name)
Definition: JointXMLnode.h:28
TListNodes & getListOfNodes()
Definition: JointXMLnode.h:39
rapidxml::xml_node< Ch > * current
Definition: JointXMLnode.h:107
iterator(JointXMLnode< Ch > &pa, size_t idx)
Definition: JointXMLnode.h:100
bool operator!=(const iterator &it) const
Definition: JointXMLnode.h:85
JointXMLnode< Ch > & parent
Definition: JointXMLnode.h:105
void add(const rapidxml::xml_node< Ch > *node)
Definition: JointXMLnode.h:27
bool operator==(const iterator &it) const
Definition: JointXMLnode.h:79


mvsim
Author(s):
autogenerated on Fri May 7 2021 03:05:51