ChannelRef.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/ChannelRefBase.h>
31 #include <ArmarXCore/interface/observers/ObserverInterface.h>
35 
36 namespace armarx
37 {
38  class Observer;
39  class ChannelRef;
41 
42  /**
43  * @class ChannelRef
44  * @ingroup VariantsGrp
45  * @brief The ChannelRef class is a reference to a channel on an \ref Observer.
46  * It is used to access data directly from a channel or to be passed to function
47  * as an identifier for a channel.
48  *
49  */
50  class ARMARXCORE_IMPORT_EXPORT ChannelRef : virtual public ChannelRefBase
51  {
52  template <class BaseClass, class VariantClass>
53  friend class GenericFactory;
54 
55  protected:
56  ChannelRef() : validationTimeout(4000), waitIntervallMs(10)
57  {
58  initialized = false;
59  }
60 
61 
62  public:
63  ChannelRef(Observer* observer, std::string channelName);
64  ChannelRef(ObserverInterfacePrx observerPrx, std::string channelName);
65 
66  ~ChannelRef() override
67  {
68  }
69 
70  // inherited from VariantDataClass
72  ice_clone() const override
73  {
74  return this->clone();
75  }
76 
77  VariantDataClassPtr clone(const Ice::Current& c = Ice::emptyCurrent) const override;
78  std::string output(const Ice::Current& c = Ice::emptyCurrent) const override;
79  VariantTypeId getType(const Ice::Current& c = Ice::emptyCurrent) const override;
80  bool validate(const Ice::Current& c = Ice::emptyCurrent) override;
81 
82  // datafield access
83  DataFieldIdentifierPtr getDataFieldIdentifier(const std::string& datafieldName);
84  TimedVariantPtr getDataField(const std::string& datafieldName);
85  const Ice::StringSeq& getDataFieldNames() const;
86  bool hasDatafield(const std::string& datafieldName) const;
87  bool getInitialized();
88  void refetchChannel();
89 
90  // helper methods for variant content access
91  template <typename T>
92  typename std::enable_if_t<std::is_base_of_v<VariantDataClass, T>, IceInternal::Handle<T>>
93  get(const std::string& key)
94  {
95  VariantPtr var = getDataField(key);
96 
97  if (!var)
98  {
99  throw armarx::exceptions::local::InvalidDataFieldException(getChannelName(), key);
100  }
101 
102  return var->get<T>();
103  }
104 
105  template <typename T>
106  typename std::enable_if_t<!std::is_base_of_v<VariantDataClass, T>, T>
107  get(const std::string& key)
108  {
109  VariantPtr var = getDataField(key);
110 
111  if (!var)
112  {
113  throw armarx::exceptions::local::InvalidDataFieldException(getChannelName(), key);
114  }
115 
116  return var->get<T>();
117  }
118 
119  // channel access
120  const std::string& getChannelName() const;
121 
122  // observer access
123  const std::string& getObserverName() const;
124  const ObserverInterfacePrx& getObserverProxy();
125 
126  /**
127  * stream operator for DataFieldIdentifier
128  */
129  friend std::ostream&
130  operator<<(std::ostream& stream, const ChannelRef& rhs)
131  {
132  stream << rhs.output();
133  return stream;
134  }
135 
136  /**
137  * stream operator for Ice shared pointer of DataFieldIdentifier
138  */
139  friend std::ostream&
140  operator<<(std::ostream& stream, const ChannelRefPtr& rhs)
141  {
142  stream << rhs->output();
143  return stream;
144  }
145 
146  public: // serialization
147  void serialize(const ObjectSerializerBasePtr& serializer,
148  const ::Ice::Current& = Ice::emptyCurrent) const override;
149  void deserialize(const ObjectSerializerBasePtr& serializer,
150  const ::Ice::Current& = Ice::emptyCurrent) override;
151 
152  private:
153  bool assureProxy();
154  void initializeDataFields(Observer* obs = nullptr);
155 
156  Ice::CommunicatorPtr communicator;
157  const IceUtil::Int64 validationTimeout;
158  const int waitIntervallMs;
159 
160  // Object interface
161  public:
162  void ice_postUnmarshal() override;
163  };
164 
165 } // namespace armarx
166 
168 {
169  const VariantTypeId ChannelRef = Variant::addTypeName("::armarx::ChannelRefBase");
170 }
171 
172 extern template class ::IceInternal::Handle<::armarx::ChannelRef>;
armarx::ChannelRef::get
std::enable_if_t< std::is_base_of_v< VariantDataClass, T >, IceInternal::Handle< T > > get(const std::string &key)
Definition: ChannelRef.h:93
armarx::Observer
Baseclass for all ArmarX Observers.
Definition: Observer.h:84
armarx::ChannelRef
The ChannelRef class is a reference to a channel on an Observer. It is used to access data directly f...
Definition: ChannelRef.h:50
armarx::ChannelRef::ChannelRef
ChannelRef()
Definition: ChannelRef.h:56
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
DataFieldIdentifier.h
armarx::ChannelRef::ice_clone
Ice::ObjectPtr ice_clone() const override
Definition: ChannelRef.h:72
armarx::ChannelRef::get
std::enable_if_t<!std::is_base_of_v< VariantDataClass, T >, T > get(const std::string &key)
Definition: ChannelRef.h:107
IceInternal::Handle< ChannelRef >
armarx::exceptions::local::InvalidDataFieldException
Definition: InvalidDataFieldException.h:33
armarx::VariantType::ChannelRef
const VariantTypeId ChannelRef
Definition: ChannelRef.h:169
armarx::VariantType
Definition: ChannelRef.h:167
armarx::VariantTypeId
Ice::Int VariantTypeId
Definition: Variant.h:43
armarx::ChannelRef::output
std::string output(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: ChannelRef.cpp:231
InvalidDataFieldException.h
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::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::ChannelRef::~ChannelRef
~ChannelRef() override
Definition: ChannelRef.h:66
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