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 for (const auto& val : getElements())
107 {
108 ret->addElement(val->cloneAsVariant());
109 }
110 return ret;
111 }
112
113 // static methods
114 ListPtr
115 List::FromAronListDTO(const data::dto::ListPtr& aron)
116 {
117 if (!aron)
118 {
119 return nullptr;
120 }
121 return std::make_shared<List>(aron);
122 }
123
124 data::dto::ListPtr
125 List::ToAronListDTO(const ListPtr& navigator)
126 {
127 return navigator ? navigator->toAronListDTO() : nullptr;
128 }
129
130 // public member functions
131 DictPtr
133 {
134 auto dict = std::make_shared<Dict>();
135 auto copy_this = FromAronDTO(toAronDTO(), getPath());
136 dict->addElement("data", copy_this);
137 return dict;
138 }
139
140 void
142 {
143 setElement(aron->elements.size(), n);
144 }
145
146 void
147 List::setElement(unsigned int i, const VariantPtr& n)
148 {
149 if (i > aron->elements.size())
150 {
152 throw error::AronException(__PRETTY_FUNCTION__,
153 "Cannot set a listelement at index " + std::to_string(i) +
154 " because this list has size " +
155 std::to_string(aron->elements.size()));
156 }
157
158 if (i == aron->elements.size())
159 {
160 childrenNavigators.push_back(n);
161 if (n)
162 {
163 aron->elements.push_back(n->toAronDTO());
164 }
165 else
166 {
167 aron->elements.push_back(nullptr);
168 }
169 }
170 else
171 {
172 childrenNavigators[i] = n;
173 if (n)
174 {
175 aron->elements[i] = (n->toAronDTO());
176 }
177 else
178 {
179 aron->elements[i] = nullptr;
180 }
181 }
182 }
183
184 bool
185 List::hasElement(unsigned int i) const
186 {
187 return i < childrenNavigators.size();
188 }
189
191 List::getElement(unsigned int i) const
192 {
193 if (i >= childrenNavigators.size())
194 {
196 throw error::ValueNotValidException(__PRETTY_FUNCTION__,
197 "The index is out of bounds (size = " +
198 std::to_string(childrenNavigators.size()) + ")",
199 std::to_string(i),
200 getPath());
201 }
202 return childrenNavigators[i];
203 }
204
205 std::vector<VariantPtr>
207 {
208 return childrenNavigators;
209 }
210
211 void
212 List::removeElement(unsigned int i)
213 {
214 // Use with care since this function will not work in a loop with increasing indexes
215 i = std::min((unsigned int)childrenNavigators.size() - 1, i);
216 childrenNavigators.erase(childrenNavigators.begin() + i);
217 aron->elements.erase(aron->elements.begin() + i);
218 }
219
220 void
222 {
223 childrenNavigators.clear();
224 aron->elements.clear();
225 }
226
227 data::dto::ListPtr
229 {
230 return aron;
231 }
232
233 // virtual implementations
234 std::string
236 {
237 return "List";
238 }
239
240 std::string
242 {
243 return "armarx::aron::data::List";
244 }
245
246 // TODO
249 {
251 throw error::NotImplementedYetException(__PRETTY_FUNCTION__);
252 }
253
254 bool
256 {
257 if (!type)
258 return true;
259
260 type::Descriptor typeDesc = type->getDescriptor();
261 switch (typeDesc)
262 {
264 {
266 auto listTypeNav = type::List::DynamicCastAndCheck(type);
267 for (const auto& nav : childrenNavigators)
268 {
269 auto childTypeNav = listTypeNav->getAcceptedType();
270 if (!nav)
271 {
272 if (childTypeNav && childTypeNav->getMaybe() == type::Maybe::NONE)
273 {
274 return false;
275 }
276 continue;
277 }
278 if (!nav->fullfillsType(childTypeNav))
279 {
280 return false;
281 }
282 }
283 return true;
284 }
286 {
288 auto tupleTypeNav = type::Tuple::DynamicCastAndCheck(type);
289 unsigned int i = 0;
290 for (const auto& childTypeNav : tupleTypeNav->getAcceptedTypes())
291 {
292 if (!this->hasElement(i))
293 {
294 return false;
295 }
296
297 auto childNav = this->getElement(i);
298 if (!childNav)
299 {
300 if (childTypeNav && childTypeNav->getMaybe() == type::Maybe::NONE)
301 {
302 return false;
303 }
304 continue;
305 }
306 if (!childNav->fullfillsType(tupleTypeNav->getAcceptedType(i)))
307 {
308 return false;
309 }
310 }
311 return true;
312 }
314 {
316 auto pairTypeNav = type::Pair::DynamicCastAndCheck(type);
317 if (childrenSize() != 2)
318 {
319 return false;
320 }
321 auto firstChildTypeNav = pairTypeNav->getFirstAcceptedType();
322 if (!childrenNavigators[0])
323 {
324 if (firstChildTypeNav && firstChildTypeNav->getMaybe() == type::Maybe::NONE)
325 {
326 return false;
327 }
328 }
329 auto secondChildTypeNav = pairTypeNav->getSecondAcceptedType();
330 if (!childrenNavigators[1])
331 {
332 if (secondChildTypeNav && secondChildTypeNav->getMaybe() == type::Maybe::NONE)
333 {
334 return false;
335 }
336 }
337 return childrenNavigators[0]->fullfillsType(firstChildTypeNav) &&
338 childrenNavigators[1]->fullfillsType(secondChildTypeNav);
339 }
340 default:
342 return false;
343 }
344 }
345
346 std::vector<VariantPtr>
348 {
349 return childrenNavigators;
350 }
351
352 size_t
354 {
355 return childrenNavigators.size();
356 }
357
360 {
361 if (!path.hasElement())
362 {
364 __PRETTY_FUNCTION__,
365 "Could not navigate without a valid path. The path was empty.");
366 }
367 unsigned int i = std::stoi(path.getFirstElement());
368 if (!hasElement(i))
369 {
371 throw error::ValueNotValidException(__PRETTY_FUNCTION__,
372 "Could not find an element of a path.",
373 std::to_string(i),
374 std::to_string(childrenSize()));
375 }
376
377 if (path.size() == 1)
378 {
379 return childrenNavigators.at(i);
380 }
381 else
382 {
383 Path next = path.withDetachedFirstElement();
384 if (!childrenNavigators.at(i))
385 {
387 throw error::AronException(__PRETTY_FUNCTION__,
388 "Could not navigate into a NULL member. Seems like the "
389 "member is optional and not set.",
390 next);
391 }
392 return childrenNavigators.at(i)->navigateAbsolute(next);
393 }
394 }
395} // 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:353
std::string getShortName() const override
get a short str representation of this variant
Definition List.cpp:235
List(const Path &path=Path())
Definition List.cpp:39
VariantPtr getElement(unsigned int) const
Definition List.cpp:191
bool hasElement(unsigned int) const
Definition List.cpp:185
std::vector< VariantPtr > getElements() const
Definition List.cpp:206
void addElement(const VariantPtr &)
Definition List.cpp:141
type::VariantPtr recalculateType() const override
recalculate the type of a data variant. Please not tha the mapping ist NOT bijective,...
Definition List.cpp:248
std::string getFullName() const override
get the full str representation of this variant
Definition List.cpp:241
void removeElement(unsigned int)
Definition List.cpp:212
static PointerType FromAronListDTO(const data::dto::ListPtr &aron)
Definition List.cpp:115
data::dto::ListPtr toAronListDTO() const
Definition List.cpp:228
bool operator==(const List &) const override
Definition List.cpp:67
void setElement(unsigned int, const VariantPtr &)
Definition List.cpp:147
VariantPtr navigateAbsolute(const Path &path) const override
naviate absolute
Definition List.cpp:359
bool fullfillsType(const type::VariantPtr &) const override
checks, if the current data variant fullfills the given type
Definition List.cpp:255
static data::dto::ListPtr ToAronListDTO(const PointerType &navigator)
Definition List.cpp:125
std::vector< VariantPtr > getChildren() const override
get the children of a data variant
Definition List.cpp:347
DictPtr getAsDict() const
Definition List.cpp:132
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:110
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:77