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::data
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  struct Converter : virtual public Visitor<typename ReaderImplementation::InputType>
52  {
53  using WriterType = WriterImplementation;
54  using ReaderType = ReaderImplementation;
55  using This = DerivedT;
56  using WriterReturnType = typename WriterImplementation::ReturnType;
57  using ReaderInputType = typename ReaderImplementation::InputType;
58  using ReaderInputTypeNonConst = typename ReaderImplementation::InputTypeNonConst;
59 
60  ReaderImplementation r;
61  WriterImplementation w;
63 
64  virtual ~Converter() = default;
65 
68  {
69  return r.getDescriptor(o);
70  }
71 
72  void
74  {
75  std::map<std::string, ReaderInputTypeNonConst> elementsOfInput;
76  std::map<std::string, WriterReturnType> elementsReturn;
77  Path p;
78  r.readDict(o, elementsOfInput, p);
79  for (const auto& [key, value] : elementsOfInput)
80  {
81  auto converted = readAndWrite<DerivedT>(value);
82  elementsReturn.insert({key, converted});
83  }
84 
85  last_returned = w.writeDict(elementsReturn, std::nullopt, p);
86  };
87 
88  void
90  {
91  std::vector<ReaderInputTypeNonConst> elementsOfInput;
92  std::vector<WriterReturnType> elementsReturn;
93  Path p;
94  r.readList(o, elementsOfInput, p);
95  for (const auto& value : elementsOfInput)
96  {
97  auto converted = readAndWrite<DerivedT>(value);
98  elementsReturn.push_back(converted);
99  }
100  last_returned = w.writeList(elementsReturn, p);
101  };
102 
103  void
105  {
106  std::string type;
107  std::vector<int> shape;
108  std::vector<unsigned char> data;
109  Path p;
110  r.readNDArray(o, shape, type, data, p);
111  last_returned = w.writeNDArray(shape, type, data.data(), p);
112  };
113 
114  void
116  {
117  int i;
118  Path p;
119  r.readInt(o, i, p);
120  last_returned = w.writeInt(i, p);
121  };
122 
123  void
125  {
126  long i;
127  Path p;
128  r.readLong(o, i, p);
129  last_returned = w.writeLong(i, p);
130  };
131 
132  void
134  {
135  float i;
136  Path p;
137  r.readFloat(o, i, p);
138  last_returned = w.writeFloat(i, p);
139  };
140 
141  void
143  {
144  double i;
145  Path p;
146  r.readDouble(o, i, p);
147  last_returned = w.writeDouble(i, p);
148  };
149 
150  void
152  {
153  bool i;
154  Path p;
155  r.readBool(o, i, p);
156  last_returned = w.writeBool(i, p);
157  };
158 
159  void
161  {
162  std::string i;
163  Path p;
164  r.readString(o, i, p);
165  last_returned = w.writeString(i, p);
166  };
167 
168  void
170  {
171  if (!r.readNull(o))
172  {
173  throw error::AronException(__PRETTY_FUNCTION__,
174  "A visitor got data but the enum is unknown.");
175  }
176  w.writeNull();
177  };
178  };
179 
180  /// the function to read from a variant and write to a writer T
181  /// returns the returntype of T
182  template <class ConverterImplementation>
183  requires isConverter<ConverterImplementation>
184  typename ConverterImplementation::WriterReturnType
185  readAndWrite(typename ConverterImplementation::ReaderInputType& o)
186  {
187  ConverterImplementation v;
188  data::visit(v, o);
189  return v.last_returned;
190  }
191 } // namespace armarx::aron::data
armarx::aron::data::Converter< ReaderImplementation, aron::data::writer::NlohmannJSONWriter, ToNlohmannJSONConverter< ReaderImplementation > >::ReaderInputTypeNonConst
typename ReaderImplementation::InputTypeNonConst ReaderInputTypeNonConst
Definition: Converter.h:58
armarx::aron::error::AronException
A base class for aron exceptions.
Definition: Exception.h:36
armarx::aron::data::Converter::visitFloat
void visitFloat(ReaderInputType &o) final
Definition: Converter.h:133
armarx::aron::data::Converter::visitList
void visitList(ReaderInputType &o) final
Definition: Converter.h:89
armarx::aron::data::Converter::getDescriptor
data::Descriptor getDescriptor(ReaderInputType &o) final
Definition: Converter.h:67
armarx::aron::data::Converter< ReaderImplementation, aron::data::writer::NlohmannJSONWriter, ToNlohmannJSONConverter< ReaderImplementation > >::ReaderType
ReaderImplementation ReaderType
Definition: Converter.h:54
armarx::aron::data::Visitor
Definition: Visitor.h:64
armarx::aron::data::writer::NlohmannJSONWriter
Definition: NlohmannJSONWriter.h:36
armarx::aron::data::Descriptor
Descriptor
Definition: Descriptor.h:179
armarx::aron::data::Converter
Converter struct providing the needed methods.
Definition: Converter.h:35
armarx::aron::Path
The Path class.
Definition: Path.h:35
armarx::aron::data::Converter::visitInt
void visitInt(ReaderInputType &o) final
Definition: Converter.h:115
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::aron::data::Converter::visitDouble
void visitDouble(ReaderInputType &o) final
Definition: Converter.h:142
armarx::aron::data::Converter::visitBool
void visitBool(ReaderInputType &o) final
Definition: Converter.h:151
armarx::aron::data::isConverter
concept isConverter
Definition: Converter.h:38
armarx::aron::data::Converter::last_returned
WriterReturnType last_returned
Definition: Converter.h:62
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::aron::data::Converter< ReaderImplementation, aron::data::writer::NlohmannJSONWriter, ToNlohmannJSONConverter< ReaderImplementation > >::ReaderInputType
typename ReaderImplementation::InputType ReaderInputType
Definition: Converter.h:57
armarx::aron::data::Converter::visitString
void visitString(ReaderInputType &o) final
Definition: Converter.h:160
armarx::aron::data::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:185
armarx::aron::requires
requires(!aron::detail::DtoAndBoAreSame< DtoT, BoT >) void toAron(std
Definition: aron_conversions.h:129
armarx::aron::data
A convenience header to include all aron files (full include, not forward declared)
Definition: aron_conversions.cpp:3
armarx::aron::data::Converter::visitUnknown
void visitUnknown(ReaderInputType &o) final
Definition: Converter.h:169
armarx::aron::data::Converter< ReaderImplementation, aron::data::writer::NlohmannJSONWriter, ToNlohmannJSONConverter< ReaderImplementation > >::WriterReturnType
typename aron::data::writer::NlohmannJSONWriter ::ReturnType WriterReturnType
Definition: Converter.h:56
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::aron::data::Converter::visitLong
void visitLong(ReaderInputType &o) final
Definition: Converter.h:124
armarx::aron::data::visit
requires isVisitor< VisitorImplementation, typename VisitorImplementation::Input > void visit(VisitorImplementation &v, typename VisitorImplementation::Input &o)
Definition: Visitor.h:136
armarx::aron::data::Converter::visitDict
void visitDict(ReaderInputType &o) final
Definition: Converter.h:73
armarx::aron::data::Converter::~Converter
virtual ~Converter()=default
T
float T
Definition: UnscentedKalmanFilterTest.cpp:38
armarx::aron::data::Converter::r
ReaderImplementation r
Definition: Converter.h:60
armarx::aron::data::Converter::w
WriterImplementation w
Definition: Converter.h:61
armarx::aron::data::ToNlohmannJSONConverter
Definition: NlohmannJSONConverter.h:47
armarx::aron::data::Converter::visitNDArray
void visitNDArray(ReaderInputType &o) final
Definition: Converter.h:104