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 
27 #include <type_traits>
28 
30 #include <ArmarXCore/interface/observers/DataFieldIdentifierBase.h>
31 #include <ArmarXCore/interface/observers/ObserverInterface.h>
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  */
51  class ARMARXCORE_IMPORT_EXPORT DatafieldRef : virtual public DatafieldRefBase
52  {
53  template <class BaseClass, class VariantClass>
54  friend class GenericFactory;
55 
56  protected:
57  DatafieldRef();
58 
59  public:
60  DatafieldRef(Observer* observer,
61  const std::string& channelName,
62  const std::string& datafieldName,
63  bool performValidation = true);
64 
65  DatafieldRef(ObserverInterfacePrx observerPrx,
66  const std::string& channelName,
67  const std::string& datafieldName,
68  bool performValidation = true);
69  DatafieldRef(ChannelRefPtr channelRef,
70  const std::string& datafieldName,
71  bool performValidation = true);
72 
73  ~DatafieldRef() override
74  {
75  }
76 
77  // inherited from VariantDataClass
78  Ice::ObjectPtr ice_clone() const override;
79 
80  VariantDataClassPtr clone(const Ice::Current& c = Ice::emptyCurrent) const override;
81  std::string output(const Ice::Current& c = Ice::emptyCurrent) const override;
82  VariantTypeId getType(const Ice::Current& c = Ice::emptyCurrent) const override;
83  bool validate(const Ice::Current& c = Ice::emptyCurrent) override;
84 
85 
86  // datafield access
87  DataFieldIdentifierPtr getDataFieldIdentifier() const;
88 
89  /**
90  * @brief Retrieves the value of the datafield from the \ref Observer.
91  * @return Variant with the type and value of the datafield.
92  */
93  TimedVariantPtr getDataField() const;
94 
95  // helper methods for variant content access
96  template <typename T>
97  typename std::enable_if_t<std::is_base_of_v<VariantDataClass, T>, IceInternal::Handle<T>>
98  get() const
99  {
100  VariantPtr var = getDataField();
101 
102  if (!var)
103  {
104  throw armarx::exceptions::local::InvalidDataFieldException(channelRef->channelName,
105  datafieldName);
106  }
107 
108  return var->get<T>();
109  }
110 
111  template <typename T>
112  typename std::enable_if_t<!std::is_base_of_v<VariantDataClass, T>, T>
113  get() const
114  {
115  VariantPtr var = getDataField();
116 
117  if (!var)
118  {
119  throw armarx::exceptions::local::InvalidDataFieldException(channelRef->channelName,
120  datafieldName);
121  }
122 
123  return var->get<T>();
124  }
125 
126  bool
127  getBool(const Ice::Current& c = Ice::emptyCurrent) const override
128  {
129  return get<bool>();
130  }
131 
132  int
133  getInt(const Ice::Current& c = Ice::emptyCurrent) const override
134  {
135  return get<int>();
136  }
137 
138  long
139  getLong(const Ice::Current& c = Ice::emptyCurrent) const override
140  {
141  return get<long>();
142  }
143 
144  float
145  getFloat(const Ice::Current& c = Ice::emptyCurrent) const override
146  {
147  return get<float>();
148  }
149 
150  double
151  getDouble(const Ice::Current& c = Ice::emptyCurrent) const override
152  {
153  return get<double>();
154  }
155 
156  std::string
157  getString(const Ice::Current& c = Ice::emptyCurrent) const override
158  {
159  return get<std::string>();
160  }
161 
162  ChannelRefPtr getChannelRef() const;
163 
164  /**
165  * stream operator for DataFieldIdentifier
166  */
167  friend std::ostream&
168  operator<<(std::ostream& stream, const DatafieldRef& rhs)
169  {
170  stream << rhs.output();
171  return stream;
172  }
173 
174  /**
175  * stream operator for Ice shared pointer of DataFieldIdentifier
176  */
177  friend std::ostream&
178  operator<<(std::ostream& stream, const DatafieldRefPtr& rhs)
179  {
180  stream << rhs->output();
181  return stream;
182  }
183 
184  public: // serialization
185  void serialize(const ObjectSerializerBasePtr& serializer,
186  const ::Ice::Current& = Ice::emptyCurrent) const override;
187  void deserialize(const ObjectSerializerBasePtr& serializer,
188  const ::Ice::Current& = Ice::emptyCurrent) override;
189 
190  private:
191  mutable DataFieldIdentifierPtr id;
192  };
193 } // namespace armarx
194 
195 namespace armarx::VariantType
196 {
197  const VariantTypeId DatafieldRef = Variant::addTypeName("::armarx::DatafieldRefBase");
198 }
199 
200 
201 extern template class ::IceInternal::Handle<::armarx::DatafieldRef>;
armarx::DatafieldRef::output
std::string output(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: DatafieldRef.cpp:146
armarx::Observer
Baseclass for all ArmarX Observers.
Definition: Observer.h:84
armarx::DatafieldRef::getString
std::string getString(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: DatafieldRef.h:157
armarx::DatafieldRef::getLong
long getLong(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: DatafieldRef.h:139
armarx::DatafieldRef::getDouble
double getDouble(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: DatafieldRef.h:151
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
DataFieldIdentifier.h
IceInternal::Handle< DatafieldRef >
armarx::DatafieldRefPtr
IceInternal::Handle< DatafieldRef > DatafieldRefPtr
Definition: Observer.h:43
armarx::exceptions::local::InvalidDataFieldException
Definition: InvalidDataFieldException.h:33
armarx::DatafieldRef::getFloat
float getFloat(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: DatafieldRef.h:145
armarx::DatafieldRef::getInt
int getInt(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: DatafieldRef.h:133
armarx::VariantType
Definition: ChannelRef.h:167
armarx::VariantTypeId
Ice::Int VariantTypeId
Definition: Variant.h:43
armarx::DatafieldRef::get
std::enable_if_t<!std::is_base_of_v< VariantDataClass, T >, T > get() const
Definition: DatafieldRef.h:113
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:197
TimedVariant.h
armarx::navigation::client::validate
void validate(const std::vector< WaypointTarget > &path)
Definition: ice_conversions.h:70
armarx::operator<<
std::ostream & operator<<(std::ostream &os, const PythonApplicationManager::Paths &paths)
Definition: PythonApplicationManager.cpp:285
ImportExport.h
armarx::GenericFactory
Definition: FactoryCollectionBase.h:51
armarx::DatafieldRef::~DatafieldRef
~DatafieldRef() override
Definition: DatafieldRef.h:73
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:38
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
ChannelRef.h
armarx::DatafieldRef::getBool
bool getBool(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: DatafieldRef.h:127
armarx::DatafieldRef::get
std::enable_if_t< std::is_base_of_v< VariantDataClass, T >, IceInternal::Handle< T > > get() const
Definition: DatafieldRef.h:98