Reader.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @author Fabian Peller (fabian dot peller at kit dot edu)
17 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
18 * GNU General Public License
19 */
20 
21 #pragma once
22 
23 // STD/STL
24 #include <memory>
25 #include <optional>
26 #include <string>
27 
28 // ArmarX
29 #include <RobotAPI/interface/aron.h>
31 
32 namespace armarx::aron::type
33 {
34  /**
35  * @brief The ReaderInterface class. It defines the interface to extract information from an aron representation
36  */
37  template <class I>
39  {
40  public:
41  using InputType = I;
42  using InputTypeNonConst = typename std::remove_const<InputType>::type;
43 
44  virtual ~ReaderInterface() = default;
45 
47 
48  /// Extract information from an Object type
49  virtual void readObject(const InputType& input,
50  std::string& name,
51  std::vector<std::string>& templates,
52  std::vector<std::string>& templateInstantiations,
53  std::map<std::string, InputTypeNonConst>& memberTypes,
54  type::Maybe& maybe,
55  Path& p) = 0;
56 
57  /// Extract information from a list type
58  virtual void readList(const InputType& input,
59  InputTypeNonConst& acceptedType,
60  type::Maybe& maybe,
61  Path& p) = 0;
62 
63  /// Extract information from a dict type
64  virtual void readDict(const InputType& input,
65  InputTypeNonConst& acceptedType,
66  type::Maybe& maybe,
67  Path& p) = 0;
68 
69  /// Extract information from a tuple type
70  virtual void readTuple(const InputType& input,
71  std::vector<InputTypeNonConst>& acceptedTypes,
72  type::Maybe& maybe,
73  Path& p) = 0;
74 
75  /// Extract information from a pair type
76  virtual void readPair(const InputType& input,
77  InputTypeNonConst& acceptedTypes1,
78  InputTypeNonConst& acceptedTypes2,
79  type::Maybe& maybe,
80  Path& p) = 0;
81 
82  /// Extract information from a ndarray type
83  virtual void readNDArray(const InputType& input,
84  int& ndim,
86  std::string& defaultValue,
87  type::Maybe& maybe,
88  Path& p) = 0;
89 
90  /// Extract information from a matrix type
91  virtual void readMatrix(const InputType& input,
92  int& rows,
93  int& cols,
95  std::string& defaultValue,
96  type::Maybe& maybe,
97  Path& p) = 0;
98 
99  /// Extract information from a quaternion type
100  virtual void readQuaternion(const InputType& input,
102  std::string& defaultValue,
103  type::Maybe& maybe,
104  Path& p) = 0;
105 
106  /// Extract information from a pointcloud type
107  virtual void readPointCloud(const InputType& input,
108  type::pointcloud::VoxelType& type,
109  std::string& defaultValue,
110  type::Maybe& maybe,
111  Path& p) = 0;
112 
113  /// Extract information from an image type
114  virtual void readImage(const InputType& input,
115  type::image::PixelType& type,
116  std::string& defaultValue,
117  type::Maybe& maybe,
118  Path& p) = 0;
119 
120  /// Extract information from an int enum type
121  virtual void readIntEnum(const InputType& input,
122  std::string& name,
123  std::map<std::string, int>& acceptedValues,
124  std::string& defaultValue,
125  type::Maybe& maybe,
126  Path& p) = 0;
127 
128  /// Extract information from an int type
129  virtual void readInt(const InputType& input,
130  std::optional<int>& defaultValue,
131  type::Maybe& maybe,
132  Path& p) = 0;
133 
134  /// Extract information from an long type
135  virtual void readLong(const InputType& input,
136  std::optional<long>& defaultValue,
137  type::Maybe& maybe,
138  Path& p) = 0;
139 
140  /// Extract information from an float type
141  virtual void readFloat(const InputType& input,
142  std::optional<float>& defaultValue,
143  type::Maybe& maybe,
144  Path& p) = 0;
145 
146  /// Extract information from an double type
147  virtual void readDouble(const InputType& input,
148  std::optional<double>& defaultValue,
149  type::Maybe& maybe,
150  Path& p) = 0;
151 
152  /// Extract information from an string type
153  virtual void readString(const InputType& input,
154  std::optional<std::string>& defaultValue,
155  type::Maybe& maybe,
156  Path& p) = 0;
157 
158  /// Extract information from an bool type
159  virtual void readBool(const InputType& input,
160  std::optional<bool>& defaultValue,
161  type::Maybe& maybe,
162  Path& p) = 0;
163 
164  /// Extract information from an time type
165  virtual void readAnyObject(const InputType& input, type::Maybe& maybe, Path& p) = 0;
166 
167  /// Check if input is null
168  virtual bool
169  readNull(InputType& input) // defaulted implementation
170  {
171  InputType nil = {};
172  return input == nil;
173  }
174 
175  // Convenience methods without path
176  /*void readObject(const InputType& input, std::string& name, std::map<std::string, InputTypeNonConst>& memberTypes, type::Maybe& maybe)
177  {
178  Path p;
179  return readObject(input, name, memberTypes, maybe, p);
180  }
181 
182  void readList(const InputType& input, InputTypeNonConst& acceptedType, type::Maybe& maybe)
183  {
184  Path p;
185  return readList(input, acceptedType, maybe, p);
186  }
187 
188  void readDict(const InputType& input, InputTypeNonConst& acceptedType, type::Maybe& maybe)
189  {
190  Path p;
191  return readDict(input, acceptedType, maybe, p);
192  }
193 
194  void readTuple(const InputType& input, std::vector<InputTypeNonConst>& acceptedTypes, type::Maybe& maybe)
195  {
196  Path p;
197  return readTuple(input, acceptedTypes, maybe, p);
198  }
199 
200  void readPair(const InputType& input, InputTypeNonConst& acceptedTypes1, InputTypeNonConst& acceptedTypes2, type::Maybe& maybe)
201  {
202  Path p;
203  return readPair(input, acceptedTypes1, acceptedTypes2, maybe, p);
204  }
205 
206  void readNDArray(const InputType& input, int& ndim, type::ndarray::ElementType& type, type::Maybe& maybe)
207  {
208  Path p;
209  return readNDArray(input, ndim, type, maybe, p);
210  }
211 
212  void readMatrix(const InputType& input, int& rows, int& cols, type::matrix::ElementType& type, type::Maybe& maybe)
213  {
214  Path p;
215  return readMatrix(input, rows, cols, type, maybe, p);
216  }
217 
218  void readQuaternion(const InputType& input, type::quaternion::ElementType& type, type::Maybe& maybe)
219  {
220  Path p;
221  return readQuaternion(input, type, maybe, p);
222  }
223 
224  void readPointCloud(const InputType& input, type::pointcloud::VoxelType& type, type::Maybe& maybe)
225  {
226  Path p;
227  return readPointCloud(input, type, maybe, p);
228  }
229 
230  void readImage(const InputType& input, type::image::PixelType& type, type::Maybe& maybe)
231  {
232  Path p;
233  return readImage(input, type, maybe, p);
234  }
235 
236  void readIntEnum(const InputType& input, std::string& name, std::map<std::string, int>& acceptedValues, type::Maybe& maybe)
237  {
238  Path p;
239  return readIntEnum(input, name, acceptedValues, maybe, p);
240  }
241 
242  void readInt(const InputType& input, type::Maybe& maybe)
243  {
244  Path p;
245  return readInt(input, maybe, p);
246  }
247 
248  void readLong(const InputType& input, type::Maybe& maybe)
249  {
250  Path p;
251  return readLong(input, maybe, p);
252  }
253 
254  void readFloat(const InputType& input, type::Maybe& maybe)
255  {
256  Path p;
257  return readFloat(input, maybe, p);
258  }
259 
260  void readDouble(const InputType& input, type::Maybe& maybe)
261  {
262  Path p;
263  return readDouble(input, maybe, p);
264  }
265 
266  void readString(const InputType& input, type::Maybe& maybe)
267  {
268  Path p;
269  return readString(input, maybe, p);
270  }
271 
272  void readBool(const InputType& input, type::Maybe& maybe)
273  {
274  Path p;
275  return readBool(input, maybe, p);
276  }
277 
278  void readAnyObject(const InputType& input, type::any::AnyObjectType& t, type::Maybe& maybe)
279  {
280  Path p;
281  return readBool(input, t, maybe, p);
282  }*/
283  };
284 
285  template <class T>
286  concept isReader = std::is_base_of<ReaderInterface<typename T::InputType>, T>::value;
287 } // namespace armarx::aron::type
armarx::aron::type::ReaderInterface::readTuple
virtual void readTuple(const InputType &input, std::vector< InputTypeNonConst > &acceptedTypes, type::Maybe &maybe, Path &p)=0
Extract information from a tuple type.
armarx::aron::type::ReaderInterface< const nlohmann::json >::InputTypeNonConst
typename std::remove_const< InputType >::type InputTypeNonConst
Definition: Reader.h:42
armarx::aron::type::ReaderInterface::readPointCloud
virtual void readPointCloud(const InputType &input, type::pointcloud::VoxelType &type, std::string &defaultValue, type::Maybe &maybe, Path &p)=0
Extract information from a pointcloud type.
armarx::aron::type::ReaderInterface::readDict
virtual void readDict(const InputType &input, InputTypeNonConst &acceptedType, type::Maybe &maybe, Path &p)=0
Extract information from a dict type.
armarx::aron::type::ReaderInterface::readBool
virtual void readBool(const InputType &input, std::optional< bool > &defaultValue, type::Maybe &maybe, Path &p)=0
Extract information from an bool type.
armarx::aron::type::ReaderInterface::readNDArray
virtual void readNDArray(const InputType &input, int &ndim, type::ndarray::ElementType &type, std::string &defaultValue, type::Maybe &maybe, Path &p)=0
Extract information from a ndarray type.
armarx::aron::type::ReaderInterface< const nlohmann::json >::InputType
const nlohmann::json InputType
Definition: Reader.h:41
armarx::aron::type::ReaderInterface::readMatrix
virtual void readMatrix(const InputType &input, int &rows, int &cols, type::matrix::ElementType &type, std::string &defaultValue, type::Maybe &maybe, Path &p)=0
Extract information from a matrix type.
armarx::aron::type::ReaderInterface::readPair
virtual void readPair(const InputType &input, InputTypeNonConst &acceptedTypes1, InputTypeNonConst &acceptedTypes2, type::Maybe &maybe, Path &p)=0
Extract information from a pair type.
armarx::aron::type::ReaderInterface::readString
virtual void readString(const InputType &input, std::optional< std::string > &defaultValue, type::Maybe &maybe, Path &p)=0
Extract information from an string type.
armarx::aron::type::ReaderInterface::readNull
virtual bool readNull(InputType &input)
Check if input is null.
Definition: Reader.h:169
armarx::aron::Path
The Path class.
Definition: Path.h:36
armarx::aron::type::isReader
concept isReader
Definition: Reader.h:286
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::aron::type::ReaderInterface::readObject
virtual void readObject(const InputType &input, std::string &name, std::vector< std::string > &templates, std::vector< std::string > &templateInstantiations, std::map< std::string, InputTypeNonConst > &memberTypes, type::Maybe &maybe, Path &p)=0
Extract information from an Object type.
armarx::aron::type::ReaderInterface::getDescriptor
virtual type::Descriptor getDescriptor(InputType &input)=0
armarx::aron::type::ReaderInterface::readQuaternion
virtual void readQuaternion(const InputType &input, type::quaternion::ElementType &type, std::string &defaultValue, type::Maybe &maybe, Path &p)=0
Extract information from a quaternion type.
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
armarx::aron::type::ReaderInterface::readImage
virtual void readImage(const InputType &input, type::image::PixelType &type, std::string &defaultValue, type::Maybe &maybe, Path &p)=0
Extract information from an image type.
armarx::aron::type::ReaderInterface::~ReaderInterface
virtual ~ReaderInterface()=default
armarx::aron::type::ReaderInterface::readAnyObject
virtual void readAnyObject(const InputType &input, type::Maybe &maybe, Path &p)=0
Extract information from an time type.
armarx::aron::type
A convenience header to include all aron files (full include, not forward declared)
Definition: aron_conversions.cpp:9
armarx::ElementTypes::ElementType
ElementType
Definition: AbstractObjectSerializer.h:32
armarx::aron::type::ReaderInterface::readInt
virtual void readInt(const InputType &input, std::optional< int > &defaultValue, type::Maybe &maybe, Path &p)=0
Extract information from an int type.
armarx::aron::type::ReaderInterface::readIntEnum
virtual void readIntEnum(const InputType &input, std::string &name, std::map< std::string, int > &acceptedValues, std::string &defaultValue, type::Maybe &maybe, Path &p)=0
Extract information from an int enum type.
Exception.h
armarx::aron::type::ReaderInterface::readFloat
virtual void readFloat(const InputType &input, std::optional< float > &defaultValue, type::Maybe &maybe, Path &p)=0
Extract information from an float type.
armarx::aron::type::ReaderInterface::readDouble
virtual void readDouble(const InputType &input, std::optional< double > &defaultValue, type::Maybe &maybe, Path &p)=0
Extract information from an double type.
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::aron::type::Descriptor
Descriptor
Definition: Descriptor.h:76
armarx::aron::type::ReaderInterface::readLong
virtual void readLong(const InputType &input, std::optional< long > &defaultValue, type::Maybe &maybe, Path &p)=0
Extract information from an long type.
armarx::aron::type::ReaderInterface
The ReaderInterface class.
Definition: Reader.h:38
armarx::aron::type::ReaderInterface::readList
virtual void readList(const InputType &input, InputTypeNonConst &acceptedType, type::Maybe &maybe, Path &p)=0
Extract information from a list type.