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 
36 namespace armarx::aron::data
37 {
38  // constructors
39  List::List(const Path& path) :
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,
47  data::Descriptor::LIST,
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
116  {
117  if (!aron)
118  {
119  return nullptr;
120  }
121  return std::make_shared<List>(aron);
122  }
123 
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  {
151  ARMARX_TRACE;
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 
190  VariantPtr
191  List::getElement(unsigned int i) const
192  {
193  if (i >= childrenNavigators.size())
194  {
195  ARMARX_TRACE;
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 
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  {
250  ARMARX_TRACE;
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  {
265  ARMARX_TRACE;
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  {
287  ARMARX_TRACE;
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  {
315  ARMARX_TRACE;
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:
341  ARMARX_TRACE;
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 
358  VariantPtr
359  List::navigateAbsolute(const Path& path) const
360  {
361  if (!path.hasElement())
362  {
363  throw error::AronException(
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  {
370  ARMARX_TRACE;
371  throw error::ValueNotValidException(__PRETTY_FUNCTION__,
372  "Could not find an element of a path.",
373  std::to_string(i),
375  }
376 
377  if (path.size() == 1)
378  {
379  return childrenNavigators.at(i);
380  }
381  else
382  {
384  if (!childrenNavigators.at(i))
385  {
386  ARMARX_TRACE;
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
armarx::aron::data::List::fullfillsType
bool fullfillsType(const type::VariantPtr &) const override
checks, if the current data variant fullfills the given type
Definition: List.cpp:255
armarx::aron::data::List::List
List(const Path &path=Path())
Definition: List.cpp:39
armarx::aron::Path::getFirstElement
std::string getFirstElement() const
Definition: Path.cpp:102
armarx::aron::data::List::getElement
VariantPtr getElement(unsigned int) const
Definition: List.cpp:191
armarx::aron::error::AronException
A base class for aron exceptions.
Definition: Exception.h:42
armarx::aron::data::List::getShortName
std::string getShortName() const override
get a short str representation of this variant
Definition: List.cpp:235
armarx::aron::type::VariantPtr
std::shared_ptr< Variant > VariantPtr
Definition: forward_declarations.h:11
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
armarx::aron::data::Variant::FromAronDTO
static VariantPtr FromAronDTO(const data::dto::GenericDataPtr &, const Path &=Path())
create a variant from a dto object
Definition: Variant.cpp:39
armarx::aron::data::List::getElements
std::vector< VariantPtr > getElements() const
Definition: List.cpp:206
armarx::aron::data::List::toAronListDTO
data::dto::ListPtr toAronListDTO() const
Definition: List.cpp:228
armarx::aron::data::List::clear
void clear()
Definition: List.cpp:221
armarx::aron::Path::hasElement
bool hasElement() const
Definition: Path.cpp:113
armarx::aron::data::List::childrenSize
size_t childrenSize() const override
get the children size of a data variant
Definition: List.cpp:353
List.h
armarx::aron::Path::size
size_t size() const
Definition: Path.cpp:119
armarx::aron::data::Variant::getPath
Path getPath() const
get the path
Definition: Variant.h:110
trace.h
armarx::aron::type::Descriptor::PAIR
@ PAIR
armarx::aron::data::Variant::path
const Path path
Definition: Variant.h:156
armarx::aron::type::Descriptor::LIST
@ LIST
detail
Definition: OpenCVUtil.cpp:127
armarx::aron::data::Descriptor
Descriptor
Definition: Descriptor.h:193
armarx::aron::data::List
Definition: List.h:43
armarx::aron::data::List::getFullName
std::string getFullName() const override
get the full str representation of this variant
Definition: List.cpp:241
armarx::aron::data::List::recalculateType
type::VariantPtr recalculateType() const override
recalculate the type of a data variant. Please not tha the mapping ist NOT bijective,...
Definition: List.cpp:248
armarx::aron::error::NotImplementedYetException
The NotImplementedYetException class.
Definition: Exception.h:88
armarx::aron::data::detail::SpecializedVariantBase< data::dto::List, List >::clone
virtual PointerType clone() const
Definition: SpecializedVariant.h:82
armarx::aron::Path::withIndex
Path withIndex(int, bool escape=false) const
Definition: Path.cpp:150
armarx::aron::data::List::removeElement
void removeElement(unsigned int)
Definition: List.cpp:212
armarx::aron::Path
The Path class.
Definition: Path.h:36
armarx::aron::data::VariantPtr
std::shared_ptr< Variant > VariantPtr
Definition: forward_declarations.h:11
armarx::aron::data::List::ToAronListDTO
static data::dto::ListPtr ToAronListDTO(const PointerType &navigator)
Definition: List.cpp:125
armarx::aron::data::List::navigateAbsolute
VariantPtr navigateAbsolute(const Path &path) const override
naviate absolute
Definition: List.cpp:359
armarx::aron::error::ValueNotValidException
The ValueNotValidException class.
Definition: Exception.h:145
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
Pair.h
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::aron::data
A convenience header to include all aron files (full include, not forward declared)
Definition: aron_conversions.cpp:3
armarx::aron::data::detail::SpecializedVariantBase< data::dto::List, List >::toAronDTO
data::dto::GenericDataPtr toAronDTO() const override
Definition: SpecializedVariant.h:103
armarx::aron::data::DictPtr
std::shared_ptr< Dict > DictPtr
Definition: Dict.h:41
armarx::aron::data::List::FromAronListDTO
static PointerType FromAronListDTO(const data::dto::ListPtr &aron)
Definition: List.cpp:115
armarx::aron::data::List::operator==
bool operator==(const List &) const override
Definition: List.cpp:67
armarx::aron::similarity::FloatSimilarity::NONE
@ NONE
Definition: FloatSimilarity.h:11
armarx::aron::Path::withDetachedFirstElement
Path withDetachedFirstElement() const
Definition: Path.cpp:205
armarx::aron::data::List::getChildren
std::vector< VariantPtr > getChildren() const override
get the children of a data variant
Definition: List.cpp:347
armarx::aron::type::detail::SpecializedVariantBase< type::dto::List, List >::DynamicCastAndCheck
static std::shared_ptr< List > DynamicCastAndCheck(const VariantPtr &n)
Definition: SpecializedVariant.h:124
armarx::aron::type::Descriptor::TUPLE
@ TUPLE
armarx::aron::data::List::getAsDict
DictPtr getAsDict() const
Definition: List.cpp:132
armarx::aron::data::List::hasElement
bool hasElement(unsigned int) const
Definition: List.cpp:185
min
T min(T t1, T t2)
Definition: gdiam.h:42
Tuple.h
List.h
armarx::aron::data::List::setElement
void setElement(unsigned int, const VariantPtr &)
Definition: List.cpp:147
armarx::aron::type::Descriptor
Descriptor
Definition: Descriptor.h:76
armarx::aron::data::ListPtr
std::shared_ptr< List > ListPtr
Definition: List.h:40
Factory.h
armarx::aron::data::List::addElement
void addElement(const VariantPtr &)
Definition: List.cpp:141
armarx::aron::data::detail::SpecializedVariantBase< data::dto::List, List >::aron
AronDataType::PointerType aron
Definition: SpecializedVariant.h:154
Exception.h