Visitor.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 <map>
27 #include <string>
28 #include <vector>
29 
30 #include "../../Descriptor.h"
31 #include "../../Exception.h"
32 
33 namespace armarx::aron::data
34 {
35  /**
36  * @see type/visitor/Visitor.h
37  */
38  template <class T>
39  struct VisitorBase
40  {
41  using Input = T;
42 
43  virtual data::Descriptor getDescriptor(Input&) = 0;
44  virtual ~VisitorBase() = default;
45  };
46 
47  /**
48  * @see type/visitor/Visitor.h
49  */
50  template <class T1, class T2>
52  {
53  using DataInput = T1;
54  using TypeInput = T2;
55 
57  virtual ~TypedVisitorBase() = default;
58  };
59 
60  /**
61  * @see type/visitor/Visitor.h
62  */
63  template <class T>
64  struct Visitor : virtual public VisitorBase<T>
65  {
66  using Input = typename VisitorBase<T>::Input;
67 
68  virtual void visitDict(Input&) {};
69  virtual void visitList(Input&) {};
70  virtual void visitNDArray(Input&) {};
71  virtual void visitInt(Input&) {};
72  virtual void visitLong(Input&) {};
73  virtual void visitFloat(Input&) {};
74  virtual void visitDouble(Input&) {};
75  virtual void visitBool(Input&) {};
76  virtual void visitString(Input&) {};
77  virtual void visitUnknown(Input&) { throw error::AronException(__PRETTY_FUNCTION__, "Unknown type in visitor."); }
78  virtual ~Visitor() = default;
79  };
80 
81  /**
82  * @see type/visitor/Visitor.h
83  */
84  template <class T1, class T2>
85  struct TypedVisitor : virtual public TypedVisitorBase<T1, T2>
86  {
89 
90  virtual void visitObject(DataInput&, TypeInput&) {};
91  virtual void visitDict(DataInput&, TypeInput&) {};
92  virtual void visitPair(DataInput&, TypeInput&) {};
93  virtual void visitTuple(DataInput&, TypeInput&) {};
94  virtual void visitList(DataInput&, TypeInput&) {};
95  virtual void visitMatrix(DataInput&, TypeInput&) {};
96  virtual void visitNDArray(DataInput&, TypeInput&) {};
97  virtual void visitQuaternion(DataInput&, TypeInput&) {};
98  virtual void visitImage(DataInput&, TypeInput&) {};
99  virtual void visitPointCloud(DataInput&, TypeInput&) {};
100  virtual void visitIntEnum(DataInput&, TypeInput&) {};
101  virtual void visitInt(DataInput&, TypeInput&) {};
102  virtual void visitLong(DataInput&, TypeInput&) {};
103  virtual void visitFloat(DataInput&, TypeInput&) {};
104  virtual void visitDouble(DataInput&, TypeInput&) {};
105  virtual void visitBool(DataInput&, TypeInput&) {};
106  virtual void visitString(DataInput&, TypeInput&) {};
107  virtual void visitAnyObject(DataInput&, TypeInput&) {};
108  virtual void visitUnknown(DataInput&, TypeInput&) { throw error::AronException(__PRETTY_FUNCTION__, "Unknown type in visitor."); }
109  virtual ~TypedVisitor() = default;
110  };
111 
112  template <class T, class Data>
113  concept isVisitor = std::is_base_of<Visitor<Data>, T>::value;
114 
115  template <class T, class Data, class Type>
116  concept isTypedVisitor = std::is_base_of<TypedVisitor<Data, Type>, T>::value;
117 
118 
119  /**
120  * @see type/visitor/Visitor.h
121  */
122  template <class VisitorImplementation>
123  requires isVisitor<VisitorImplementation, typename VisitorImplementation::Input>
124  void visit(VisitorImplementation& v, typename VisitorImplementation::Input& o)
125  {
126  auto descriptor = v.getDescriptor(o);
127  switch (descriptor)
128  {
130  return v.visitList(o);
132  return v.visitDict(o);
134  return v.visitNDArray(o);
136  return v.visitInt(o);
138  return v.visitLong(o);
140  return v.visitFloat(o);
142  return v.visitDouble(o);
144  return v.visitString(o);
146  return v.visitBool(o);
148  return v.visitUnknown(o);
149  }
150  }
151 
152  /**
153  * @see type/visitor/Visitor.h
154  * This implemntation visits a data and type representation simultaneously (based on the type descriptor).
155  * Does NOT check if the data and type representation match!
156  */
157  template <class VisitorImplementation>
158  requires isTypedVisitor<VisitorImplementation, typename VisitorImplementation::DataInput, typename VisitorImplementation::TypeInput>
159  void visit(VisitorImplementation& v, typename VisitorImplementation::DataInput& o, typename VisitorImplementation::TypeInput& t)
160  {
161  auto descriptor = v.getDescriptor(o, t);
162  switch (descriptor)
163  {
165  return v.visitObject(o, t);
167  return v.visitList(o, t);
169  return v.visitDict(o, t);
171  return v.visitPair(o, t);
173  return v.visitTuple(o, t);
175  return v.visitNDArray(o, t);
177  return v.visitMatrix(o, t);
179  return v.visitImage(o, t);
181  return v.visitPointCloud(o, t);
183  return v.visitQuaternion(o, t);
185  return v.visitInt(o, t);
187  return v.visitLong(o, t);
189  return v.visitFloat(o, t);
191  return v.visitDouble(o, t);
193  return v.visitString(o, t);
195  return v.visitBool(o, t);
197  return v.visitAnyObject(o, t);
199  return v.visitIntEnum(o, t);
201  return v.visitUnknown(o, t);
202  }
203  }
204 }
armarx::aron::type::Descriptor::FLOAT
@ FLOAT
armarx::aron::data::Visitor::visitLong
virtual void visitLong(Input &)
Definition: Visitor.h:72
armarx::aron::type::Descriptor::DOUBLE
@ DOUBLE
armarx::aron::error::AronException
A base class for aron exceptions.
Definition: Exception.h:42
armarx::aron::data::Descriptor::NDARRAY
@ NDARRAY
armarx::aron::data::TypedVisitor::visitImage
virtual void visitImage(DataInput &, TypeInput &)
Definition: Visitor.h:98
armarx::aron::data::TypedVisitor::visitFloat
virtual void visitFloat(DataInput &, TypeInput &)
Definition: Visitor.h:103
armarx::aron::data::Descriptor::LIST
@ LIST
armarx::aron::data::Visitor::visitInt
virtual void visitInt(Input &)
Definition: Visitor.h:71
armarx::aron::type::Descriptor::IMAGE
@ IMAGE
armarx::aron::data::Descriptor::STRING
@ STRING
armarx::aron::data::TypedVisitor::visitMatrix
virtual void visitMatrix(DataInput &, TypeInput &)
Definition: Visitor.h:95
armarx::aron::data::TypedVisitor::visitDict
virtual void visitDict(DataInput &, TypeInput &)
Definition: Visitor.h:91
armarx::aron::data::TypedVisitor::visitNDArray
virtual void visitNDArray(DataInput &, TypeInput &)
Definition: Visitor.h:96
armarx::aron::data::isVisitor
concept isVisitor
Definition: Visitor.h:113
armarx::aron::data::TypedVisitor::visitList
virtual void visitList(DataInput &, TypeInput &)
Definition: Visitor.h:94
armarx::aron::data::Visitor
Definition: Visitor.h:64
armarx::aron::data::TypedVisitor::visitInt
virtual void visitInt(DataInput &, TypeInput &)
Definition: Visitor.h:101
armarx::aron::data::TypedVisitor::visitDouble
virtual void visitDouble(DataInput &, TypeInput &)
Definition: Visitor.h:104
armarx::aron::type::Descriptor::PAIR
@ PAIR
armarx::aron::type::Descriptor::LIST
@ LIST
armarx::aron::data::Descriptor
Descriptor
Definition: Descriptor.h:193
armarx::aron::data::TypedVisitorBase::~TypedVisitorBase
virtual ~TypedVisitorBase()=default
armarx::aron::data::Descriptor::FLOAT
@ FLOAT
armarx::aron::data::TypedVisitor
Definition: Visitor.h:85
armarx::aron::type::Descriptor::NDARRAY
@ NDARRAY
armarx::aron::data::Descriptor::INT
@ INT
armarx::aron::data::Visitor::visitDouble
virtual void visitDouble(Input &)
Definition: Visitor.h:74
armarx::aron::data::Descriptor::DICT
@ DICT
armarx::aron::data::isTypedVisitor
concept isTypedVisitor
Definition: Visitor.h:116
armarx::aron::type::Descriptor::BOOL
@ BOOL
armarx::aron::data::TypedVisitor::visitLong
virtual void visitLong(DataInput &, TypeInput &)
Definition: Visitor.h:102
armarx::aron::data::VisitorBase::~VisitorBase
virtual ~VisitorBase()=default
armarx::aron::data::Visitor::visitNDArray
virtual void visitNDArray(Input &)
Definition: Visitor.h:70
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::aron::data::Visitor::visitUnknown
virtual void visitUnknown(Input &)
Definition: Visitor.h:77
armarx::aron::data::Visitor::visitFloat
virtual void visitFloat(Input &)
Definition: Visitor.h:73
armarx::aron::data::Visitor::visitBool
virtual void visitBool(Input &)
Definition: Visitor.h:75
armarx::aron::data::TypedVisitor::visitPointCloud
virtual void visitPointCloud(DataInput &, TypeInput &)
Definition: Visitor.h:99
armarx::aron::type::Descriptor::MATRIX
@ MATRIX
armarx::aron::data::TypedVisitor::visitIntEnum
virtual void visitIntEnum(DataInput &, TypeInput &)
Definition: Visitor.h:100
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< data::VariantPtr, const type::VariantPtr >::DataInput
data::VariantPtr DataInput
Definition: Visitor.h:53
armarx::aron::data::TypedVisitor::visitQuaternion
virtual void visitQuaternion(DataInput &, TypeInput &)
Definition: Visitor.h:97
armarx::aron::data::TypedVisitorBase
Definition: Visitor.h:51
armarx::aron::data::Visitor::visitDict
virtual void visitDict(Input &)
Definition: Visitor.h:68
armarx::aron::data::TypedVisitor::visitAnyObject
virtual void visitAnyObject(DataInput &, TypeInput &)
Definition: Visitor.h:107
armarx::aron::data
A convenience header to include all aron files (full include, not forward declared)
Definition: aron_conversions.cpp:3
armarx::aron::data::Visitor::visitString
virtual void visitString(Input &)
Definition: Visitor.h:76
armarx::aron::type::Descriptor::STRING
@ STRING
armarx::aron::type::Descriptor::LONG
@ LONG
armarx::aron::data::TypedVisitor::visitBool
virtual void visitBool(DataInput &, TypeInput &)
Definition: Visitor.h:105
armarx::aron::data::TypedVisitor::visitObject
virtual void visitObject(DataInput &, TypeInput &)
Definition: Visitor.h:90
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::aron::data::Visitor::visitList
virtual void visitList(Input &)
Definition: Visitor.h:69
armarx::aron::type::Descriptor::INT
@ INT
armarx::aron::data::TypedVisitorBase< data::VariantPtr, const type::VariantPtr >::TypeInput
const type::VariantPtr TypeInput
Definition: Visitor.h:54
armarx::aron::data::TypedVisitor::visitTuple
virtual void visitTuple(DataInput &, TypeInput &)
Definition: Visitor.h:93
armarx::aron::type::Descriptor::INT_ENUM
@ INT_ENUM
armarx::aron::type::Descriptor::OBJECT
@ OBJECT
armarx::aron::type::Descriptor::UNKNOWN
@ UNKNOWN
armarx::aron::data::Descriptor::LONG
@ LONG
armarx::aron::type::Descriptor::DICT
@ DICT
armarx::aron::type::Descriptor::TUPLE
@ TUPLE
armarx::aron::data::visit
requires isVisitor< VisitorImplementation, typename VisitorImplementation::Input > void visit(VisitorImplementation &v, typename VisitorImplementation::Input &o)
Definition: Visitor.h:124
armarx::aron::data::Descriptor::DOUBLE
@ DOUBLE
armarx::aron::type::Descriptor::POINTCLOUD
@ POINTCLOUD
armarx::aron::data::TypedVisitor::~TypedVisitor
virtual ~TypedVisitor()=default
armarx::aron::data::TypedVisitorBase::getDescriptor
virtual type::Descriptor getDescriptor(DataInput &, TypeInput &)=0
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::aron::data::TypedVisitor::visitUnknown
virtual void visitUnknown(DataInput &, TypeInput &)
Definition: Visitor.h:108
armarx::aron::data::Descriptor::UNKNOWN
@ UNKNOWN
armarx::aron::data::TypedVisitor::visitString
virtual void visitString(DataInput &, TypeInput &)
Definition: Visitor.h:106
armarx::aron::data::VisitorBase
Definition: Visitor.h:39
armarx::aron::type::Descriptor
Descriptor
Definition: Descriptor.h:76
armarx::aron::data::VisitorBase< data::VariantPtr >::Input
data::VariantPtr Input
Definition: Visitor.h:41
armarx::aron::type::Descriptor::ANY_OBJECT
@ ANY_OBJECT
armarx::aron::data::VisitorBase::getDescriptor
virtual data::Descriptor getDescriptor(Input &)=0
armarx::aron::data::TypedVisitor::visitPair
virtual void visitPair(DataInput &, TypeInput &)
Definition: Visitor.h:92
armarx::aron::data::Visitor::~Visitor
virtual ~Visitor()=default