YARP
Yet Another Robot Platform
Timer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef YARP_OS_TIMER_H
10 #define YARP_OS_TIMER_H
11 
12 #include <yarp/os/api.h>
13 
14 #ifndef YARP_NO_DEPRECATED // since YARP 3.3
15 #define YARP_INCLUDING_DEPRECATED_HEADER_ON_PURPOSE
16 #include <yarp/os/Mutex.h>
17 #undef YARP_INCLUDING_DEPRECATED_HEADER_ON_PURPOSE
18 #endif
19 
20 #include <functional>
21 #include <mutex>
22 
23 namespace yarp {
24 namespace os {
25 
27 {
31  double lastExpected;
32 
36  double lastReal;
37 
43 
47  double currentReal;
48 
52  double lastDuration;
53 
57  unsigned int runCount;
58 };
59 
61 {
62  TimerSettings(double inPeriod) :
63  period(inPeriod),
64  totalTime(0.0),
65  totalRunCount(0),
66  tolerance(0.001)
67  {
68  }
69  TimerSettings(double inPeriod, size_t count, double seconds) :
70  period(inPeriod),
71  totalTime(seconds),
72  totalRunCount(count),
73  tolerance(0.001)
74  {
75  }
76  TimerSettings(double inPeriod, size_t count, double seconds, double inTollerance) :
77  period(inPeriod),
78  totalTime(seconds),
79  totalRunCount(count),
80  tolerance(inTollerance)
81  {
82  }
83 
84  bool operator==(const TimerSettings& rhs) const
85  {
86  return period == rhs.period && totalTime == rhs.totalTime && totalRunCount == rhs.totalRunCount && tolerance == rhs.tolerance;
87  }
88 
89 
100  double period;
101  double totalTime;
103  double tolerance;
104 };
105 
107 {
108 public:
109  typedef std::function<bool(const yarp::os::YarpTimerEvent&)> TimerCallback;
110  Timer(const Timer&) = delete;
111  Timer operator=(const Timer&) = delete;
112 
113 #ifndef YARP_NO_DEPRECATED // Since YARP 3.3
129  Timer(const yarp::os::TimerSettings& settings,
130  const TimerCallback& callback,
131  bool newThread,
132  yarp::os::Mutex* mutex);
133 
146  template <class T>
148  bool (T::*callback)(const yarp::os::YarpTimerEvent&),
149  T* object,
150  bool newThread,
151  yarp::os::Mutex* mutex) :
152 
153  Timer(settings, std::bind(callback, object, std::placeholders::_1), newThread, mutex)
154  {
155  }
156 
161  template <class T>
163  bool (T::*callback)(const yarp::os::YarpTimerEvent&) const,
164  const T* object,
165  bool newThread,
166  yarp::os::Mutex* mutex) :
167 
168  Timer(settings, std::bind(callback, object, std::placeholders::_1), newThread, mutex)
169  {
170  }
172 #endif
173 
186  Timer(const yarp::os::TimerSettings& settings,
187  const TimerCallback& callback,
188  bool newThread,
189  std::mutex* mutex = nullptr);
190 
202  template <class T>
204  bool (T::*callback)(const yarp::os::YarpTimerEvent&),
205  T* object,
206  bool newThread,
207  std::mutex* mutex = nullptr) :
208 
209  Timer(settings, std::bind(callback, object, std::placeholders::_1), newThread, mutex)
210  {
211  }
212 
216  template <class T>
218  bool (T::*callback)(const yarp::os::YarpTimerEvent&) const,
219  const T* object,
220  bool newThread,
221  std::mutex* mutex = nullptr) :
222 
223  Timer(settings, std::bind(callback, object, std::placeholders::_1), newThread, mutex)
224  {
225  }
226 
227  virtual ~Timer();
228 
233  void setSettings(const yarp::os::TimerSettings& settings);
234 
239  const yarp::os::TimerSettings getSettings();
240 
241  virtual bool start();
242 
243  virtual bool step();
244 
245  virtual void stop();
246 
247  virtual bool isRunning();
248 
249 #ifndef DOXYGEN_SHOULD_SKIP_THIS
250  class PrivateImpl;
251 
252 private:
253  PrivateImpl* impl;
254 #endif // DOXYGEN_SHOULD_SKIP_THIS
255 };
256 
257 } // namespace os
258 } // namespace yarp
259 
260 #endif // YARP_OS_TIMER_H
yarp::os::TimerSettings::TimerSettings
TimerSettings(double inPeriod, size_t count, double seconds)
Definition: Timer.h:69
YARP_WARNING_PUSH
#define YARP_WARNING_PUSH
Starts a temporary alteration of the enabled warnings.
Definition: system.h:334
yarp::os::TimerSettings::TimerSettings
TimerSettings(double inPeriod)
Definition: Timer.h:62
yarp::os::YarpTimerEvent::lastExpected
double lastExpected
lastExpected when the last callback actually happened
Definition: Timer.h:31
yarp::os::TimerSettings::period
double period
Definition: Timer.h:100
yarp::os::Timer::operator=
Timer operator=(const Timer &)=delete
yarp::os::Timer::Timer
Timer(const Timer &)=delete
api.h
YARP_os_API
#define YARP_os_API
Definition: api.h:19
yarp::os::TimerSettings::TimerSettings
TimerSettings(double inPeriod, size_t count, double seconds, double inTollerance)
Definition: Timer.h:76
yarp::os::YarpTimerEvent
Definition: Timer.h:27
yarp::os::YarpTimerEvent::runCount
unsigned int runCount
runCount the count of calls
Definition: Timer.h:57
yarp::os::TimerSettings::totalTime
double totalTime
Definition: Timer.h:101
yarp::os::YarpTimerEvent::lastReal
double lastReal
lastReal when the last callback actually happened
Definition: Timer.h:36
yarp::os::Timer::TimerCallback
std::function< bool(const yarp::os::YarpTimerEvent &)> TimerCallback
Definition: Timer.h:109
yarp::os::YarpTimerEvent::currentReal
double currentReal
currentReal When the current callback is actually being called
Definition: Timer.h:47
yarp::os::Timer::Timer
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:217
yarp::os::TimerSettings::totalRunCount
size_t totalRunCount
Definition: Timer.h:102
YARP_WARNING_POP
#define YARP_WARNING_POP
Ends a temporary alteration of the enabled warnings.
Definition: system.h:335
yarp::os::Timer::Timer
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:203
Mutex.h
yarp::os::YarpTimerEvent::currentExpected
double currentExpected
currentExpected this is when the current callback should have been called
Definition: Timer.h:42
yarp::os::TimerSettings::operator==
bool operator==(const TimerSettings &rhs) const
Definition: Timer.h:84
yarp::os::Timer::Timer
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:162
yarp::os::Timer
Definition: Timer.h:107
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::os::YarpTimerEvent::lastDuration
double lastDuration
lastDuration Contains the duration of the last callback
Definition: Timer.h:52
yarp::os::Mutex
Basic wrapper for mutual exclusion.
Definition: Mutex.h:35
yarp::os::TimerSettings
Definition: Timer.h:61
YARP_DISABLE_DEPRECATED_WARNING
#define YARP_DISABLE_DEPRECATED_WARNING
Disable deprecated warnings in the following code.
Definition: system.h:336
yarp::os::TimerSettings::tolerance
double tolerance
Definition: Timer.h:103
yarp::os::Timer::Timer
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:147