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
36namespace 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
71 Ice::ObjectPtr
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 {
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 {
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
172extern template class ::IceInternal::Handle<::armarx::ChannelRef>;
std::ostream & operator<<(std::ostream &strm, const AbstractInterface &a)
#define ARMARXCORE_IMPORT_EXPORT
constexpr T c
The ChannelRef class is a reference to a channel on an Observer.
Definition ChannelRef.h:51
std::string output(const Ice::Current &c=Ice::emptyCurrent) const override
friend class GenericFactory
Definition ChannelRef.h:53
VariantDataClassPtr clone(const Ice::Current &c=Ice::emptyCurrent) const override
std::enable_if_t<!std::is_base_of_v< VariantDataClass, T >, T > get(const std::string &key)
Definition ChannelRef.h:107
~ChannelRef() override
Definition ChannelRef.h:66
Ice::ObjectPtr ice_clone() const override
Definition ChannelRef.h:72
TimedVariantPtr getDataField(const std::string &datafieldName)
const std::string & getChannelName() const
std::enable_if_t< std::is_base_of_v< VariantDataClass, T >, IceInternal::Handle< T > > get(const std::string &key)
Definition ChannelRef.h:93
Baseclass for all ArmarX Observers.
Definition Observer.h:87
static VariantTypeId addTypeName(const std::string &typeName)
Register a new type for the use in a Variant.
Definition Variant.cpp:869
::IceInternal::Handle<::Ice::Communicator > CommunicatorPtr
Definition IceManager.h:49
const VariantTypeId ChannelRef
Definition ChannelRef.h:169
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< TimedVariant > TimedVariantPtr
IceInternal::Handle< Variant > VariantPtr
Definition Variant.h:41
IceInternal::Handle< ChannelRef > ChannelRefPtr
Definition ChannelRef.h:40
Ice::Int VariantTypeId
Definition Variant.h:43
IceInternal::Handle< DataFieldIdentifier > DataFieldIdentifierPtr
Typedef of DataFieldIdentifierPtr as IceInternal::Handle<DataFieldIdentifier> for convenience.