List.cpp
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// Header
25#include "List.h"
26
27// ArmarX
30
35
36namespace armarx::aron::data
37{
38 // constructors
40 detail::ContainerVariant<data::dto::List, List>::ContainerVariant(data::Descriptor::LIST,
41 path)
42 {
43 }
44
45 List::List(const data::dto::ListPtr& l, const Path& path) :
46 detail::ContainerVariant<data::dto::List, List>::ContainerVariant(l,
48 path)
49 {
50 unsigned int i = 0;
51 for (const auto& dataPtr : l->elements)
52 {
53 childrenNavigators.push_back(FromAronDTO(dataPtr, path.withIndex(i++)));
54 }
55 }
56
57 List::List(const std::vector<VariantPtr>& n, const Path& path) : List(path)
58 {
59 for (const auto& dataPtr : n)
60 {
61 addElement(dataPtr);
62 }
63 }
64
65 // operators
66 bool
67 List::operator==(const List& other) const
68 {
69 unsigned int i = 0;
70 for (const auto& nav : childrenNavigators)
71 {
72 if (!other.hasElement(i))
73 {
74 return false;
75 }
76 if (!nav)
77 {
78 if (!other.getElement(i))
79 {
80 return false;
81 }
82 }
83 if (!(*nav == other.getElement(i)))
84 {
85 return false;
86 }
87 ++i;
88 }
89 return true;
90 }
91
92 bool
93 List::operator==(const ListPtr& other) const
94 {
95 if (!other)
96 {
97 return false;
98 }
99 return *this == *other;
100 }
101
102 ListPtr
103 List::clone(const Path& p) const
104 {
105 ListPtr ret(new List(p));
106 ret->setImportance(getImportance());
107 for (const auto& val : getElements())
108 {
109 ret->addElement(val->cloneAsVariant());
110 }
111 return ret;
112 }
113
114 // static methods
115 ListPtr
116 List::FromAronListDTO(const data::dto::ListPtr& aron)
117 {
118 if (!aron)
119 {
120 return nullptr;
121 }
122 return std::make_shared<List>(aron);
123 }
124
125 data::dto::ListPtr
126 List::ToAronListDTO(const ListPtr& navigator)
127 {
128 return navigator ? navigator->toAronListDTO() : nullptr;
129 }
130
131 // public member functions
132 DictPtr
134 {
135 auto dict = std::make_shared<Dict>();
136 auto copy_this = FromAronDTO(toAronDTO(), getPath());
137 dict->addElement("data", copy_this);
138 return dict;
139 }
140
141 void
143 {
144 setElement(aron->elements.size(), n);
145 }
146
147 void
148 List::setElement(unsigned int i, const VariantPtr& n)
149 {
150 if (i > aron->elements.size())
151 {
153 throw error::AronException(__PRETTY_FUNCTION__,
154 "Cannot set a listelement at index " + std::to_string(i) +
155 " because this list has size " +
156 std::to_string(aron->elements.size()));
157 }
158
159 if (i == aron->elements.size())
160 {
161 childrenNavigators.push_back(n);
162 if (n)
163 {
164 aron->elements.push_back(n->toAronDTO());
165 }
166 else
167 {
168 aron->elements.push_back(nullptr);
169 }
170 }
171 else
172 {
173 childrenNavigators[i] = n;
174 if (n)
175 {
176 aron->elements[i] = (n->toAronDTO());
177 }
178 else
179 {
180 aron->elements[i] = nullptr;
181 }
182 }
183 }
184
185 bool
186 List::hasElement(unsigned int i) const
187 {
188 return i < childrenNavigators.size();
189 }
190
192 List::getElement(unsigned int i) const
193 {
194 if (i >= childrenNavigators.size())
195 {
197 throw error::ValueNotValidException(__PRETTY_FUNCTION__,
198 "The index is out of bounds (size = " +
199 std::to_string(childrenNavigators.size()) + ")",
200 std::to_string(i),
201 getPath());
202 }
203 return childrenNavigators[i];
204 }
205
206 std::vector<VariantPtr>
208 {
209 return childrenNavigators;
210 }
211
212 void
213 List::removeElement(unsigned int i)
214 {
215 // Use with care since this function will not work in a loop with increasing indexes
216 i = std::min((unsigned int)childrenNavigators.size() - 1, i);
217 childrenNavigators.erase(childrenNavigators.begin() + i);
218 aron->elements.erase(aron->elements.begin() + i);
219 }
220
221 void
223 {
224 childrenNavigators.clear();
225 aron->elements.clear();
226 }
227
228 data::dto::ListPtr
230 {
231 return aron;
232 }
233
234 // virtual implementations
235 std::string
237 {
238 return "List";
239 }
240
241 std::string
243 {
244 return "armarx::aron::data::List";
245 }
246
247 // TODO
250 {
252 throw error::NotImplementedYetException(__PRETTY_FUNCTION__);
253 }
254
255 bool
257 {
258 if (!type)
259 return true;
260
261 type::Descriptor typeDesc = type->getDescriptor();
262 switch (typeDesc)
263 {
265 {
267 auto listTypeNav = type::List::DynamicCastAndCheck(type);
268 for (const auto& nav : childrenNavigators)
269 {
270 auto childTypeNav = listTypeNav->getAcceptedType();
271 if (!nav)
272 {
273 if (childTypeNav && childTypeNav->getMaybe() == type::Maybe::NONE)
274 {
275 return false;
276 }
277 continue;
278 }
279 if (!nav->fullfillsType(childTypeNav))
280 {
281 return false;
282 }
283 }
284 return true;
285 }
287 {
289 auto tupleTypeNav = type::Tuple::DynamicCastAndCheck(type);
290 unsigned int i = 0;
291 for (const auto& childTypeNav : tupleTypeNav->getAcceptedTypes())
292 {
293 if (!this->hasElement(i))
294 {
295 return false;
296 }
297
298 auto childNav = this->getElement(i);
299 if (!childNav)
300 {
301 if (childTypeNav && childTypeNav->getMaybe() == type::Maybe::NONE)
302 {
303 return false;
304 }
305 continue;
306 }
307 if (!childNav->fullfillsType(tupleTypeNav->getAcceptedType(i)))
308 {
309 return false;
310 }
311 }
312 return true;
313 }
315 {
317 auto pairTypeNav = type::Pair::DynamicCastAndCheck(type);
318 if (childrenSize() != 2)
319 {
320 return false;
321 }
322 auto firstChildTypeNav = pairTypeNav->getFirstAcceptedType();
323 if (!childrenNavigators[0])
324 {
325 if (firstChildTypeNav && firstChildTypeNav->getMaybe() == type::Maybe::NONE)
326 {
327 return false;
328 }
329 }
330 auto secondChildTypeNav = pairTypeNav->getSecondAcceptedType();
331 if (!childrenNavigators[1])
332 {
333 if (secondChildTypeNav && secondChildTypeNav->getMaybe() == type::Maybe::NONE)
334 {
335 return false;
336 }
337 }
338 return childrenNavigators[0]->fullfillsType(firstChildTypeNav) &&
339 childrenNavigators[1]->fullfillsType(secondChildTypeNav);
340 }
341 default:
343 return false;
344 }
345 }
346
347 std::vector<VariantPtr>
349 {
350 return childrenNavigators;
351 }
352
353 size_t
355 {
356 return childrenNavigators.size();
357 }
358
361 {
362 if (!path.hasElement())
363 {
365 __PRETTY_FUNCTION__,
366 "Could not navigate without a valid path. The path was empty.");
367 }
368 unsigned int i = std::stoi(path.getFirstElement());
369 if (!hasElement(i))
370 {
372 throw error::ValueNotValidException(__PRETTY_FUNCTION__,
373 "Could not find an element of a path.",
374 std::to_string(i),
375 std::to_string(childrenSize()));
376 }
377
378 if (path.size() == 1)
379 {
380 return childrenNavigators.at(i);
381 }
382 else
383 {
384 Path next = path.withDetachedFirstElement();
385 if (!childrenNavigators.at(i))
386 {
388 throw error::AronException(__PRETTY_FUNCTION__,
389 "Could not navigate into a NULL member. Seems like the "
390 "member is optional and not set.",
391 next);
392 }
393 return childrenNavigators.at(i)->navigateAbsolute(next);
394 }
395 }
396} // namespace armarx::aron::data
The Path class.
Definition Path.h:36
size_t childrenSize() const override
get the children size of a data variant
Definition List.cpp:354
std::string getShortName() const override
get a short str representation of this variant
Definition List.cpp:236
List(const Path &path=Path())
Definition List.cpp:39
VariantPtr getElement(unsigned int) const
Definition List.cpp:192
bool hasElement(unsigned int) const
Definition List.cpp:186
std::vector< VariantPtr > getElements() const
Definition List.cpp:207
void addElement(const VariantPtr &)
Definition List.cpp:142
type::VariantPtr recalculateType() const override
recalculate the type of a data variant. Please not tha the mapping ist NOT bijective,...
Definition List.cpp:249
std::string getFullName() const override
get the full str representation of this variant
Definition List.cpp:242
void removeElement(unsigned int)
Definition List.cpp:213
static PointerType FromAronListDTO(const data::dto::ListPtr &aron)
Definition List.cpp:116
data::dto::ListPtr toAronListDTO() const
Definition List.cpp:229
bool operator==(const List &) const override
Definition List.cpp:67
void setElement(unsigned int, const VariantPtr &)
Definition List.cpp:148
VariantPtr navigateAbsolute(const Path &path) const override
naviate absolute
Definition List.cpp:360
bool fullfillsType(const type::VariantPtr &) const override
checks, if the current data variant fullfills the given type
Definition List.cpp:256
static data::dto::ListPtr ToAronListDTO(const PointerType &navigator)
Definition List.cpp:126
std::vector< VariantPtr > getChildren() const override
get the children of a data variant
Definition List.cpp:348
DictPtr getAsDict() const
Definition List.cpp:133
static VariantPtr FromAronDTO(const data::dto::GenericDataPtr &, const Path &=Path())
create a variant from a dto object
Definition Variant.cpp:39
Path getPath() const
get the path
Definition Variant.h:111
A base class for aron exceptions.
Definition Exception.h:37
The NotImplementedYetException class.
Definition Exception.h:61
The ValueNotValidException class.
Definition Exception.h:99
static std::shared_ptr< List > DynamicCastAndCheck(const VariantPtr &n)
A convenience header to include all aron files (full include, not forward declared)
std::shared_ptr< Dict > DictPtr
Definition Dict.h:42
std::shared_ptr< List > ListPtr
Definition List.h:41
std::shared_ptr< Variant > VariantPtr
A convenience header to include all aron files (full include, not forward declared)
std::shared_ptr< Variant > VariantPtr
#define ARMARX_TRACE
Definition trace.h:75