StringValueMap.h
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@kit.edu)
20* @date 2012
21* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22* GNU General Public License
23*/
24
25#pragma once
26
27#include <map>
28
29#include <IceUtil/Handle.h>
30
33#include <ArmarXCore/interface/observers/VariantContainers.h>
36
37namespace armarx
38{
39 class StringValueMap;
41
42 /**
43 * \class StringValueMap
44 * \ingroup VariantsGrp
45 * \brief The StringValueMap class is a subclass of VariantContainer and is comparable to a std::map<std::string, T> containing values of type T.
46 */
48 virtual public VariantContainer,
49 virtual public StringValueMapBase
50 {
51 public:
53 StringValueMap(const StringValueMap& source);
54 explicit StringValueMap(const ContainerType& subType);
55 explicit StringValueMap(VariantTypeId subType);
57 VariantContainerBasePtr
58 cloneContainer(const Ice::Current& c = Ice::emptyCurrent) const override;
59 Ice::ObjectPtr ice_clone() const override;
60
61
62 // element manipulation
63 void addElement(const std::string& key,
64 const VariantContainerBasePtr& variantContainer,
65 const Ice::Current& c = Ice::emptyCurrent) override;
66 void addVariant(const std::string& key, const Variant& variant);
67 void setElement(const std::string& key,
68 const VariantContainerBasePtr& variantContainer,
69 const Ice::Current& c = Ice::emptyCurrent) override;
70
71 /**
72 * @brief setElements adds all pairs from @p map to the current instance using StringValueMap::setElement().
73 \code
74 armarx::StringValueMapPtr container = ...;
75 std::map<std::string, std::string> intMap;
76 intMap["p1"] = 1;
77 intMap["p2"] = 2;
78 container->setElements<int>(intMap);
79 \endcode
80 */
81 template <typename ValueType>
82 void setElements(const std::map<std::string, ValueType>& map);
83
84 /**
85 * @brief setElements adds elements with @p keyVec as keys and @p values as associated values using StringValueMap::setElement(). If the vectors sizes do not match, only the minimal amount of pairs is added.
86 */
87 template <typename ValueType>
88 void setElements(const std::vector<std::string>& keyVec,
89 const std::vector<ValueType>& values);
90 /**
91 * @brief clear calls ::clear() on the internal StringValueMap::elements container
92 * @param c
93 */
94 void clear(const Ice::Current& c = Ice::emptyCurrent) override;
95
96 // getters
97 Ice::Int getType(const Ice::Current& c = Ice::emptyCurrent) const override;
98
99 static VariantTypeId getStaticType(const Ice::Current& c = Ice::emptyCurrent);
100 // VariantTypeId getSubType(const Ice::Current& c = Ice::emptyCurrent) const;
101 int getSize(const Ice::Current& c = Ice::emptyCurrent) const override;
102 bool validateElements(const Ice::Current& c = Ice::emptyCurrent) override;
103
104
105 /**
106 * @brief getElementBase is the slice-interface implementation for
107 * getting an Element and only returns a basepointer, so a manual upcast
108 * is usually necessary.
109 *
110 * This function exists only for completeness and compatibility. Usually
111 * you should use the getElement()-function.
112 * @param index is the index of the Element in the list
113 * @param c Not needed, leave blank.
114 * @throw IndexOutOfBoundsException
115 * @return a base variant pointer
116 */
117 VariantContainerBasePtr
118 getElementBase(const std::string& key,
119 const Ice::Current& c = Ice::emptyCurrent) const override;
120
121 /**
122 * @brief getElement is the getter-function to retrieve variants from
123 * the list.
124 *
125
126 * @param index is the index of the Element in the list
127 * @throw IndexOutOfBoundsException
128 * @return a variant pointer
129 */
130 template <typename ContainerType>
132 getElement(const std::string& key) const
133 {
136
137 if (!ptr)
138 {
139 throw InvalidTypeException();
140 }
141
142 return ptr;
143 }
144
145 /**
146 * @brief getVariant returns a pointer to a Variant object associated with \p key
147 * @param key
148 * @return
149 */
150 VariantPtr getVariant(const std::string& key) const;
151
152 // Convenience functions
153 /**
154 * @brief toStdMap creates a std::map<std::string, ValueType> from this container. ValueType must be of a type that can be stored in a Variant.
155 */
156 template <class ValueType>
157 std::map<std::string, ValueType>
159 {
160 std::map<std::string, ValueType> map;
161 StringVariantContainerBaseMap::const_iterator it = elements.begin();
162
163 for (; it != elements.end(); it++)
164 {
165 const std::string& key = it->first;
166 map[key] = getVariant(key)->get<ValueType>();
167 }
168
169 return map;
170 }
171
172 /**
173 * @brief toContainerStdMap creates a std::map<std::string, Type> from this container. Type must be a subclass of VariantContainer. Use this for nested VariantContainers.
174 */
175 template <typename Type>
176 std::map<std::string, Type>
178 {
179 std::map<std::string, Type> map;
180 StringVariantContainerBaseMap::const_iterator it = elements.begin();
181
182 for (; it != elements.end(); it++)
183 {
184 const std::string& key = it->first;
186 }
187
188 return map;
189 }
190
191 /**
192 * @brief FromStdMap creates a StringValueMap from a std::map<std::string, Type>.
193 *
194 \code
195 std::map<std::string, std::string> intMap;
196 intMap["p1"] = 1;
197 intMap["p2"] = 2;
198 armarx::StringValueMapPtr container = armarx::StringValueMap::FromStdMap<int>(intMap);
199 \endcode
200 */
201 template <typename Type>
202 static StringValueMapPtr
203 FromStdMap(const std::map<std::string, Type>& map)
204 {
205 StringValueMapPtr result = new StringValueMap();
206
207 for (typename std::map<std::string, Type>::const_iterator it = map.begin();
208 it != map.end();
209 it++)
210 {
211 result->addVariant(it->first, it->second);
212 }
213
214 return result;
215 }
216
217 /**
218 * @brief FromStdMap creates a StringValueMap from a std::map<std::string, Type>.
219 *
220 \code
221 std::map<std::string, VariantContainerPtr> containerMap;
222 cotnainerMap["c1"] = new VariantContainer();
223 containerMap["c2"] = new VariantContainer();
224 armarx::StringValueMapPtr container = armarx::StringValueMap::FromContainerStdMap<int>(intMap);
225 \endcode
226 */
227 template <typename Type>
228 static StringValueMapPtr
229 FromContainerStdMap(const std::map<std::string, Type>& map)
230 {
231 StringValueMapPtr result = new StringValueMap();
232
233 for (typename std::map<std::string, Type>::const_iterator it = map.begin();
234 it != map.end();
235 it++)
236 {
237 result->addElement(it->first, it->second);
238 }
239
240 return result;
241 }
242
243 static std::string getTypePrefix();
244
245 public: // serialization
246 void serialize(const ObjectSerializerBasePtr& serializer,
247 const ::Ice::Current& = Ice::emptyCurrent) const override;
248 void deserialize(const ObjectSerializerBasePtr& serializer,
249 const ::Ice::Current& = Ice::emptyCurrent) override;
250
251 // VariantContainerBase interface
252
253 std::string toString(const Ice::Current& = Ice::emptyCurrent) const override;
254
255 protected:
257 };
258
259} // namespace armarx
260
261namespace armarx::VariantType
262{
264
266} // namespace armarx::VariantType
267
268namespace armarx
269{
270
271
272 /////////////////////////////////////////////////////////////////////////
273 // Implementations
274 /////////////////////////////////////////////////////////////////////////
275 template <typename ValueType>
276 void
277 StringValueMap::setElements(const std::map<std::string, ValueType>& map)
278 {
279 typename std::map<std::string, ValueType>::const_iterator it = map.begin();
280
281 for (; it != map.end(); it++)
282 {
283 setElement(it->first, it->second);
284 }
285 }
286
287 template <typename ValueType>
288 void
289 StringValueMap::setElements(const std::vector<std::string>& keyVec,
290 const std::vector<ValueType>& values)
291 {
292
293 int size = std::min(keyVec.size(), values.size());
294
295 if (keyVec.size() != values.size())
296 {
297 ARMARX_WARNING_S << "Size of keys vector does not match values vector size:"
298 << keyVec.size() << " vs. " << values.size();
299 }
300
301 for (int i = 0; i < size; i++)
302 {
303 setElement(keyVec[i], values[i]);
304 }
305 }
306
307} // namespace armarx
#define ARMARXCORE_IMPORT_EXPORT
constexpr T c
The StringValueMap class is a subclass of VariantContainer and is comparable to a std::map<std::strin...
std::map< std::string, Type > toContainerStdMap() const
toContainerStdMap creates a std::map<std::string, Type> from this container.
static StringValueMapPtr FromContainerStdMap(const std::map< std::string, Type > &map)
FromStdMap creates a StringValueMap from a std::map<std::string, Type>.
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
void setElements(const std::map< std::string, ValueType > &map)
setElements adds all pairs from map to the current instance using StringValueMap::setElement().
std::map< std::string, ValueType > toStdMap()
toStdMap creates a std::map<std::string, ValueType> from this container.
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)
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
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 StringValueMapPtr FromStdMap(const std::map< std::string, Type > &map)
FromStdMap creates a StringValueMap from a std::map<std::string, Type>.
static std::string getTypePrefix()
VariantContainer is the base class of all other Variant container classes.
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
#define ARMARX_WARNING_S
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:213
const VariantTypeId StringValueMap
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< StringValueMap > StringValueMapPtr
Ice::Int VariantTypeId
Definition Variant.h:43