Converter.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 "../rw/Reader.h"
27 #include "../rw/Writer.h"
28 #include "../visitor/Visitor.h"
29 
30 namespace armarx::aron::type
31 {
32  // prototypes
33  template <class ReaderImplementation, class WriterImplementation, class DerivedT>
34  requires isReader<ReaderImplementation> && isWriter<WriterImplementation>
35  struct Converter;
36 
37  template <class T>
38  concept isConverter =
39  std::is_base_of<Converter<typename T::ReaderType, typename T::WriterType, typename T::This>,
40  T>::value;
41 
42  template <class ConverterImplementation>
43  requires isConverter<ConverterImplementation>
44  typename ConverterImplementation::WriterReturnType
45  readAndWrite(typename ConverterImplementation::ReaderInputType& o);
46 
47  /// Converter struct providing the needed methods.
48  /// WriterImplementation is a writer class, TODO: add concepts
49  template <class ReaderImplementation, class WriterImplementation, class DerivedT>
50  requires isReader<ReaderImplementation> && isWriter<WriterImplementation>
51 
52  struct Converter : virtual public Visitor<typename ReaderImplementation::InputType>
53  {
54  using WriterType = WriterImplementation;
55  using ReaderType = ReaderImplementation;
56  using This = DerivedT;
57  using WriterReturnType = typename WriterImplementation::ReturnType;
58  using ReaderInputType = typename ReaderImplementation::InputType;
59  using ReaderInputTypeNonConst = typename ReaderImplementation::InputTypeNonConst;
60 
61  ReaderImplementation r;
62  WriterImplementation w;
64 
65  virtual ~Converter() = default;
66 
69  {
70  return r.getDescriptor(o);
71  }
72 
73  void
75  {
76  std::map<std::string, ReaderInputTypeNonConst> elementsOfInput;
77  std::string name;
78  std::vector<std::string> templates;
79  std::vector<std::string> templateInstantiations;
80  type::Maybe maybe;
81  std::map<std::string, WriterReturnType> elementsReturn;
82  Path p;
83  r.readObject(o, name, templates, templateInstantiations, elementsOfInput, maybe, p);
84  for (const auto& [key, value] : elementsOfInput)
85  {
86  auto converted = readAndWrite<DerivedT>(value);
87  elementsReturn.insert({key, converted});
88  }
89 
90  last_returned = w.writeObject(
91  name, templates, templateInstantiations, elementsReturn, std::nullopt, maybe, p);
92  }
93 
94  void
96  {
97  ReaderInputTypeNonConst acceptedType;
98  type::Maybe maybe;
99  Path p;
100  r.readDict(o, acceptedType, maybe, p);
101 
102  auto converted = readAndWrite<DerivedT>(acceptedType);
103 
104  last_returned = w.writeDict(converted, maybe, p);
105  };
106 
107  void
109  {
110  ReaderInputTypeNonConst acceptedType;
111  type::Maybe maybe;
112  Path p;
113  r.readList(o, acceptedType, maybe, p);
114 
115  auto converted = readAndWrite<DerivedT>(acceptedType);
116 
117  last_returned = w.writeList(converted, maybe, p);
118  };
119 
120  void
122  {
123  ReaderInputTypeNonConst acceptedType1;
124  ReaderInputTypeNonConst acceptedType2;
125  type::Maybe maybe;
126  Path p;
127  r.readPair(o, acceptedType1, acceptedType2, maybe, p);
128 
129  auto converted1 = readAndWrite<DerivedT>(acceptedType1);
130  auto converted2 = readAndWrite<DerivedT>(acceptedType2);
131 
132  last_returned = w.writePair(converted1, converted2, maybe, p);
133  };
134 
135  void
137  {
138  std::vector<ReaderInputTypeNonConst> acceptedTypes;
139  std::vector<WriterReturnType> elementsReturn;
140  type::Maybe maybe;
141  Path p;
142  r.readTuple(o, acceptedTypes, maybe, p);
143 
144  for (const auto& el : acceptedTypes)
145  {
146  auto converted = readAndWrite<DerivedT>(el);
147  elementsReturn.push_back(converted);
148  }
149 
150  last_returned = w.writeTuple(elementsReturn, maybe, p);
151  };
152 
153  void
155  {
156  type::Maybe maybe;
158  std::string defaultValue;
159  int ndim;
160  Path p;
161  r.readNDArray(o, ndim, type, defaultValue, maybe, p);
162  last_returned = w.writeNDArray(ndim, type, defaultValue, maybe, p);
163  };
164 
165  void
167  {
168  type::Maybe maybe;
170  std::string defaultValue;
171  int rows;
172  int cols;
173  Path p;
174  r.readMatrix(o, rows, cols, type, defaultValue, maybe, p);
175  last_returned = w.writeMatrix(rows, cols, type, defaultValue, maybe, p);
176  };
177 
178  void
180  {
181  type::Maybe maybe;
183  std::string defaultValue;
184  Path p;
185  r.readQuaternion(o, type, defaultValue, maybe, p);
186  last_returned = w.writeQuaternion(type, defaultValue, maybe, p);
187  };
188 
189  void
191  {
192  type::Maybe maybe;
193  type::image::PixelType type;
194  std::string defaultValue;
195  Path p;
196  r.readImage(o, type, defaultValue, maybe, p);
197  last_returned = w.writeImage(type, defaultValue, maybe, p);
198  };
199 
200  void
202  {
203  type::Maybe maybe;
204  type::pointcloud::VoxelType type;
205  std::string defaultValue;
206  Path p;
207  r.readPointCloud(o, type, defaultValue, maybe, p);
208  last_returned = w.writePointCloud(type, defaultValue, maybe, p);
209  };
210 
211  void
213  {
214  type::Maybe maybe;
215  std::string name;
216  std::map<std::string, int> values;
217  std::string defaultValue;
218  Path p;
219  r.readIntEnum(o, name, values, defaultValue, maybe, p);
220  last_returned = w.writeIntEnum(name, values, defaultValue, maybe, p);
221  };
222 
223  void
225  {
226  type::Maybe maybe;
227  Path p;
228  std::optional<int> defaultValue;
229  r.readInt(o, defaultValue, maybe, p);
230  last_returned = w.writeInt(defaultValue, maybe, p);
231  };
232 
233  void
235  {
236  type::Maybe maybe;
237  Path p;
238  std::optional<long> defaultValue;
239  r.readLong(o, defaultValue, maybe, p);
240  last_returned = w.writeLong(defaultValue, maybe, p);
241  };
242 
243  void
245  {
246  type::Maybe maybe;
247  Path p;
248  std::optional<float> defaultValue;
249  r.readFloat(o, defaultValue, maybe, p);
250  last_returned = w.writeFloat(defaultValue, maybe, p);
251  };
252 
253  void
255  {
256  type::Maybe maybe;
257  Path p;
258  std::optional<double> defaultValue;
259  r.readDouble(o, defaultValue, maybe, p);
260  last_returned = w.writeDouble(defaultValue, maybe, p);
261  };
262 
263  void
265  {
266  type::Maybe maybe;
267  Path p;
268  std::optional<bool> defaultValue;
269  r.readBool(o, defaultValue, maybe, p);
270  last_returned = w.writeBool(defaultValue, maybe, p);
271  };
272 
273  void
275  {
276  type::Maybe maybe;
277  Path p;
278  std::optional<std::string> defaultValue;
279  r.readString(o, defaultValue, maybe, p);
280  last_returned = w.writeString(defaultValue, maybe, p);
281  };
282 
283  void
285  {
286  if (!r.readNull(o))
287  {
288  throw error::AronException(__PRETTY_FUNCTION__,
289  "A visitor got type but the enum is unknown.");
290  }
291  w.writeNull();
292  }
293  };
294 
295  /// the function to read from a variant and write to a writer T
296  /// returns the returntype of T
297  template <class ConverterImplementation>
298  requires isConverter<ConverterImplementation>
299 
300  typename ConverterImplementation::WriterReturnType
301  readAndWrite(typename ConverterImplementation::ReaderInputType& o)
302  {
303  ConverterImplementation v;
304  type::visit(v, o);
305  return v.last_returned;
306  }
307 } // namespace armarx::aron::type
armarx::aron::type::Converter::visitDouble
void visitDouble(ReaderInputType &o) final
Definition: Converter.h:254
armarx::aron::type::Converter::w
WriterImplementation w
Definition: Converter.h:62
armarx::aron::error::AronException
A base class for aron exceptions.
Definition: Exception.h:42
armarx::aron::type::Converter::last_returned
WriterReturnType last_returned
Definition: Converter.h:63
armarx::aron::type::Converter::visitTuple
void visitTuple(ReaderInputType &o) final
Definition: Converter.h:136
armarx::aron::type::isConverter
concept isConverter
Definition: Converter.h:38
armarx::aron::type::Converter::visitIntEnum
void visitIntEnum(ReaderInputType &o) final
Definition: Converter.h:212
armarx::aron::type::Converter::visitMatrix
void visitMatrix(ReaderInputType &o) final
Definition: Converter.h:166
armarx::aron::type::Visitor
The Visitor struct.
Definition: Visitor.h:104
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
armarx::aron::type::Converter::r
ReaderImplementation r
Definition: Converter.h:61
armarx::aron::type::Converter::visitUnknown
void visitUnknown(ReaderInputType &o) final
Definition: Converter.h:284
armarx::aron::type::Converter::visitString
void visitString(ReaderInputType &o) final
Definition: Converter.h:274
armarx::aron::type::Converter::visitLong
void visitLong(ReaderInputType &o) final
Definition: Converter.h:234
armarx::aron::type::Converter::visitFloat
void visitFloat(ReaderInputType &o) final
Definition: Converter.h:244
armarx::aron::type::Converter< aron::type::reader::NlohmannJSONReader, WriterImplementation, FromNlohmannJSONConverter< WriterImplementation > >::WriterReturnType
typename WriterImplementation::ReturnType WriterReturnType
Definition: Converter.h:57
armarx::aron::type::Converter::~Converter
virtual ~Converter()=default
armarx::aron::Path
The Path class.
Definition: Path.h:36
armarx::aron::type::Converter::visitBool
void visitBool(ReaderInputType &o) final
Definition: Converter.h:264
armarx::aron::type::Converter::visitPointCloud
void visitPointCloud(ReaderInputType &o) final
Definition: Converter.h:201
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::aron::type::Converter< aron::type::reader::NlohmannJSONReader, WriterImplementation, FromNlohmannJSONConverter< WriterImplementation > >::ReaderInputTypeNonConst
typename aron::type::reader::NlohmannJSONReader ::InputTypeNonConst ReaderInputTypeNonConst
Definition: Converter.h:59
armarx::aron::type::Converter::visitPair
void visitPair(ReaderInputType &o) final
Definition: Converter.h:121
armarx::aron::type::Converter< aron::type::reader::NlohmannJSONReader, WriterImplementation, FromNlohmannJSONConverter< WriterImplementation > >::WriterType
WriterImplementation WriterType
Definition: Converter.h:54
armarx::aron::type
A convenience header to include all aron files (full include, not forward declared)
Definition: aron_conversions.cpp:9
armarx::ElementTypes::ElementType
ElementType
Definition: AbstractObjectSerializer.h:32
armarx::aron::type::Converter::visitList
void visitList(ReaderInputType &o) final
Definition: Converter.h:108
armarx::aron::type::Converter
Converter struct providing the needed methods.
Definition: Converter.h:35
armarx::aron::type::Converter::visitDict
void visitDict(ReaderInputType &o) final
Definition: Converter.h:95
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::aron::type::Converter::visitImage
void visitImage(ReaderInputType &o) final
Definition: Converter.h:190
armarx::aron::type::reader::NlohmannJSONReader
Definition: NlohmannJSONReader.h:35
armarx::aron::type::readAndWrite
requires isConverter< ConverterImplementation > ConverterImplementation::WriterReturnType readAndWrite(typename ConverterImplementation::ReaderInputType &o)
the function to read from a variant and write to a writer T returns the returntype of T
Definition: Converter.h:301
armarx::aron::type::Converter::getDescriptor
type::Descriptor getDescriptor(ReaderInputType &o) final
Definition: Converter.h:68
armarx::aron::requires
requires(not detail::is_optional< BoT >::value) void toAron(std
Definition: aron_conversions.h:157
armarx::aron::type::FromNlohmannJSONConverter
Definition: NlohmannJSONConverter.h:36
armarx::aron::type::Converter::visitObject
void visitObject(ReaderInputType &o) final
Definition: Converter.h:74
armarx::aron::type::Converter< aron::type::reader::NlohmannJSONReader, WriterImplementation, FromNlohmannJSONConverter< WriterImplementation > >::ReaderInputType
typename aron::type::reader::NlohmannJSONReader ::InputType ReaderInputType
Definition: Converter.h:58
armarx::aron::type::visit
void visit(VisitorImplementation &v, typename VisitorImplementation::Input &t)
The visit function.
Definition: Visitor.h:42
armarx::aron::type::Converter::visitInt
void visitInt(ReaderInputType &o) final
Definition: Converter.h:224
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::aron::type::Converter::visitQuaternion
void visitQuaternion(ReaderInputType &o) final
Definition: Converter.h:179
armarx::aron::type::Descriptor
Descriptor
Definition: Descriptor.h:76
armarx::aron::type::Converter::visitNDArray
void visitNDArray(ReaderInputType &o) final
Definition: Converter.h:154