VariantContainer.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 2011
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 
25 #pragma once
26 
27 
29 #include <ArmarXCore/interface/observers/VariantContainers.h>
30 #include <ArmarXCore/interface/observers/VariantBase.h>
32 
33 #include <IceUtil/Handle.h>
34 
35 #include <string>
36 #include <map>
37 
38 
39 namespace armarx::VariantType
40 {
41  const VariantTypeId VariantContainer = Variant::addTypeName("::armarx::VariantContainerBase");
42 }
43 
44 namespace armarx
45 {
46  class VariantContainer;
48 
49 
50  class SingleVariant;
52 
53  /**
54  * \class VariantContainer
55  * \ingroup VariantsGrp
56  * \brief VariantContainer is the base class of all other Variant container classes.
57  *
58  * Each VariantContainer can contain values of the types
59  *
60  * \li VariantContainer or subclasses thereof
61  * \li SingleVariant
62  *
63  * Since VariantContainer inherits from VariantDataClass it is possible
64  * to put a VariantContainer into a Variant to send it via Ice.
65  * \see StringValueMap
66  * \see SingleTypeVariantList
67  *
68  \code
69  // VariantContainer can be replaced with any subclass of it
70  armarx::VariantContainerPtr variantContainer;
71  // create variant from VariantContainer
72  armarx::VariantPtr variant = new armarx::Variant(variantContainer);
73  // get VariantContainer from Variant
74  armarx::VariantContainerPtr variantContainer2 = variant->get<armarx::VariantContainer>();
75  if (variantContainer2)
76  {
77  // work with the container
78  }
79  else
80  {
81  // value of the variant was not a VariantContainer
82  }
83  \endcode
84  */
86  virtual public VariantContainerBase
87  {
88  public:
89  ContainerTypePtr getContainerType(const Ice::Current& c = Ice::emptyCurrent) const override;
90  void setContainerType(const ContainerTypePtr& containerType, const Ice::Current& c = Ice::emptyCurrent) override;
91 
92  // VariantDataClass interface
93  public:
94  VariantDataClassPtr clone(const Ice::Current& c = Ice::emptyCurrent) const override;
95  std::string output(const Ice::Current& c = Ice::emptyCurrent) const override;
96  Ice::Int getType(const Ice::Current& c = Ice::emptyCurrent) const override;
97  bool validate(const Ice::Current& c = Ice::emptyCurrent) override;
98  };
99 
100 
101  /**
102  * \class SingleVariant
103  * \ingroup VariantsGrp
104  * \brief The SingleVariant class is required to store single Variant instances in VariantContainer subclasses.
105  *
106  * Without this class the compiler would not be able to compile code which is storing single Variants instead of
107  * VariantContainers as values inside a VariantContainer instance .
108  */
110  virtual public VariantContainer,
111  virtual public SingleVariantBase
112  {
113  public:
114  SingleVariant();
115  SingleVariant(const Variant& variant);
116  SingleVariant(Variant& variant);
117  SingleVariant(Variant&& variant);
119  SingleVariant& operator=(const SingleVariant& source);
120  VariantContainerBasePtr cloneContainer(const::Ice::Current& = Ice::emptyCurrent) const override;
121  Ice::ObjectPtr ice_clone() const override;
122  VariantBasePtr getElementBase(const Ice::Current& = Ice::emptyCurrent) const override;
123  VariantPtr get() const;
124  void setVariant(const VariantPtr& variant);
125 
126  int getSize(const::Ice::Current& = Ice::emptyCurrent) const override;
127  ContainerTypePtr getContainerType(const Ice::Current& c = Ice::emptyCurrent) const override;
128  void setContainerType(const ContainerTypePtr& containerType, const Ice::Current& c = Ice::emptyCurrent) override;
129  static VariantTypeId getStaticType(const Ice::Current& c = Ice::emptyCurrent);
130  // int getSubType(const::Ice::Current & = Ice::emptyCurrent) const { return subType;}
131  void clear(const::Ice::Current& = Ice::emptyCurrent) override { }
132  bool validateElements(const::Ice::Current& = Ice::emptyCurrent) override;
133 
134  public: // serialization
135  void serialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current& = Ice::emptyCurrent) const override;
136  void deserialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current& = Ice::emptyCurrent) override;
137 
138  template<class T>
139  static T fromString(const std::string& s)
140  {
141  std::istringstream stream(s);
142  T t;
143  stream >> t;
144  return t;
145  }
146 
147  static std::string getTypePrefix();
148 
149  // VariantContainerBase interface
150  public:
151  std::string toString(const Ice::Current& c) const override;
152  };
153 
154  class ContainerDummy : virtual public VariantContainer
155  {
156  public:
157  ContainerDummy();
159  VariantContainerBasePtr cloneContainer(const Ice::Current& = Ice::emptyCurrent) const override;
160  Ice::ObjectPtr ice_clone() const override;
161  ContainerTypePtr getContainerType(const Ice::Current& = Ice::emptyCurrent) const override;
162  void setContainerType(const ContainerTypePtr& containerType, const Ice::Current& = Ice::emptyCurrent) override;
163  void clear(const Ice::Current& = Ice::emptyCurrent) override {}
164  int getSize(const Ice::Current& = Ice::emptyCurrent) const override;
165  bool validateElements(const Ice::Current& = Ice::emptyCurrent) override;
166  public: // serialization
167  void serialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current& = Ice::emptyCurrent) const override {}
168  void deserialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current& = Ice::emptyCurrent) override {}
169 
170  // VariantContainerBase interface
171  public:
172  std::string toString(const Ice::Current& c) const override;
173  };
174 
175  class ContainerTypeI : virtual public ContainerType
176  {
177  public:
179  ContainerTypeI(VariantTypeId variantType);
180  ContainerTypePtr clone(const Ice::Current& = Ice::emptyCurrent) const override;
181  };
183 
184 
186  {
187  public:
188  VariantContainerType(std::string containerType);
189  const ContainerTypeI operator()(VariantTypeId typeId) const;
190  const ContainerTypeI operator()(const ContainerType& subType) const;
191  static bool compare(const ContainerTypePtr& type1, const ContainerTypePtr& secondType);
192  static std::string allTypesToString(const ContainerTypePtr& type);
193  static ContainerTypePtr FromString(const std::string& typeStr);
194  static std::string GetInnerType(const std::string& typeStr);
195  static std::string GetInnerType(ContainerTypePtr type);
196  private:
197  std::string containerType;
198  ContainerTypePtr thisType;
199  };
200 }
201 
202 namespace armarx::VariantType
203 {
205 }
206 
207 extern template class ::IceInternal::Handle< ::armarx::SingleVariant>;
208 extern template class ::IceInternal::Handle< ::armarx::ContainerTypeI>;
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::ContainerDummy::toString
std::string toString(const Ice::Current &c) const override
Definition: VariantContainer.cpp:343
armarx::VariantContainerType::GetInnerType
static std::string GetInnerType(const std::string &typeStr)
Definition: VariantContainer.cpp:281
armarx::SingleVariant::getTypePrefix
static std::string getTypePrefix()
Definition: VariantContainer.cpp:144
armarx::ContainerDummy::getContainerType
ContainerTypePtr getContainerType(const Ice::Current &=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:322
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::ContainerDummy::getSize
int getSize(const Ice::Current &=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:332
armarx::SingleVariant::clear
void clear(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: VariantContainer.h:131
armarx::ContainerDummy::serialize
void serialize(const ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: VariantContainer.h:167
armarx::ContainerDummy::ice_clone
Ice::ObjectPtr ice_clone() const override
Definition: VariantContainer.cpp:317
armarx::ContainerTypeI::ContainerTypeI
ContainerTypeI()
Definition: VariantContainer.h:178
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::ContainerDummy::setContainerType
void setContainerType(const ContainerTypePtr &containerType, const Ice::Current &=Ice::emptyCurrent) override
Definition: VariantContainer.cpp:327
armarx::VariantContainerType::VariantContainerType
VariantContainerType(std::string containerType)
Definition: VariantContainer.cpp:179
armarx::ContainerTypeI::clone
ContainerTypePtr clone(const Ice::Current &=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:164
armarx::SingleVariant
The SingleVariant class is required to store single Variant instances in VariantContainer subclasses.
Definition: VariantContainer.h:109
armarx::VariantContainerType::compare
static bool compare(const ContainerTypePtr &type1, const ContainerTypePtr &secondType)
Definition: VariantContainer.cpp:203
armarx::VariantContainerType::operator()
const ContainerTypeI operator()(VariantTypeId typeId) const
Definition: VariantContainer.cpp:186
armarx::ContainerDummy::deserialize
void deserialize(const ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: VariantContainer.h:168
armarx::VariantType
Definition: ChannelRef.h:160
armarx::SingleVariant::fromString
static T fromString(const std::string &s)
Definition: VariantContainer.h:139
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::VariantContainerType
Definition: VariantContainer.h:185
armarx::navigation::client::validate
void validate(const std::vector< WaypointTarget > &path)
Definition: ice_conversions.h:70
armarx::ContainerDummy::ContainerDummy
ContainerDummy()
Definition: VariantContainer.cpp:298
armarx::VariantType::SingleVariantContainer
const VariantContainerType SingleVariantContainer
Definition: VariantContainer.h:204
armarx::ContainerTypeI
Definition: VariantContainer.h:175
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::ContainerDummy::cloneContainer
VariantContainerBasePtr cloneContainer(const Ice::Current &=Ice::emptyCurrent) const override
Definition: VariantContainer.cpp:312
armarx::aron::type::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Definition: Object.h:36
ARMARXCORE_IMPORT_EXPORT
#define ARMARXCORE_IMPORT_EXPORT
Definition: ImportExport.h:38
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
Variant.h
armarx::VariantContainerType::FromString
static ContainerTypePtr FromString(const std::string &typeStr)
Definition: VariantContainer.cpp:262
armarx::ContainerDummy
Definition: VariantContainer.h:154
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::ContainerDummy::validateElements
bool validateElements(const Ice::Current &=Ice::emptyCurrent) override
Definition: VariantContainer.cpp:337
armarx::ContainerDummy::clear
void clear(const Ice::Current &=Ice::emptyCurrent) override
Definition: VariantContainer.h:163
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::VariantContainerType::allTypesToString
static std::string allTypesToString(const ContainerTypePtr &type)
Definition: VariantContainer.cpp:232