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