YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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#include <functional>
12#include <mutex>
13
14namespace yarp::os {
15
17{
22
26 double lastReal;
27
33
38
43
47 unsigned int runCount;
48};
49
51{
53 period(inPeriod),
54 totalTime(0.0),
55 totalRunCount(0),
56 tolerance(0.001)
57 {
58 }
59 TimerSettings(double inPeriod, size_t count, double seconds) :
60 period(inPeriod),
61 totalTime(seconds),
62 totalRunCount(count),
63 tolerance(0.001)
64 {
65 }
66 TimerSettings(double inPeriod, size_t count, double seconds, double inTollerance) :
67 period(inPeriod),
68 totalTime(seconds),
69 totalRunCount(count),
70 tolerance(inTollerance)
71 {
72 }
73
74 bool operator==(const TimerSettings& rhs) const
75 {
76 return period == rhs.period && totalTime == rhs.totalTime && totalRunCount == rhs.totalRunCount && tolerance == rhs.tolerance;
77 }
78
79
90 double period;
91 double totalTime;
93 double tolerance;
94};
95
97{
98public:
99 typedef std::function<bool(const yarp::os::YarpTimerEvent&)> TimerCallback;
100 Timer(const Timer&) = delete;
101 Timer operator=(const Timer&) = delete;
102
115 Timer(const yarp::os::TimerSettings& settings,
116 const TimerCallback& callback,
117 bool newThread,
118 std::mutex* mutex = nullptr);
119
131 template <class T>
133 bool (T::*callback)(const yarp::os::YarpTimerEvent&),
134 T* object,
136 std::mutex* mutex = nullptr) :
137
138 Timer(settings, std::bind(callback, object, std::placeholders::_1), newThread, mutex)
139 {
140 }
141
145 template <class T>
147 bool (T::*callback)(const yarp::os::YarpTimerEvent&) const,
148 const T* object,
150 std::mutex* mutex = nullptr) :
151
152 Timer(settings, std::bind(callback, object, std::placeholders::_1), newThread, mutex)
153 {
154 }
155
156 virtual ~Timer();
157
162 void setSettings(const yarp::os::TimerSettings& settings);
163
168 const yarp::os::TimerSettings getSettings();
169
170 virtual bool start();
171
172 virtual bool step();
173
174 virtual void stop();
175
176 virtual bool isRunning();
177
178#ifndef DOXYGEN_SHOULD_SKIP_THIS
179 class PrivateImpl;
180
181private:
182 PrivateImpl* impl;
183#endif // DOXYGEN_SHOULD_SKIP_THIS
184};
185
186} // namespace yarp::os
187
188#endif // YARP_OS_TIMER_H
A mini-server for performing network communication in the background.
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:132
Timer(const Timer &)=delete
std::function< bool(const yarp::os::YarpTimerEvent &)> TimerCallback
Definition Timer.h:99
Timer operator=(const Timer &)=delete
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:146
STL namespace.
An interface to the operating system, including Port based communication.
The main, catch-all namespace for YARP.
Definition dirs.h:16
TimerSettings(double inPeriod, size_t count, double seconds, double inTollerance)
Definition Timer.h:66
bool operator==(const TimerSettings &rhs) const
Definition Timer.h:74
TimerSettings(double inPeriod, size_t count, double seconds)
Definition Timer.h:59
TimerSettings(double inPeriod)
Definition Timer.h:52
double lastReal
lastReal when the last callback actually happened
Definition Timer.h:26
double lastExpected
lastExpected when the last callback actually happened
Definition Timer.h:21
double currentReal
currentReal When the current callback is actually being called
Definition Timer.h:37
double lastDuration
lastDuration Contains the duration of the last callback
Definition Timer.h:42
unsigned int runCount
runCount the count of calls
Definition Timer.h:47
double currentExpected
currentExpected this is when the current callback should have been called
Definition Timer.h:32
#define YARP_os_API
Definition api.h:18