VLink 2.0.0
A high-performance communication middleware
载入中...
搜索中...
未找到
vlink::Timer类 参考final

Event-loop-driven repeating or one-shot timer. 更多...

#include <timer.h>

vlink::Timer 的协作图:

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 MessageLoopget_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.

成员类型定义说明

◆ Callback

using vlink::Timer::Callback = std::function<void()>

Callback type invoked on each timer tick.

构造及析构函数说明

◆ Timer() [1/4]

vlink::Timer::Timer ( )
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.

◆ Timer() [2/4]

vlink::Timer::Timer ( class MessageLoop * message_loop)
explicit

Constructs a timer attached to message_loop with no interval or callback yet.

参数
message_loopThe MessageLoop to deliver callbacks on.
函数调用图:

◆ Timer() [3/4]

vlink::Timer::Timer ( class MessageLoop * message_loop,
uint32_t interval_ms,
int32_t loop_count = kInfinite,
Callback && callback = nullptr )
explicit

Constructs and fully configures a timer attached to a loop.

参数
message_loopThe MessageLoop to deliver callbacks on.
interval_msTick interval in milliseconds. When zero, falls back to kMinInterval (10000).
loop_countNumber of times to fire (kInfinite for indefinite repeat). Default: kInfinite.
callbackCallback to invoke on each tick. Default: nullptr.
函数调用图:

◆ Timer() [4/4]

vlink::Timer::Timer ( uint32_t interval_ms,
int32_t loop_count = kInfinite,
Callback && callback = nullptr )
explicit

Constructs a timer without attaching it to a loop.

Call attach() before start().

参数
interval_msTick interval in milliseconds.
loop_countNumber of times to fire. Default: kInfinite.
callbackCallback to invoke on each tick. Default: nullptr.

◆ ~Timer()

vlink::Timer::~Timer ( )

Destructor. Stops the timer and detaches it from the loop.

成员函数说明

◆ attach()

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_loopLoop to attach to.
返回
true on success.
函数调用图:

◆ call_once()

bool vlink::Timer::call_once ( class MessageLoop * message_loop,
uint32_t interval_ms,
Callback && callback,
uint16_t priority = 0 )
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_loopThe MessageLoop to deliver the callback on.
interval_msDelay in milliseconds.
callbackCallback to invoke once.
priorityPriority of the timer task (default: 0).
返回
true if the timer was successfully posted.
函数调用图:
这是这个函数的调用关系图:

◆ detach()

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.

◆ get_interval()

uint32_t vlink::Timer::get_interval ( ) const
nodiscard

Returns the current tick interval in milliseconds.

返回
Interval in milliseconds.

◆ get_invoke_count()

uint64_t vlink::Timer::get_invoke_count ( ) const
nodiscard

Returns the total number of times the callback has been invoked since start().

返回
Cumulative invocation count.

◆ get_loop_count()

int32_t vlink::Timer::get_loop_count ( ) const
nodiscard

Returns the configured total loop count.

返回
Loop count, or kInfinite (-1) for indefinite repeat.

◆ get_message_loop()

class MessageLoop * vlink::Timer::get_message_loop ( ) const
nodiscard

Returns the MessageLoop this timer is attached to.

返回
Pointer to the associated loop, or nullptr if detached.
函数调用图:

◆ get_priority()

uint16_t vlink::Timer::get_priority ( ) const
nodiscard

Returns the task dispatch priority.

Relevant only when the timer is attached to a kPriorityType MessageLoop.

返回
Priority value (higher = dispatched sooner).

◆ get_remain_loop_count()

int32_t vlink::Timer::get_remain_loop_count ( ) const
nodiscard

Returns the number of remaining ticks before the timer stops automatically.

返回
Remaining tick count, or kInfinite if repeating indefinitely.

◆ is_active()

bool vlink::Timer::is_active ( ) const
nodiscard

Returns true if the timer is currently running (armed and has remaining ticks).

返回
true if the timer is active.

◆ is_strict()

bool vlink::Timer::is_strict ( ) const
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.

◆ restart()

void vlink::Timer::restart ( )

Resets the countdown to zero and continues firing.

Equivalent to stop() followed by start() but also resets get_invoke_count().

◆ set_callback()

void vlink::Timer::set_callback ( Callback && callback)

Replaces the callback invoked on each tick.

参数
callbackNew callback.

◆ set_interval()

void vlink::Timer::set_interval ( uint32_t interval_ms)

Changes the tick interval.

Takes effect on the next start() or restart() call. When set to zero, the interval falls back to kMinInterval (10000).

参数
interval_msNew interval in milliseconds.

◆ set_loop_count()

void vlink::Timer::set_loop_count ( int32_t loop_count)

Changes the total number of ticks.

参数
loop_countNew loop count. Use kInfinite for indefinite repeat.

◆ set_priority()

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.

参数
priorityPriority value (higher = earlier dispatch).

◆ set_strict()

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.

参数
stricttrue to enable catch-up firing.

◆ start()

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.

参数
callbackOptional replacement callback. Default: nullptr (keep existing).

◆ stop()

void vlink::Timer::stop ( )

Disarms the timer without destroying it.

After stop(), is_active() returns false. Call start() to re-arm.

◆ MessageLoop

friend class MessageLoop
friend

类成员变量说明

◆ kInfinite

int vlink::Timer::kInfinite {-1}
staticconstexpr

Sentinel loop count meaning repeat indefinitely.


该类的文档由以下文件生成: