251 lines
7.8 KiB
C++
251 lines
7.8 KiB
C++
/**
|
|
* @file include/ActivMiddleware/ActivFramework/ThreadTimer.h
|
|
*
|
|
* @brief ThreadTimer header file.
|
|
*/
|
|
|
|
#if (!defined ACTIV_FRAMEWORK_THREAD_TIMER_H)
|
|
#define ACTIV_FRAMEWORK_THREAD_TIMER_H
|
|
|
|
#include "ActivMiddleware/ActivFramework/External.h"
|
|
|
|
#include "ActivMiddleware/ActivBase/FieldTypes/Time.h"
|
|
#include "ActivMiddleware/CxxYZ/functional.h"
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
// Forward declaration.
|
|
class ManagedEndPoint;
|
|
|
|
//! ThreadTimer namespace.
|
|
namespace ThreadTimer
|
|
{
|
|
|
|
class CountdownTimer;
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! Timer callback function definition. The timestamp is that returned by IntervalTimer::GetMicrosecondTimeStamp().
|
|
typedef CxxYZ::function<void (uint64_t microsecondTimeStamp)> CallbackFunction;
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
typedef size_t TimerId;
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! Impl namespace.
|
|
namespace Impl
|
|
{
|
|
|
|
class TimerInterval
|
|
{
|
|
protected:
|
|
TimerInterval(const uint64_t intervalInMicroseconds) :
|
|
m_intervalInMicroseconds(intervalInMicroseconds)
|
|
{
|
|
}
|
|
|
|
public:
|
|
const uint64_t m_intervalInMicroseconds;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! Thread timer base class.
|
|
template<bool IS_COUNTDOWN_TIMER>
|
|
class ThreadTimerBase : private Boost::noncopyable
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param managedEndPoint the ManagedEndPoint to add a timer to.
|
|
* @param name the name of the timer (for logging).
|
|
* @param timerInterval the interval of the timer in microseconds.
|
|
* @param callbackFunction the function to call when timer expires.
|
|
*
|
|
* @throw std::exception
|
|
* @throw StatusCodeException
|
|
*/
|
|
ACTIV_FRAMEWORK_API
|
|
ThreadTimerBase(ManagedEndPoint &managedEndPoint, const String &name, const TimerInterval timerInterval,
|
|
const CallbackFunction &callbackFunction);
|
|
|
|
//! Destructor.
|
|
ACTIV_FRAMEWORK_API
|
|
~ThreadTimerBase();
|
|
|
|
//! Cancel.
|
|
ACTIV_FRAMEWORK_API
|
|
void Cancel();
|
|
|
|
private:
|
|
ManagedEndPoint & m_managedEndPoint;
|
|
const uint64_t m_interval; ///< Timer interval in microseconds.
|
|
Boost::optional<TimerId> m_timerId; ///< The timer service id for this timer.
|
|
|
|
friend class ThreadTimer::CountdownTimer;
|
|
};
|
|
|
|
} // namespace Impl
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
class Microseconds : public Impl::TimerInterval
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param interval interval in microseconds between chimes.
|
|
*/
|
|
explicit Microseconds(const uint64_t interval) :
|
|
Impl::TimerInterval(interval)
|
|
{
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
class Milliseconds : public Impl::TimerInterval
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param interval interval in milliseconds between chimes.
|
|
*/
|
|
explicit Milliseconds(const uint64_t interval) :
|
|
Impl::TimerInterval(interval * 1000)
|
|
{
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
class Seconds : public Impl::TimerInterval
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param interval interval in seconds between chimes.
|
|
*/
|
|
explicit Seconds(const uint64_t interval) :
|
|
Impl::TimerInterval(interval * Time::Milliseconds::NUMBER_PER_SECOND * 1000)
|
|
{
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
class Minutes : public Impl::TimerInterval
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param interval interval in minutes between chimes.
|
|
*/
|
|
explicit Minutes(const uint64_t interval) :
|
|
Impl::TimerInterval(interval * Time::Milliseconds::NUMBER_PER_MINUTE * 1000)
|
|
{
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
class Hours : public Impl::TimerInterval
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param interval interval in hours between chimes.
|
|
*/
|
|
explicit Hours(const uint64_t interval) :
|
|
Impl::TimerInterval(interval * Time::Milliseconds::NUMBER_PER_HOUR * 1000)
|
|
{
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! Countdown timer.
|
|
class CountdownTimer : public Impl::ThreadTimerBase<true>
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param managedEndPoint the ManagedEndPoint to add a timer to.
|
|
* @param name the name of the timer (for logging).
|
|
* @param timerInterval the interval of the timer in milliseconds.
|
|
* @param callbackFunction the function to call when timer expires.
|
|
*
|
|
* @throw std::exception
|
|
* @throw StatusCodeException
|
|
*/
|
|
template<typename T>
|
|
CountdownTimer(ManagedEndPoint &managedEndPoint, const String &name, const Impl::TimerInterval timerInterval,
|
|
const T &callbackFunction) :
|
|
Impl::ThreadTimerBase<true>(managedEndPoint, name, timerInterval,
|
|
// Note by value for callbackFunction.
|
|
CxxYZ::bind(&CountdownTimer::InvokeCallback<T>, this, typename BindWrapper<T>::type(callbackFunction), CxxYZ::_1))
|
|
{
|
|
}
|
|
|
|
private:
|
|
template<typename T>
|
|
void InvokeCallback(const T &callbackFunction, const uint64_t timestamp)
|
|
{
|
|
// A countdown timer is implicitly removed from the ManagedEndPointTimerService.
|
|
// Hence in case the callback deletes this object (or Cancel()s it), we need to clear down m_timerId first to avoid
|
|
// an invalid timer removal in our destructor.
|
|
m_timerId = Boost::none;
|
|
callbackFunction(timestamp);
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! Repeating timer.
|
|
class ChimeTimer : public Impl::ThreadTimerBase<false>
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param managedEndPoint the ManagedEndPoint to add a timer to.
|
|
* @param name the name of the timer (for logging).
|
|
* @param timerInterval the interval of the timer in milliseconds.
|
|
* @param callbackFunction the function to call when timer expires.
|
|
*
|
|
* @throw std::exception
|
|
* @throw StatusCodeException
|
|
*/
|
|
template<typename T>
|
|
ChimeTimer(ManagedEndPoint &managedEndPoint, const String &name, const Impl::TimerInterval timerInterval,
|
|
const T &callbackFunction) :
|
|
Impl::ThreadTimerBase<false>(managedEndPoint, name, timerInterval, callbackFunction)
|
|
{
|
|
}
|
|
};
|
|
|
|
} // namespace ThreadTimer
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_FRAMEWORK_THREAD_TIMER_H)
|