Files

117 lines
3.8 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/ThreadTimer.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 ThreadTimer::CallbackFunction CallbackFunction;
/**
* @brief Constructor
*
* @param managedEndPoint the ManagedEndPoint to add the timer to.
* @param timerInterval the interval of the timer.
*/
ChimeHelper(ManagedEndPoint &managedEndPoint, const ThreadTimer::Impl::TimerInterval timerInterval);
//! 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 ThreadTimer::Impl::TimerInterval timerInterval, const uint64_t dummy);
CxxYZ::unique_ptr<ThreadTimer::ChimeTimer> m_pChimeTimer; ///< The chime timer.
CxxYZ::deque<CallbackFunction> m_functionList; ///< The list of callback functions.
};
// ---------------------------------------------------------------------------------------------------------------------------------
inline ChimeHelper::ChimeHelper(ManagedEndPoint &managedEndPoint, const ThreadTimer::Impl::TimerInterval timerInterval)
{
// 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), timerInterval, 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_pChimeTimer))
{
// 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 ThreadTimer::Impl::TimerInterval timerInterval, const uint64_t dummy)
{
m_pChimeTimer.reset(new ThreadTimer::ChimeTimer(managedEndPoint, Fmt::Format("ChimeHelper_{}", timerInterval.m_intervalInMicroseconds),
timerInterval, 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)