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