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