RecursiveVisitor.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T),
5  * Karlsruhe Institute of Technology (KIT), all rights reserved.
6  *
7  * ArmarX is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * ArmarX is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * @author Fabian Peller-Konrad (fabian dot peller-konrad at kit dot edu)
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 #pragma once
25 
26 #include <type_traits>
27 
28 #include "../../Descriptor.h"
29 #include "Visitor.h"
30 
31 namespace armarx::aron::data
32 {
33  /**
34  * @see type/visitor/RecursiveVisitor.h
35  */
36  template <class T>
37  struct RecursiveVisitor : virtual public VisitorBase<T>
38  {
39  using Input = typename VisitorBase<T>::Input;
40  using InputNonConst = typename std::remove_const<Input>::type;
41 
42  using MapElements = std::map<std::string, InputNonConst>;
43  using ListElements = std::vector<InputNonConst>;
44 
45  virtual MapElements getDictElements(Input&) = 0;
46  virtual ListElements getListElements(Input&) = 0;
47 
48  virtual void visitDictOnEnter(Input& element) {};
49  virtual void visitDictOnExit(Input& element) {};
50  virtual void visitListOnEnter(Input& element) {};
51  virtual void visitListOnExit(Input& element) {};
52 
53  virtual void visitNDArray(Input& element) {};
54  virtual void visitInt(Input& element) {};
55  virtual void visitLong(Input& element) {};
56  virtual void visitFloat(Input& element) {};
57  virtual void visitDouble(Input& element) {};
58  virtual void visitBool(Input& element) {};
59  virtual void visitString(Input& element) {};
60  virtual void visitUnknown(Input& element)
61  {
62  if (!element)
63  {
64  throw error::AronException(__PRETTY_FUNCTION__, "Unknown type in visitor. The element was NULL.");
65  }
66  else
67  {
68  throw error::AronException(__PRETTY_FUNCTION__, "Unknown type in visitor. The element was set but descriptor was undefined.");
69  }
70  }
71  virtual ~RecursiveVisitor() = default;
72  };
73 
74  /**
75  * @see type/visitor/RecursiveVisitor.h
76  */
77  template <class T1, class T2>
78  struct RecursiveTypedVisitor : virtual public TypedVisitorBase<T1, T2>
79  {
82 
83  using TypeInputNonConst = typename std::remove_const<TypeInput>::type;
84  using DataInputNonConst = typename std::remove_const<DataInput>::type;
85 
86  using MapElements = std::map<std::string, std::pair<DataInputNonConst, TypeInputNonConst>>;
87  using ListElements = std::vector<std::pair<DataInputNonConst, TypeInputNonConst>>;
88  using PairElements = std::pair<std::pair<DataInputNonConst, TypeInputNonConst>, std::pair<DataInputNonConst, TypeInputNonConst>>;
89  using TupleElements = std::vector<std::pair<DataInputNonConst, TypeInputNonConst>>;
90 
96 
97  virtual void visitObjectOnEnter(DataInput& elementData, TypeInput& elementType) {};
98  virtual void visitObjectOnExit(DataInput& elementData, TypeInput& elementType) {};
99  virtual void visitDictOnEnter(DataInput& elementData, TypeInput& elementType) {};
100  virtual void visitDictOnExit(DataInput& elementData, TypeInput& elementType) {};
101  virtual void visitPairOnEnter(DataInput& elementData, TypeInput& elementType) {};
102  virtual void visitPairOnExit(DataInput& elementData, TypeInput& elementType) {};
103  virtual void visitTupleOnEnter(DataInput& elementData, TypeInput& elementType) {};
104  virtual void visitTupleOnExit(DataInput& elementData, TypeInput& elementType) {};
105  virtual void visitListOnEnter(DataInput& elementData, TypeInput& elementType) {};
106  virtual void visitListOnExit(DataInput& elementData, TypeInput& elementType) {};
107 
108  virtual void visitMatrix(DataInput& elementData, TypeInput& elementType) {};
109  virtual void visitNDArray(DataInput& elementData, TypeInput& elementType) {};
110  virtual void visitQuaternion(DataInput& elementData, TypeInput& elementType) {};
111  virtual void visitImage(DataInput& elementData, TypeInput& elementType) {};
112  virtual void visitPointCloud(DataInput& elementData, TypeInput& elementType) {};
113  virtual void visitIntEnum(DataInput& elementData, TypeInput& elementType) {};
114  virtual void visitInt(DataInput& elementData, TypeInput& elementType) {};
115  virtual void visitLong(DataInput& elementData, TypeInput& elementType) {};
116  virtual void visitFloat(DataInput& elementData, TypeInput& elementType) {};
117  virtual void visitDouble(DataInput& elementData, TypeInput& elementType) {};
118  virtual void visitBool(DataInput& elementData, TypeInput& elementType) {};
119  virtual void visitString(DataInput& elementData, TypeInput& elementType) {};
120  virtual void visitAnyObject(DataInput& elementData, TypeInput& elementType) {};
121  virtual void visitUnknown(DataInput& elementData, TypeInput& elementType)
122  {
123  if (!elementData)
124  {
125  throw error::AronException(__PRETTY_FUNCTION__, "Unknown type in visitor. The element was NULL.");
126  }
127  else
128  {
129  throw error::AronException(__PRETTY_FUNCTION__, "Unknown type in visitor. The element was set but descriptor was undefined.");
130  }
131  }
132  virtual ~RecursiveTypedVisitor() = default;
133  };
134 
135  template <class T, class Data>
136  concept isRecursiveVisitor = std::is_base_of<RecursiveVisitor<Data>, T>::value;
137 
138  template <class T, class Data, class Type>
139  concept isRecursiveTypedVisitor = std::is_base_of<RecursiveTypedVisitor<Data, Type>, T>::value;
140 
141  /**
142  * @see type/visitor/RecursiveVisitor.h
143  */
144  template <class RecursiveVisitorImplementation>
145  requires isRecursiveVisitor<RecursiveVisitorImplementation, typename RecursiveVisitorImplementation::Input>
146  void visitRecursive(RecursiveVisitorImplementation& v, typename RecursiveVisitorImplementation::Input& o)
147  {
148  data::Descriptor descriptor = v.getDescriptor(o);
149  switch (descriptor)
150  {
152  {
153  v.visitListOnEnter(o);
154  unsigned int i = 0;
155  for (auto& value : v.getListElements(o))
156  {
158  i++;
159  }
160  v.visitListOnExit(o);
161  return;
162  }
164  {
165  v.visitDictOnEnter(o);
166  for (auto& [key, value] : v.getDictElements(o))
167  {
169  }
170  v.visitDictOnExit(o);
171  return;
172  }
174  return v.visitNDArray(o);
176  return v.visitInt(o);
178  return v.visitLong(o);
180  return v.visitFloat(o);
182  return v.visitDouble(o);
184  return v.visitString(o);
186  return v.visitBool(o);
188  return v.visitUnknown(o);
189  }
190  }
191 
192  /**
193  * @see type/visitor/RecursiveVisitor.h
194  * @see data/visitor/Visitor.h
195  */
196  template <class RecursiveVisitorImplementation>
197  requires isRecursiveTypedVisitor<RecursiveVisitorImplementation, typename RecursiveVisitorImplementation::DataInput, typename RecursiveVisitorImplementation::TypeInput>
198  void visitRecursive(RecursiveVisitorImplementation& v, typename RecursiveVisitorImplementation::DataInput& o, typename RecursiveVisitorImplementation::TypeInput& t)
199  {
200  type::Descriptor descriptor = v.getDescriptor(o, t);
201  switch (descriptor)
202  {
204  {
205  v.visitListOnEnter(o, t);
206  unsigned int i = 0;
207  for (auto& [value, acceptedType] : v.getListElements(o, t))
208  {
209  visitRecursive(v, value, acceptedType);
210  i++;
211  }
212  v.visitListOnExit(o, t);
213  return;
214  }
216  {
217  v.visitPairOnEnter(o, t);
218  auto pair = v.getPairElements(o, t);
219  auto first = pair.first;
220  auto second = pair.second;
221  visitRecursive(v, first.first, first.second);
222  visitRecursive(v, second.first, second.second);
223  v.visitPairOnExit(o, t);
224  return;
225  }
227  {
228  v.visitTupleOnEnter(o, t);
229  unsigned int i = 0;
230  for (auto& [value, acceptedType] : v.getTupleElements(o, t))
231  {
232  visitRecursive(v, value, acceptedType);
233  i++;
234  }
235  v.visitTupleOnExit(o, t);
236  return;
237  }
239  {
240  v.visitDictOnEnter(o, t);
241  for (auto& [key, pair] : v.getDictElements(o, t))
242  {
243  visitRecursive(v, pair.first, pair.second);
244  }
245  v.visitDictOnExit(o, t);
246  return;
247  }
249  {
250  v.visitObjectOnEnter(o, t);
251  for (auto& [key, pair] : v.getObjectElements(o, t))
252  {
253  visitRecursive(v, pair.first, pair.second);
254  }
255  v.visitObjectOnExit(o, t);
256  return;
257  }
259  return v.visitNDArray(o, t);
261  return v.visitMatrix(o, t);
263  return v.visitImage(o, t);
265  return v.visitPointCloud(o, t);
267  return v.visitQuaternion(o, t);
269  return v.visitInt(o, t);
271  return v.visitLong(o, t);
273  return v.visitFloat(o, t);
275  return v.visitDouble(o, t);
277  return v.visitString(o, t);
279  return v.visitBool(o, t);
281  return v.visitAnyObject(o, t);
283  return v.visitIntEnum(o, t);
285  return v.visitUnknown(o, t);
286  }
287  }
288 }
armarx::aron::type::Descriptor::FLOAT
@ FLOAT
armarx::aron::data::RecursiveTypedVisitor::visitObjectOnExit
virtual void visitObjectOnExit(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:98
armarx::aron::data::RecursiveTypedVisitor::getPairElements
virtual PairElements getPairElements(DataInput &, TypeInput &)=0
armarx::aron::type::Descriptor::DOUBLE
@ DOUBLE
armarx::aron::error::AronException
A base class for aron exceptions.
Definition: Exception.h:42
armarx::aron::data::RecursiveTypedVisitor::visitListOnEnter
virtual void visitListOnEnter(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:105
Visitor.h
armarx::aron::data::RecursiveVisitor::~RecursiveVisitor
virtual ~RecursiveVisitor()=default
armarx::aron::data::Descriptor::NDARRAY
@ NDARRAY
armarx::aron::data::RecursiveTypedVisitor::visitString
virtual void visitString(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:119
armarx::aron::data::Descriptor::LIST
@ LIST
armarx::aron::data::RecursiveVisitor::visitInt
virtual void visitInt(Input &element)
Definition: RecursiveVisitor.h:54
armarx::aron::type::Descriptor::IMAGE
@ IMAGE
armarx::aron::data::RecursiveTypedVisitor::visitTupleOnEnter
virtual void visitTupleOnEnter(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:103
armarx::aron::data::Descriptor::STRING
@ STRING
armarx::aron::data::RecursiveTypedVisitor::visitPointCloud
virtual void visitPointCloud(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:112
armarx::aron::data::RecursiveTypedVisitor::getTupleElements
virtual TupleElements getTupleElements(DataInput &, TypeInput &)=0
armarx::aron::data::RecursiveTypedVisitor::visitPairOnEnter
virtual void visitPairOnEnter(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:101
armarx::aron::data::RecursiveVisitor< data::VariantPtr >::InputNonConst
typename std::remove_const< Input >::type InputNonConst
Definition: RecursiveVisitor.h:40
armarx::aron::data::RecursiveVisitor::visitString
virtual void visitString(Input &element)
Definition: RecursiveVisitor.h:59
armarx::aron::data::RecursiveTypedVisitor::visitIntEnum
virtual void visitIntEnum(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:113
armarx::aron::type::Descriptor::PAIR
@ PAIR
armarx::aron::data::RecursiveVisitor::visitDouble
virtual void visitDouble(Input &element)
Definition: RecursiveVisitor.h:57
armarx::aron::data::RecursiveTypedVisitor< data::VariantPtr, const type::VariantPtr >::MapElements
std::map< std::string, std::pair< DataInputNonConst, TypeInputNonConst > > MapElements
Definition: RecursiveVisitor.h:86
armarx::aron::type::Descriptor::LIST
@ LIST
armarx::aron::data::Descriptor
Descriptor
Definition: Descriptor.h:193
armarx::aron::data::RecursiveVisitor< data::VariantPtr >::Input
typename VisitorBase< data::VariantPtr >::Input Input
Definition: RecursiveVisitor.h:39
armarx::aron::data::Descriptor::FLOAT
@ FLOAT
armarx::aron::data::RecursiveVisitor::visitListOnExit
virtual void visitListOnExit(Input &element)
Definition: RecursiveVisitor.h:51
armarx::aron::data::RecursiveVisitor::visitDictOnEnter
virtual void visitDictOnEnter(Input &element)
Definition: RecursiveVisitor.h:48
armarx::aron::data::RecursiveTypedVisitor< data::VariantPtr, const type::VariantPtr >::PairElements
std::pair< std::pair< DataInputNonConst, TypeInputNonConst >, std::pair< DataInputNonConst, TypeInputNonConst > > PairElements
Definition: RecursiveVisitor.h:88
armarx::aron::type::Descriptor::NDARRAY
@ NDARRAY
armarx::aron::data::Descriptor::INT
@ INT
armarx::aron::data::RecursiveVisitor::getListElements
virtual ListElements getListElements(Input &)=0
armarx::aron::data::RecursiveTypedVisitor::getObjectElements
virtual MapElements getObjectElements(DataInput &, TypeInput &)=0
armarx::aron::data::RecursiveTypedVisitor::visitPairOnExit
virtual void visitPairOnExit(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:102
armarx::aron::data::Descriptor::DICT
@ DICT
armarx::aron::data::isRecursiveVisitor
concept isRecursiveVisitor
Definition: RecursiveVisitor.h:136
armarx::aron::type::Descriptor::BOOL
@ BOOL
armarx::aron::data::RecursiveTypedVisitor
Definition: RecursiveVisitor.h:78
armarx::aron::data::isRecursiveTypedVisitor
concept isRecursiveTypedVisitor
Definition: RecursiveVisitor.h:139
armarx::aron::data::RecursiveVisitor::getDictElements
virtual MapElements getDictElements(Input &)=0
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::aron::data::RecursiveTypedVisitor< data::VariantPtr, const type::VariantPtr >::TypeInput
typename TypedVisitorBase< data::VariantPtr, const type::VariantPtr >::TypeInput TypeInput
Definition: RecursiveVisitor.h:81
armarx::aron::data::RecursiveTypedVisitor::visitObjectOnEnter
virtual void visitObjectOnEnter(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:97
armarx::aron::data::RecursiveTypedVisitor::visitDouble
virtual void visitDouble(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:117
armarx::aron::type::Descriptor::MATRIX
@ MATRIX
armarx::aron::data::RecursiveTypedVisitor::getListElements
virtual ListElements getListElements(DataInput &, TypeInput &)=0
armarx::aron::data::RecursiveTypedVisitor::visitListOnExit
virtual void visitListOnExit(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:106
armarx::aron::data::RecursiveVisitor
Definition: RecursiveVisitor.h:37
armarx::aron::data::RecursiveTypedVisitor::visitMatrix
virtual void visitMatrix(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:108
armarx::aron::type::Descriptor::QUATERNION
@ QUATERNION
armarx::aron::data::Descriptor::BOOL
@ BOOL
armarx::aron::requires
requires(!aron::detail::DtoAndBoAreSame< DtoT, BoT >) void toAron(std
Definition: aron_conversions.h:127
armarx::aron::data::TypedVisitorBase::DataInput
T1 DataInput
Definition: Visitor.h:53
armarx::aron::data::RecursiveTypedVisitor::visitFloat
virtual void visitFloat(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:116
armarx::aron::data::TypedVisitorBase
Definition: Visitor.h:51
armarx::aron::data
A convenience header to include all aron files (full include, not forward declared)
Definition: aron_conversions.cpp:3
armarx::aron::data::RecursiveTypedVisitor< data::VariantPtr, const type::VariantPtr >::DataInput
typename TypedVisitorBase< data::VariantPtr, const type::VariantPtr >::DataInput DataInput
Definition: RecursiveVisitor.h:80
armarx::aron::data::RecursiveTypedVisitor::visitDictOnExit
virtual void visitDictOnExit(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:100
armarx::aron::type::Descriptor::STRING
@ STRING
armarx::aron::type::Descriptor::LONG
@ LONG
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::aron::type::Descriptor::INT
@ INT
armarx::aron::data::RecursiveVisitor::visitLong
virtual void visitLong(Input &element)
Definition: RecursiveVisitor.h:55
armarx::aron::data::RecursiveTypedVisitor::visitImage
virtual void visitImage(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:111
armarx::aron::data::TypedVisitorBase::TypeInput
T2 TypeInput
Definition: Visitor.h:54
armarx::aron::data::RecursiveVisitor::visitUnknown
virtual void visitUnknown(Input &element)
Definition: RecursiveVisitor.h:60
armarx::aron::type::Descriptor::INT_ENUM
@ INT_ENUM
armarx::aron::data::RecursiveTypedVisitor::visitTupleOnExit
virtual void visitTupleOnExit(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:104
armarx::aron::data::RecursiveTypedVisitor::visitUnknown
virtual void visitUnknown(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:121
armarx::aron::type::Descriptor::OBJECT
@ OBJECT
armarx::aron::data::RecursiveTypedVisitor< data::VariantPtr, const type::VariantPtr >::TypeInputNonConst
typename std::remove_const< TypeInput >::type TypeInputNonConst
Definition: RecursiveVisitor.h:83
armarx::aron::data::visitRecursive
requires isRecursiveVisitor< RecursiveVisitorImplementation, typename RecursiveVisitorImplementation::Input > void visitRecursive(RecursiveVisitorImplementation &v, typename RecursiveVisitorImplementation::Input &o)
Definition: RecursiveVisitor.h:146
armarx::aron::type::Descriptor::UNKNOWN
@ UNKNOWN
armarx::aron::data::Descriptor::LONG
@ LONG
armarx::aron::type::Descriptor::DICT
@ DICT
armarx::aron::data::RecursiveTypedVisitor::getDictElements
virtual MapElements getDictElements(DataInput &, TypeInput &)=0
armarx::aron::type::Descriptor::TUPLE
@ TUPLE
armarx::aron::data::RecursiveTypedVisitor::visitNDArray
virtual void visitNDArray(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:109
armarx::aron::data::RecursiveTypedVisitor< data::VariantPtr, const type::VariantPtr >::ListElements
std::vector< std::pair< DataInputNonConst, TypeInputNonConst > > ListElements
Definition: RecursiveVisitor.h:87
armarx::aron::data::RecursiveVisitor::visitDictOnExit
virtual void visitDictOnExit(Input &element)
Definition: RecursiveVisitor.h:49
armarx::aron::data::Descriptor::DOUBLE
@ DOUBLE
armarx::aron::type::Descriptor::POINTCLOUD
@ POINTCLOUD
armarx::aron::data::RecursiveVisitor::visitNDArray
virtual void visitNDArray(Input &element)
Definition: RecursiveVisitor.h:53
armarx::aron::data::RecursiveTypedVisitor::~RecursiveTypedVisitor
virtual ~RecursiveTypedVisitor()=default
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::aron::data::RecursiveVisitor< data::VariantPtr >::ListElements
std::vector< InputNonConst > ListElements
Definition: RecursiveVisitor.h:43
armarx::aron::data::RecursiveTypedVisitor::visitDictOnEnter
virtual void visitDictOnEnter(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:99
armarx::aron::data::Descriptor::UNKNOWN
@ UNKNOWN
armarx::aron::data::RecursiveTypedVisitor::visitBool
virtual void visitBool(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:118
armarx::aron::data::VisitorBase
Definition: Visitor.h:39
armarx::aron::data::RecursiveVisitor::visitFloat
virtual void visitFloat(Input &element)
Definition: RecursiveVisitor.h:56
armarx::aron::data::RecursiveVisitor::visitListOnEnter
virtual void visitListOnEnter(Input &element)
Definition: RecursiveVisitor.h:50
armarx::aron::type::Descriptor
Descriptor
Definition: Descriptor.h:76
armarx::aron::data::RecursiveTypedVisitor::visitQuaternion
virtual void visitQuaternion(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:110
armarx::aron::data::RecursiveVisitor< data::VariantPtr >::MapElements
std::map< std::string, InputNonConst > MapElements
Definition: RecursiveVisitor.h:42
armarx::aron::data::VisitorBase::Input
T Input
Definition: Visitor.h:41
armarx::aron::data::RecursiveTypedVisitor::visitInt
virtual void visitInt(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:114
armarx::aron::type::Descriptor::ANY_OBJECT
@ ANY_OBJECT
armarx::aron::data::RecursiveTypedVisitor::visitLong
virtual void visitLong(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:115
armarx::aron::data::RecursiveTypedVisitor::visitAnyObject
virtual void visitAnyObject(DataInput &elementData, TypeInput &elementType)
Definition: RecursiveVisitor.h:120
armarx::aron::data::RecursiveTypedVisitor< data::VariantPtr, const type::VariantPtr >::DataInputNonConst
typename std::remove_const< DataInput >::type DataInputNonConst
Definition: RecursiveVisitor.h:84
armarx::aron::data::RecursiveVisitor::visitBool
virtual void visitBool(Input &element)
Definition: RecursiveVisitor.h:58
armarx::aron::data::RecursiveTypedVisitor< data::VariantPtr, const type::VariantPtr >::TupleElements
std::vector< std::pair< DataInputNonConst, TypeInputNonConst > > TupleElements
Definition: RecursiveVisitor.h:89