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  //ARMARX_INFO << "convert visitObject";
77 
78  std::map<std::string, ReaderInputTypeNonConst> elementsOfInput;
79  std::string name;
80  std::vector<std::string> templates;
81  std::vector<std::string> templateInstantiations;
82  type::Maybe maybe;
83  std::map<std::string, WriterReturnType> elementsReturn;
84  Path p;
85  r.readObject(o, name, templates, templateInstantiations, elementsOfInput, maybe, p);
86  for (const auto& [key, value] : elementsOfInput)
87  {
88  auto converted = readAndWrite<DerivedT>(value);
89  elementsReturn.insert({key, converted});
90  }
91 
92  last_returned = w.writeObject(
93  name, templates, templateInstantiations, elementsReturn, std::nullopt, maybe, p);
94  }
95 
96  void
98  {
99  //ARMARX_INFO << "convert visitDict";
100 
101  ReaderInputTypeNonConst acceptedType;
102  type::Maybe maybe;
103  Path p;
104  r.readDict(o, acceptedType, maybe, p);
105 
106  auto converted = readAndWrite<DerivedT>(acceptedType);
107 
108  last_returned = w.writeDict(converted, maybe, p);
109  };
110 
111  void
113  {
114  //ARMARX_INFO << "convert visitList";
115 
116  ReaderInputTypeNonConst acceptedType;
117  type::Maybe maybe;
118  Path p;
119  r.readList(o, acceptedType, maybe, p);
120 
121  auto converted = readAndWrite<DerivedT>(acceptedType);
122 
123  last_returned = w.writeList(converted, maybe, p);
124  };
125 
126  void
128  {
129  //ARMARX_INFO << "convert visitPair";
130 
131  ReaderInputTypeNonConst acceptedType1;
132  ReaderInputTypeNonConst acceptedType2;
133  type::Maybe maybe;
134  Path p;
135  r.readPair(o, acceptedType1, acceptedType2, maybe, p);
136 
137  auto converted1 = readAndWrite<DerivedT>(acceptedType1);
138  auto converted2 = readAndWrite<DerivedT>(acceptedType2);
139 
140  last_returned = w.writePair(converted1, converted2, maybe, p);
141  };
142 
143  void
145  {
146  //ARMARX_INFO << "convert visitTuple";
147 
148  std::vector<ReaderInputTypeNonConst> acceptedTypes;
149  std::vector<WriterReturnType> elementsReturn;
150  type::Maybe maybe;
151  Path p;
152  r.readTuple(o, acceptedTypes, maybe, p);
153 
154  for (const auto& el : acceptedTypes)
155  {
156  auto converted = readAndWrite<DerivedT>(el);
157  elementsReturn.push_back(converted);
158  }
159 
160  last_returned = w.writeTuple(elementsReturn, maybe, p);
161  };
162 
163  void
165  {
166  //ARMARX_INFO << "convert visitNDArray";
167 
168  type::Maybe maybe;
170  std::string defaultValue;
171  int ndim;
172  Path p;
173  r.readNDArray(o, ndim, type, defaultValue, maybe, p);
174  last_returned = w.writeNDArray(ndim, type, defaultValue, maybe, p);
175  };
176 
177  void
179  {
180  //ARMARX_INFO << "convert visitMatrix";
181 
182  type::Maybe maybe;
184  std::string defaultValue;
185  int rows;
186  int cols;
187  Path p;
188  r.readMatrix(o, rows, cols, type, defaultValue, maybe, p);
189  last_returned = w.writeMatrix(rows, cols, type, defaultValue, maybe, p);
190  };
191 
192  void
194  {
195  //ARMARX_INFO << "convert visitQuaternion";
196 
197  type::Maybe maybe;
199  std::string defaultValue;
200  Path p;
201  r.readQuaternion(o, type, defaultValue, maybe, p);
202  last_returned = w.writeQuaternion(type, defaultValue, maybe, p);
203  };
204 
205  void
207  {
208  //ARMARX_INFO << "convert visitImage";
209 
210  type::Maybe maybe;
211  type::image::PixelType type;
212  std::string defaultValue;
213  Path p;
214  r.readImage(o, type, defaultValue, maybe, p);
215  last_returned = w.writeImage(type, defaultValue, maybe, p);
216  };
217 
218  void
220  {
221  //ARMARX_INFO << "convert visitPointCloud";
222 
223  type::Maybe maybe;
224  type::pointcloud::VoxelType type;
225  std::string defaultValue;
226  Path p;
227  r.readPointCloud(o, type, defaultValue, maybe, p);
228  last_returned = w.writePointCloud(type, defaultValue, maybe, p);
229  };
230 
231  void
233  {
234  //ARMARX_INFO << "convert visitIntEnum";
235 
236  type::Maybe maybe;
237  std::string name;
238  std::map<std::string, int> values;
239  std::string defaultValue;
240  Path p;
241  r.readIntEnum(o, name, values, defaultValue, maybe, p);
242  last_returned = w.writeIntEnum(name, values, defaultValue, maybe, p);
243  };
244 
245  void
247  {
248  //ARMARX_INFO << "convert visitInt";
249 
250  type::Maybe maybe;
251  Path p;
252  std::optional<int> defaultValue;
253  r.readInt(o, defaultValue, maybe, p);
254  last_returned = w.writeInt(defaultValue, maybe, p);
255  };
256 
257  void
259  {
260  //ARMARX_INFO << "convert visitLong";
261 
262  type::Maybe maybe;
263  Path p;
264  std::optional<long> defaultValue;
265  r.readLong(o, defaultValue, maybe, p);
266  last_returned = w.writeLong(defaultValue, maybe, p);
267  };
268 
269  void
271  {
272  //ARMARX_INFO << "convert visitFloat";
273 
274  type::Maybe maybe;
275  Path p;
276  std::optional<float> defaultValue;
277  r.readFloat(o, defaultValue, maybe, p);
278  last_returned = w.writeFloat(defaultValue, maybe, p);
279  };
280 
281  void
283  {
284  //ARMARX_INFO << "convert visitDouble";
285 
286  type::Maybe maybe;
287  Path p;
288  std::optional<double> defaultValue;
289  r.readDouble(o, defaultValue, maybe, p);
290  last_returned = w.writeDouble(defaultValue, maybe, p);
291  };
292 
293  void
295  {
296  //ARMARX_INFO << "convert visitBool";
297 
298  type::Maybe maybe;
299  Path p;
300  std::optional<bool> defaultValue;
301  r.readBool(o, defaultValue, maybe, p);
302  last_returned = w.writeBool(defaultValue, maybe, p);
303  };
304 
305  void
307  {
308  //ARMARX_INFO << "convert visitString";
309 
310  type::Maybe maybe;
311  Path p;
312  std::optional<std::string> defaultValue;
313  r.readString(o, defaultValue, maybe, p);
314  last_returned = w.writeString(defaultValue, maybe, p);
315  };
316 
317  void
319  {
320  //ARMARX_INFO << "convert visitUnknown";
321 
322  if (!r.readNull(o))
323  {
324  throw error::AronException(__PRETTY_FUNCTION__,
325  "A visitor got type but the enum is unknown.");
326  }
327  w.writeNull();
328  }
329  };
330 
331  /// the function to read from a variant and write to a writer T
332  /// returns the returntype of T
333  template <class ConverterImplementation>
334  requires isConverter<ConverterImplementation>
335 
336  typename ConverterImplementation::WriterReturnType
337  readAndWrite(typename ConverterImplementation::ReaderInputType& o)
338  {
339  ConverterImplementation v;
340  type::visit(v, o);
341  return v.last_returned;
342  }
343 } // namespace armarx::aron::type
armarx::aron::type::Converter::visitDouble
void visitDouble(ReaderInputType &o) final
Definition: Converter.h:282
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:144
armarx::aron::type::isConverter
concept isConverter
Definition: Converter.h:38
armarx::aron::type::Converter::visitIntEnum
void visitIntEnum(ReaderInputType &o) final
Definition: Converter.h:232
armarx::aron::type::Converter::visitMatrix
void visitMatrix(ReaderInputType &o) final
Definition: Converter.h:178
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:318
armarx::aron::type::Converter::visitString
void visitString(ReaderInputType &o) final
Definition: Converter.h:306
armarx::aron::type::Converter::visitLong
void visitLong(ReaderInputType &o) final
Definition: Converter.h:258
armarx::aron::type::Converter::visitFloat
void visitFloat(ReaderInputType &o) final
Definition: Converter.h:270
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:294
armarx::aron::type::Converter::visitPointCloud
void visitPointCloud(ReaderInputType &o) final
Definition: Converter.h:219
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:127
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::aron::requires
requires(!aron::detail::DtoAndBoAreSame< DtoT, BoT >) void toAron(std
Definition: aron_conversions.h:127
armarx::ElementTypes::ElementType
ElementType
Definition: AbstractObjectSerializer.h:32
armarx::aron::type::Converter::visitList
void visitList(ReaderInputType &o) final
Definition: Converter.h:112
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:97
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:206
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:337
armarx::aron::type::Converter::getDescriptor
type::Descriptor getDescriptor(ReaderInputType &o) final
Definition: Converter.h:68
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:246
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::aron::type::Converter::visitQuaternion
void visitQuaternion(ReaderInputType &o) final
Definition: Converter.h:193
armarx::aron::type::Descriptor
Descriptor
Definition: Descriptor.h:76
armarx::aron::type::Converter::visitNDArray
void visitNDArray(ReaderInputType &o) final
Definition: Converter.h:164