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 <SimoxUtility/meta/boost_hana.h>
26 #include <SimoxUtility/meta/enum/adapt_enum.h>
27 
28 #include <ArmarXCore/interface/observers/ObserverInterface.h>
35 
36 
37 namespace armarx
38 {
39  /**
40  * @defgroup Library-DebugObserverHelper DebugObserverHelper
41  * @ingroup ArmarXCore
42  * A description of the library DebugObserverHelper.
43  *
44  * @class DebugObserverHelper
45  * @ingroup Library-DebugObserverHelper
46  * @brief Brief description of class DebugObserverHelper.
47  *
48  * Detailed description of class DebugObserverHelper.
49  */
51  {
52  //create
53  public:
54  DebugObserverHelper(const DebugObserverInterfacePrx& prx = nullptr, bool batchmode = false);
55  template<class T>
56  DebugObserverHelper(const T& name, const DebugObserverInterfacePrx& prx = nullptr, bool batchmode = false) :
57  DebugObserverHelper(prx, batchmode)
58  {
59  setChannelName(name);
60  }
61 
62  //configure
63  public:
66 
67  const std::string& getChannelName() const;
68  void setChannelName(const std::string& name);
69  void setChannelName(const ManagedIceObject& obj);
70  void setChannelName(const ManagedIceObject* obj);
71  void setChannelName(const ManagedIceObjectPlugin& obj);
72  void setChannelName(const ManagedIceObjectPlugin* obj);
73 
74  //management
75  public:
76  void setDebugObserverBatchModeEnabled(bool enable);
78 
79  //setDebugObserverDatafield
80  public:
81  void setDebugObserverDatafield(const std::string& channelName, const std::string& datafieldName, const TimedVariantPtr& value) const;
82  void setDebugObserverDatafield(const std::string& channelName, const std::string& datafieldName, const VariantPtr& value) const;
83  void setDebugObserverDatafield(const std::string& channelName, const std::string& datafieldName, const VariantBasePtr& value) const;
84  template<class T>
85  void setDebugObserverDatafield(const std::string& channelName, const std::string& datafieldName, const T& t)
86  {
87  setDebugObserverDatafield(channelName, datafieldName, IceUtil::Time::now(), t);
88  }
89  template<class T>
90  void setDebugObserverDatafield(const std::string& channelName, const std::string& datafieldName, const IceUtil::Time& time, const T& value) const
91  {
93  TimedVariantPtr var;
94  using VariantIntMaxT = Ice::Int;
95  [[maybe_unused]] static constexpr std::uint64_t IceIntTMax =
96  static_cast<std::uint64_t>(std::numeric_limits<VariantIntMaxT>::max());
97  if constexpr(
98  std::is_same_v<T, IceUtil::Time> ||
99  meta::TypeTemplateTraits::IsInstanceOfV<std::chrono::duration, T> ||
100  meta::TypeTemplateTraits::IsInstanceOfV<std::chrono::time_point, T>
101  )
102  {
103  ARMARX_TRACE;
104  var = new TimedVariant(TimestampVariant(value), time);
105  }
106  else if constexpr(std::is_unsigned_v<T>&& !std::is_same_v<bool, T>)
107  {
108  ARMARX_TRACE;
109  if (_float_fallback_for_big_too_large_ints)
110  {
111  if (value < IceIntTMax)
112  {
113  ARMARX_TRACE;
114  var = new TimedVariant(Variant(static_cast<VariantIntMaxT>(value)), time);
115  }
116  else
117  {
118  ARMARX_TRACE;
119  var = new TimedVariant(Variant(static_cast<double>(value)), time);
120  }
121  }
122  else
123  {
124  ARMARX_TRACE;
125  ARMARX_CHECK_LESS_EQUAL(value, IceIntTMax);
126  var = new TimedVariant(Variant(static_cast<VariantIntMaxT>(value)), time);
127  }
128  }
129  else if constexpr(simox::meta::has_hana_accessor_v<T>)
130  {
131  ARMARX_TRACE;
132  namespace hana = boost::hana;
133  static constexpr auto accessors = hana::accessors<T>();
134  hana::for_each(accessors, [&](auto & e)
135  {
137  const auto varname = hana::to<char const*>(hana::first(e));
138  const auto elemName = datafieldName + '_' + varname;
139  setDebugObserverDatafield(channelName, elemName, time, hana::second(e)(value));
140  });
141  return;
142  }
143  else if constexpr(simox::meta::is_enum_adapted_v<T>)
144  {
145  ARMARX_TRACE;
146  var = new TimedVariant(Variant(std::to_string(value)), time);
147  }
148  else if constexpr(std::is_same_v<char, T>)
149  {
150  ARMARX_TRACE;
151  var = new TimedVariant(Variant(static_cast<int>(value)), time);
152  }
153  else
154  {
155  ARMARX_TRACE;
156  var = new TimedVariant(Variant(value), time);
157  }
158  setDebugObserverDatafield(channelName, datafieldName, var);
159  }
160  template<class T, class...Ts>
161  void setDebugObserverDatafield(const std::string& channelName, const std::string& datafieldName, const std::chrono::duration<Ts...> duration, const T& value) const
162  {
163  ARMARX_TRACE;
165  channelName, datafieldName,
166  IceUtil::Time::microSeconds(std::chrono::duration_cast<std::chrono::microseconds>(duration).count()),
167  value);
168  }
169  template<class T, class...Ts>
170  void setDebugObserverDatafield(const std::string& channelName, const std::string& datafieldName, const std::chrono::time_point<Ts...> timepoint, const T& value) const
171  {
172  ARMARX_TRACE;
173  setDebugObserverDatafield(channelName, datafieldName, timepoint.time_since_epoch(), value);
174  }
175 
176  void setDebugObserverDatafield(const std::string& channelName, const std::string& datafieldName, const IceUtil::Time& time, const VariantPtr& value) const;
177  template<class...Ts>
178  void setDebugObserverDatafield(const std::string& channelName, const std::string& datafieldName, const std::chrono::duration<Ts...> duration, const VariantPtr& value) const
179  {
180  ARMARX_TRACE;
182  channelName, datafieldName,
183  IceUtil::Time::microSeconds(std::chrono::duration_cast<std::chrono::microseconds>(duration).count()),
184  value);
185  }
186  template<class...Ts>
187  void setDebugObserverDatafield(const std::string& channelName, const std::string& datafieldName, const std::chrono::time_point<Ts...> timepoint, const VariantPtr& value) const
188  {
189  ARMARX_TRACE;
190  setDebugObserverDatafield(channelName, datafieldName, timepoint.time_since_epoch(), value);
191  }
192 
193  void setDebugObserverDatafield(const std::string& channelName, const std::string& datafieldName, const IceUtil::Time& time, const Variant& value) const;
194  template<class...Ts>
195  void setDebugObserverDatafield(const std::string& channelName, const std::string& datafieldName, const std::chrono::duration<Ts...> duration, const Variant& value) const
196  {
197  ARMARX_TRACE;
199  channelName, datafieldName,
200  IceUtil::Time::microSeconds(std::chrono::duration_cast<std::chrono::microseconds>(duration).count()),
201  value);
202  }
203  template<class...Ts>
204  void setDebugObserverDatafield(const std::string& channelName, const std::string& datafieldName, const std::chrono::time_point<Ts...> timepoint, const Variant& value) const
205  {
206  ARMARX_TRACE;
207  setDebugObserverDatafield(channelName, datafieldName, timepoint.time_since_epoch(), value);
208  }
209 
210  template<class T>
211  void setDebugObserverDatafield(const std::string& datafieldName, const T& t)
212  {
213  ARMARX_TRACE;
214  setDebugObserverDatafield(_channelName, datafieldName, t);
215  }
216 
217  //set channel / remove
218  public:
219  void setDebugObserverChannel(const std::string& channelName, StringVariantBaseMap valueMap) const;
220  void removeDebugObserverChannel(const std::string& channelname) const;
221  void removeDebugObserverDatafield(const std::string& channelName, const std::string& datafieldName) const;
222  void removeAllDebugObserverChannels() const;
223 
224 
225  void setFloatFallbackForBigTooLargeInts(bool useFallback);
226 
227 
228  //data
229  private:
230  std::string _channelName;
231  DebugObserverInterfacePrx _observer;
232  bool _batchMode{false};
233  mutable std::map<std::string, StringVariantBaseMap> _batch;
234  bool _float_fallback_for_big_too_large_ints = true;
235 
236  };
237 
238 }
ManagedIceObjectPlugin.h
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::DebugObserverHelper::setDebugObserverDatafield
void setDebugObserverDatafield(const std::string &datafieldName, const T &t)
Definition: DebugObserverHelper.h:211
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:161
armarx::StringVariantBaseMap
std::map< std::string, VariantBasePtr > StringVariantBaseMap
Definition: ManagedIceObject.h:111
armarx::DebugObserverHelper::sendDebugObserverBatch
void sendDebugObserverBatch()
Definition: DebugObserverHelper.cpp:63
armarx::DebugObserverHelper::setDebugObserverDatafield
void setDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName, const T &t)
Definition: DebugObserverHelper.h:85
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:56
armarx::DebugObserverHelper::removeDebugObserverDatafield
void removeDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName) const
Definition: DebugObserverHelper.cpp:135
armarx::DebugObserverHelper::DebugObserverHelper
DebugObserverHelper(const DebugObserverInterfacePrx &prx=nullptr, bool batchmode=false)
ARMARX_TRACE_LITE
#define ARMARX_TRACE_LITE
Definition: trace.h:85
armarx::DebugObserverHelper::setDebugObserverBatchModeEnabled
void setDebugObserverBatchModeEnabled(bool enable)
Definition: DebugObserverHelper.cpp:58
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::DebugObserverHelper::getChannelName
const std::string & getChannelName() const
Definition: DebugObserverHelper.cpp:160
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::DebugObserverHelper::getDebugObserver
const DebugObserverInterfacePrx & getDebugObserver() const
Definition: DebugObserverHelper.cpp:149
TimestampVariant.h
armarx::DebugObserverHelper::setChannelName
void setChannelName(const std::string &name)
Definition: DebugObserverHelper.cpp:166
max
T max(T t1, T t2)
Definition: gdiam.h:48
armarx::DebugObserverHelper::removeDebugObserverChannel
void removeDebugObserverChannel(const std::string &channelname) const
Definition: DebugObserverHelper.cpp:128
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:187
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:40
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:163
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:170
armarx::DebugObserverHelper::setDebugObserverDatafield
void setDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName, const TimedVariantPtr &value) const
Definition: DebugObserverHelper.cpp:74
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:204
armarx::DebugObserverHelper::setDebugObserver
void setDebugObserver(const DebugObserverInterfacePrx &prx)
Definition: DebugObserverHelper.cpp:154
armarx::DebugObserverHelper::setDebugObserverDatafield
void setDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName, const IceUtil::Time &time, const T &value) const
Definition: DebugObserverHelper.h:90
IceInternal::ProxyHandle<::IceProxy::armarx::DebugObserverInterface >
armarx::DebugObserverHelper::setFloatFallbackForBigTooLargeInts
void setFloatFallbackForBigTooLargeInts(bool useFallback)
Definition: DebugObserverHelper.cpp:49
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
armarx::ManagedIceObjectPlugin
Definition: ManagedIceObjectPlugin.h:50
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:195
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::DebugObserverHelper
Brief description of class DebugObserverHelper.
Definition: DebugObserverHelper.h:50
armarx::DebugObserverHelper::removeAllDebugObserverChannels
void removeAllDebugObserverChannels() const
Definition: DebugObserverHelper.cpp:142
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::TimedVariant
Definition: TimedVariant.h:40
armarx::DebugObserverHelper::setDebugObserverChannel
void setDebugObserverChannel(const std::string &channelName, StringVariantBaseMap valueMap) const
Definition: DebugObserverHelper.cpp:121
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:178