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 w.writeImportance(last_returned, r.readImportance(o));
87 };
88
89 void
91 {
92 std::vector<ReaderInputTypeNonConst> elementsOfInput;
93 std::vector<WriterReturnType> elementsReturn;
94 Path p;
95 r.readList(o, elementsOfInput, p);
96 for (const auto& value : elementsOfInput)
97 {
98 auto converted = readAndWrite<DerivedT>(value);
99 elementsReturn.push_back(converted);
100 }
101 last_returned = w.writeList(elementsReturn, p);
102 w.writeImportance(last_returned, r.readImportance(o));
103 };
104
105 void
107 {
108 std::string type;
109 std::vector<int> shape;
110 std::vector<unsigned char> data;
111 Path p;
112 r.readNDArray(o, shape, type, data, p);
113 last_returned = w.writeNDArray(shape, type, data.data(), p);
114 w.writeImportance(last_returned, r.readImportance(o));
115 };
116
117 void
119 {
120 int i;
121 Path p;
122 r.readInt(o, i, p);
123 last_returned = w.writeInt(i, p);
124 w.writeImportance(last_returned, r.readImportance(o));
125 };
126
127 void
129 {
130 long i;
131 Path p;
132 r.readLong(o, i, p);
133 last_returned = w.writeLong(i, p);
134 w.writeImportance(last_returned, r.readImportance(o));
135 };
136
137 void
139 {
140 float i;
141 Path p;
142 r.readFloat(o, i, p);
143 last_returned = w.writeFloat(i, p);
144 w.writeImportance(last_returned, r.readImportance(o));
145 };
146
147 void
149 {
150 double i;
151 Path p;
152 r.readDouble(o, i, p);
153 last_returned = w.writeDouble(i, p);
154 w.writeImportance(last_returned, r.readImportance(o));
155 };
156
157 void
159 {
160 bool i;
161 Path p;
162 r.readBool(o, i, p);
163 last_returned = w.writeBool(i, p);
164 w.writeImportance(last_returned, r.readImportance(o));
165 };
166
167 void
169 {
170 std::string i;
171 Path p;
172 r.readString(o, i, p);
173 last_returned = w.writeString(i, p);
174 w.writeImportance(last_returned, r.readImportance(o));
175 };
176
177 void
179 {
180 if (!r.readNull(o))
181 {
182 throw error::AronException(__PRETTY_FUNCTION__,
183 "A visitor got data but the enum is unknown.");
184 }
185 w.writeNull();
186 };
187 };
188
189 /// the function to read from a variant and write to a writer T
190 /// returns the returntype of T
191 template <class ConverterImplementation>
192 requires isConverter<ConverterImplementation>
193 typename ConverterImplementation::WriterReturnType
194 readAndWrite(typename ConverterImplementation::ReaderInputType& o)
195 {
196 ConverterImplementation v;
197 data::visit(v, o);
198 return v.last_returned;
199 }
200} // 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:194
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:148
void visitFloat(ReaderInputType &o) final
Definition Converter.h:138
ReaderImplementation ReaderType
Definition Converter.h:54
void visitNDArray(ReaderInputType &o) final
Definition Converter.h:106
WriterReturnType last_returned
Definition Converter.h:62
void visitBool(ReaderInputType &o) final
Definition Converter.h:158
ReaderImplementation r
Definition Converter.h:60
data::Descriptor getDescriptor(ReaderInputType &o) final
Definition Converter.h:67
void visitLong(ReaderInputType &o) final
Definition Converter.h:128
void visitString(ReaderInputType &o) final
Definition Converter.h:168
void visitUnknown(ReaderInputType &o) final
Definition Converter.h:178
void visitInt(ReaderInputType &o) final
Definition Converter.h:118
typename ReaderImplementation::InputType ReaderInputType
Definition Converter.h:57
virtual ~Converter()=default
void visitList(ReaderInputType &o) final
Definition Converter.h:90
void visitDict(ReaderInputType &o) final
Definition Converter.h:73