AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
alexaClientSDK::avsCommon::utils::timing::Timer Class Reference

Timer to schedule task for delayed and periodic execution. More...

#include <Timer.h>

Public Types

enum  PeriodType { PeriodType::ABSOLUTE, PeriodType::RELATIVE }
 Specifies different ways to apply the period of a recurring task. More...
 

Public Member Functions

 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 Public Member Functions

static size_t getForever () noexcept
 Static member function to get FOREVER. More...
 

Static Public Attributes

static constexpr size_t FOREVER = 0
 

Detailed Description

Timer to schedule task for delayed and periodic execution.

A Timer is used to schedule a callable type to run in the future.

Member Enumeration Documentation

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

Constructor & Destructor Documentation

◆ Timer()

alexaClientSDK::avsCommon::utils::timing::Timer::Timer ( const std::shared_ptr< sdkInterfaces::timing::TimerDelegateFactoryInterface > &  timerDelegateFactory = avs::initialization::SDKPrimitivesProvider::getInstance() ->getTimerDelegateFactory())
noexcept

Constructs a Timer.

Parameters
[in]timerDelegateFactoryA factory to create the TimerDelegate. By if the parameter is nullptr, implementation will use TimerDelegateFactory.

◆ ~Timer()

alexaClientSDK::avsCommon::utils::timing::Timer::~Timer ( )
noexcept

Destructs a Timer.

Member Function Documentation

◆ 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
DelayInitial delay type. This can be any specialization of std::chrono::duration convertible to std::chrono::nanoseconds.
PeriodPeriod type. This can be any specialization of std::chrono::duration convertible to std::chrono::nanoseconds.
TaskThe type of task to execute.
ArgsThe argument types for the task to execute.
Parameters
[in]delayThe non-negative time to wait before making the first task call. Negative values will cause this function to return false.
[in]periodThe non-negative time to wait between subsequent task calls. Negative values will cause this function to return false.
[in]periodTypeThe type of period to use when making subsequent task calls.
[in]maxCountThe 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]taskA callable type representing a task.
[in]argsThe 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
PeriodPeriod and initial delay type. This can be any specialization of std::chrono::duration convertible to std::chrono::nanoseconds.
TaskThe type of task to execute. This must be callable with args parameters.
ArgsThe argument types for the task to execute.
Parameters
[in]periodThe non-negative time to wait before each task call. Negative values will cause this function to return false.
[in]periodTypeThe type of period to use when making subsequent task calls.
[in]maxCountThe 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]taskA callable type representing a task.
[in]argsThe 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
DelayDelay type. This can be any specialization of std::chrono::duration convertible to std::chrono::nanoseconds.
TaskThe type of task to execute.
ArgsThe argument types for the task to execute.
Parameters
[in]delayThe non-negative time to wait before calling task. Negative values will cause this function to return an invalid future.
[in]taskA callable type representing a task.
[in]argsThe 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.

Member Data Documentation

◆ 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:

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0