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
35namespace 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>
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 {
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
202namespace armarx::VariantType
203{
205
208} // namespace armarx::VariantType
uint8_t index
#define ARMARXCORE_IMPORT_EXPORT
constexpr T c
The SingleTypeVariantList class is a subclass of VariantContainer and is comparable to a std::vector<...
void addVariant(const Variant &variant)
static VariantTypeId getStaticType(const Ice::Current &c=Ice::emptyCurrent)
IceInternal::Handle< ContainerType > getElement(int index) const
VariantPtr getVariant(int index) const
static SingleTypeVariantListPtr FromContainerStdVector(const std::vector< T1 > &vec)
std::vector< Type > toStdVector() const
static std::map< T1, T2 > MakeMap(SingleTypeVariantListPtr l1, SingleTypeVariantListPtr l2)
VariantContainerBasePtr cloneContainer(const Ice::Current &c=Ice::emptyCurrent) const override
VariantContainerBasePtr getElementBase(int index, 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
std::vector< Type > toContainerStdVector() const
static SingleTypeVariantListPtr FromStdVector(const std::vector< T1 > &vec)
int getSize(const Ice::Current &c=Ice::emptyCurrent) const override
Ice::Int getType(const Ice::Current &c=Ice::emptyCurrent) const override
Ice::ObjectPtr ice_clone() const override
void clear(const Ice::Current &c=Ice::emptyCurrent) override
void addElement(const VariantContainerBasePtr &variantContainer, const Ice::Current &c=Ice::emptyCurrent) override
SingleTypeVariantList & operator=(const SingleTypeVariantList &source)
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
const VariantTypeId SingleTypeVariantList
const VariantContainerType List
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< SingleTypeVariantList > SingleTypeVariantListPtr
IceInternal::Handle< Variant > VariantPtr
Definition Variant.h:41
Ice::Int VariantTypeId
Definition Variant.h:43
SingleTypeVariantList STVarList