JointXMLnode.h
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2024 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) throw std::runtime_error("++ called on end() iterator!?");
55  current = current->next_sibling();
56  JointXMLnode<Ch>::TListNodes& lst = parent.getListOfNodes();
57  while (!current && lst_idx < lst.size())
58  {
59  lst_idx++;
60  if (lst_idx < lst.size())
61  current = lst[lst_idx]->first_node();
62  else
63  current = nullptr;
64  }
65  return *this;
66  }
67 
69  {
70  if (!current) throw std::runtime_error("-> called on end() iterator!?");
71  return current;
72  }
73 
75  {
76  if (!current) 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) && (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():
89  iterator(JointXMLnode<Ch>& pa) : parent(pa), lst_idx(0), current(nullptr)
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) : parent(pa), lst_idx(idx), current(nullptr) {}
100 
102  size_t lst_idx; // => lst.size() means this is "end()"
104 
105  }; // end class iterator
106 
107  iterator begin() { return iterator(*this); }
108  iterator end() { return iterator(*this, nodes_.size()); }
109 };
110 
111 } // namespace mvsim
mvsim::JointXMLnode::iterator::current
rapidxml::xml_node< Ch > * current
Definition: JointXMLnode.h:103
mvsim
Definition: Client.h:21
mvsim::JointXMLnode::end
iterator end()
Definition: JointXMLnode.h:108
mvsim::JointXMLnode::nodes_
TListNodes nodes_
Definition: JointXMLnode.h:25
mvsim::JointXMLnode::iterator::operator==
bool operator==(const iterator &it) const
Definition: JointXMLnode.h:80
mvsim::JointXMLnode
Definition: basic_types.h:53
mvsim::JointXMLnode::iterator::lst_idx
size_t lst_idx
Definition: JointXMLnode.h:102
mvsim::JointXMLnode::iterator::operator++
iterator & operator++()
Definition: JointXMLnode.h:52
rapidxml::xml_node::first_node
xml_node< Ch > * first_node(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const
Definition: rapidxml.hpp:936
mvsim::JointXMLnode::first_node
const rapidxml::xml_node< Ch > * first_node(const char *name) const
Definition: JointXMLnode.h:30
mvsim::JointXMLnode::iterator
Definition: JointXMLnode.h:45
mvsim::JointXMLnode::iterator::iterator
iterator(JointXMLnode< Ch > &pa, size_t idx)
Definition: JointXMLnode.h:99
mvsim::JointXMLnode::iterator::operator!=
bool operator!=(const iterator &it) const
Definition: JointXMLnode.h:85
rapidxml::xml_node
Definition: rapidxml.hpp:137
mvsim::JointXMLnode::getListOfNodes
const TListNodes & getListOfNodes() const
Definition: JointXMLnode.h:42
mvsim::JointXMLnode::iterator::operator*
rapidxml::xml_node< Ch > * operator*() const
Definition: JointXMLnode.h:74
mvsim::JointXMLnode::iterator::operator->
rapidxml::xml_node< Ch > * operator->() const
Definition: JointXMLnode.h:68
rapidxml.hpp
mvsim::JointXMLnode::begin
iterator begin()
Definition: JointXMLnode.h:107
mvsim::JointXMLnode::getListOfNodes
TListNodes & getListOfNodes()
Definition: JointXMLnode.h:41
mvsim::JointXMLnode::iterator::parent
JointXMLnode< Ch > & parent
Definition: JointXMLnode.h:101
mvsim::JointXMLnode::TListNodes
std::vector< const rapidxml::xml_node< Ch > * > TListNodes
Definition: JointXMLnode.h:22
mvsim::JointXMLnode::iterator::iterator
iterator(JointXMLnode< Ch > &pa)
Definition: JointXMLnode.h:89
mvsim::JointXMLnode::add
void add(const rapidxml::xml_node< Ch > *node)
Definition: JointXMLnode.h:28


mvsim
Author(s):
autogenerated on Wed May 28 2025 02:13:08