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