SingleTypeVariantList.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 
29 
30 #include <IceUtil/UUID.h>
31 #include <Ice/ObjectAdapter.h>
32 #include <Ice/ValueFactory.h>
33 
34 namespace armarx
35 {
37  {
38  typeContainer = VariantType::List(VariantType::Invalid).clone();
39  }
40 
41  SingleTypeVariantList::SingleTypeVariantList(const ContainerType& subType)
42 
43  {
44  this->typeContainer = VariantType::List(subType).clone();
45  }
46 
48  {
49  this->typeContainer = VariantType::List(subType).clone();
50  }
51 
53  IceUtil::Shared(source),
54  VariantContainerBase(source),
55  SingleTypeVariantListBase(source)
56  {
57  *this = source;
58  }
59 
61  {
62  typeContainer = ContainerTypePtr::dynamicCast(source.typeContainer->clone());
63  elements.clear();
64 
65  for (unsigned int i = 0; i < source.elements.size(); i++)
66  {
67  elements.push_back(source.elements[i]->cloneContainer());
68  }
69 
70  return *this;
71  }
72 
73  VariantContainerBasePtr SingleTypeVariantList::cloneContainer(const Ice::Current& c) const
74  {
75  VariantContainerBasePtr result = new SingleTypeVariantList(*this);
76  return result;
77  }
78 
80  {
81  return this->clone();
82  }
83 
84  void SingleTypeVariantList::addElement(const VariantContainerBasePtr& variantContainer, const Ice::Current& c)
85  {
86  if (!VariantContainerType::compare(variantContainer->getContainerType(), getContainerType()->subType)
88  {
89  throw exceptions::user::InvalidTypeException(getContainerType()->subType->typeId, variantContainer->getContainerType()->typeId);
90  }
91 
93  {
94  getContainerType()->subType = variantContainer->getContainerType()->clone();
95  }
96 
97  elements.push_back(variantContainer->cloneContainer());
98  }
99 
101  {
103  {
104  getContainerType()->subType = new ContainerTypeI(variant.getType());
105  }
106  else if (!getContainerType()->subType || getContainerType()->subType->typeId != Variant::typeToString(variant.getType()))
107  {
109  }
110 
111  addElement(new SingleVariant(variant));
112  }
113 
114  void SingleTypeVariantList::clear(const Ice::Current& c)
115  {
116  elements.clear();
117  }
118 
120  {
122  }
123 
124  int SingleTypeVariantList::getSize(const Ice::Current& c) const
125  {
126  return int(elements.size());
127  }
128 
129  bool SingleTypeVariantList::validateElements(const Ice::Current& c)
130  {
131  VariantContainerBaseList::iterator it = elements.begin();
132  bool result = true;
133 
134  for (; it != elements.end(); it++)
135  {
136  result = result && (*it)->validateElements();
137  }
138 
139  return result;
140  }
141 
142 
143  VariantContainerBasePtr SingleTypeVariantList::getElementBase(int index, const Ice::Current& c) const
144  {
145  if (index >= getSize(c))
146  {
147  throw IndexOutOfBoundsException();
148  }
149 
150  return elements.at(index);
151  }
152 
153 
155  {
156  VariantPtr ptr = getElement<SingleVariant>(index)->get();
157 
158  if (!ptr)
159  {
160  throw InvalidTypeException();
161  }
162 
163  return ptr;
164  }
165 
166  void SingleTypeVariantList::serialize(const ObjectSerializerBasePtr& serializer, const Ice::Current&) const
167  {
168  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
169  AbstractObjectSerializerPtr arr = obj->createElement();
170  arr->setElementType(ElementTypes::eArray);
171 
172  for (size_t i = 0; i < elements.size(); i++)
173  {
174  arr->setIceObject(i, elements.at(i));
175  }
176 
177  obj->setString("type", ice_id());
178  obj->setElement("array", arr);
179  }
180 
181  void SingleTypeVariantList::deserialize(const ObjectSerializerBasePtr& serializer, const Ice::Current&)
182  {
183  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
184  AbstractObjectSerializerPtr arr = obj->getElement("array");
185 
186  for (size_t i = 0; i < arr->size(); i++)
187  {
188  VariantContainerBasePtr c = VariantContainerBasePtr::dynamicCast(arr->getIceObject(i));
189 
190  if (c)
191  {
192  addElement(c);
193  }
194  else
195  {
196  throw LocalException("Could not cast to VariantContainerBasePtr");
197  }
198  }
199  }
200 
202  {
203  return "::armarx::SingleTypeVariantListBase";
204  }
205 
206 
207  std::string SingleTypeVariantList::toString(const Ice::Current&) const
208  {
209  std::stringstream ss;
210 
211  for (const auto& element : elements)
212  {
213  if (elements[elements.size() - 1] != element)
214  {
215  ss << element->toString() << "\n";
216  }
217  else
218  {
219  ss << element->toString();
220  }
221  }
222 
223  return ss.str();
224  }
225 
226 
228  {
230  }
231 }
armarx::SingleTypeVariantList::validateElements
bool validateElements(const Ice::Current &c=Ice::emptyCurrent) override
Definition: SingleTypeVariantList.cpp:129
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
SingleTypeVariantList.h
armarx::VariantType::List
const VariantContainerType List
Definition: SingleTypeVariantList.h:191
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::Variant::getType
VariantTypeId getType(const Ice::Current &c=Ice::emptyCurrent) const override
Return the Variant's internal type.
Definition: Variant.cpp:574
armarx::ElementTypes::eArray
@ eArray
Definition: AbstractObjectSerializer.h:36
armarx::SingleTypeVariantList::getVariant
VariantPtr getVariant(int index) const
Definition: SingleTypeVariantList.cpp:154
AbstractObjectSerializer.h
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::exceptions::user::InvalidTypeException
Definition: InvalidTypeException.h:35
IceUtil
Definition: Instance.h:21
armarx::SingleTypeVariantList::toString
std::string toString(const Ice::Current &=Ice::emptyCurrent) const override
Definition: SingleTypeVariantList.cpp:207
armarx::SingleTypeVariantList::cloneContainer
VariantContainerBasePtr cloneContainer(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: SingleTypeVariantList.cpp:73
IceInternal::Handle< Variant >
InvalidTypeException.h
armarx::SingleVariant
The SingleVariant class is required to store single Variant instances in VariantContainer subclasses.
Definition: VariantContainer.h:109
armarx::VariantContainerType::compare
static bool compare(const ContainerTypePtr &type1, const ContainerTypePtr &secondType)
Definition: VariantContainer.cpp:203
armarx::VariantType::Invalid
const VariantTypeId Invalid
Definition: Variant.h:914
armarx::SingleTypeVariantList
The SingleTypeVariantList class is a subclass of VariantContainer and is comparable to a std::vector<...
Definition: SingleTypeVariantList.h:47
armarx::Variant::typeToString
static std::string typeToString(VariantTypeId typeId)
Return the name of the registered type typeId.
Definition: Variant.cpp:732
armarx::SingleTypeVariantList::clear
void clear(const Ice::Current &c=Ice::emptyCurrent) override
Definition: SingleTypeVariantList.cpp:114
armarx::VariantTypeId
Ice::Int VariantTypeId
Definition: Variant.h:44
armarx::SingleTypeVariantList::operator=
SingleTypeVariantList & operator=(const SingleTypeVariantList &source)
Definition: SingleTypeVariantList.cpp:60
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:681
armarx::SingleTypeVariantList::getStaticType
static VariantTypeId getStaticType(const Ice::Current &c=Ice::emptyCurrent)
Definition: SingleTypeVariantList.cpp:119
armarx::SingleTypeVariantList::deserialize
void deserialize(const ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: SingleTypeVariantList.cpp:181
armarx::SingleTypeVariantList::addElement
void addElement(const VariantContainerBasePtr &variantContainer, const Ice::Current &c=Ice::emptyCurrent) override
Definition: SingleTypeVariantList.cpp:84
armarx::SingleTypeVariantList::getElementBase
VariantContainerBasePtr getElementBase(int index, const Ice::Current &c=Ice::emptyCurrent) const override
getElementBase is the slice-interface implementation for getting an Element and only returns a basepo...
Definition: SingleTypeVariantList.cpp:143
armarx::ContainerTypeI
Definition: VariantContainer.h:175
armarx::SingleTypeVariantList::serialize
void serialize(const ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: SingleTypeVariantList.cpp:166
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::aron::type::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Definition: Object.h:36
armarx::SingleTypeVariantList::ice_clone
Ice::ObjectPtr ice_clone() const override
Definition: SingleTypeVariantList.cpp:79
armarx::SingleTypeVariantList::SingleTypeVariantList
SingleTypeVariantList()
Definition: SingleTypeVariantList.cpp:36
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::SingleTypeVariantList::getSize
int getSize(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: SingleTypeVariantList.cpp:124
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::SingleTypeVariantList::addVariant
void addVariant(const Variant &variant)
Definition: SingleTypeVariantList.cpp:100
armarx::VariantContainer::getContainerType
ContainerTypePtr getContainerType(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:348
armarx::SingleTypeVariantList::getType
Ice::Int getType(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: SingleTypeVariantList.cpp:227
armarx::SingleTypeVariantList::getTypePrefix
static std::string getTypePrefix()
Definition: SingleTypeVariantList.cpp:201