YARP
Yet Another Robot Platform
Timer.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #ifndef YARP_OS_TIMER_H
7 #define YARP_OS_TIMER_H
8 
9 #include <yarp/os/api.h>
10 
11 #ifndef YARP_NO_DEPRECATED // since YARP 3.3
12 #define YARP_INCLUDING_DEPRECATED_HEADER_ON_PURPOSE
13 #include <yarp/os/Mutex.h>
14 #undef YARP_INCLUDING_DEPRECATED_HEADER_ON_PURPOSE
15 #endif
16 
17 #include <functional>
18 #include <mutex>
19 
20 namespace yarp {
21 namespace os {
22 
24 {
28  double lastExpected;
29 
33  double lastReal;
34 
40 
44  double currentReal;
45 
49  double lastDuration;
50 
54  unsigned int runCount;
55 };
56 
58 {
59  TimerSettings(double inPeriod) :
60  period(inPeriod),
61  totalTime(0.0),
62  totalRunCount(0),
63  tolerance(0.001)
64  {
65  }
66  TimerSettings(double inPeriod, size_t count, double seconds) :
67  period(inPeriod),
68  totalTime(seconds),
69  totalRunCount(count),
70  tolerance(0.001)
71  {
72  }
73  TimerSettings(double inPeriod, size_t count, double seconds, double inTollerance) :
74  period(inPeriod),
75  totalTime(seconds),
76  totalRunCount(count),
77  tolerance(inTollerance)
78  {
79  }
80 
81  bool operator==(const TimerSettings& rhs) const
82  {
83  return period == rhs.period && totalTime == rhs.totalTime && totalRunCount == rhs.totalRunCount && tolerance == rhs.tolerance;
84  }
85 
86 
97  double period;
98  double totalTime;
99  size_t totalRunCount;
100  double tolerance;
101 };
102 
104 {
105 public:
106  typedef std::function<bool(const yarp::os::YarpTimerEvent&)> TimerCallback;
107  Timer(const Timer&) = delete;
108  Timer operator=(const Timer&) = delete;
109 
110 #ifndef YARP_NO_DEPRECATED // Since YARP 3.3
126  Timer(const yarp::os::TimerSettings& settings,
127  const TimerCallback& callback,
128  bool newThread,
129  yarp::os::Mutex* mutex);
130 
143  template <class T>
145  bool (T::*callback)(const yarp::os::YarpTimerEvent&),
146  T* object,
147  bool newThread,
148  yarp::os::Mutex* mutex) :
149 
150  Timer(settings, std::bind(callback, object, std::placeholders::_1), newThread, mutex)
151  {
152  }
153 
158  template <class T>
160  bool (T::*callback)(const yarp::os::YarpTimerEvent&) const,
161  const T* object,
162  bool newThread,
163  yarp::os::Mutex* mutex) :
164 
165  Timer(settings, std::bind(callback, object, std::placeholders::_1), newThread, mutex)
166  {
167  }
169 #endif
170 
183  Timer(const yarp::os::TimerSettings& settings,
184  const TimerCallback& callback,
185  bool newThread,
186  std::mutex* mutex = nullptr);
187 
199  template <class T>
201  bool (T::*callback)(const yarp::os::YarpTimerEvent&),
202  T* object,
203  bool newThread,
204  std::mutex* mutex = nullptr) :
205 
206  Timer(settings, std::bind(callback, object, std::placeholders::_1), newThread, mutex)
207  {
208  }
209 
213  template <class T>
215  bool (T::*callback)(const yarp::os::YarpTimerEvent&) const,
216  const T* object,
217  bool newThread,
218  std::mutex* mutex = nullptr) :
219 
220  Timer(settings, std::bind(callback, object, std::placeholders::_1), newThread, mutex)
221  {
222  }
223 
224  virtual ~Timer();
225 
230  void setSettings(const yarp::os::TimerSettings& settings);
231 
236  const yarp::os::TimerSettings getSettings();
237 
238  virtual bool start();
239 
240  virtual bool step();
241 
242  virtual void stop();
243 
244  virtual bool isRunning();
245 
246 #ifndef DOXYGEN_SHOULD_SKIP_THIS
247  class PrivateImpl;
248 
249 private:
250  PrivateImpl* impl;
251 #endif // DOXYGEN_SHOULD_SKIP_THIS
252 };
253 
254 } // namespace os
255 } // namespace yarp
256 
257 #endif // YARP_OS_TIMER_H
Basic wrapper for mutual exclusion.
Definition: Mutex.h:32
Timer(const yarp::os::TimerSettings &settings, bool(T::*callback)(const yarp::os::YarpTimerEvent &), T *object, bool newThread, std::mutex *mutex=nullptr)
Timer constructor.
Definition: Timer.h:200
Timer(const yarp::os::TimerSettings &settings, bool(T::*callback)(const yarp::os::YarpTimerEvent &), T *object, bool newThread, yarp::os::Mutex *mutex)
Timer constructor.
Definition: Timer.h:144
Timer(const Timer &)=delete
std::function< bool(const yarp::os::YarpTimerEvent &)> TimerCallback
Definition: Timer.h:106
Timer operator=(const Timer &)=delete
Timer(const yarp::os::TimerSettings &settings, bool(T::*callback)(const yarp::os::YarpTimerEvent &) const, const T *object, bool newThread, yarp::os::Mutex *mutex)
const version.
Definition: Timer.h:159
Timer(const yarp::os::TimerSettings &settings, bool(T::*callback)(const yarp::os::YarpTimerEvent &) const, const T *object, bool newThread, std::mutex *mutex=nullptr)
const version.
Definition: Timer.h:214
The main, catch-all namespace for YARP.
Definition: dirs.h:16
TimerSettings(double inPeriod, size_t count, double seconds, double inTollerance)
Definition: Timer.h:73
size_t totalRunCount
Definition: Timer.h:99
bool operator==(const TimerSettings &rhs) const
Definition: Timer.h:81
TimerSettings(double inPeriod, size_t count, double seconds)
Definition: Timer.h:66
TimerSettings(double inPeriod)
Definition: Timer.h:59
double lastReal
lastReal when the last callback actually happened
Definition: Timer.h:33
double lastExpected
lastExpected when the last callback actually happened
Definition: Timer.h:28
double currentReal
currentReal When the current callback is actually being called
Definition: Timer.h:44
double lastDuration
lastDuration Contains the duration of the last callback
Definition: Timer.h:49
unsigned int runCount
runCount the count of calls
Definition: Timer.h:54
double currentExpected
currentExpected this is when the current callback should have been called
Definition: Timer.h:39
#define YARP_WARNING_POP
Ends a temporary alteration of the enabled warnings.
Definition: system.h:332
#define YARP_WARNING_PUSH
Starts a temporary alteration of the enabled warnings.
Definition: system.h:331
#define YARP_DISABLE_DEPRECATED_WARNING
Disable deprecated warnings in the following code.
Definition: system.h:333
#define YARP_os_API
Definition: api.h:18