DatafieldRef.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 ArmarX::Core
19 * @author Kai Welke (welke@kit.edu)
20 * @date 2011 Kai Welke
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 
25 #pragma once
26 
28 #include <ArmarXCore/interface/observers/ObserverInterface.h>
29 #include <ArmarXCore/interface/observers/DataFieldIdentifierBase.h>
34 
35 #include <type_traits>
36 
37 namespace armarx
38 {
39 
40 
41  class DatafieldRef;
43 
44  /**
45  * @class DatafieldRef
46  * @ingroup VariantsGrp
47  * @brief The DatafieldRef class is similar to the \ref ChannelRef, but points
48  * to a specific Datafield instead of to a complete channel.
49  * It can be used to query (@ref getDataField()) the data of the datafield from the \ref Observer.
50  */
52  virtual public DatafieldRefBase
53  {
54  template <class BaseClass, class VariantClass>
55  friend class GenericFactory;
56  protected:
57  DatafieldRef();
58  public:
59  DatafieldRef(Observer* observer, const std::string& channelName, const std::string& datafieldName, bool performValidation = true);
60 
61  DatafieldRef(ObserverInterfacePrx observerPrx, const std::string& channelName, const std::string& datafieldName, bool performValidation = true);
62  DatafieldRef(ChannelRefPtr channelRef, const std::string& datafieldName, bool performValidation = true);
63  ~DatafieldRef() override {}
64 
65  // inherited from VariantDataClass
66  Ice::ObjectPtr ice_clone() const override;
67 
68  VariantDataClassPtr clone(const Ice::Current& c = Ice::emptyCurrent) const override;
69  std::string output(const Ice::Current& c = Ice::emptyCurrent) const override;
70  VariantTypeId getType(const Ice::Current& c = Ice::emptyCurrent) const override;
71  bool validate(const Ice::Current& c = Ice::emptyCurrent) override;
72 
73 
74  // datafield access
75  DataFieldIdentifierPtr getDataFieldIdentifier() const;
76 
77  /**
78  * @brief Retrieves the value of the datafield from the \ref Observer.
79  * @return Variant with the type and value of the datafield.
80  */
81  TimedVariantPtr getDataField() const;
82 
83 
84  // helper methods for variant content access
85  template<typename T>
86  typename std::enable_if_t<std::is_base_of_v<VariantDataClass, T>, IceInternal::Handle<T> >
87  get() const
88  {
89  VariantPtr var = getDataField();
90 
91  if (!var)
92  {
93  throw armarx::exceptions::local::InvalidDataFieldException(channelRef->channelName, datafieldName);
94  }
95 
96  return var->get<T>();
97  }
98 
99  template<typename T>
100  typename std::enable_if_t < !std::is_base_of_v<VariantDataClass, T>, T >
101  get() const
102  {
103  VariantPtr var = getDataField();
104 
105  if (!var)
106  {
107  throw armarx::exceptions::local::InvalidDataFieldException(channelRef->channelName, datafieldName);
108  }
109 
110  return var->get<T>();
111  }
112 
113  bool getBool(const Ice::Current& c = Ice::emptyCurrent) const override
114  {
115  return get<bool>();
116  }
117  int getInt(const Ice::Current& c = Ice::emptyCurrent) const override
118  {
119  return get<int>();
120  }
121  long getLong(const Ice::Current& c = Ice::emptyCurrent) const override
122  {
123  return get<long>();
124  }
125  float getFloat(const Ice::Current& c = Ice::emptyCurrent) const override
126  {
127  return get<float>();
128  }
129  double getDouble(const Ice::Current& c = Ice::emptyCurrent) const override
130  {
131  return get<double>();
132  }
133  std::string getString(const Ice::Current& c = Ice::emptyCurrent) const override
134  {
135  return get<std::string>();
136  }
137 
138 
139  ChannelRefPtr getChannelRef() const;
140 
141  /**
142  * stream operator for DataFieldIdentifier
143  */
144  friend std::ostream& operator<<(std::ostream& stream, const DatafieldRef& rhs)
145  {
146  stream << rhs.output();
147  return stream;
148  }
149 
150  /**
151  * stream operator for Ice shared pointer of DataFieldIdentifier
152  */
153  friend std::ostream& operator<<(std::ostream& stream, const DatafieldRefPtr& rhs)
154  {
155  stream << rhs->output();
156  return stream;
157  }
158 
159  public: // serialization
160  void serialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current& = Ice::emptyCurrent) const override;
161  void deserialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current& = Ice::emptyCurrent) override;
162 
163  private:
164  mutable DataFieldIdentifierPtr id;
165  };
166 }
167 namespace armarx::VariantType
168 {
169  const VariantTypeId DatafieldRef = Variant::addTypeName("::armarx::DatafieldRefBase");
170 }
171 
172 
173 
174 extern template class ::IceInternal::Handle<::armarx::DatafieldRef>;
175 
176 
177 
armarx::DatafieldRef::get
std::enable_if_t< !std::is_base_of_v< VariantDataClass, T >, T > get() const
Definition: DatafieldRef.h:101
armarx::DatafieldRef::output
std::string output(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: DatafieldRef.cpp:132
armarx::Observer
Baseclass for all ArmarX Observers.
Definition: Observer.h:80
armarx::DatafieldRef::getString
std::string getString(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: DatafieldRef.h:133
armarx::DatafieldRef::getLong
long getLong(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: DatafieldRef.h:121
armarx::DatafieldRef::getDouble
double getDouble(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: DatafieldRef.h:129
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
DataFieldIdentifier.h
IceInternal::Handle< DatafieldRef >
armarx::DatafieldRefPtr
IceInternal::Handle< DatafieldRef > DatafieldRefPtr
Definition: Observer.h:44
armarx::DatafieldRef::operator<<
friend std::ostream & operator<<(std::ostream &stream, const DatafieldRefPtr &rhs)
stream operator for Ice shared pointer of DataFieldIdentifier
Definition: DatafieldRef.h:153
armarx::exceptions::local::InvalidDataFieldException
Definition: InvalidDataFieldException.h:33
armarx::DatafieldRef::getFloat
float getFloat(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: DatafieldRef.h:125
armarx::DatafieldRef::getInt
int getInt(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: DatafieldRef.h:117
armarx::VariantType
Definition: ChannelRef.h:160
armarx::VariantTypeId
Ice::Int VariantTypeId
Definition: Variant.h:44
armarx::DatafieldRef::operator<<
friend std::ostream & operator<<(std::ostream &stream, const DatafieldRef &rhs)
stream operator for DataFieldIdentifier
Definition: DatafieldRef.h:144
InvalidDataFieldException.h
armarx::DatafieldRef
The DatafieldRef class is similar to the ChannelRef, but points to a specific Datafield instead of to...
Definition: DatafieldRef.h:51
armarx::VariantType::DatafieldRef
const VariantTypeId DatafieldRef
Definition: DatafieldRef.h:169
TimedVariant.h
armarx::navigation::client::validate
void validate(const std::vector< WaypointTarget > &path)
Definition: ice_conversions.h:70
ImportExport.h
armarx::GenericFactory
Definition: FactoryCollectionBase.h:51
armarx::DatafieldRef::~DatafieldRef
~DatafieldRef() override
Definition: DatafieldRef.h:63
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
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
ChannelRef.h
armarx::DatafieldRef::getBool
bool getBool(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: DatafieldRef.h:113
armarx::DatafieldRef::get
std::enable_if_t< std::is_base_of_v< VariantDataClass, T >, IceInternal::Handle< T > > get() const
Definition: DatafieldRef.h:87