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 
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 
78  template <typename ValueType>
79  void setElements(const std::map<std::string, ValueType>& map);
80 
84  template <typename ValueType>
85  void setElements(const std::vector<std::string>& keyVec, const std::vector<ValueType>& values);
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 
113  VariantContainerBasePtr getElementBase(const std::string& key, const Ice::Current& c = Ice::emptyCurrent) const override;
114 
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 
143  VariantPtr getVariant(const std::string& key) const;
144 
145 
146 
147 
148  // Convenience functions
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 
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 
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 
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 
259  // Implementations
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:37
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