Timer to schedule task for delayed and periodic execution.
More...
#include <Timer.h>
|
| Timer (const std::shared_ptr< sdkInterfaces::timing::TimerDelegateFactoryInterface > &timerDelegateFactory=avs::initialization::SDKPrimitivesProvider::getInstance() ->getTimerDelegateFactory()) noexcept |
| Constructs a Timer . More...
|
|
| ~Timer () noexcept |
| Destructs a Timer . More...
|
|
template<typename Delay , typename Period , typename Task , typename... Args> |
bool | start (Delay &&delay, Period &&period, PeriodType periodType, size_t maxCount, Task &&task, Args &&... args) noexcept |
| Submit a callable type for periodic execution. More...
|
|
template<typename Period , typename Task , typename... Args> |
bool | start (Period &&period, PeriodType periodType, size_t maxCount, Task &&task, Args &&... args) noexcept |
| Submit a callable type for periodic execution. More...
|
|
template<typename Delay , typename Task , typename... Args> |
auto | start (Delay &&delay, Task &&task, Args &&... args) noexcept -> std::future< decltype(task(args...))> |
| Submit a callable type for single execution with future result. More...
|
|
void | stop () noexcept |
| Stop the timer. More...
|
|
bool | isActive () const noexcept |
| Check if timer is active. More...
|
|
|
static constexpr size_t | FOREVER = 0 |
|
Timer to schedule task for delayed and periodic execution.
A Timer
is used to schedule a callable type to run in the future.
◆ PeriodType
Specifies different ways to apply the period of a recurring task.
Enumerator |
---|
ABSOLUTE | The period specifies the time from the start of one task call to the start of the next task call. This period type ensures task calls occur on a predictable cadence.
- Note
- A timer makes one task call at a time, so if a task call takes more than one period to execute, the subsequent calls which would have occured while the task was still executing will be skipped, and the next call will not occur until the next period-multiple after the original task call completes.
|
RELATIVE | The period specifies the time from the end of one task call to the start of the next task call. This period type ensures a specific amount of idle time between task calls.
|
◆ Timer()
Constructs a Timer
.
- Parameters
-
◆ ~Timer()
alexaClientSDK::avsCommon::utils::timing::Timer::~Timer |
( |
| ) |
|
|
noexcept |
◆ getForever()
static size_t alexaClientSDK::avsCommon::utils::timing::Timer::getForever |
( |
| ) |
|
|
inlinestaticnoexcept |
Static member function to get FOREVER.
◆ isActive()
bool alexaClientSDK::avsCommon::utils::timing::Timer::isActive |
( |
| ) |
const |
|
noexcept |
Check if timer is active.
Reports whether the Timer
is active. A timer is considered active if it is waiting to start a call to the task, or if a call to the task is in progress. A timer is only considered inactive if it has not been started, if all requested/scheduled calls to the task have completed, or after a call to stop()
.
- Returns
true
if the Timer
is active, else false
.
◆ start() [1/3]
template<typename Delay , typename Period , typename Task , typename... Args>
bool alexaClientSDK::avsCommon::utils::timing::Timer::start |
( |
Delay && |
delay, |
|
|
Period && |
period, |
|
|
PeriodType |
periodType, |
|
|
size_t |
maxCount, |
|
|
Task && |
task, |
|
|
Args &&... |
args |
|
) |
| |
|
noexcept |
Submit a callable type for periodic execution.
Submits a callable type (function, lambda expression, bind expression, or another function object) to be executed after an initial delay, and then called repeatedly on a fixed time schedule. A Timer
instance manages only one running Timer
at a time; calling start()
on an already-running Timer
will fail. Note that the template parameters are typically inferred from the function paramters when calling start()
.
- Template Parameters
-
Delay | Initial delay type. This can be any specialization of std::chrono::duration convertible to std::chrono::nanoseconds. |
Period | Period type. This can be any specialization of std::chrono::duration convertible to std::chrono::nanoseconds. |
Task | The type of task to execute. |
Args | The argument types for the task to execute. |
- Parameters
-
[in] | delay | The non-negative time to wait before making the first task call. Negative values will cause this function to return false. |
[in] | period | The non-negative time to wait between subsequent task calls. Negative values will cause this function to return false. |
[in] | periodType | The type of period to use when making subsequent task calls. |
[in] | maxCount | The desired number of times to call task. Timer::getForever() means to call forever until Timer::stop() is called. Note that fewer than maxCount calls may occur if periodType is PeriodType::ABSOLUTE and the task runtime exceeds period. |
[in] | task | A callable type representing a task. |
[in] | args | The arguments to call the task with. |
- Returns
true
if the timer started, else false
.
◆ start() [2/3]
template<typename Period , typename Task , typename... Args>
bool alexaClientSDK::avsCommon::utils::timing::Timer::start |
( |
Period && |
period, |
|
|
PeriodType |
periodType, |
|
|
size_t |
maxCount, |
|
|
Task && |
task, |
|
|
Args &&... |
args |
|
) |
| |
|
noexcept |
Submit a callable type for periodic execution.
Submits a callable type (function, lambda expression, bind expression, or another function object) to be executed repeatedly on a fixed time schedule. A Timer
instance manages only one running Timer
at a time; calling start()
on an already-running Timer
will fail. Note that the template parameters are typically inferred from the function paramters when calling start()
.
- Template Parameters
-
Period | Period and initial delay type. This can be any specialization of std::chrono::duration convertible to std::chrono::nanoseconds. |
Task | The type of task to execute. This must be callable with args parameters. |
Args | The argument types for the task to execute. |
- Parameters
-
[in] | period | The non-negative time to wait before each task call. Negative values will cause this function to return false. |
[in] | periodType | The type of period to use when making subsequent task calls. |
[in] | maxCount | The desired number of times to call task. Timer::getForever() means to call forever until Timer::stop() is called. Note that fewer than maxCount calls may occur if periodType is PeriodType::ABSOLUTE and the task runtime exceeds period. |
[in] | task | A callable type representing a task. |
[in] | args | The arguments to call task with. |
- Returns
true
if the timer started, else false
.
◆ start() [3/3]
template<typename Delay , typename Task , typename... Args>
auto alexaClientSDK::avsCommon::utils::timing::Timer::start |
( |
Delay && |
delay, |
|
|
Task && |
task, |
|
|
Args &&... |
args |
|
) |
| -> std::future<decltype(task(args...))> |
|
noexcept |
Submit a callable type for single execution with future result.
Submits a callable type (function, lambda expression, bind expression, or another function object) to be executed once, after the specified duration. A Timer
instance manages only one running Timer
at a time; calling start()
on an already-running Timer
will fail. Note that the template parameters are typically inferred from the function parameters when calling start()
.
- Template Parameters
-
Delay | Delay type. This can be any specialization of std::chrono::duration convertible to std::chrono::nanoseconds. |
Task | The type of task to execute. |
Args | The argument types for the task to execute. |
- Parameters
-
[in] | delay | The non-negative time to wait before calling task. Negative values will cause this function to return an invalid future. |
[in] | task | A callable type representing a task. |
[in] | args | The arguments to call task with. |
- Returns
- A valid
std::future
for the return value of task
if the timer started, else an invalid std::future
. Note that the promise will be broken if Timer::stop() is called before task is called.
◆ stop()
void alexaClientSDK::avsCommon::utils::timing::Timer::stop |
( |
| ) |
|
|
noexcept |
Stop the timer.
Stops the Timer
(if running). This will not interrupt an active call to the task, but will prevent any subsequent calls to the task. If stop()
is called while the task is executing, this function will block until the task completes.
- Note
- In the special case that
stop()
is called from inside the task function, stop()
will still prevent any subsequent calls to the task, but will not block as described above.
◆ FOREVER
constexpr size_t alexaClientSDK::avsCommon::utils::timing::Timer::FOREVER = 0 |
|
static |
Value for start()'s
maxCount
parameter which indicates that the Timer
should continue firing indefinitely.
The documentation for this class was generated from the following file:
- /workplace/avs-device-sdk/AVSCommon/Utils/include/AVSCommon/Utils/Timing/Timer.h
AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0