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::os {
21 
23 {
27  double lastExpected;
28 
32  double lastReal;
33 
39 
43  double currentReal;
44 
48  double lastDuration;
49 
53  unsigned int runCount;
54 };
55 
57 {
58  TimerSettings(double inPeriod) :
59  period(inPeriod),
60  totalTime(0.0),
61  totalRunCount(0),
62  tolerance(0.001)
63  {
64  }
65  TimerSettings(double inPeriod, size_t count, double seconds) :
66  period(inPeriod),
67  totalTime(seconds),
68  totalRunCount(count),
69  tolerance(0.001)
70  {
71  }
72  TimerSettings(double inPeriod, size_t count, double seconds, double inTollerance) :
73  period(inPeriod),
74  totalTime(seconds),
75  totalRunCount(count),
76  tolerance(inTollerance)
77  {
78  }
79 
80  bool operator==(const TimerSettings& rhs) const
81  {
82  return period == rhs.period && totalTime == rhs.totalTime && totalRunCount == rhs.totalRunCount && tolerance == rhs.tolerance;
83  }
84 
85 
96  double period;
97  double totalTime;
98  size_t totalRunCount;
99  double tolerance;
100 };
101 
103 {
104 public:
105  typedef std::function<bool(const yarp::os::YarpTimerEvent&)> TimerCallback;
106  Timer(const Timer&) = delete;
107  Timer operator=(const Timer&) = delete;
108 
109 #ifndef YARP_NO_DEPRECATED // Since YARP 3.3
125  Timer(const yarp::os::TimerSettings& settings,
126  const TimerCallback& callback,
127  bool newThread,
128  yarp::os::Mutex* mutex);
129 
142  template <class T>
144  bool (T::*callback)(const yarp::os::YarpTimerEvent&),
145  T* object,
146  bool newThread,
147  yarp::os::Mutex* mutex) :
148 
149  Timer(settings, std::bind(callback, object, std::placeholders::_1), newThread, mutex)
150  {
151  }
152 
157  template <class T>
159  bool (T::*callback)(const yarp::os::YarpTimerEvent&) const,
160  const T* object,
161  bool newThread,
162  yarp::os::Mutex* mutex) :
163 
164  Timer(settings, std::bind(callback, object, std::placeholders::_1), newThread, mutex)
165  {
166  }
168 #endif
169 
182  Timer(const yarp::os::TimerSettings& settings,
183  const TimerCallback& callback,
184  bool newThread,
185  std::mutex* mutex = nullptr);
186 
198  template <class T>
200  bool (T::*callback)(const yarp::os::YarpTimerEvent&),
201  T* object,
202  bool newThread,
203  std::mutex* mutex = nullptr) :
204 
205  Timer(settings, std::bind(callback, object, std::placeholders::_1), newThread, mutex)
206  {
207  }
208 
212  template <class T>
214  bool (T::*callback)(const yarp::os::YarpTimerEvent&) const,
215  const T* object,
216  bool newThread,
217  std::mutex* mutex = nullptr) :
218 
219  Timer(settings, std::bind(callback, object, std::placeholders::_1), newThread, mutex)
220  {
221  }
222 
223  virtual ~Timer();
224 
229  void setSettings(const yarp::os::TimerSettings& settings);
230 
235  const yarp::os::TimerSettings getSettings();
236 
237  virtual bool start();
238 
239  virtual bool step();
240 
241  virtual void stop();
242 
243  virtual bool isRunning();
244 
245 #ifndef DOXYGEN_SHOULD_SKIP_THIS
246  class PrivateImpl;
247 
248 private:
249  PrivateImpl* impl;
250 #endif // DOXYGEN_SHOULD_SKIP_THIS
251 };
252 
253 } // namespace yarp::os
254 
255 #endif // YARP_OS_TIMER_H
Basic wrapper for mutual exclusion.
Definition: Mutex.h:31
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:199
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:143
Timer(const Timer &)=delete
std::function< bool(const yarp::os::YarpTimerEvent &)> TimerCallback
Definition: Timer.h:105
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:158
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:213
An interface to the operating system, including Port based communication.
TimerSettings(double inPeriod, size_t count, double seconds, double inTollerance)
Definition: Timer.h:72
size_t totalRunCount
Definition: Timer.h:98
bool operator==(const TimerSettings &rhs) const
Definition: Timer.h:80
TimerSettings(double inPeriod, size_t count, double seconds)
Definition: Timer.h:65
TimerSettings(double inPeriod)
Definition: Timer.h:58
double lastReal
lastReal when the last callback actually happened
Definition: Timer.h:32
double lastExpected
lastExpected when the last callback actually happened
Definition: Timer.h:27
double currentReal
currentReal When the current callback is actually being called
Definition: Timer.h:43
double lastDuration
lastDuration Contains the duration of the last callback
Definition: Timer.h:48
unsigned int runCount
runCount the count of calls
Definition: Timer.h:53
double currentExpected
currentExpected this is when the current callback should have been called
Definition: Timer.h:38
#define YARP_WARNING_POP
Ends a temporary alteration of the enabled warnings.
Definition: system.h:334
#define YARP_WARNING_PUSH
Starts a temporary alteration of the enabled warnings.
Definition: system.h:333
#define YARP_DISABLE_DEPRECATED_WARNING
Disable deprecated warnings in the following code.
Definition: system.h:335
#define YARP_os_API
Definition: api.h:18