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 
31 
32 #include <boost/regex.hpp>
33 
34 #include <IceUtil/UUID.h>
35 #include <Ice/ObjectAdapter.h>
36 #include <Ice/ValueFactory.h>
37 
38 template class ::IceInternal::Handle<::armarx::SingleVariant>;
39 template class ::IceInternal::Handle<::armarx::ContainerTypeI>;
40 
41 
42 namespace armarx
43 {
44  SingleVariant::SingleVariant()
45  {
46  element = new Variant();
47 
48  typeContainer = new ContainerTypeI();
49  typeContainer->typeId = Variant::typeToString(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(variant.getType()); //Variant::addTypeName(Variant::typeToString(subType));
58  }
59 
60  SingleVariant::SingleVariant(const SingleVariant& source) :
61  Shared(source),
62  VariantContainerBase(source),
64  SingleVariantBase(source)
65  {
66  *this = source;
67  }
68 
70  {
71  // ARMARX_IMPORTANT_S << VariantContainerType::allTypesToString(source.typeContainer) << "::operator =" << LogSender::CreateBackTrace();
72 
73  element = VariantPtr::dynamicCast(source.element)->clone();
74  //ARMARX_WARNING_S << "type source: " << source.element->data->ice_id() << " new element: " << element->data->ice_id();
75  typeContainer = source.typeContainer->clone();
76  return *this;
77  }
78 
79  VariantContainerBasePtr SingleVariant::cloneContainer(const::Ice::Current&) const
80  {
81  return new SingleVariant(*this);
82  }
83 
85  {
86  return this->clone();
87  }
88 
89  VariantBasePtr SingleVariant::getElementBase(const Ice::Current&) const
90  {
91  return element;
92  }
93 
95  {
96  return VariantPtr::dynamicCast(getElementBase());
97  }
98 
100  {
101  element = variant->clone();
102  typeContainer = new ContainerTypeI();
103  typeContainer->typeId = Variant::typeToString(element->getType()); //Variant::addTypeName(Variant::typeToString(subType));
104 
105  }
106 
107  int SingleVariant::getSize(const Ice::Current&) const
108  {
109  return 1;
110  }
111 
112  ContainerTypePtr SingleVariant::getContainerType(const Ice::Current& c) const
113  {
114  return typeContainer;
115  }
116 
117  void SingleVariant::setContainerType(const ContainerTypePtr& containerType, const Ice::Current& c)
118  {
119  this->typeContainer = containerType->clone();
120  }
121 
123  {
125  }
126 
127  bool SingleVariant::validateElements(const Ice::Current&)
128  {
129  return element->validate();
130  }
131 
132  void SingleVariant::serialize(const ObjectSerializerBasePtr& serializer, const Ice::Current&) const
133  {
134  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
135  obj->setVariant("variant", get());
136  }
137 
138  void SingleVariant::deserialize(const ObjectSerializerBasePtr& serializer, const Ice::Current&)
139  {
140  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
141  setVariant(obj->getVariant("variant"));
142  }
143 
145  {
146  return "::armarx::SingleVariantBase";
147  }
148 
149  std::string SingleVariant::toString(const Ice::Current& c) const
150  {
151  VariantPtr variant = VariantPtr::dynamicCast(element);
152  std::string variantString = variant->getOutputValueOnly();
153  return variantString;
154  }
155 
156 
157 
158 
160  {
161  typeId = Variant::typeToString(variantType);
162  }
163 
164  ContainerTypePtr ContainerTypeI::clone(const Ice::Current&) const
165  {
166  ContainerTypePtr newType = new ContainerTypeI();
167  newType->typeId = this->typeId;
168 
169  if (subType)
170  {
171  newType->subType = subType->clone();
172  }
173 
174  return newType;
175  }
176 
177 
178 
180  {
181  this->containerType = containerType;
182  thisType = new ContainerTypeI();
183  thisType->typeId = containerType;//Variant::addTypeName(containerType);
184  }
185 
187  {
188 
189  ContainerTypeI result = *(ContainerTypeIPtr::dynamicCast(thisType->clone()));
190  result.subType = new ContainerTypeI();
191  result.subType->typeId = Variant::typeToString(typeId); //SingleVariant::getTypePrefix();
192  // result->subType->subType = new ContainerTypeI();
193  // result->subType->subType->typeId = Variant::typeToString(typeId);
194  return result;
195  }
196  const ContainerTypeI VariantContainerType::operator()(const ContainerType& subType) const
197  {
198  ContainerTypeI result = *(ContainerTypeIPtr::dynamicCast(thisType->clone()));
199  result.subType = subType.clone();
200  return result;
201  }
202 
203  bool VariantContainerType::compare(const ContainerTypePtr& firstType, const ContainerTypePtr& secondType)
204  {
205  ContainerTypePtr type1 = firstType;
206  ContainerTypePtr type2 = secondType;
207 
208  if (!type1 && !type2)
209  {
210  return false;
211  }
212 
213  while (type1 || type2)
214  {
215  if ((type1 && !type2) || (!type1 && type2))
216  {
217  return false;
218  }
219 
220  if (type1->typeId != type2->typeId)
221  {
222  return false;
223  }
224 
225  type1 = type1->subType;
226  type2 = type2->subType;
227  }
228 
229  return true;
230  }
231 
232  std::string VariantContainerType::allTypesToString(const ContainerTypePtr& type)
233  {
234  std::string result;
235  ContainerTypePtr curType = type;
236  std::vector<std::string> typeStrings;
237 
238  while (curType)
239  {
240  // result += curType->typeId;
241  typeStrings.push_back(curType->typeId);
242  curType = curType->subType;
243  }
244 
245  std::vector<std::string>::reverse_iterator rit = typeStrings.rbegin();
246 
247  for (; rit != typeStrings.rend(); rit++)
248  {
249  if (result.empty())
250  {
251  result = *rit;
252  }
253  else
254  {
255  result = *rit + "(" + result + ")";
256  }
257  }
258 
259  return result;
260  }
261 
262  ContainerTypePtr VariantContainerType::FromString(const std::string& typeStr)
263  {
264  boost::regex exp("([a-zA-Z:0-9_\\-]+?)\\(([a-zA-Z:0-9_\\-()]+)\\)");
265  boost::match_results<std::string::const_iterator> matches;
266  ContainerTypePtr result = new ContainerTypeI();
267 
268  if (boost::regex_search(typeStr, matches, exp))
269  {
270  result->subType = FromString(matches[2]);
271  result->typeId = matches[1];
272  }
273  else
274  {
275  result->typeId = typeStr;
276  }
277 
278  return result;
279  }
280 
281  std::string VariantContainerType::GetInnerType(const std::string& typeStr)
282  {
283  return GetInnerType(FromString(typeStr));
284  }
285 
286  std::string VariantContainerType::GetInnerType(ContainerTypePtr type)
287  {
288  while (type->subType)
289  {
290  type = type->subType;
291  }
292  return type->typeId;
293  }
294 
295 
296 
297 
299  {
300  typeContainer = new ContainerTypeI();
301  typeContainer->typeId = Variant::typeToString(VariantType::Invalid); //Variant::addTypeName(Variant::typeToString(subType));
302 
303  }
304 
306  Shared(source),
307  VariantContainerBase(source)
308  {
309  typeContainer = source.typeContainer->clone();
310  }
311 
312  VariantContainerBasePtr ContainerDummy::cloneContainer(const Ice::Current&) const
313  {
314  return new ContainerDummy(*this);
315  }
316 
318  {
319  return this->clone();
320  }
321 
322  ContainerTypePtr ContainerDummy::getContainerType(const Ice::Current&) const
323  {
324  return typeContainer;
325  }
326 
327  void ContainerDummy::setContainerType(const ContainerTypePtr& containerType, const Ice::Current&)
328  {
329  typeContainer = containerType;
330  }
331 
332  int ContainerDummy::getSize(const Ice::Current&) const
333  {
334  return 0;
335  }
336 
337  bool ContainerDummy::validateElements(const Ice::Current&)
338  {
339  return true;
340  }
341 
342 
343  std::string ContainerDummy::toString(const Ice::Current& c) const
344  {
345  return "-";
346  }
347 
348  ContainerTypePtr VariantContainer::getContainerType(const Ice::Current& c) const
349  {
350  return typeContainer;
351  }
352 
353  void VariantContainer::setContainerType(const ContainerTypePtr& containerType, const Ice::Current& c)
354  {
355  typeContainer = containerType;
356  }
357 
358  VariantDataClassPtr VariantContainer::clone(const Ice::Current& c) const
359  {
360  return cloneContainer(c);
361  }
362 
363  std::string VariantContainer::output(const Ice::Current& c) const
364  {
365  return toString();
366  }
367 
368  Ice::Int VariantContainer::getType(const Ice::Current& c) const
369  {
371  }
372 
373  bool VariantContainer::validate(const Ice::Current& c)
374  {
375  return validateElements(c);
376  }
377 
379  SingleVariant(static_cast<const Variant&>(variant))
380  {}
381 
383  SingleVariant(static_cast<const Variant&>(variant))
384  {}
385 
386 }
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::ContainerDummy::toString
std::string toString(const Ice::Current &c) const override
Definition: VariantContainer.cpp:343
armarx::SingleVariant::serialize
void serialize(const ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:132
armarx::SingleVariant::operator=
SingleVariant & operator=(const SingleVariant &source)
Definition: VariantContainer.cpp:69
armarx::VariantContainerType::GetInnerType
static std::string GetInnerType(const std::string &typeStr)
Definition: VariantContainer.cpp:281
armarx::SingleVariant::toString
std::string toString(const Ice::Current &c) const override
Definition: VariantContainer.cpp:149
armarx::Variant::getType
VariantTypeId getType(const Ice::Current &c=Ice::emptyCurrent) const override
Return the Variant's internal type.
Definition: Variant.cpp:574
VariantContainer.h
armarx::SingleVariant::getTypePrefix
static std::string getTypePrefix()
Definition: VariantContainer.cpp:144
armarx::VariantType::VariantContainer
const VariantTypeId VariantContainer
Definition: VariantContainer.h:41
armarx::SingleVariant::setContainerType
void setContainerType(const ContainerTypePtr &containerType, const Ice::Current &c=Ice::emptyCurrent) override
Definition: VariantContainer.cpp:117
armarx::SingleVariant::validateElements
bool validateElements(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: VariantContainer.cpp:127
armarx::ContainerDummy::getContainerType
ContainerTypePtr getContainerType(const Ice::Current &=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:322
armarx::SingleVariant::SingleVariant
SingleVariant()
Definition: VariantContainer.cpp:44
AbstractObjectSerializer.h
armarx::VariantContainer::output
std::string output(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:363
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
DataFieldIdentifier.h
armarx::ContainerDummy::getSize
int getSize(const Ice::Current &=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:332
armarx::SingleVariant::cloneContainer
VariantContainerBasePtr cloneContainer(const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:79
armarx::ContainerDummy::ice_clone
Ice::ObjectPtr ice_clone() const override
Definition: VariantContainer.cpp:317
armarx::SingleVariant::getStaticType
static VariantTypeId getStaticType(const Ice::Current &c=Ice::emptyCurrent)
Definition: VariantContainer.cpp:122
armarx::ContainerTypeI::ContainerTypeI
ContainerTypeI()
Definition: VariantContainer.h:178
IceInternal::Handle<::armarx::VariantBase >
InvalidTypeException.h
armarx::ContainerDummy::setContainerType
void setContainerType(const ContainerTypePtr &containerType, const Ice::Current &=Ice::emptyCurrent) override
Definition: VariantContainer.cpp:327
armarx::VariantContainerType::VariantContainerType
VariantContainerType(std::string containerType)
Definition: VariantContainer.cpp:179
armarx::SingleVariant::getElementBase
VariantBasePtr getElementBase(const Ice::Current &=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:89
armarx::SingleVariant::get
VariantPtr get() const
Definition: VariantContainer.cpp:94
armarx::ContainerTypeI::clone
ContainerTypePtr clone(const Ice::Current &=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:164
armarx::SingleVariant
The SingleVariant class is required to store single Variant instances in VariantContainer subclasses.
Definition: VariantContainer.h:109
armarx::SingleVariant::deserialize
void deserialize(const ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: VariantContainer.cpp:138
armarx::VariantContainerType::compare
static bool compare(const ContainerTypePtr &type1, const ContainerTypePtr &secondType)
Definition: VariantContainer.cpp:203
armarx::VariantContainerType::operator()
const ContainerTypeI operator()(VariantTypeId typeId) const
Definition: VariantContainer.cpp:186
armarx::VariantType::Invalid
const VariantTypeId Invalid
Definition: Variant.h:914
UnknownTypeException.h
armarx::Variant::typeToString
static std::string typeToString(VariantTypeId typeId)
Return the name of the registered type typeId.
Definition: Variant.cpp:732
armarx::VariantTypeId
Ice::Int VariantTypeId
Definition: Variant.h:44
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:681
armarx::SingleVariant::getSize
int getSize(const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:107
armarx::VariantContainer
VariantContainer is the base class of all other Variant container classes.
Definition: VariantContainer.h:85
armarx::VariantContainer::validate
bool validate(const Ice::Current &c=Ice::emptyCurrent) override
Definition: VariantContainer.cpp:373
armarx::SingleVariant::ice_clone
Ice::ObjectPtr ice_clone() const override
Definition: VariantContainer.cpp:84
armarx::ContainerDummy::ContainerDummy
ContainerDummy()
Definition: VariantContainer.cpp:298
armarx::ContainerTypeI
Definition: VariantContainer.h:175
armarx::VariantContainer::getType
Ice::Int getType(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:368
armarx::skills::gui::matches
bool matches(std::string skillName, std::vector< std::string > &searches)
Definition: SkillManagerWrapper.cpp:59
armarx::viz::toString
const char * toString(InteractionFeedbackType type)
Definition: Interaction.h:27
armarx::VariantContainer::clone
VariantDataClassPtr clone(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:358
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
armarx::ContainerDummy::cloneContainer
VariantContainerBasePtr cloneContainer(const Ice::Current &=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:312
armarx::aron::type::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Definition: Object.h:36
armarx::SingleVariant::setVariant
void setVariant(const VariantPtr &variant)
Definition: VariantContainer.cpp:99
armarx::SingleVariant::getContainerType
ContainerTypePtr getContainerType(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:112
armarx::VariantContainerType::FromString
static ContainerTypePtr FromString(const std::string &typeStr)
Definition: VariantContainer.cpp:262
armarx::ContainerDummy
Definition: VariantContainer.h:154
armarx::ContainerDummy::validateElements
bool validateElements(const Ice::Current &=Ice::emptyCurrent) override
Definition: VariantContainer.cpp:337
armarx::VariantContainer::setContainerType
void setContainerType(const ContainerTypePtr &containerType, const Ice::Current &c=Ice::emptyCurrent) override
Definition: VariantContainer.cpp:353
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::Variant::addTypeName
static VariantTypeId addTypeName(const std::string &typeName)
Register a new type for the use in a Variant.
Definition: Variant.cpp:751
armarx::VariantContainer::getContainerType
ContainerTypePtr getContainerType(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:348
armarx::VariantContainerType::allTypesToString
static std::string allTypesToString(const ContainerTypePtr &type)
Definition: VariantContainer.cpp:232