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
36namespace armarx
37{
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
84 Ice::ObjectPtr
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
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
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
constexpr T c
The SingleVariant class is required to store single Variant instances in VariantContainer subclasses.
The StringValueMap class is a subclass of VariantContainer and is comparable to a std::map<std::strin...
static VariantTypeId getStaticType(const Ice::Current &c=Ice::emptyCurrent)
StringValueMap(bool forceSingleTypeMap=true)
void addElement(const std::string &key, const VariantContainerBasePtr &variantContainer, const Ice::Current &c=Ice::emptyCurrent) override
std::string toString(const Ice::Current &=Ice::emptyCurrent) const override
void setElement(const std::string &key, const VariantContainerBasePtr &variantContainer, const Ice::Current &c=Ice::emptyCurrent) override
VariantContainerBasePtr cloneContainer(const Ice::Current &c=Ice::emptyCurrent) const override
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...
bool validateElements(const Ice::Current &c=Ice::emptyCurrent) override
void addVariant(const std::string &key, const Variant &variant)
void deserialize(const ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) override
int getSize(const Ice::Current &c=Ice::emptyCurrent) const override
Ice::Int getType(const Ice::Current &c=Ice::emptyCurrent) const override
IceInternal::Handle< ContainerType > getElement(const std::string &key) const
getElement is the getter-function to retrieve variants from the list.
Ice::ObjectPtr ice_clone() const override
void serialize(const ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) const override
VariantPtr getVariant(const std::string &key) const
getVariant returns a pointer to a Variant object associated with key
void clear(const Ice::Current &c=Ice::emptyCurrent) override
clear calls clear() on the internal StringValueMap::elements container
StringValueMap & operator=(const StringValueMap &source)
static std::string getTypePrefix()
static bool compare(const ContainerTypePtr &type1, const ContainerTypePtr &secondType)
VariantDataClassPtr clone(const Ice::Current &c=Ice::emptyCurrent) const override
ContainerTypePtr getContainerType(const Ice::Current &c=Ice::emptyCurrent) const override
The Variant class is described here: Variants.
Definition Variant.h:224
static VariantTypeId addTypeName(const std::string &typeName)
Register a new type for the use in a Variant.
Definition Variant.cpp:869
static std::string typeToString(VariantTypeId typeId)
Return the name of the registered type typeId.
Definition Variant.cpp:848
const VariantTypeId Invalid
Definition Variant.h:915
const VariantContainerType Map
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< Variant > VariantPtr
Definition Variant.h:41
IceInternal::Handle< AbstractObjectSerializer > AbstractObjectSerializerPtr
Ice::Int VariantTypeId
Definition Variant.h:43