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