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
30namespace armarx::aron::data
31{
32 // prototypes
33 template <class ReaderImplementation, class WriterImplementation, class DerivedT>
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>
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>
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
The Path class.
Definition Path.h:36
A base class for aron exceptions.
Definition Exception.h:37
A convenience header to include all aron files (full include, not forward declared)
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
void visit(VisitorImplementation &v, typename VisitorImplementation::Input &o)
Definition Visitor.h:136
A convenience header to include all aron files (full include, not forward declared)
Converter struct providing the needed methods.
Definition Converter.h:52
WriterImplementation WriterType
Definition Converter.h:53
WriterImplementation w
Definition Converter.h:61
typename ReaderImplementation::InputTypeNonConst ReaderInputTypeNonConst
Definition Converter.h:58
typename WriterImplementation::ReturnType WriterReturnType
Definition Converter.h:56
void visitDouble(ReaderInputType &o) final
Definition Converter.h:142
void visitFloat(ReaderInputType &o) final
Definition Converter.h:133
ReaderImplementation ReaderType
Definition Converter.h:54
void visitNDArray(ReaderInputType &o) final
Definition Converter.h:104
WriterReturnType last_returned
Definition Converter.h:62
void visitBool(ReaderInputType &o) final
Definition Converter.h:151
ReaderImplementation r
Definition Converter.h:60
data::Descriptor getDescriptor(ReaderInputType &o) final
Definition Converter.h:67
void visitLong(ReaderInputType &o) final
Definition Converter.h:124
void visitString(ReaderInputType &o) final
Definition Converter.h:160
void visitUnknown(ReaderInputType &o) final
Definition Converter.h:169
void visitInt(ReaderInputType &o) final
Definition Converter.h:115
typename ReaderImplementation::InputType ReaderInputType
Definition Converter.h:57
virtual ~Converter()=default
void visitList(ReaderInputType &o) final
Definition Converter.h:89
void visitDict(ReaderInputType &o) final
Definition Converter.h:73