DebugObserverHelper.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package ArmarXCore::ArmarXObjects::DebugObserverHelper
17 * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
18 * @date 2021
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23#pragma once
24
25#include <boost/hana/for_each.hpp>
26
27#include <SimoxUtility/meta/boost_hana.h>
28#include <SimoxUtility/meta/enum/adapt_enum.h>
29
32#include <ArmarXCore/interface/observers/ObserverInterface.h>
37
38namespace armarx
39{
40 /**
41 * @defgroup Library-DebugObserverHelper DebugObserverHelper
42 * @ingroup ArmarXCore
43 * A description of the library DebugObserverHelper.
44 *
45 * @class DebugObserverHelper
46 * @ingroup Library-DebugObserverHelper
47 * @brief Brief description of class DebugObserverHelper.
48 *
49 * Detailed description of class DebugObserverHelper.
50 */
52 {
53 //create
54 public:
55 DebugObserverHelper(const DebugObserverInterfacePrx& prx = nullptr, bool batchmode = false);
56
57 template <class T>
59 const DebugObserverInterfacePrx& prx = nullptr,
60 bool batchmode = false) :
61 DebugObserverHelper(prx, batchmode)
62 {
63 setChannelName(name);
64 }
65
66 //configure
67 public:
70
71 const std::string& getChannelName() const;
72 void setChannelName(const std::string& name);
73 void setChannelName(const ManagedIceObject& obj);
74 void setChannelName(const ManagedIceObject* obj);
77
78 //management
79 public:
80 void setDebugObserverBatchModeEnabled(bool enable);
82
83 //setDebugObserverDatafield
84 public:
85 void setDebugObserverDatafield(const std::string& channelName,
86 const std::string& datafieldName,
87 const TimedVariantPtr& value) const;
88 void setDebugObserverDatafield(const std::string& channelName,
89 const std::string& datafieldName,
90 const VariantPtr& value) const;
91 void setDebugObserverDatafield(const std::string& channelName,
92 const std::string& datafieldName,
93 const VariantBasePtr& value) const;
94
95 template <class T>
96 void
97 setDebugObserverDatafield(const std::string& channelName,
98 const std::string& datafieldName,
99 const T& t)
100 {
101 setDebugObserverDatafield(channelName, datafieldName, IceUtil::Time::now(), t);
102 }
103
104 template <class T>
105 void
106 setDebugObserverDatafield(const std::string& channelName,
107 const std::string& datafieldName,
108 const IceUtil::Time& time,
109 const T& value) const
110 {
112 TimedVariantPtr var;
113 using VariantIntMaxT = Ice::Int;
114 [[maybe_unused]] static constexpr std::uint64_t IceIntTMax =
115 static_cast<std::uint64_t>(std::numeric_limits<VariantIntMaxT>::max());
116 if constexpr (std::is_same_v<T, IceUtil::Time> ||
119 {
121 var = new TimedVariant(TimestampVariant(value), time);
122 }
123 else if constexpr (std::is_unsigned_v<T> && !std::is_same_v<bool, T>)
124 {
126 if (_float_fallback_for_big_too_large_ints)
127 {
128 if (value < IceIntTMax)
129 {
131 var = new TimedVariant(Variant(static_cast<VariantIntMaxT>(value)), time);
132 }
133 else
134 {
136 var = new TimedVariant(Variant(static_cast<double>(value)), time);
137 }
138 }
139 else
140 {
142 ARMARX_CHECK_LESS_EQUAL(value, IceIntTMax);
143 var = new TimedVariant(Variant(static_cast<VariantIntMaxT>(value)), time);
144 }
145 }
146 else if constexpr (simox::meta::has_hana_accessor_v<T>)
147 {
149 namespace hana = boost::hana;
150 static constexpr auto accessors = hana::accessors<T>();
151 hana::for_each(accessors,
152 [&](auto& e)
153 {
155 const auto varname = hana::to<char const*>(hana::first(e));
156 const auto elemName = datafieldName + '_' + varname;
158 channelName, elemName, time, hana::second(e)(value));
159 });
160 return;
161 }
162 else if constexpr (simox::meta::is_enum_adapted_v<T>)
163 {
165 var = new TimedVariant(Variant(std::to_string(value)), time);
166 }
167 else if constexpr (std::is_same_v<char, T>)
168 {
170 var = new TimedVariant(Variant(static_cast<int>(value)), time);
171 }
172 else
173 {
175 var = new TimedVariant(Variant(value), time);
176 }
177 setDebugObserverDatafield(channelName, datafieldName, var);
178 }
179
180 template <class T, class... Ts>
181 void
182 setDebugObserverDatafield(const std::string& channelName,
183 const std::string& datafieldName,
184 const std::chrono::duration<Ts...> duration,
185 const T& value) const
186 {
189 channelName,
190 datafieldName,
191 IceUtil::Time::microSeconds(
192 std::chrono::duration_cast<std::chrono::microseconds>(duration).count()),
193 value);
194 }
195
196 template <class T, class... Ts>
197 void
198 setDebugObserverDatafield(const std::string& channelName,
199 const std::string& datafieldName,
200 const std::chrono::time_point<Ts...> timepoint,
201 const T& value) const
202 {
205 channelName, datafieldName, timepoint.time_since_epoch(), value);
206 }
207
208 void setDebugObserverDatafield(const std::string& channelName,
209 const std::string& datafieldName,
210 const IceUtil::Time& time,
211 const VariantPtr& value) const;
212
213 template <class... Ts>
214 void
215 setDebugObserverDatafield(const std::string& channelName,
216 const std::string& datafieldName,
217 const std::chrono::duration<Ts...> duration,
218 const VariantPtr& value) const
219 {
222 channelName,
223 datafieldName,
224 IceUtil::Time::microSeconds(
225 std::chrono::duration_cast<std::chrono::microseconds>(duration).count()),
226 value);
227 }
228
229 template <class... Ts>
230 void
231 setDebugObserverDatafield(const std::string& channelName,
232 const std::string& datafieldName,
233 const std::chrono::time_point<Ts...> timepoint,
234 const VariantPtr& value) const
235 {
238 channelName, datafieldName, timepoint.time_since_epoch(), value);
239 }
240
241 void setDebugObserverDatafield(const std::string& channelName,
242 const std::string& datafieldName,
243 const IceUtil::Time& time,
244 const Variant& value) const;
245
246 template <class... Ts>
247 void
248 setDebugObserverDatafield(const std::string& channelName,
249 const std::string& datafieldName,
250 const std::chrono::duration<Ts...> duration,
251 const Variant& value) const
252 {
255 channelName,
256 datafieldName,
257 IceUtil::Time::microSeconds(
258 std::chrono::duration_cast<std::chrono::microseconds>(duration).count()),
259 value);
260 }
261
262 template <class... Ts>
263 void
264 setDebugObserverDatafield(const std::string& channelName,
265 const std::string& datafieldName,
266 const std::chrono::time_point<Ts...> timepoint,
267 const Variant& value) const
268 {
271 channelName, datafieldName, timepoint.time_since_epoch(), value);
272 }
273
274 template <class T>
275 void
276 setDebugObserverDatafield(const std::string& datafieldName, const T& t)
277 {
279 setDebugObserverDatafield(_channelName, datafieldName, t);
280 }
281
282 //set channel / remove
283 public:
284 void setDebugObserverChannel(const std::string& channelName,
285 StringVariantBaseMap valueMap) const;
286 void removeDebugObserverChannel(const std::string& channelname) const;
287 void removeDebugObserverDatafield(const std::string& channelName,
288 const std::string& datafieldName) const;
290
291
292 void setFloatFallbackForBigTooLargeInts(bool useFallback);
293
294
295 //data
296 private:
297 std::string _channelName;
299 bool _batchMode{false};
300 mutable std::map<std::string, StringVariantBaseMap> _batch;
301 bool _float_fallback_for_big_too_large_ints = true;
302 };
303
304} // namespace armarx
void setDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName, const T &t)
void setDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName, const TimedVariantPtr &value) const
void setDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName, const std::chrono::duration< Ts... > duration, const VariantPtr &value) const
void setDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName, const std::chrono::duration< Ts... > duration, const T &value) const
void setDebugObserverChannel(const std::string &channelName, StringVariantBaseMap valueMap) const
DebugObserverHelper(const T &name, const DebugObserverInterfacePrx &prx=nullptr, bool batchmode=false)
void setDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName, const std::chrono::time_point< Ts... > timepoint, const VariantPtr &value) const
void removeDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName) const
void setChannelName(const std::string &name)
const DebugObserverInterfacePrx & getDebugObserver() const
void removeDebugObserverChannel(const std::string &channelname) const
DebugObserverHelper(const DebugObserverInterfacePrx &prx=nullptr, bool batchmode=false)
void setDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName, const std::chrono::time_point< Ts... > timepoint, const Variant &value) const
void setDebugObserverDatafield(const std::string &datafieldName, const T &t)
void setDebugObserver(const DebugObserverInterfacePrx &prx)
void setDebugObserverBatchModeEnabled(bool enable)
void setDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName, const std::chrono::duration< Ts... > duration, const Variant &value) const
void setDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName, const std::chrono::time_point< Ts... > timepoint, const T &value) const
void setFloatFallbackForBigTooLargeInts(bool useFallback)
const std::string & getChannelName() const
void setDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName, const IceUtil::Time &time, const T &value) const
The ManagedIceObject is the base class for all ArmarX objects.
Implements a Variant type for timestamps.
The Variant class is described here: Variants.
Definition Variant.h:224
#define ARMARX_CHECK_LESS_EQUAL(lhs, rhs)
This macro evaluates whether lhs is less or equal (<=) rhs and if it turns out to be false it will th...
This file offers overloads of toIce() and fromIce() functions for STL container types.
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugObserverInterface > DebugObserverInterfacePrx
std::map< std::string, VariantBasePtr > StringVariantBaseMap
IceInternal::Handle< TimedVariant > TimedVariantPtr
IceInternal::Handle< Variant > VariantPtr
Definition Variant.h:41
::IceInternal::Handle<::armarx::VariantBase > VariantBasePtr
#define ARMARX_TRACE
Definition trace.h:77
#define ARMARX_TRACE_LITE
Definition trace.h:98