|
VLink 2.0.0
A high-performance communication middleware
|
Event-loop-driven repeating or one-shot timer. 更多...
#include <timer.h>
Public 类型 | |
| using | Callback = std::function<void()> |
| Callback type invoked on each timer tick. | |
Public 成员函数 | |
| Timer () | |
| Constructs a detached timer with no message loop, interval or callback. | |
| Timer (class MessageLoop *message_loop) | |
Constructs a timer attached to message_loop with no interval or callback yet. | |
| Timer (class MessageLoop *message_loop, uint32_t interval_ms, int32_t loop_count=kInfinite, Callback &&callback=nullptr) | |
| Constructs and fully configures a timer attached to a loop. | |
| Timer (uint32_t interval_ms, int32_t loop_count=kInfinite, Callback &&callback=nullptr) | |
| Constructs a timer without attaching it to a loop. | |
| ~Timer () | |
| Destructor. Stops the timer and detaches it from the loop. | |
| bool | is_active () const |
Returns true if the timer is currently running (armed and has remaining ticks). | |
| bool | is_strict () const |
Returns true if strict mode is enabled. | |
| uint32_t | get_interval () const |
| Returns the current tick interval in milliseconds. | |
| int32_t | get_loop_count () const |
| Returns the configured total loop count. | |
| int32_t | get_remain_loop_count () const |
| Returns the number of remaining ticks before the timer stops automatically. | |
| uint64_t | get_invoke_count () const |
Returns the total number of times the callback has been invoked since start(). | |
| uint16_t | get_priority () const |
| Returns the task dispatch priority. | |
| class MessageLoop * | get_message_loop () const |
Returns the MessageLoop this timer is attached to. | |
| bool | attach (class MessageLoop *message_loop) |
Attaches the timer to a MessageLoop. | |
| bool | detach () |
Detaches the timer from its MessageLoop without stopping it. | |
| void | start (Callback &&callback=nullptr) |
| Arms and starts the timer. | |
| void | restart () |
| Resets the countdown to zero and continues firing. | |
| void | stop () |
| Disarms the timer without destroying it. | |
| void | set_strict (bool strict) |
| Enables or disables strict (catch-up) firing mode. | |
| void | set_interval (uint32_t interval_ms) |
| Changes the tick interval. | |
| void | set_loop_count (int32_t loop_count) |
| Changes the total number of ticks. | |
| void | set_callback (Callback &&callback) |
| Replaces the callback invoked on each tick. | |
| void | set_priority (uint16_t priority) |
| Sets the dispatch priority for the timer task. | |
静态 Public 成员函数 | |
| static bool | call_once (class MessageLoop *message_loop, uint32_t interval_ms, Callback &&callback, uint16_t priority=0) |
Posts a one-shot timer to a MessageLoop without creating a Timer object. | |
静态 Public 属性 | |
| static constexpr int | kInfinite {-1} |
| Sentinel loop count meaning repeat indefinitely. | |
友元 | |
| class | MessageLoop |
Event-loop-driven repeating or one-shot timer.
Callbacks are dispatched on the thread of the associated MessageLoop.
| using vlink::Timer::Callback = std::function<void()> |
Callback type invoked on each timer tick.
|
explicit |
Constructs a detached timer with no message loop, interval or callback.
Must be configured with attach(), set_interval() and set_callback() before start() can be called.
|
explicit |
Constructs a timer attached to message_loop with no interval or callback yet.
| message_loop | The MessageLoop to deliver callbacks on. |
|
explicit |
Constructs and fully configures a timer attached to a loop.
| message_loop | The MessageLoop to deliver callbacks on. |
| interval_ms | Tick interval in milliseconds. When zero, falls back to kMinInterval (10000). |
| loop_count | Number of times to fire (kInfinite for indefinite repeat). Default: kInfinite. |
| callback | Callback to invoke on each tick. Default: nullptr. |
| vlink::Timer::~Timer | ( | ) |
Destructor. Stops the timer and detaches it from the loop.
| bool vlink::Timer::attach | ( | class MessageLoop * | message_loop | ) |
Attaches the timer to a MessageLoop.
Must be called before start() if the timer was constructed without a loop. If the timer is already attached to a different loop it is detached first.
| message_loop | Loop to attach to. |
true on success.
|
static |
Posts a one-shot timer to a MessageLoop without creating a Timer object.
The timer fires exactly once after interval_ms milliseconds and is then destroyed. Useful for simple delayed tasks where lifetime management of a Timer object is inconvenient.
| message_loop | The MessageLoop to deliver the callback on. |
| interval_ms | Delay in milliseconds. |
| callback | Callback to invoke once. |
| priority | Priority of the timer task (default: 0). |
true if the timer was successfully posted. | bool vlink::Timer::detach | ( | ) |
Detaches the timer from its MessageLoop without stopping it.
After detaching the timer stops firing. It can be re-attached to a different loop.
true on success.
|
nodiscard |
Returns the current tick interval in milliseconds.
|
nodiscard |
Returns the total number of times the callback has been invoked since start().
|
nodiscard |
Returns the configured total loop count.
kInfinite (-1) for indefinite repeat.
|
nodiscard |
Returns the MessageLoop this timer is attached to.
nullptr if detached.
|
nodiscard |
Returns the task dispatch priority.
Relevant only when the timer is attached to a kPriorityType MessageLoop.
|
nodiscard |
Returns the number of remaining ticks before the timer stops automatically.
kInfinite if repeating indefinitely.
|
nodiscard |
Returns true if the timer is currently running (armed and has remaining ticks).
true if the timer is active.
|
nodiscard |
Returns true if strict mode is enabled.
In strict mode, missed ticks (due to a busy loop) are fired immediately on the next loop iteration to compensate for schedule drift.
true if strict mode is on. | void vlink::Timer::restart | ( | ) |
Resets the countdown to zero and continues firing.
Equivalent to stop() followed by start() but also resets get_invoke_count().
| void vlink::Timer::set_callback | ( | Callback && | callback | ) |
Replaces the callback invoked on each tick.
| callback | New callback. |
| void vlink::Timer::set_interval | ( | uint32_t | interval_ms | ) |
| void vlink::Timer::set_loop_count | ( | int32_t | loop_count | ) |
Changes the total number of ticks.
| loop_count | New loop count. Use kInfinite for indefinite repeat. |
| void vlink::Timer::set_priority | ( | uint16_t | priority | ) |
Sets the dispatch priority for the timer task.
Only effective when the associated loop is of type kPriorityType.
| priority | Priority value (higher = earlier dispatch). |
| void vlink::Timer::set_strict | ( | bool | strict | ) |
Enables or disables strict (catch-up) firing mode.
When true, if the loop was busy and one or more ticks were missed, they are fired immediately on the next loop iteration. Default is false.
| strict | true to enable catch-up firing. |
| void vlink::Timer::start | ( | Callback && | callback = nullptr | ) |
Arms and starts the timer.
If callback is provided it replaces the previously set callback. The first tick fires after one full interval.
| callback | Optional replacement callback. Default: nullptr (keep existing). |
| void vlink::Timer::stop | ( | ) |
Disarms the timer without destroying it.
After stop(), is_active() returns false. Call start() to re-arm.
|
friend |
|
staticconstexpr |
Sentinel loop count meaning repeat indefinitely.