119 lines
3.7 KiB
C++
119 lines
3.7 KiB
C++
/**
|
|
* @file include/ActivMiddleware/Misc/ChimeHelper.h
|
|
*
|
|
* @brief Helper class to run multiple functions off a single chime.
|
|
*/
|
|
|
|
#if (!defined ACTIV_MISC_CHIME_HELPER_H)
|
|
#define ACTIV_MISC_CHIME_HELPER_H
|
|
|
|
#include "ActivMiddleware/ActivFramework/Timer.h"
|
|
|
|
#include "ActivMiddleware/CxxYZ/deque.h"
|
|
#include "ActivMiddleware/Misc/Fmt.h"
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
/**
|
|
* @brief Helper class to run a series of functions off a single chime timer.
|
|
*/
|
|
class ChimeHelper
|
|
{
|
|
public:
|
|
typedef Timer::TickChimeCallbackFunction CallbackFunction;
|
|
|
|
/**
|
|
* @brief Constructor
|
|
*
|
|
* @param managedEndPoint the ManagedEndPoint to add the timer to.
|
|
* @param interval the interval of the timer.
|
|
*/
|
|
ChimeHelper(ManagedEndPoint &managedEndPoint, const Timer::Interval interval);
|
|
|
|
//! Add a callback function to the front of the list.
|
|
template <typename F>
|
|
void DoFirst(const F &fn);
|
|
|
|
//! Add a callback function to the end of the list.
|
|
template <typename F>
|
|
void DoLast(const F &fn);
|
|
|
|
private:
|
|
//! Chime callback
|
|
void OnChime(const uint64_t timestamp);
|
|
|
|
//! Create the chime timer if it does not already exist.
|
|
void CreateChimeTimer();
|
|
|
|
//! Create the chime timer if it does not already exist (implementation).
|
|
void CreateChimeTimerImpl(ManagedEndPoint &managedEndPoint, const Timer::Interval interval, const uint64_t dummy);
|
|
|
|
Timer m_timer; ///< The timer.
|
|
CxxYZ::deque<CallbackFunction> m_functionList; ///< The list of callback functions.
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline ChimeHelper::ChimeHelper(ManagedEndPoint &managedEndPoint, const Timer::Interval interval)
|
|
{
|
|
// HACk: Store a function to create the chime timer. The timer will not actually get created until the first task is added.
|
|
m_functionList.emplace_back(
|
|
CxxYZ::bind(&ChimeHelper::CreateChimeTimerImpl, this, CxxYZ::ref(managedEndPoint), interval, CxxYZ::_1));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template <typename F>
|
|
inline void ChimeHelper::DoFirst(const F &fn)
|
|
{
|
|
CreateChimeTimer();
|
|
m_functionList.emplace_front(fn);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template <typename F>
|
|
inline void ChimeHelper::DoLast(const F &fn)
|
|
{
|
|
CreateChimeTimer();
|
|
m_functionList.emplace_back(fn);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void ChimeHelper::CreateChimeTimer()
|
|
{
|
|
if (ACTIV_UNLIKELY(!m_timer.IsSet()))
|
|
{
|
|
// If the chime timer has not been created yet, the function list will just contain a bound version of
|
|
// CreateChimeTimerImpl().
|
|
assert(m_functionList.size() == 1);
|
|
m_functionList.front()(0);
|
|
m_functionList.clear();
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void ChimeHelper::CreateChimeTimerImpl(ManagedEndPoint &managedEndPoint, const Timer::Interval interval, const uint64_t dummy)
|
|
{
|
|
m_timer = Timer::CreateTickChime(managedEndPoint, Fmt::Format("ChimeHelper_{}", interval.m_intervalInMicroseconds), interval,
|
|
CxxYZ::bind(&ChimeHelper::OnChime, this, CxxYZ::_1));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void ChimeHelper::OnChime(uint64_t timestamp)
|
|
{
|
|
ACTIV_FOREACH(const CallbackFunction &function, m_functionList)
|
|
{
|
|
function(timestamp);
|
|
}
|
|
}
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_MISC_CHIME_HELPER_H)
|