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::type
32 {
33  /**
34  * @brief The visitRecursive function. Calls visitX of a RecursiveVisitorImplementation recursively. For more information please see Visitor.h
35  */
36  template <class RecursiveVisitorImplementation>
37  void
38  visitRecursive(RecursiveVisitorImplementation& v,
39  typename RecursiveVisitorImplementation::Input& t)
40  {
41  auto descriptor = v.getDescriptor(t);
42  switch (descriptor)
43  {
45  {
46  v.visitListOnEnter(t);
47  auto acceptedType = v.getListAcceptedType(t);
48  visitRecursive(v, acceptedType);
49  v.visitListOnExit(t);
50  return;
51  }
53  {
54  v.visitPairOnEnter(t);
55  auto acceptedTypes = v.getPairAcceptedTypes(t);
56  visitRecursive(v, acceptedTypes.first);
57  visitRecursive(v, acceptedTypes.second);
58  v.visitPairOnExit(t);
59  return;
60  }
62  {
63  v.visitTupleOnEnter(t);
64  unsigned int i = 0;
65  for (const auto& acceptedType : v.getTupleAcceptedTypes(t))
66  {
67  visitRecursive(v, acceptedType);
68  i++;
69  }
70  v.visitTupleOnExit(t);
71  return;
72  }
74  {
75  v.visitDictOnEnter(t);
76  auto acceptedType = v.getDictAcceptedType(t);
77  visitRecursive(v, acceptedType);
78  v.visitDictOnExit(t);
79  return;
80  }
82  {
83  v.visitObjectOnEnter(t);
84  for (const auto& [key, acceptedType] : v.getObjectElements(t))
85  {
86  visitRecursive(v, acceptedType);
87  }
88  v.visitObjectOnExit(t);
89  return;
90  }
92  return v.visitNDArray(t);
94  return v.visitMatrix(t);
96  return v.visitImage(t);
98  return v.visitPointCloud(t);
100  return v.visitQuaternion(t);
102  return v.visitInt(t);
104  return v.visitLong(t);
106  return v.visitFloat(t);
108  return v.visitDouble(t);
110  return v.visitString(t);
112  return v.visitBool(t);
114  return v.visitAnyObject(t);
116  return v.visitIntEnum(t);
118  return v.visitUnknown(t);
119  }
120  }
121 
122  /**
123  * @brief The RecursiveVisitor struct. It differs from the Visitor struct (@see Visitor.h), because it provides special visitXOnEnter and visitXOnExit methods for container types.
124  * Further, it defines abstract methods to get the children from the input representation which is used by the visitRecursive method.
125  */
126  template <class T>
127  struct RecursiveVisitor : virtual public VisitorBase<T>
128  {
129  using Input = typename VisitorBase<T>::Input;
130  using InputNonConst = typename std::remove_const<Input>::type;
131 
132  using ObjectElements = std::map<std::string, InputNonConst>;
133  using PairElements = std::pair<InputNonConst, InputNonConst>;
134  using TupleElements = std::vector<InputNonConst>;
135 
141 
142  virtual void visitObjectOnEnter(Input&){};
143  virtual void visitObjectOnExit(Input&){};
144  virtual void visitDictOnEnter(Input&){};
145  virtual void visitDictOnExit(Input&){};
146  virtual void visitPairOnEnter(Input&){};
147  virtual void visitPairOnExit(Input&){};
148  virtual void visitTupleOnEnter(Input&){};
149  virtual void visitTupleOnExit(Input&){};
150  virtual void visitListOnEnter(Input&){};
151  virtual void visitListOnExit(Input&){};
152 
153  virtual void visitMatrix(Input&){};
154  virtual void visitNDArray(Input&){};
155  virtual void visitQuaternion(Input&){};
156  virtual void visitImage(Input&){};
157  virtual void visitPointCloud(Input&){};
158  virtual void visitIntEnum(Input&){};
159  virtual void visitInt(Input&){};
160  virtual void visitLong(Input&){};
161  virtual void visitFloat(Input&){};
162  virtual void visitDouble(Input&){};
163  virtual void visitBool(Input&){};
164  virtual void visitString(Input&){};
165  virtual void visitAnyObject(Input&){};
166 
167  virtual void
169  {
170  throw error::AronException(__PRETTY_FUNCTION__, "Unknown type in visitor.");
171  }
172 
173  virtual ~RecursiveVisitor() = default;
174  };
175 } // namespace armarx::aron::type
armarx::aron::type::RecursiveVisitor< const type::VariantPtr >::ObjectElements
std::map< std::string, InputNonConst > ObjectElements
Definition: RecursiveVisitor.h:132
armarx::aron::type::Descriptor::FLOAT
@ FLOAT
armarx::aron::type::VisitorBase::Input
T Input
Definition: Visitor.h:94
armarx::aron::type::RecursiveVisitor::visitObjectOnEnter
virtual void visitObjectOnEnter(Input &)
Definition: RecursiveVisitor.h:142
armarx::aron::type::RecursiveVisitor::visitNDArray
virtual void visitNDArray(Input &)
Definition: RecursiveVisitor.h:154
armarx::aron::type::Descriptor::DOUBLE
@ DOUBLE
armarx::aron::error::AronException
A base class for aron exceptions.
Definition: Exception.h:42
armarx::aron::type::RecursiveVisitor::visitDouble
virtual void visitDouble(Input &)
Definition: RecursiveVisitor.h:162
armarx::aron::type::RecursiveVisitor::getTupleAcceptedTypes
virtual TupleElements getTupleAcceptedTypes(Input &)=0
armarx::aron::type::Descriptor::IMAGE
@ IMAGE
armarx::aron::type::RecursiveVisitor::getPairAcceptedTypes
virtual PairElements getPairAcceptedTypes(Input &)=0
armarx::aron::type::RecursiveVisitor::visitTupleOnExit
virtual void visitTupleOnExit(Input &)
Definition: RecursiveVisitor.h:149
armarx::aron::type::RecursiveVisitor::visitTupleOnEnter
virtual void visitTupleOnEnter(Input &)
Definition: RecursiveVisitor.h:148
armarx::aron::type::RecursiveVisitor::visitPairOnExit
virtual void visitPairOnExit(Input &)
Definition: RecursiveVisitor.h:147
armarx::aron::type::RecursiveVisitor::visitUnknown
virtual void visitUnknown(Input &)
Definition: RecursiveVisitor.h:168
armarx::aron::type::RecursiveVisitor< const type::VariantPtr >::TupleElements
std::vector< InputNonConst > TupleElements
Definition: RecursiveVisitor.h:134
armarx::aron::type::Descriptor::PAIR
@ PAIR
armarx::aron::type::VisitorBase
The VisitorBase struct.
Definition: Visitor.h:92
Visitor.h
armarx::aron::type::Descriptor::LIST
@ LIST
armarx::aron::type::RecursiveVisitor< const type::VariantPtr >::PairElements
std::pair< InputNonConst, InputNonConst > PairElements
Definition: RecursiveVisitor.h:133
armarx::aron::type::RecursiveVisitor::getObjectAcceptedTypes
virtual ObjectElements getObjectAcceptedTypes(Input &)=0
armarx::aron::type::RecursiveVisitor::getDictAcceptedType
virtual InputNonConst getDictAcceptedType(Input &)=0
armarx::aron::type::Descriptor::NDARRAY
@ NDARRAY
armarx::aron::type::RecursiveVisitor< const type::VariantPtr >::Input
typename VisitorBase< const type::VariantPtr >::Input Input
Definition: RecursiveVisitor.h:129
armarx::aron::type::RecursiveVisitor::visitDictOnExit
virtual void visitDictOnExit(Input &)
Definition: RecursiveVisitor.h:145
armarx::aron::type::RecursiveVisitor::visitPointCloud
virtual void visitPointCloud(Input &)
Definition: RecursiveVisitor.h:157
armarx::aron::type::Descriptor::BOOL
@ BOOL
armarx::aron::type::RecursiveVisitor< const type::VariantPtr >::InputNonConst
typename std::remove_const< Input >::type InputNonConst
Definition: RecursiveVisitor.h:130
armarx::aron::type::RecursiveVisitor::visitListOnEnter
virtual void visitListOnEnter(Input &)
Definition: RecursiveVisitor.h:150
armarx::aron::type::RecursiveVisitor::visitPairOnEnter
virtual void visitPairOnEnter(Input &)
Definition: RecursiveVisitor.h:146
armarx::aron::type::RecursiveVisitor::visitImage
virtual void visitImage(Input &)
Definition: RecursiveVisitor.h:156
armarx::aron::type::RecursiveVisitor::visitObjectOnExit
virtual void visitObjectOnExit(Input &)
Definition: RecursiveVisitor.h:143
armarx::aron::type::RecursiveVisitor::~RecursiveVisitor
virtual ~RecursiveVisitor()=default
armarx::aron::type::Descriptor::MATRIX
@ MATRIX
armarx::aron::type::RecursiveVisitor::visitBool
virtual void visitBool(Input &)
Definition: RecursiveVisitor.h:163
armarx::aron::type::Descriptor::QUATERNION
@ QUATERNION
armarx::aron::type::RecursiveVisitor::visitIntEnum
virtual void visitIntEnum(Input &)
Definition: RecursiveVisitor.h:158
armarx::aron::type::RecursiveVisitor::visitAnyObject
virtual void visitAnyObject(Input &)
Definition: RecursiveVisitor.h:165
armarx::aron::type
A convenience header to include all aron files (full include, not forward declared)
Definition: aron_conversions.cpp:9
armarx::aron::type::RecursiveVisitor::visitFloat
virtual void visitFloat(Input &)
Definition: RecursiveVisitor.h:161
armarx::aron::type::RecursiveVisitor::visitLong
virtual void visitLong(Input &)
Definition: RecursiveVisitor.h:160
armarx::aron::type::visitRecursive
void visitRecursive(RecursiveVisitorImplementation &v, typename RecursiveVisitorImplementation::Input &t)
The visitRecursive function.
Definition: RecursiveVisitor.h:38
armarx::aron::type::RecursiveVisitor::getListAcceptedType
virtual InputNonConst getListAcceptedType(Input &)=0
armarx::aron::type::Descriptor::STRING
@ STRING
armarx::aron::type::Descriptor::LONG
@ LONG
armarx::aron::type::RecursiveVisitor::visitMatrix
virtual void visitMatrix(Input &)
Definition: RecursiveVisitor.h:153
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::type::RecursiveVisitor::visitQuaternion
virtual void visitQuaternion(Input &)
Definition: RecursiveVisitor.h:155
armarx::aron::type::RecursiveVisitor::visitListOnExit
virtual void visitListOnExit(Input &)
Definition: RecursiveVisitor.h:151
armarx::aron::type::Descriptor::INT_ENUM
@ INT_ENUM
armarx::aron::type::Descriptor::OBJECT
@ OBJECT
armarx::aron::type::Descriptor::UNKNOWN
@ UNKNOWN
armarx::aron::type::Descriptor::DICT
@ DICT
armarx::aron::type::Descriptor::TUPLE
@ TUPLE
armarx::aron::type::RecursiveVisitor
The RecursiveVisitor struct.
Definition: RecursiveVisitor.h:127
armarx::aron::type::RecursiveVisitor::visitInt
virtual void visitInt(Input &)
Definition: RecursiveVisitor.h:159
armarx::aron::type::Descriptor::POINTCLOUD
@ POINTCLOUD
armarx::aron::type::RecursiveVisitor::visitDictOnEnter
virtual void visitDictOnEnter(Input &)
Definition: RecursiveVisitor.h:144
armarx::aron::type::RecursiveVisitor::visitString
virtual void visitString(Input &)
Definition: RecursiveVisitor.h:164
armarx::aron::type::Descriptor::ANY_OBJECT
@ ANY_OBJECT