VariantContainer.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarXCore::core
19  * @author Kai Welke (welke at kit dot edu)
20  * @date 2011
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 
26 #include <boost/regex.hpp>
27 
28 #include <Ice/ObjectAdapter.h>
29 #include <Ice/ValueFactory.h>
30 #include <IceUtil/UUID.h>
31 
37 
38 template class ::IceInternal::Handle<::armarx::SingleVariant>;
39 template class ::IceInternal::Handle<::armarx::ContainerTypeI>;
40 
41 namespace armarx
42 {
43  SingleVariant::SingleVariant()
44  {
45  element = new Variant();
46 
47  typeContainer = new ContainerTypeI();
48  typeContainer->typeId = Variant::typeToString(
49  VariantType::Invalid); //Variant::addTypeName(Variant::typeToString(subType));
50  }
51 
52  SingleVariant::SingleVariant(const Variant& variant)
53  {
54  element = new Variant(variant);
55 
56  typeContainer = new ContainerTypeI();
57  typeContainer->typeId = Variant::typeToString(
58  variant.getType()); //Variant::addTypeName(Variant::typeToString(subType));
59  }
60 
61  SingleVariant::SingleVariant(const SingleVariant& source) :
62  Shared(source),
63  VariantContainerBase(source),
65  SingleVariantBase(source)
66  {
67  *this = source;
68  }
69 
72  {
73  // ARMARX_IMPORTANT_S << VariantContainerType::allTypesToString(source.typeContainer) << "::operator =" << LogSender::CreateBackTrace();
74 
75  element = VariantPtr::dynamicCast(source.element)->clone();
76  //ARMARX_WARNING_S << "type source: " << source.element->data->ice_id() << " new element: " << element->data->ice_id();
77  typeContainer = source.typeContainer->clone();
78  return *this;
79  }
80 
81  VariantContainerBasePtr
82  SingleVariant::cloneContainer(const ::Ice::Current&) const
83  {
84  return new SingleVariant(*this);
85  }
86 
89  {
90  return this->clone();
91  }
92 
94  SingleVariant::getElementBase(const Ice::Current&) const
95  {
96  return element;
97  }
98 
101  {
102  return VariantPtr::dynamicCast(getElementBase());
103  }
104 
105  void
107  {
108  element = variant->clone();
109  typeContainer = new ContainerTypeI();
110  typeContainer->typeId = Variant::typeToString(
111  element->getType()); //Variant::addTypeName(Variant::typeToString(subType));
112  }
113 
114  int
115  SingleVariant::getSize(const Ice::Current&) const
116  {
117  return 1;
118  }
119 
120  ContainerTypePtr
121  SingleVariant::getContainerType(const Ice::Current& c) const
122  {
123  return typeContainer;
124  }
125 
126  void
127  SingleVariant::setContainerType(const ContainerTypePtr& containerType, const Ice::Current& c)
128  {
129  this->typeContainer = containerType->clone();
130  }
131 
133  SingleVariant::getStaticType(const Ice::Current& c)
134  {
136  }
137 
138  bool
139  SingleVariant::validateElements(const Ice::Current&)
140  {
141  return element->validate();
142  }
143 
144  void
145  SingleVariant::serialize(const ObjectSerializerBasePtr& serializer, const Ice::Current&) const
146  {
147  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
148  obj->setVariant("variant", get());
149  }
150 
151  void
152  SingleVariant::deserialize(const ObjectSerializerBasePtr& serializer, const Ice::Current&)
153  {
154  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
155  setVariant(obj->getVariant("variant"));
156  }
157 
158  std::string
160  {
161  return "::armarx::SingleVariantBase";
162  }
163 
164  std::string
165  SingleVariant::toString(const Ice::Current& c) const
166  {
167  VariantPtr variant = VariantPtr::dynamicCast(element);
168  std::string variantString = variant->getOutputValueOnly();
169  return variantString;
170  }
171 
173  {
174  typeId = Variant::typeToString(variantType);
175  }
176 
177  ContainerTypePtr
178  ContainerTypeI::clone(const Ice::Current&) const
179  {
180  ContainerTypePtr newType = new ContainerTypeI();
181  newType->typeId = this->typeId;
182 
183  if (subType)
184  {
185  newType->subType = subType->clone();
186  }
187 
188  return newType;
189  }
190 
192  {
193  this->containerType = containerType;
194  thisType = new ContainerTypeI();
195  thisType->typeId = containerType; //Variant::addTypeName(containerType);
196  }
197 
198  const ContainerTypeI
200  {
201 
202  ContainerTypeI result = *(ContainerTypeIPtr::dynamicCast(thisType->clone()));
203  result.subType = new ContainerTypeI();
204  result.subType->typeId = Variant::typeToString(typeId); //SingleVariant::getTypePrefix();
205  // result->subType->subType = new ContainerTypeI();
206  // result->subType->subType->typeId = Variant::typeToString(typeId);
207  return result;
208  }
209 
210  const ContainerTypeI
211  VariantContainerType::operator()(const ContainerType& subType) const
212  {
213  ContainerTypeI result = *(ContainerTypeIPtr::dynamicCast(thisType->clone()));
214  result.subType = subType.clone();
215  return result;
216  }
217 
218  bool
219  VariantContainerType::compare(const ContainerTypePtr& firstType,
220  const ContainerTypePtr& secondType)
221  {
222  ContainerTypePtr type1 = firstType;
223  ContainerTypePtr type2 = secondType;
224 
225  if (!type1 && !type2)
226  {
227  return false;
228  }
229 
230  while (type1 || type2)
231  {
232  if ((type1 && !type2) || (!type1 && type2))
233  {
234  return false;
235  }
236 
237  if (type1->typeId != type2->typeId)
238  {
239  return false;
240  }
241 
242  type1 = type1->subType;
243  type2 = type2->subType;
244  }
245 
246  return true;
247  }
248 
249  std::string
250  VariantContainerType::allTypesToString(const ContainerTypePtr& type)
251  {
252  std::string result;
253  ContainerTypePtr curType = type;
254  std::vector<std::string> typeStrings;
255 
256  while (curType)
257  {
258  // result += curType->typeId;
259  typeStrings.push_back(curType->typeId);
260  curType = curType->subType;
261  }
262 
263  std::vector<std::string>::reverse_iterator rit = typeStrings.rbegin();
264 
265  for (; rit != typeStrings.rend(); rit++)
266  {
267  if (result.empty())
268  {
269  result = *rit;
270  }
271  else
272  {
273  result = *rit + "(" + result + ")";
274  }
275  }
276 
277  return result;
278  }
279 
280  ContainerTypePtr
281  VariantContainerType::FromString(const std::string& typeStr)
282  {
283  boost::regex exp("([a-zA-Z:0-9_\\-]+?)\\(([a-zA-Z:0-9_\\-()]+)\\)");
284  boost::match_results<std::string::const_iterator> matches;
285  ContainerTypePtr result = new ContainerTypeI();
286 
287  if (boost::regex_search(typeStr, matches, exp))
288  {
289  result->subType = FromString(matches[2]);
290  result->typeId = matches[1];
291  }
292  else
293  {
294  result->typeId = typeStr;
295  }
296 
297  return result;
298  }
299 
300  std::string
301  VariantContainerType::GetInnerType(const std::string& typeStr)
302  {
303  return GetInnerType(FromString(typeStr));
304  }
305 
306  std::string
307  VariantContainerType::GetInnerType(ContainerTypePtr type)
308  {
309  while (type->subType)
310  {
311  type = type->subType;
312  }
313  return type->typeId;
314  }
315 
317  {
318  typeContainer = new ContainerTypeI();
319  typeContainer->typeId = Variant::typeToString(
320  VariantType::Invalid); //Variant::addTypeName(Variant::typeToString(subType));
321  }
322 
324  Shared(source), VariantContainerBase(source)
325  {
326  typeContainer = source.typeContainer->clone();
327  }
328 
329  VariantContainerBasePtr
330  ContainerDummy::cloneContainer(const Ice::Current&) const
331  {
332  return new ContainerDummy(*this);
333  }
334 
337  {
338  return this->clone();
339  }
340 
341  ContainerTypePtr
342  ContainerDummy::getContainerType(const Ice::Current&) const
343  {
344  return typeContainer;
345  }
346 
347  void
348  ContainerDummy::setContainerType(const ContainerTypePtr& containerType, const Ice::Current&)
349  {
350  typeContainer = containerType;
351  }
352 
353  int
354  ContainerDummy::getSize(const Ice::Current&) const
355  {
356  return 0;
357  }
358 
359  bool
360  ContainerDummy::validateElements(const Ice::Current&)
361  {
362  return true;
363  }
364 
365  std::string
366  ContainerDummy::toString(const Ice::Current& c) const
367  {
368  return "-";
369  }
370 
371  ContainerTypePtr
372  VariantContainer::getContainerType(const Ice::Current& c) const
373  {
374  return typeContainer;
375  }
376 
377  void
378  VariantContainer::setContainerType(const ContainerTypePtr& containerType, const Ice::Current& c)
379  {
380  typeContainer = containerType;
381  }
382 
383  VariantDataClassPtr
384  VariantContainer::clone(const Ice::Current& c) const
385  {
386  return cloneContainer(c);
387  }
388 
389  std::string
390  VariantContainer::output(const Ice::Current& c) const
391  {
392  return toString();
393  }
394 
395  Ice::Int
396  VariantContainer::getType(const Ice::Current& c) const
397  {
399  }
400 
401  bool
402  VariantContainer::validate(const Ice::Current& c)
403  {
404  return validateElements(c);
405  }
406 
408  SingleVariant(static_cast<const Variant&>(variant))
409  {
410  }
411 
413  SingleVariant(static_cast<const Variant&>(variant))
414  {
415  }
416 
417 } // namespace armarx
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:223
armarx::ContainerDummy::toString
std::string toString(const Ice::Current &c) const override
Definition: VariantContainer.cpp:366
armarx::SingleVariant::serialize
void serialize(const ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:145
armarx::SingleVariant::operator=
SingleVariant & operator=(const SingleVariant &source)
Definition: VariantContainer.cpp:71
armarx::VariantContainerType::GetInnerType
static std::string GetInnerType(const std::string &typeStr)
Definition: VariantContainer.cpp:301
armarx::SingleVariant::toString
std::string toString(const Ice::Current &c) const override
Definition: VariantContainer.cpp:165
armarx::Variant::getType
VariantTypeId getType(const Ice::Current &c=Ice::emptyCurrent) const override
Return the Variant's internal type.
Definition: Variant.cpp:679
VariantContainer.h
armarx::SingleVariant::getTypePrefix
static std::string getTypePrefix()
Definition: VariantContainer.cpp:159
armarx::VariantType::VariantContainer
const VariantTypeId VariantContainer
Definition: VariantContainer.h:40
armarx::SingleVariant::setContainerType
void setContainerType(const ContainerTypePtr &containerType, const Ice::Current &c=Ice::emptyCurrent) override
Definition: VariantContainer.cpp:127
armarx::SingleVariant::validateElements
bool validateElements(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: VariantContainer.cpp:139
armarx::ContainerDummy::getContainerType
ContainerTypePtr getContainerType(const Ice::Current &=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:342
armarx::SingleVariant::SingleVariant
SingleVariant()
Definition: VariantContainer.cpp:43
AbstractObjectSerializer.h
armarx::VariantContainer::output
std::string output(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:390
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
DataFieldIdentifier.h
armarx::ContainerDummy::getSize
int getSize(const Ice::Current &=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:354
armarx::SingleVariant::cloneContainer
VariantContainerBasePtr cloneContainer(const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:82
armarx::ContainerDummy::ice_clone
Ice::ObjectPtr ice_clone() const override
Definition: VariantContainer.cpp:336
armarx::SingleVariant::getStaticType
static VariantTypeId getStaticType(const Ice::Current &c=Ice::emptyCurrent)
Definition: VariantContainer.cpp:133
armarx::ContainerTypeI::ContainerTypeI
ContainerTypeI()
Definition: VariantContainer.h:203
IceInternal::Handle<::armarx::VariantBase >
InvalidTypeException.h
armarx::ContainerDummy::setContainerType
void setContainerType(const ContainerTypePtr &containerType, const Ice::Current &=Ice::emptyCurrent) override
Definition: VariantContainer.cpp:348
armarx::VariantContainerType::VariantContainerType
VariantContainerType(std::string containerType)
Definition: VariantContainer.cpp:191
armarx::SingleVariant::getElementBase
VariantBasePtr getElementBase(const Ice::Current &=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:94
armarx::SingleVariant::get
VariantPtr get() const
Definition: VariantContainer.cpp:100
armarx::ContainerTypeI::clone
ContainerTypePtr clone(const Ice::Current &=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:178
armarx::SingleVariant
The SingleVariant class is required to store single Variant instances in VariantContainer subclasses.
Definition: VariantContainer.h:107
armarx::SingleVariant::deserialize
void deserialize(const ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: VariantContainer.cpp:152
armarx::VariantContainerType::compare
static bool compare(const ContainerTypePtr &type1, const ContainerTypePtr &secondType)
Definition: VariantContainer.cpp:219
armarx::VariantContainerType::operator()
const ContainerTypeI operator()(VariantTypeId typeId) const
Definition: VariantContainer.cpp:199
armarx::VariantType::Invalid
const VariantTypeId Invalid
Definition: Variant.h:915
UnknownTypeException.h
armarx::Variant::typeToString
static std::string typeToString(VariantTypeId typeId)
Return the name of the registered type typeId.
Definition: Variant.cpp:848
armarx::VariantTypeId
Ice::Int VariantTypeId
Definition: Variant.h:43
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:661
armarx::SingleVariant::getSize
int getSize(const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:115
armarx::VariantContainer
VariantContainer is the base class of all other Variant container classes.
Definition: VariantContainer.h:84
armarx::VariantContainer::validate
bool validate(const Ice::Current &c=Ice::emptyCurrent) override
Definition: VariantContainer.cpp:402
armarx::SingleVariant::ice_clone
Ice::ObjectPtr ice_clone() const override
Definition: VariantContainer.cpp:88
armarx::ContainerDummy::ContainerDummy
ContainerDummy()
Definition: VariantContainer.cpp:316
armarx::ContainerTypeI
Definition: VariantContainer.h:200
armarx::VariantContainer::getType
Ice::Int getType(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:396
armarx::skills::gui::matches
bool matches(std::string skillName, std::vector< std::string > &searches)
Definition: SkillManagerWrapper.cpp:66
armarx::viz::toString
const char * toString(InteractionFeedbackType type)
Definition: Interaction.h:28
armarx::VariantContainer::clone
VariantDataClassPtr clone(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:384
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:917
armarx::ContainerDummy::cloneContainer
VariantContainerBasePtr cloneContainer(const Ice::Current &=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:330
armarx::aron::type::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Definition: Object.h:36
armarx::SingleVariant::setVariant
void setVariant(const VariantPtr &variant)
Definition: VariantContainer.cpp:106
armarx::SingleVariant::getContainerType
ContainerTypePtr getContainerType(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:121
armarx::VariantContainerType::FromString
static ContainerTypePtr FromString(const std::string &typeStr)
Definition: VariantContainer.cpp:281
armarx::ContainerDummy
Definition: VariantContainer.h:162
armarx::ContainerDummy::validateElements
bool validateElements(const Ice::Current &=Ice::emptyCurrent) override
Definition: VariantContainer.cpp:360
armarx::VariantContainer::setContainerType
void setContainerType(const ContainerTypePtr &containerType, const Ice::Current &c=Ice::emptyCurrent) override
Definition: VariantContainer.cpp:378
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::Variant::addTypeName
static VariantTypeId addTypeName(const std::string &typeName)
Register a new type for the use in a Variant.
Definition: Variant.cpp:869
armarx::VariantContainer::getContainerType
ContainerTypePtr getContainerType(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:372
armarx::VariantContainerType::allTypesToString
static std::string allTypesToString(const ContainerTypePtr &type)
Definition: VariantContainer.cpp:250