SingleTypeVariantList.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 <IceUtil/Handle.h>
28 
30 #include <ArmarXCore/interface/observers/VariantContainers.h>
34 
35 namespace armarx
36 {
40 
41  /**
42  * \class SingleTypeVariantList
43  * \ingroup VariantsGrp
44  * \brief The SingleTypeVariantList class is a subclass of VariantContainer and is comparable to a std::vector<T> containing values of type T.
45  */
47  virtual public VariantContainer,
48  virtual public SingleTypeVariantListBase
49  {
50  public:
53  explicit SingleTypeVariantList(const ContainerType& subType);
54  explicit SingleTypeVariantList(VariantTypeId subType);
56  VariantContainerBasePtr
57  cloneContainer(const Ice::Current& c = Ice::emptyCurrent) const override;
58  Ice::ObjectPtr ice_clone() const override;
59 
60  // element manipulation
61  void addElement(const VariantContainerBasePtr& variantContainer,
62  const Ice::Current& c = Ice::emptyCurrent) override;
63  void addVariant(const Variant& variant);
64  void clear(const Ice::Current& c = Ice::emptyCurrent) override;
65 
66  // getters
67  Ice::Int getType(const Ice::Current& c = Ice::emptyCurrent) const override;
68  static VariantTypeId getStaticType(const Ice::Current& c = Ice::emptyCurrent);
69  int getSize(const Ice::Current& c = Ice::emptyCurrent) const override;
70  bool validateElements(const Ice::Current& c = Ice::emptyCurrent) override;
71 
72  /**
73  * @brief getElementBase is the slice-interface implementation for
74  * getting an Element and only returns a basepointer, so a manual upcast
75  * is usually necessary.
76  *
77  * This function exists only for completeness and compatibility. Usually
78  * you should use the getElement()-function.
79  * @param index is the index of the Element in the list
80  * @param c Not needed, leave blank.
81  * @throw IndexOutOfBoundsException
82  * @return a base variant pointer
83  */
84  VariantContainerBasePtr
85  getElementBase(int index, const Ice::Current& c = Ice::emptyCurrent) const override;
86 
87  template <typename ContainerType>
89  getElement(int index) const
90  {
93 
94  if (!ptr)
95  {
96  throw InvalidTypeException();
97  }
98 
99  if (!ptr)
100  {
101  throw exceptions::user::InvalidTypeException(ContainerType::ice_staticId(), "");
102  }
103 
104  return ptr;
105  }
106 
107  VariantPtr getVariant(int index) const;
108 
109  template <typename T1, typename T2>
110  static std::map<T1, T2>
112  {
113  if (l1->getSize() != l2->getSize())
114  {
115  throw LocalException("List lengths do not match:")
116  << l1->getSize() << " != " << l2->getSize();
117  }
118 
119  std::map<T1, T2> result;
120  size_t size = l1->getSize();
121 
122  for (size_t i = 0; i < size; i++)
123  {
124  result[l1->getVariant(i)->get<T1>()] = l2->getVariant(i)->get<T2>();
125  }
126 
127  return result;
128  }
129 
130  template <typename T1>
132  FromStdVector(const std::vector<T1>& vec)
133  {
135 
136  for (size_t i = 0; i < vec.size(); i++)
137  {
138  result->addVariant(vec.at(i));
139  }
140 
141  return result;
142  }
143 
144  template <typename T1>
146  FromContainerStdVector(const std::vector<T1>& vec)
147  {
149 
150  for (size_t i = 0; i < vec.size(); i++)
151  {
152  result->addElement(vec.at(i));
153  }
154 
155  return result;
156  }
157 
158  public: // serialization
159  void serialize(const ObjectSerializerBasePtr& serializer,
160  const ::Ice::Current& = Ice::emptyCurrent) const override;
161  void deserialize(const ObjectSerializerBasePtr& serializer,
162  const ::Ice::Current& = Ice::emptyCurrent) override;
163 
164 
165  static std::string getTypePrefix();
166 
167  template <typename Type>
168  std::vector<Type>
169  toStdVector() const
170  {
171  std::vector<Type> result(getSize());
172 
173  for (int i = 0; i < getSize(); i++)
174  {
175  result.at(i) = getVariant(i)->get<Type>();
176  }
177 
178  return result;
179  }
180 
181  template <typename Type>
182  std::vector<Type>
184  {
185  std::vector<Type> result(getSize());
186 
187  for (int i = 0; i < getSize(); i++)
188  {
189  Type p = getElement<typename Type::element_type>(i);
190  result.at(i) = p;
191  }
192 
193  return result;
194  }
195 
196  // VariantContainerBase interface
197  public:
198  std::string toString(const Ice::Current& = Ice::emptyCurrent) const override;
199  };
200 } // namespace armarx
201 
202 namespace armarx::VariantType
203 {
205 
208 } // namespace armarx::VariantType
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:223
armarx::VariantType::List
const VariantContainerType List
Definition: SingleTypeVariantList.h:204
index
uint8_t index
Definition: EtherCATFrame.h:59
VariantContainer.h
armarx::SingleTypeVariantList::MakeMap
static std::map< T1, T2 > MakeMap(SingleTypeVariantListPtr l1, SingleTypeVariantListPtr l2)
Definition: SingleTypeVariantList.h:111
armarx::SingleTypeVariantList::getElement
IceInternal::Handle< ContainerType > getElement(int index) const
Definition: SingleTypeVariantList.h:89
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::exceptions::user::InvalidTypeException
Definition: InvalidTypeException.h:35
armarx::SingleTypeVariantList::toStdVector
std::vector< Type > toStdVector() const
Definition: SingleTypeVariantList.h:169
armarx::SingleTypeVariantList::toContainerStdVector
std::vector< Type > toContainerStdVector() const
Definition: SingleTypeVariantList.h:183
IceInternal::Handle< SingleTypeVariantList >
InvalidTypeException.h
armarx::SingleTypeVariantList::FromContainerStdVector
static SingleTypeVariantListPtr FromContainerStdVector(const std::vector< T1 > &vec)
Definition: SingleTypeVariantList.h:146
armarx::SingleTypeVariantList
The SingleTypeVariantList class is a subclass of VariantContainer and is comparable to a std::vector<...
Definition: SingleTypeVariantList.h:46
armarx::VariantType
Definition: ChannelRef.h:167
armarx::aron::similarity::FloatSimilarity::Type
Type
The Type enum.
Definition: FloatSimilarity.h:10
armarx::VariantTypeId
Ice::Int VariantTypeId
Definition: Variant.h:43
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:661
armarx::SingleTypeVariantList::FromStdVector
static SingleTypeVariantListPtr FromStdVector(const std::vector< T1 > &vec)
Definition: SingleTypeVariantList.h:132
armarx::VariantContainer
VariantContainer is the base class of all other Variant container classes.
Definition: VariantContainer.h:84
armarx::VariantContainerType
Definition: VariantContainer.h:213
armarx::viz::toString
const char * toString(InteractionFeedbackType type)
Definition: Interaction.h:28
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:917
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
Variant.h
armarx::VariantType::SingleTypeVariantList
const VariantTypeId SingleTypeVariantList
Definition: SingleTypeVariantList.h:206
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::Variant::addTypeName
static VariantTypeId addTypeName(const std::string &typeName)
Register a new type for the use in a Variant.
Definition: Variant.cpp:869
armarx::SingleTypeVariantList::getTypePrefix
static std::string getTypePrefix()
Definition: SingleTypeVariantList.cpp:218