AlexaClientSDK  1.26.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

#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 (std::shared_ptr< sdkInterfaces::timing::TimerDelegateFactoryInterface > timerDelegateFactory=avs::initialization::SDKPrimitivesProvider::getInstance() ->getTimerDelegateFactory())
 
 ~Timer ()
 
template<typename Rep , typename Period , typename Task , typename... Args>
bool start (const std::chrono::duration< Rep, Period > &delay, const std::chrono::duration< Rep, Period > &period, PeriodType periodType, size_t maxCount, Task task, Args &&... args)
 
template<typename Rep , typename Period , typename Task , typename... Args>
bool start (const std::chrono::duration< Rep, Period > &period, PeriodType periodType, size_t maxCount, Task task, Args &&... args)
 
template<typename Rep , typename Period , typename Task , typename... Args>
auto start (const std::chrono::duration< Rep, Period > &delay, Task task, Args &&... args) -> std::future< decltype(task(args...))>
 
void stop ()
 
bool isActive () const
 

Static Public Member Functions

static size_t getForever ()
 Static member function to get FOREVER. More...
 
static std::string getTag ()
 Static member function to get TAG. More...
 

Static Public Attributes

static const size_t FOREVER = 0
 

Detailed Description

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 ( std::shared_ptr< sdkInterfaces::timing::TimerDelegateFactoryInterface timerDelegateFactory = avs::initialization::SDKPrimitivesProvider::getInstance()->getTimerDelegateFactory())

Contructs a Timer.

Parameters
timerDelegateFactoryA factory to create the TimerDelegate.

◆ ~Timer()

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

Destructs a Timer.

Member Function Documentation

◆ getForever()

static size_t alexaClientSDK::avsCommon::utils::timing::Timer::getForever ( )
inlinestatic

Static member function to get FOREVER.

◆ getTag()

static std::string alexaClientSDK::avsCommon::utils::timing::Timer::getTag ( )
inlinestatic

Static member function to get TAG.

◆ isActive()

bool alexaClientSDK::avsCommon::utils::timing::Timer::isActive ( ) const

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 Rep , typename Period , typename Task , typename... Args>
bool alexaClientSDK::avsCommon::utils::timing::Timer::start ( const std::chrono::duration< Rep, Period > &  delay,
const std::chrono::duration< Rep, Period > &  period,
PeriodType  periodType,
size_t  maxCount,
Task  task,
Args &&...  args 
)

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
RepA type for measuring 'ticks' in a generic std::chrono::duration.
PeriodA type for representing the number of ticks per second in a generic std::chrono::duration.
TaskThe type of task to execute.
ArgsThe argument types for the task to execute.
Parameters
delayThe non-negative time to wait before making the first task call. Negative values will cause this function to return false.
periodThe non-negative time to wait between subsequent task calls. Negative values will cause this function to return false.
periodTypeThe type of period to use when making subsequent task calls.
maxCountThe desired number of times to call task. Timer::getForever() means to call forever until stop() is called. Note that fewer than maxCount calls may occur if periodType is PeriodType::ABSOLUTE and the task runtime exceeds period.
taskA callable type representing a task.
argsThe arguments to call the task with.
Returns
true if the timer started, else false.

◆ start() [2/3]

template<typename Rep , typename Period , typename Task , typename... Args>
bool alexaClientSDK::avsCommon::utils::timing::Timer::start ( const std::chrono::duration< Rep, Period > &  period,
PeriodType  periodType,
size_t  maxCount,
Task  task,
Args &&...  args 
)

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
RepA type for measuring 'ticks' in a generic std::chrono::duration.
PeriodA type for representing the number of ticks per second in a generic std::chrono::duration.
TaskThe type of task to execute.
ArgsThe argument types for the task to execute.
Parameters
periodThe non-negative time to wait before each task call. Negative values will cause this function to return false.
periodTypeThe type of period to use when making subsequent task calls.
maxCountThe desired number of times to call task. Timer::getForever() means to call forever until stop() is called. Note that fewer than maxCount calls may occur if periodType is PeriodType::ABSOLUTE and the task runtime exceeds period.
taskA callable type representing a task.
argsThe arguments to call task with.
Returns
true if the timer started, else false.

◆ start() [3/3]

template<typename Rep , typename Period , typename Task , typename... Args>
auto alexaClientSDK::avsCommon::utils::timing::Timer::start ( const std::chrono::duration< Rep, Period > &  delay,
Task  task,
Args &&...  args 
) -> std::future<decltype(task(args...))>

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 paramters when calling start().

Template Parameters
RepA type for measuring 'ticks' in a generic std::chrono::duration.
PeriodA type for representing the number of ticks per second in a generic std::chrono::duration.
TaskThe type of task to execute.
ArgsThe argument types for the task to execute.
Parameters
delayThe non-negative time to wait before calling task. Negative values will cause this function to return an invalid future.
taskA callable type representing a task.
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 stop() is called before task is called.

◆ stop()

void alexaClientSDK::avsCommon::utils::timing::Timer::stop ( )

Stops the Timer (if running). This will not interrupt an active call to the task, but will prevent any subequent 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

const 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 files:

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