JointXMLnode.h
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2023 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 #include <vector>
14 
15 namespace mvsim
16 {
18 template <typename Ch>
19 class JointXMLnode
20 {
21  public:
22  using TListNodes = std::vector<const rapidxml::xml_node<Ch>*>;
23 
24  private:
26 
27  public:
28  void add(const rapidxml::xml_node<Ch>* node) { nodes_.push_back(node); }
29 
30  const rapidxml::xml_node<Ch>* first_node(const char* name) const
31  {
32  const rapidxml::xml_node<Ch>* ret = nullptr;
33  for (const auto& node : nodes_)
34  {
35  ret = node->first_node(name);
36  if (ret != nullptr) return ret;
37  }
38  return ret;
39  }
40 
42  const TListNodes& getListOfNodes() const { return nodes_; }
43 
44  // Iterators-like interface ----------------------
45  class iterator
46  {
47  public:
48  template <typename Ch_>
49  friend class JointXMLnode;
50 
51  // ++it
53  {
54  if (!current)
55  throw std::runtime_error("++ called on end() iterator!?");
56  current = current->next_sibling();
57  JointXMLnode<Ch>::TListNodes& lst = parent.getListOfNodes();
58  while (!current && lst_idx < lst.size())
59  {
60  lst_idx++;
61  if (lst_idx < lst.size())
62  current = lst[lst_idx]->first_node();
63  else
64  current = nullptr;
65  }
66  return *this;
67  }
68 
70  {
71  if (!current)
72  throw std::runtime_error("-> called on end() iterator!?");
73  return current;
74  }
75 
77  {
78  if (!current)
79  throw std::runtime_error("* called on end() iterator!?");
80  return current;
81  }
82 
83  bool operator==(const iterator& it) const
84  {
85  return (this->current == it.current) &&
86  (this->lst_idx == it.lst_idx) &&
87  (&this->parent == &it.parent);
88  }
89  bool operator!=(const iterator& it) const { return !(*this == it); }
90 
91  private:
92  // begin():
94  : parent(pa), lst_idx(0), current(nullptr)
95  {
96  JointXMLnode<Ch>::TListNodes& lst = parent.getListOfNodes();
97  while (!current && lst_idx < lst.size())
98  {
99  current = lst[lst_idx]->first_node();
100  if (!current) lst_idx++;
101  }
102  }
103  // end()
104  iterator(JointXMLnode<Ch>& pa, size_t idx)
105  : parent(pa), lst_idx(idx), current(nullptr)
106  {
107  }
108 
110  size_t lst_idx; // => lst.size() means this is "end()"
112 
113  }; // end class iterator
114 
115  iterator begin() { return iterator(*this); }
116  iterator end() { return iterator(*this, nodes_.size()); }
117 };
118 
119 } // namespace mvsim
This file contains rapidxml parser and DOM implementation.
iterator(JointXMLnode< Ch > &pa)
Definition: JointXMLnode.h:93
const rapidxml::xml_node< Ch > * first_node(const char *name) const
Definition: JointXMLnode.h:30
bool operator!=(const iterator &it) const
Definition: JointXMLnode.h:89
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:69
const TListNodes & getListOfNodes() const
Definition: JointXMLnode.h:42
std::vector< const rapidxml::xml_node< Ch > * > TListNodes
Definition: JointXMLnode.h:22
TListNodes & getListOfNodes()
Definition: JointXMLnode.h:41
rapidxml::xml_node< Ch > * current
Definition: JointXMLnode.h:111
iterator(JointXMLnode< Ch > &pa, size_t idx)
Definition: JointXMLnode.h:104
JointXMLnode< Ch > & parent
Definition: JointXMLnode.h:109
rapidxml::xml_node< Ch > * operator*() const
Definition: JointXMLnode.h:76
void add(const rapidxml::xml_node< Ch > *node)
Definition: JointXMLnode.h:28
bool operator==(const iterator &it) const
Definition: JointXMLnode.h:83


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:21