StringValueMap.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 
31 // Ice Includes
32 #include <Ice/ObjectAdapter.h>
33 #include <Ice/ValueFactory.h>
34 #include <IceUtil/UUID.h>
35 
36 namespace armarx
37 {
38  StringValueMap::StringValueMap(bool forceSingleTypeMap) : forceSingleTypeMap(forceSingleTypeMap)
39  {
40  typeContainer = VariantType::Map(VariantType::Invalid).clone();
41  }
42 
44  {
45  this->typeContainer = VariantType::Map(subType).clone();
46  // type = Variant::addTypeName(getTypePrefix() + Variant::typeToString(subType));
47  }
48 
49  armarx::StringValueMap::StringValueMap(const ContainerType& subType)
50  {
51  this->typeContainer = VariantType::Map(subType).clone();
52  }
53 
55  IceUtil::Shared(source), VariantContainerBase(source), StringValueMapBase(source)
56  {
57  *this = source;
58  }
59 
62  {
63 
64  this->typeContainer = ContainerTypePtr::dynamicCast(source.typeContainer->clone());
65  elements.clear();
66  auto it = source.elements.begin();
67 
68  for (; it != source.elements.end(); it++)
69  {
70  elements[it->first] = it->second->cloneContainer();
71  }
72 
73  return *this;
74  }
75 
76  VariantContainerBasePtr
77  StringValueMap::cloneContainer(const Ice::Current& c) const
78  {
79  VariantContainerBasePtr result = new StringValueMap(*this);
80 
81  return result;
82  }
83 
86  {
87  return this->clone();
88  }
89 
90  void
91  StringValueMap::addElement(const std::string& key,
92  const VariantContainerBasePtr& variantContainer,
93  const Ice::Current& c)
94  {
95  if (elements.find(key) != elements.end())
96  {
97  throw KeyAlreadyExistsException();
98  }
99 
100  setElement(key, variantContainer->cloneContainer());
101  }
102 
103  void
104  StringValueMap::addVariant(const std::string& key, const Variant& variant)
105  {
106  setElement(key, new SingleVariant(variant));
107  }
108 
109  void
110  StringValueMap::setElement(const std::string& key,
111  const VariantContainerBasePtr& variantContainer,
112  const Ice::Current& c)
113  {
114  if (forceSingleTypeMap &&
115  !VariantContainerType::compare(variantContainer->getContainerType(),
116  getContainerType()->subType) &&
118  {
120  getContainerType()->subType->typeId, variantContainer->getContainerType()->typeId);
121  }
122 
124  {
125  getContainerType()->subType = variantContainer->getContainerType()->clone();
126  }
127 
128  elements[key] = (variantContainer->cloneContainer());
129  }
130 
131  void
132  StringValueMap::clear(const Ice::Current& c)
133  {
134  elements.clear();
135  }
136 
138  StringValueMap::getStaticType(const Ice::Current& c)
139  {
141  }
142 
143  int
144  StringValueMap::getSize(const Ice::Current& c) const
145  {
146  return int(elements.size());
147  }
148 
149  bool
150  StringValueMap::validateElements(const Ice::Current& c)
151  {
152  auto it = elements.begin();
153  bool result = true;
154 
155  for (; it != elements.end(); it++)
156  {
157  result = result && it->second->validateElements();
158  }
159 
160  return result;
161  }
162 
163  VariantContainerBasePtr
164  StringValueMap::getElementBase(const std::string& key, const Ice::Current& c) const
165  {
166  auto it = elements.find(key);
167 
168  if (it == elements.end())
169  {
170  throw IndexOutOfBoundsException();
171  }
172 
173  return it->second;
174  }
175 
176  VariantPtr
177  StringValueMap::getVariant(const std::string& key) const
178  {
179  VariantPtr ptr = getElement<SingleVariant>(key)->get();
180 
181  if (!ptr)
182  {
183  throw InvalidTypeException();
184  }
185 
186  return ptr;
187  }
188 
189  std::string
191  {
192  return "::armarx::StringValueMapBase";
193  }
194 
195  std::string
196  StringValueMap::toString(const Ice::Current&) const
197  {
198  std::stringstream ss;
199 
200  for (const auto& element : elements)
201  {
202  ss << element.first << ": " << element.second->toString() << "\n";
203  }
204 
205  return ss.str();
206  }
207 
208  void
209  StringValueMap::serialize(const ObjectSerializerBasePtr& serializer, const Ice::Current&) const
210  {
211  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
212  AbstractObjectSerializerPtr newObj = obj->createElement();
213 
214  StringVariantContainerBaseMap::const_iterator it = elements.begin();
215 
216  for (; it != elements.end(); it++)
217  {
218  newObj->setIceObject(it->first, it->second);
219  }
220 
221  // obj->setString("type", ice_id());
222  obj->setElement("map", newObj);
223  }
224 
225  void
226  StringValueMap::deserialize(const ObjectSerializerBasePtr& serializer, const Ice::Current&)
227  {
228  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
229  AbstractObjectSerializerPtr map = obj->getElement("map");
230  Ice::StringSeq keys = map->getElementNames();
231 
232  for (Ice::StringSeq::iterator it = keys.begin(); it != keys.end(); it++)
233  {
234  VariantContainerBasePtr c =
235  VariantContainerBasePtr::dynamicCast(map->getIceObject(*it));
236 
237  if (c)
238  {
239  addElement(*it, c);
240  }
241  else
242  {
243  throw LocalException("Could not cast to VariantContainerBasePtr");
244  }
245  }
246  }
247 
248  Ice::Int
249  armarx::StringValueMap::getType(const Ice::Current&) const
250  {
252  }
253 } // namespace armarx
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:223
armarx::StringValueMap::setElement
void setElement(const std::string &key, const VariantContainerBasePtr &variantContainer, const Ice::Current &c=Ice::emptyCurrent) override
Definition: StringValueMap.cpp:110
armarx::StringValueMap::getVariant
VariantPtr getVariant(const std::string &key) const
getVariant returns a pointer to a Variant object associated with key
Definition: StringValueMap.cpp:177
armarx::VariantType::Map
const VariantContainerType Map
Definition: StringValueMap.h:263
armarx::StringValueMap::getStaticType
static VariantTypeId getStaticType(const Ice::Current &c=Ice::emptyCurrent)
Definition: StringValueMap.cpp:138
AbstractObjectSerializer.h
armarx::StringValueMap::cloneContainer
VariantContainerBasePtr cloneContainer(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: StringValueMap.cpp:77
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::exceptions::user::InvalidTypeException
Definition: InvalidTypeException.h:35
IceUtil
Definition: Instance.h:21
armarx::StringValueMap::getSize
int getSize(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: StringValueMap.cpp:144
armarx::StringValueMap::operator=
StringValueMap & operator=(const StringValueMap &source)
Definition: StringValueMap.cpp:61
IceInternal::Handle< Variant >
InvalidTypeException.h
StringValueMap.h
armarx::StringValueMap::getType
Ice::Int getType(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: StringValueMap.cpp:249
armarx::StringValueMap::forceSingleTypeMap
bool forceSingleTypeMap
Definition: StringValueMap.h:256
armarx::SingleVariant
The SingleVariant class is required to store single Variant instances in VariantContainer subclasses.
Definition: VariantContainer.h:107
armarx::VariantContainerType::compare
static bool compare(const ContainerTypePtr &type1, const ContainerTypePtr &secondType)
Definition: VariantContainer.cpp:219
armarx::VariantType::Invalid
const VariantTypeId Invalid
Definition: Variant.h:915
armarx::StringValueMap::toString
std::string toString(const Ice::Current &=Ice::emptyCurrent) const override
Definition: StringValueMap.cpp:196
armarx::StringValueMap::addElement
void addElement(const std::string &key, const VariantContainerBasePtr &variantContainer, const Ice::Current &c=Ice::emptyCurrent) override
Definition: StringValueMap.cpp:91
armarx::Variant::typeToString
static std::string typeToString(VariantTypeId typeId)
Return the name of the registered type typeId.
Definition: Variant.cpp:848
armarx::StringValueMap::addVariant
void addVariant(const std::string &key, const Variant &variant)
Definition: StringValueMap.cpp:104
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::StringValueMap::StringValueMap
StringValueMap(bool forceSingleTypeMap=true)
Definition: StringValueMap.cpp:38
armarx::StringValueMap::validateElements
bool validateElements(const Ice::Current &c=Ice::emptyCurrent) override
Definition: StringValueMap.cpp:150
armarx::StringValueMap::deserialize
void deserialize(const ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: StringValueMap.cpp:226
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::aron::type::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Definition: Object.h:36
armarx::StringValueMap::clear
void clear(const Ice::Current &c=Ice::emptyCurrent) override
clear calls ::clear() on the internal StringValueMap::elements container
Definition: StringValueMap.cpp:132
armarx::StringValueMap::getElementBase
VariantContainerBasePtr getElementBase(const std::string &key, const Ice::Current &c=Ice::emptyCurrent) const override
getElementBase is the slice-interface implementation for getting an Element and only returns a basepo...
Definition: StringValueMap.cpp:164
armarx::StringValueMap
The StringValueMap class is a subclass of VariantContainer and is comparable to a std::map<std::strin...
Definition: StringValueMap.h:47
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::StringValueMap::serialize
void serialize(const ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: StringValueMap.cpp:209
armarx::StringValueMap::getTypePrefix
static std::string getTypePrefix()
Definition: StringValueMap.cpp:190
armarx::StringValueMap::ice_clone
Ice::ObjectPtr ice_clone() const override
Definition: StringValueMap.cpp:85