Object.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 "Object.h"
26 
27 #include <SimoxUtility/algorithm/get_map_keys_values.h>
28 #include <SimoxUtility/algorithm/string/string_conversion.h>
29 
30 namespace armarx::aron::type
31 {
32 
33  // constructors
34  Object::Object(const Path& path) :
35  detail::ContainerVariant<type::dto::AronObject, Object>(type::Descriptor::OBJECT, path)
36  {
37  }
38 
39  Object::Object(const type::dto::AronObject& o, const Path& path) :
40  detail::ContainerVariant<type::dto::AronObject, Object>(o, type::Descriptor::OBJECT, path)
41  {
42  setObjectName(o.objectName);
43  for (const auto& [key, t] : o.elementTypes)
44  {
45  memberTypes[key] = FromAronDTO(*t, path.withElement(key));
46  }
47  if (o.parent)
48  {
49  extends = FromAronObjectDTO(o.parent);
50  }
51  }
52 
53  ObjectPtr
54  Object::FromAronObjectDTO(const type::dto::AronObjectPtr& aron, const aron::Path& path)
55  {
56  if (!aron)
57  {
58  return nullptr;
59  }
60  return std::make_shared<Object>(*aron, path);
61  }
62 
63  type::dto::AronObjectPtr
65  {
66  if (!aron)
67  {
68  return nullptr;
69  }
70  return aron->toAronObjectDTO();
71  }
72 
73  bool
74  Object::checkObjectName(const std::string& s) const
75  {
76  if (s.empty())
77  {
78  throw error::AronException(__PRETTY_FUNCTION__, "The object name is empty.", getPath());
79  }
80 
81  return true;
82  }
83 
84  // public member functions
85  void
86  Object::setMemberTypes(const std::map<std::string, VariantPtr>& memberTypes)
87  {
88  this->memberTypes = memberTypes;
89  for (const auto& [key, value] : memberTypes)
90  {
91  aron->elementTypes[key] = value->toAronDTO();
92  }
93  }
94 
95  void
96  Object::setTemplates(const std::vector<std::string>& templates)
97  {
98  aron->templates = templates;
99  }
100 
101  void
102  Object::setTemplateInstantiations(const std::vector<std::string>& templateInstantiations) const
103  {
104  aron->templateInstantiations = templateInstantiations;
105  }
106 
107  std::map<std::string, VariantPtr>
109  {
110  std::map<std::string, VariantPtr> ret = memberTypes;
111  if (extends)
112  {
113  for (const auto& [key, t] : extends->getMemberTypes())
114  {
115  ret.insert({key, t});
116  }
117  }
118  return ret;
119  }
120 
121  std::map<std::string, VariantPtr>
123  {
124  return memberTypes;
125  }
126 
127  VariantPtr
128  Object::getMemberType(const std::string& s) const
129  {
130  if (memberTypes.find(s) == memberTypes.end() and not(extends and extends->hasMemberType(s)))
131  {
133  "ObjectNavigator",
134  "getMemberType",
135  "Member not set. The list of all members is: " +
136  simox::alg::to_string(simox::alg::get_keys(memberTypes)),
137  s);
138  }
139  if (memberTypes.find(s) == memberTypes.end())
140  {
141  return extends->getMemberType(s);
142  }
143  return memberTypes.at(s);
144  }
145 
146  void
147  Object::addMemberType(const std::string& k, const VariantPtr& v)
148  {
149  if (k.empty())
150  {
151  throw error::AronException(
152  __PRETTY_FUNCTION__, "Cannot set an element with an empty key.", getPath());
153  }
154 
156 
157  this->aron->elementTypes[k] = v->toAronDTO();
158  memberTypes[k] = v;
159  }
160 
161  void
162  Object::setObjectName(const std::string& n)
163  {
164  checkObjectName(n);
165  //path.setRootIdentifier(n);
166  this->aron->objectName = n;
167  }
168 
169  void
170  Object::setExtends(const std::shared_ptr<Object>& p)
171  {
173  type::dto::AronObjectPtr ex = p->toAronObjectDTO();
175  aron->parent = ex;
176  extends = p;
177  }
178 
179  bool
180  Object::hasMemberType(const std::string& k) const
181  {
182  return memberTypes.count(k) > 0 or (extends && extends->hasMemberType(k));
183  }
184 
185  std::string
187  {
188  std::vector<std::string> split = simox::alg::split(aron->objectName, "::");
189  return split[split.size() - 1];
190  }
191 
192  std::string
194  {
195  if (aron->templateInstantiations.empty())
196  {
197  return getObjectName();
198  }
199  return getObjectName() + "<" + simox::alg::join(aron->templateInstantiations, ", ") + ">";
200  }
201 
202  std::string
204  {
205  if (aron->templates.empty())
206  {
207  return getObjectName();
208  }
209  return getObjectName() + "<" + simox::alg::join(aron->templates, ", ") + ">";
210  }
211 
212  void
213  Object::addTemplate(const std::string& s) const
214  {
215  if (std::find(aron->templates.begin(), aron->templates.end(), s) != aron->templates.end())
216  {
218  __PRETTY_FUNCTION__, "The template already exists!", s);
219  }
220  aron->templates.push_back(s);
221  }
222 
223  void
224  Object::addTemplateInstantiation(const std::string& s) const
225  {
226  if (std::find(aron->templateInstantiations.begin(),
227  aron->templateInstantiations.end(),
228  s) != aron->templateInstantiations.end())
229  {
231  __PRETTY_FUNCTION__, "The template arg already exists!", s);
232  }
233  aron->templateInstantiations.push_back(s);
234  }
235 
236  std::vector<std::string>
238  {
239  return aron->templates;
240  }
241 
242  std::vector<std::string>
244  {
245  return aron->templateInstantiations;
246  }
247 
248  std::string
250  {
251  return this->aron->objectName;
252  }
253 
254  std::shared_ptr<Object>
256  {
257  return extends;
258  }
259 
260  std::vector<std::string>
262  {
263  std::vector<std::string> ret;
264  for (const auto& [k, _] : memberTypes)
265  {
266  ret.push_back(k);
267  }
268  if (extends)
269  {
270  for (const auto& s : extends->getAllKeys())
271  {
272  ret.push_back(s);
273  }
274  }
275  return ret;
276  }
277 
278  type::dto::AronObjectPtr
280  {
281  return this->aron;
282  }
283 
284  // virtual implementations
285  std::vector<VariantPtr>
287  {
288  std::vector<VariantPtr> ret;
289  for (const auto& [k, t] : memberTypes)
290  {
291  ret.push_back(t);
292  }
293  if (extends)
294  {
295  for (const auto& t : extends->getChildren())
296  {
297  ret.push_back(t);
298  }
299  }
300  return ret;
301  }
302 
303  size_t
305  {
306  return memberTypes.size();
307  }
308 
309  std::string
311  {
312  return "Object<" + this->aron->objectName +
313  (extends ? (" : " + extends->getShortName()) : "") + ">";
314  }
315 
316  std::string
318  {
319  return "armarx::aron::type::Object<" + this->aron->objectName +
320  (extends ? (" : " + extends->getFullName()) : "") + ">";
321  }
322 
323  std::string
325  {
326  return "armarx::aron::type::Object";
327  }
328 
329  std::string
331  {
332  return "Object";
333  }
334 
335  VariantPtr
336  Object::navigateAbsolute(const Path& path) const
337  {
338  if (!path.hasElement())
339  {
340  throw error::AronException(
341  __PRETTY_FUNCTION__, "Could not navigate without a valid path", path);
342  }
343 
344  std::string el = path.getFirstElement();
345  if (!hasMemberType(el))
346  {
348  __PRETTY_FUNCTION__, "Could not find an element of a path.", el, path);
349  }
350 
351  if (path.size() == 1)
352  {
353  return memberTypes.at(el);
354  }
355  else
356  {
358  if (!memberTypes.at(el))
359  {
360  throw error::AronException(
361  __PRETTY_FUNCTION__, "Could not navigate into a NULL member.", next);
362  }
363  return memberTypes.at(el)->navigateAbsolute(next);
364  }
365  }
366 } // namespace armarx::aron::type
armarx::aron::type::Object::GetFullNamePrefix
static std::string GetFullNamePrefix()
Definition: Object.cpp:324
armarx::aron::Path::withElement
Path withElement(const std::string &, bool escape=false) const
Definition: Path.cpp:161
armarx::aron::Path::getFirstElement
std::string getFirstElement() const
Definition: Path.cpp:102
armarx::aron::error::AronException
A base class for aron exceptions.
Definition: Exception.h:42
armarx::aron::type::VariantPtr
std::shared_ptr< Variant > VariantPtr
Definition: forward_declarations.h:11
armarx::aron::type::Object::getMemberType
VariantPtr getMemberType(const std::string &) const
Definition: Object.cpp:128
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
armarx::aron::type::Variant::FromAronDTO
static VariantPtr FromAronDTO(const type::dto::GenericType &, const Path &=Path())
create a variant object from an dto object
Definition: Variant.cpp:39
armarx::aron::type::Object::getAllKeys
std::vector< std::string > getAllKeys() const
Definition: Object.cpp:261
armarx::aron::Path::hasElement
bool hasElement() const
Definition: Path.cpp:113
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::aron::Path::size
size_t size() const
Definition: Path.cpp:119
armarx::aron::type::Object::getObjectNameWithTemplates
std::string getObjectNameWithTemplates() const
Definition: Object.cpp:203
armarx::aron::type::Object::setObjectName
void setObjectName(const std::string &)
Definition: Object.cpp:162
armarx::aron::type::Object
The Object class.
Definition: Object.h:42
detail
Definition: OpenCVUtil.cpp:127
armarx::aron::type::Object::getExtends
std::shared_ptr< Object > getExtends() const
Definition: Object.cpp:255
armarx::aron::type::Object::getObjectName
std::string getObjectName() const
Definition: Object.cpp:249
armarx::aron::type::Object::getTemplateInstantiations
std::vector< std::string > getTemplateInstantiations() const
Definition: Object.cpp:243
armarx::aron::Path
The Path class.
Definition: Path.h:36
Object.h
armarx::aron::error::ValueNotValidException
The ValueNotValidException class.
Definition: Exception.h:145
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::aron::type::Object::getChildren
std::vector< VariantPtr > getChildren() const override
get all child elements
Definition: Object.cpp:286
armarx::aron::type::Object::setExtends
void setExtends(const std::shared_ptr< Object > &)
Definition: Object.cpp:170
armarx::aron::type::Object::navigateAbsolute
VariantPtr navigateAbsolute(const Path &path) const override
naviate absolute
Definition: Object.cpp:336
armarx::aron::type::Object::FromAronObjectDTO
static ObjectPtr FromAronObjectDTO(const type::dto::AronObjectPtr &, const aron::Path &path=aron::Path())
Definition: Object.cpp:54
armarx::aron::type::detail::SpecializedVariantBase< type::dto::AronObject, Object >::aron
type::dto::AronObject ::PointerType aron
Definition: SpecializedVariant.h:137
armarx::aron::type::Object::hasMemberType
bool hasMemberType(const std::string &) const
Definition: Object.cpp:180
armarx::aron::type
A convenience header to include all aron files (full include, not forward declared)
Definition: aron_conversions.cpp:9
armarx::aron::type::Object::setTemplateInstantiations
void setTemplateInstantiations(const std::vector< std::string > &) const
Definition: Object.cpp:102
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::aron::type::Object::setMemberTypes
void setMemberTypes(const std::map< std::string, VariantPtr > &)
Definition: Object.cpp:86
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::aron::type::Object::getDirectMemberTypes
std::map< std::string, VariantPtr > getDirectMemberTypes() const
Definition: Object.cpp:122
armarx::aron::type::Object::GetNamePrefix
static std::string GetNamePrefix()
Definition: Object.cpp:330
armarx::aron::type::Object::childrenSize
size_t childrenSize() const override
Definition: Object.cpp:304
armarx::aron::type::Object::Object
Object(const Path &=Path())
Definition: Object.cpp:34
armarx::aron::type::Object::ToAronObjectDTO
static type::dto::AronObjectPtr ToAronObjectDTO(const ObjectPtr &)
Definition: Object.cpp:64
armarx::aron::type::Object::addTemplate
void addTemplate(const std::string &) const
Definition: Object.cpp:213
armarx::aron::type::Descriptor::OBJECT
@ OBJECT
armarx::aron::Path::withDetachedFirstElement
Path withDetachedFirstElement() const
Definition: Path.cpp:205
armarx::aron::type::Object::addMemberType
void addMemberType(const std::string &, const VariantPtr &)
Definition: Object.cpp:147
armarx::aron::type::Object::addTemplateInstantiation
void addTemplateInstantiation(const std::string &) const
Definition: Object.cpp:224
armarx::aron::type::Object::getFullName
std::string getFullName() const override
get the full name of this specific type
Definition: Object.cpp:317
armarx::aron::type::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Definition: Object.h:36
armarx::aron::type::Object::setTemplates
void setTemplates(const std::vector< std::string > &)
Definition: Object.cpp:96
armarx::aron::type::Variant::path
const Path path
Definition: Variant.h:139
armarx::aron::type::Object::getMemberTypes
std::map< std::string, VariantPtr > getMemberTypes() const
Definition: Object.cpp:108
armarx::aron::type::Object::getShortName
std::string getShortName() const override
get a short name of this specific type
Definition: Object.cpp:310
armarx::aron::type::Descriptor
Descriptor
Definition: Descriptor.h:76
armarx::aron::type::Object::checkObjectName
bool checkObjectName(const std::string &) const
Definition: Object.cpp:74
armarx::aron::type::Object::toAronObjectDTO
type::dto::AronObjectPtr toAronObjectDTO() const
Definition: Object.cpp:279
armarx::aron::type::Object::getTemplates
std::vector< std::string > getTemplates() const
Definition: Object.cpp:237
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::aron::type::Object::getObjectNameWithTemplateInstantiations
std::string getObjectNameWithTemplateInstantiations() const
Definition: Object.cpp:193
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:36
armarx::aron::type::Variant::getPath
Path getPath() const
Definition: Variant.h:99
armarx::aron::type::Object::getObjectNameWithoutNamespace
std::string getObjectNameWithoutNamespace() const
Definition: Object.cpp:186