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 
38 namespace 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>
58  DebugObserverHelper(const T& name,
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);
75  void setChannelName(const ManagedIceObjectPlugin& obj);
76  void setChannelName(const ManagedIceObjectPlugin* 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  {
111  ARMARX_TRACE;
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> ||
117  meta::TypeTemplateTraits::IsInstanceOfV<std::chrono::duration, T> ||
118  meta::TypeTemplateTraits::IsInstanceOfV<std::chrono::time_point, T>)
119  {
120  ARMARX_TRACE;
121  var = new TimedVariant(TimestampVariant(value), time);
122  }
123  else if constexpr (std::is_unsigned_v<T> && !std::is_same_v<bool, T>)
124  {
125  ARMARX_TRACE;
126  if (_float_fallback_for_big_too_large_ints)
127  {
128  if (value < IceIntTMax)
129  {
130  ARMARX_TRACE;
131  var = new TimedVariant(Variant(static_cast<VariantIntMaxT>(value)), time);
132  }
133  else
134  {
135  ARMARX_TRACE;
136  var = new TimedVariant(Variant(static_cast<double>(value)), time);
137  }
138  }
139  else
140  {
141  ARMARX_TRACE;
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  {
148  ARMARX_TRACE;
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  {
164  ARMARX_TRACE;
165  var = new TimedVariant(Variant(std::to_string(value)), time);
166  }
167  else if constexpr (std::is_same_v<char, T>)
168  {
169  ARMARX_TRACE;
170  var = new TimedVariant(Variant(static_cast<int>(value)), time);
171  }
172  else
173  {
174  ARMARX_TRACE;
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  {
187  ARMARX_TRACE;
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  {
203  ARMARX_TRACE;
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  {
220  ARMARX_TRACE;
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  {
236  ARMARX_TRACE;
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  {
253  ARMARX_TRACE;
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  {
269  ARMARX_TRACE;
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  {
278  ARMARX_TRACE;
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;
289  void removeAllDebugObserverChannels() const;
290 
291 
292  void setFloatFallbackForBigTooLargeInts(bool useFallback);
293 
294 
295  //data
296  private:
297  std::string _channelName;
298  DebugObserverInterfacePrx _observer;
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
ManagedIceObjectPlugin.h
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:223
armarx::DebugObserverHelper::setDebugObserverDatafield
void setDebugObserverDatafield(const std::string &datafieldName, const T &t)
Definition: DebugObserverHelper.h:276
armarx::DebugObserverHelper::setDebugObserverDatafield
void setDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName, const std::chrono::duration< Ts... > duration, const T &value) const
Definition: DebugObserverHelper.h:182
armarx::StringVariantBaseMap
std::map< std::string, VariantBasePtr > StringVariantBaseMap
Definition: ManagedIceObject.h:110
armarx::DebugObserverHelper::sendDebugObserverBatch
void sendDebugObserverBatch()
Definition: DebugObserverHelper.cpp:71
armarx::DebugObserverHelper::setDebugObserverDatafield
void setDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName, const T &t)
Definition: DebugObserverHelper.h:97
TemplateMetaProgramming.h
trace.h
armarx::TimestampVariant
Definition: TimestampVariant.h:54
armarx::DebugObserverHelper::DebugObserverHelper
DebugObserverHelper(const T &name, const DebugObserverInterfacePrx &prx=nullptr, bool batchmode=false)
Definition: DebugObserverHelper.h:58
armarx::DebugObserverHelper::removeDebugObserverDatafield
void removeDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName) const
Definition: DebugObserverHelper.cpp:170
armarx::DebugObserverHelper::DebugObserverHelper
DebugObserverHelper(const DebugObserverInterfacePrx &prx=nullptr, bool batchmode=false)
ARMARX_TRACE_LITE
#define ARMARX_TRACE_LITE
Definition: trace.h:98
armarx::DebugObserverHelper::setDebugObserverBatchModeEnabled
void setDebugObserverBatchModeEnabled(bool enable)
Definition: DebugObserverHelper.cpp:64
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::DebugObserverHelper::getChannelName
const std::string & getChannelName() const
Definition: DebugObserverHelper.cpp:201
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:77
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::DebugObserverHelper::getDebugObserver
const DebugObserverInterfacePrx & getDebugObserver() const
Definition: DebugObserverHelper.cpp:187
TimestampVariant.h
armarx::DebugObserverHelper::setChannelName
void setChannelName(const std::string &name)
Definition: DebugObserverHelper.cpp:208
max
T max(T t1, T t2)
Definition: gdiam.h:51
armarx::DebugObserverHelper::removeDebugObserverChannel
void removeDebugObserverChannel(const std::string &channelname) const
Definition: DebugObserverHelper.cpp:162
armarx::DebugObserverHelper::setDebugObserverDatafield
void setDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName, const std::chrono::time_point< Ts... > timepoint, const VariantPtr &value) const
Definition: DebugObserverHelper.h:231
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
ARMARX_CHECK_LESS_EQUAL
#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...
Definition: ExpressionException.h:109
ExpressionException.h
TimedVariant.h
armarx::ManagedIceObject
The ManagedIceObject is the base class for all ArmarX objects.
Definition: ManagedIceObject.h:162
armarx::DebugObserverHelper::setDebugObserverDatafield
void setDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName, const std::chrono::time_point< Ts... > timepoint, const T &value) const
Definition: DebugObserverHelper.h:198
armarx::DebugObserverHelper::setDebugObserverDatafield
void setDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName, const TimedVariantPtr &value) const
Definition: DebugObserverHelper.cpp:83
armarx::DebugObserverHelper::setDebugObserverDatafield
void setDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName, const std::chrono::time_point< Ts... > timepoint, const Variant &value) const
Definition: DebugObserverHelper.h:264
armarx::DebugObserverHelper::setDebugObserver
void setDebugObserver(const DebugObserverInterfacePrx &prx)
Definition: DebugObserverHelper.cpp:194
armarx::DebugObserverHelper::setDebugObserverDatafield
void setDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName, const IceUtil::Time &time, const T &value) const
Definition: DebugObserverHelper.h:106
IceInternal::ProxyHandle<::IceProxy::armarx::DebugObserverInterface >
armarx::DebugObserverHelper::setFloatFallbackForBigTooLargeInts
void setFloatFallbackForBigTooLargeInts(bool useFallback)
Definition: DebugObserverHelper.cpp:50
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:917
armarx::ManagedIceObjectPlugin
Definition: ManagedIceObjectPlugin.h:49
armarx::DebugObserverHelper::setDebugObserverDatafield
void setDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName, const std::chrono::duration< Ts... > duration, const Variant &value) const
Definition: DebugObserverHelper.h:248
T
float T
Definition: UnscentedKalmanFilterTest.cpp:38
armarx::DebugObserverHelper
Brief description of class DebugObserverHelper.
Definition: DebugObserverHelper.h:51
armarx::DebugObserverHelper::removeAllDebugObserverChannels
void removeAllDebugObserverChannels() const
Definition: DebugObserverHelper.cpp:179
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::TimedVariant
Definition: TimedVariant.h:39
armarx::DebugObserverHelper::setDebugObserverChannel
void setDebugObserverChannel(const std::string &channelName, StringVariantBaseMap valueMap) const
Definition: DebugObserverHelper.cpp:153
armarx::DebugObserverHelper::setDebugObserverDatafield
void setDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName, const std::chrono::duration< Ts... > duration, const VariantPtr &value) const
Definition: DebugObserverHelper.h:215