VariantWriter.cpp
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 #include "VariantWriter.h"
22 
23 #include <memory>
24 #include <numeric>
25 
28 
30 {
31 
34  {
36  }
37 
39  VariantWriter::writeObject(const std::string& name,
40  const std::vector<std::string>& templates,
41  const std::vector<std::string>& templateInstantiations,
42  const std::map<std::string, aron::type::VariantPtr>& memberTypes,
43  const std::optional<aron::type::VariantPtr>& extends,
44  const type::Maybe maybe,
45  const Path& p)
46  {
47  auto o = std::make_shared<type::Object>(p);
48  o->setObjectName(name);
49  o->setTemplates(templates);
50  o->setTemplateInstantiations(templateInstantiations);
51  o->setMemberTypes(memberTypes);
52  o->setMaybe(maybe);
53  if (extends.has_value())
54  {
55  auto ex = type::Object::DynamicCast(*extends);
56  o->setExtends(ex);
57  }
58  return o;
59  }
60 
63  const type::Maybe maybe,
64  const Path& p)
65  {
66  auto o = std::make_shared<type::List>(p);
67  o->setAcceptedType(acceptedType);
68  o->setMaybe(maybe);
69  return o;
70  }
71 
74  const type::Maybe maybe,
75  const Path& p)
76  {
77  auto o = std::make_shared<type::Dict>(p);
78  o->setAcceptedType(acceptedType);
79  o->setMaybe(maybe);
80  return o;
81  }
82 
85  const aron::type::VariantPtr& acceptedType2,
86  const type::Maybe maybe,
87  const Path& p)
88  {
89  auto o = std::make_shared<type::Pair>(p);
90  o->setFirstAcceptedType(acceptedType1);
91  o->setSecondAcceptedType(acceptedType2);
92  o->setMaybe(maybe);
93  return o;
94  }
95 
97  VariantWriter::writeTuple(const std::vector<aron::type::VariantPtr>& acceptedTypes,
98  const type::Maybe maybe,
99  const Path& p)
100  {
101  auto o = std::make_shared<type::Tuple>(p);
102  o->setAcceptedTypes(acceptedTypes);
103  o->setMaybe(maybe);
104  return o;
105  }
106 
109  const type::ndarray::ElementType type,
110  const std::string& defaultValue,
111  const type::Maybe maybe,
112  const Path& p)
113  {
114  auto o = std::make_shared<type::NDArray>(p);
115  o->setMaybe(maybe);
116  o->setElementType(type);
117  o->setDefaultValue(defaultValue);
118  return o;
119  }
120 
123  const int cols,
124  const type::matrix::ElementType type,
125  const std::string& defaultValue,
126  const type::Maybe maybe,
127  const Path& p)
128  {
129  auto o = std::make_shared<type::Matrix>(p);
130  o->setMaybe(maybe);
131  o->setRows(rows);
132  o->setCols(cols);
133  o->setElementType(type);
134  o->setDefaultValue(defaultValue);
135  return o;
136  }
137 
140  const std::string& defaultValue,
141  const type::Maybe maybe,
142  const Path& p)
143  {
144  auto o = std::make_shared<type::Quaternion>(p);
145  o->setMaybe(maybe);
146  o->setElementType(type);
147  o->setDefaultValue(defaultValue);
148  return o;
149  }
150 
152  VariantWriter::writeImage(const type::image::PixelType type,
153  const std::string& defaultValue,
154  const type::Maybe maybe,
155  const Path& p)
156  {
157  auto o = std::make_shared<type::Image>(p);
158  o->setMaybe(maybe);
159  o->setPixelType(type);
160  o->setDefaultValue(defaultValue);
161  return o;
162  }
163 
165  VariantWriter::writePointCloud(const type::pointcloud::VoxelType type,
166  const std::string& defaultValue,
167  const type::Maybe maybe,
168  const Path& p)
169  {
170  auto o = std::make_shared<type::PointCloud>(p);
171  o->setMaybe(maybe);
172  o->setVoxelType(type);
173  o->setDefaultValue(defaultValue);
174  return o;
175  }
176 
178  VariantWriter::writeIntEnum(const std::string& name,
179  const std::map<std::string, int>& acceptedValues,
180  const std::string& defaultValue,
181  const type::Maybe maybe,
182  const Path& p)
183  {
184  auto o = std::make_shared<type::IntEnum>(p);
185  o->setEnumName(name);
186  o->setAcceptedValueMap(acceptedValues);
187  o->setDefaultValueName(defaultValue);
188  o->setMaybe(maybe);
189  return o;
190  }
191 
193  VariantWriter::writeInt(const std::optional<int>& defaultValue,
194  const type::Maybe maybe,
195  const Path& p)
196  {
197  auto o = std::make_shared<type::Int>(p);
198  o->setDefaultValue(defaultValue);
199  o->setMaybe(maybe);
200  return o;
201  }
202 
204  VariantWriter::writeLong(const std::optional<long>& defaultValue,
205  const type::Maybe maybe,
206  const Path& p)
207  {
208  auto o = std::make_shared<type::Long>(p);
209  o->setDefaultValue(defaultValue);
210  o->setMaybe(maybe);
211  return o;
212  }
213 
215  VariantWriter::writeFloat(const std::optional<float>& defaultValue,
216  const type::Maybe maybe,
217  const Path& p)
218  {
219  auto o = std::make_shared<type::Float>(p);
220  o->setDefaultValue(defaultValue);
221  o->setMaybe(maybe);
222  return o;
223  }
224 
226  VariantWriter::writeDouble(const std::optional<double>& defaultValue,
227  const type::Maybe maybe,
228  const Path& p)
229  {
230  auto o = std::make_shared<type::Double>(p);
231  o->setDefaultValue(defaultValue);
232  o->setMaybe(maybe);
233  return o;
234  }
235 
237  VariantWriter::writeString(const std::optional<std::string>& defaultValue,
238  const type::Maybe maybe,
239  const Path& p)
240  {
241  auto o = std::make_shared<type::String>(p);
242  o->setDefaultValue(defaultValue);
243  o->setMaybe(maybe);
244  return o;
245  }
246 
248  VariantWriter::writeBool(const std::optional<bool>& defaultValue,
249  const type::Maybe maybe,
250  const Path& p)
251  {
252  auto o = std::make_shared<type::Bool>(p);
253  o->setMaybe(maybe);
254  o->setDefaultValue(defaultValue);
255  return o;
256  }
257 
259  VariantWriter::writeAnyObject(const type::Maybe maybe, const Path& p)
260  {
261  auto o = std::make_shared<type::AnyObject>(p);
262  o->setMaybe(maybe);
263  return o;
264  }
265 } // namespace armarx::aron::type::writer
armarx::aron::type::writer::VariantWriter::writeInt
ReturnType writeInt(const std::optional< int > &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a int from the params.
Definition: VariantWriter.cpp:193
armarx::aron::type::writer::VariantWriter::writePointCloud
ReturnType writePointCloud(const type::pointcloud::VoxelType, const std::string &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a pointcloud from the params.
Definition: VariantWriter.cpp:165
armarx::aron::type::VariantPtr
std::shared_ptr< Variant > VariantPtr
Definition: forward_declarations.h:11
armarx::aron::type::writer::VariantWriter::getDescriptor
type::Descriptor getDescriptor(ReturnTypeConst &input) final
Definition: VariantWriter.cpp:33
VariantVisitor.h
armarx::aron::type::writer::VariantWriter::writeList
ReturnType writeList(const ReturnType &acceptedType, const type::Maybe maybe, const Path &p=Path()) override
armarx::aron::type::writer::VariantWriter::writeFloat
ReturnType writeFloat(const std::optional< float > &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a float from the params.
Definition: VariantWriter.cpp:215
All.h
armarx::aron::type::writer::VariantWriter::writeBool
ReturnType writeBool(const std::optional< bool > &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a bool from the params.
Definition: VariantWriter.cpp:248
armarx::aron::type::writer::VariantWriter::writeQuaternion
ReturnType writeQuaternion(const type::quaternion::ElementType, const std::string &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a quaternion from the params.
Definition: VariantWriter.cpp:139
armarx::aron::type::writer::VariantWriter::writeString
ReturnType writeString(const std::optional< std::string > &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a string from the params.
Definition: VariantWriter.cpp:237
armarx::aron::Path
The Path class.
Definition: Path.h:36
armarx::aron::type::writer::VariantWriter::writeObject
ReturnType writeObject(const std::string &name, const std::vector< std::string > &templates, const std::vector< std::string > &templateInstantiations, const std::map< std::string, ReturnType > &memberTypes, const std::optional< ReturnType > &extends, const type::Maybe maybe, const Path &p=Path()) override
Definition: VariantWriter.cpp:39
armarx::aron::type::WriterInterface< aron::type::VariantPtr >::ReturnTypeConst
typename std::add_const< ReturnType >::type ReturnTypeConst
Definition: Writer.h:42
armarx::aron::type::detail::SpecializedVariantBase< type::dto::AronObject, Object >::DynamicCast
static Object & DynamicCast(Variant &n)
Definition: SpecializedVariant.h:106
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
armarx::ElementTypes::ElementType
ElementType
Definition: AbstractObjectSerializer.h:32
armarx::aron::type::writer::VariantWriter::writeDouble
ReturnType writeDouble(const std::optional< double > &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a double from the params.
Definition: VariantWriter.cpp:226
armarx::aron::type::writer::VariantWriter::writeDict
ReturnType writeDict(const ReturnType &acceptedType, const type::Maybe maybe, const Path &p=Path()) override
armarx::aron::type::writer::VariantWriter::writeMatrix
ReturnType writeMatrix(const int rows, const int cols, const type::matrix::ElementType type, const std::string &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a matrix from the params.
Definition: VariantWriter.cpp:122
VariantWriter.h
armarx::aron::type::writer::VariantWriter::writeAnyObject
ReturnType writeAnyObject(const type::Maybe maybe, const Path &p=Path()) override
Construct a time from the params.
Definition: VariantWriter.cpp:259
armarx::aron::type::ConstVariantVisitor::GetDescriptor
static type::Descriptor GetDescriptor(Input &n)
Definition: VariantVisitor.cpp:35
armarx::aron::type::writer::VariantWriter::writeLong
ReturnType writeLong(const std::optional< long > &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a long from the params.
Definition: VariantWriter.cpp:204
armarx::aron::type::writer
Definition: NlohmannJSONWriter.cpp:32
armarx::aron::type::writer::VariantWriter::writeIntEnum
ReturnType writeIntEnum(const std::string &name, const std::map< std::string, int > &acceptedValues, const std::string &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a int enum from the params.
Definition: VariantWriter.cpp:178
armarx::aron::type::Descriptor
Descriptor
Definition: Descriptor.h:76
armarx::aron::type::writer::VariantWriter::writePair
ReturnType writePair(const ReturnType &acceptedType1, const ReturnType &acceptedType2, const type::Maybe maybe, const Path &p=Path()) override
Definition: VariantWriter.cpp:84
armarx::aron::type::writer::VariantWriter::writeNDArray
ReturnType writeNDArray(const int ndim, const type::ndarray::ElementType, const std::string &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a ndarray from the params.
Definition: VariantWriter.cpp:108
armarx::aron::type::writer::VariantWriter::writeTuple
ReturnType writeTuple(const std::vector< ReturnType > &acceptedTypes, const type::Maybe maybe, const Path &p=Path()) override
Definition: VariantWriter.cpp:97
armarx::aron::type::writer::VariantWriter::writeImage
ReturnType writeImage(const type::image::PixelType, const std::string &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a image from the params.
Definition: VariantWriter.cpp:152