BTimer¶
Defined in header <BUtils/BTimer>
Overview¶
Class BTimer provides repetitive and single-shot timers with a minimum precision of 1 millisecond.
Public Types¶
enum | BTimerStatus {Active, Stop} |
Properties¶
bool | singleShot |
std::chrono::milliseconds | interval |
std::chrono::milliseconds | timeout |
Public Functions¶
BTimer() |
|
~BTimer() |
|
bool | operator>(const BTimer& rtimer) const |
bool | operator<(const BTimer& rtimer) const |
bool | operator=(const BTimer& rtimer) const |
void | start() |
void | stop() |
bool | isActive() const |
bool | isSingleShot() const |
int32 | id() const |
uint32 | interval() const |
uint32 | timeout() const |
void | reset() |
void | setActive(bool) |
void | callOnInterval(std::function timer_action) |
void | callOnTimeout(std::function timer_action) |
void | setInterval(uint32 _interval) |
void | setInterval(std::chrono::milliseconds _interval) |
void | setTimeout(uint32 _timeout) |
void | setTimeout(std::chrono::milliseconds _timeout) |
void | setSingleShot(bool singleshot) |
Static Public Functions¶
uint | precision() |
void | setPrecision(uint) |
Detailed Description¶
Class BTimer provides repetitive and single-shot timers with a minimum precision of 1 millisecond.
BTimer provides a easy to user programing interface for doing periodic jobs in your application. Just create a timer and set up the properties, then start it. You can change the properties of a timer at any time.
Example for a one second timer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <BUtils/BTimer>
#include <unistd.h>
#include <iostream>
void timerAction() {
std::cout << "I'm timer action." << std::endl;
}
int main() {
BUtils::BTimer timer;
timer.callOnTimeout(timerAction);
timer.setTimeout(1000);
timer.start();
// If timer object is destroyed, the timer event do not exist as well.
// Sleep to make the timeout event occur.
sleep(2);
}
|
BTimer’s timer event system is designed to work in multi-threads environments, but BTimer object itself doesn’t. Do not share a single BTimer object in threads, just create and use it in the same thread.
Accuracy and Timer Resolution¶
The accuracy of timers depends on the underlying operating system and hardware. On most system and hardware platform, system clock has an accuracy of microsecond is very common.
On most platforms, BTimer can support a resolution of 1 millisecond. But under heavy work load (such as many timer events) or high CPU usage (the timer event loop can’t wake up immediately) can make the precision not so accurate.
Member Type Documentation¶
-
enum
BTimerStatus
¶
This enum type is used when calling isActive() const
and setActive(bool)
.
Constant | Value | Description |
---|---|---|
BTimer::Active | 0 | Timer is activated. |
BTimer::Stop | 1 | Timer is stop. |
Property Documentation¶
-
bool
singleShot
¶
This property holds whether the timer triggers the interval action when interval timeout occurs.
If true, the interval action will be triggered after every interval period unless timeout occurs. The default value is false.
Access functions:¶
bool | isSingleShot() const |
void | setSingleShot(bool singleshot) |
-
std::chrono::milliseconds
interval
¶
This property holds the interval period of this timer. After every interval period, interval action will be triggered. The default value is 0, which means no interval.
Access functions:¶
uint32 | interval() const |
void | setInterval(std::chrono::milliseconds) |
-
std::chrono::milliseconds
timeout
¶
This property holds the expiration time of this timer. After timeout, the timer will be removed from the timer system until next start calls.
Note
The default value is the maximum value of unsigned int, which means “infinite” for this timer.
Access functions:¶
uint32 | timeout() const |
void | setTimeout(std::chrono::milliseconds) |
Member Function Documentation¶
Construct a BTimer object.
-
BTimer
::
~BTimer
()¶
Destruct a BTimer object.
-
bool
BTimer
::
operator>
(const BTimer &rtimer) const¶
Returns true if id is greater than rtimer’s id.
-
bool
BTimer
::
operator<
(const BTimer &rtimer) const¶
Returns true if id is less than rtimer’s id.
-
bool
BTimer
::
operator==
(const BTimer &rtimer) const¶
Returns true if id is equal to rtimer’s id.
-
void
BTimer
::
start
()¶
Start this timer; takes no effects if timeout is 0.
-
void
BTimer
::
stop
()¶
Stop this timer; takes no effects if this timer is expired(timeout occurs).
-
bool
BTimer
::
isActive
() const¶
Returns true if this timer is running.
-
bool
BTimer
::
isSingleShot
() const¶
Returns true if interval action is only triggered once.
-
int32
BTimer
::
id
() const¶
Returns the id of this timer.
-
uint32
BTimer
::
interval
() const¶
Returns the timeout interval of this timer in milliseconds.
-
uint32
BTimer
::
timeout
() const¶
Returns the timeout of this timer in milliseconds.
-
void
BTimer
::
reset
()¶
Reset all properties of this timer(except timer id) to default value and stop this timer.
-
void
BTimer
::
setActive
(bool _active)¶
Takes no effects calling by user.
-
void
BTimer
::
callOnInterval
(std::function<void()> timer_action)¶
Set the action that will be triggered after timeout interval.
-
void
BTimer
::
callOnTimeout
(std::function<void()> timer_action)¶
Set the action that will be triggered after timeout.
-
void
BTimer
::
setInterval
(uint32 _interval)¶
-
void
BTimer
::
setInterval
(std::chrono::milliseconds _interval)¶
Set the timeout interval in milliseconds. Default value is 0.
-
void
BTimer
::
setTimeout
(uint32 _timeout)¶
-
void
BTimer
::
setTimeout
(std::chrono::milliseconds _timeout)¶
Set the timeout in milliseconds. Default value is the maximum number of unsigned int.
-
void
BTimer
::
setSingleShot
(bool singleshot)¶
The interval action will be triggered only once if singleshot is true.
-
static uint
BTimer
::
precision
()¶
Returns the precision of timer in milliseconds. Default value is 1 millisecond.
-
static void
BTimer
::
setPrecision
(uint)¶
Set the timer precision in milliseconds.