Files
externals/linux/include/ActivMiddleware/ActivFramework/Timer.h
T
2023-12-15 13:15:08 -05:00

246 lines
6.5 KiB
C++

/**
* @file include/ActivMiddleware/ActivFramework/Timer.h
*
* @brief Timer header file.
*/
#if (!defined ACTIV_FRAMEWORK_TIMER_H)
#define ACTIV_FRAMEWORK_TIMER_H
#include "ActivMiddleware/ActivFramework/External.h"
#include "ActivMiddleware/ActivBase/FieldTypes/TimeCommon.h"
#include "ActivMiddleware/CxxYZ/functional.h"
#include ACTIV_BOOST_INCLUDE(optional/optional.hpp)
//! Activ namespace.
namespace Activ
{
// Forward declarations.
class DateTime;
class ManagedEndPoint;
class ManagedEndPointTimerService;
class Time;
//! Timer class.
class Timer
{
public:
//! Tick and countdown timer callback function definition.
typedef CxxYZ::function<void (uint64_t microsecondTimeStamp)> TickChimeCallbackFunction;
//! Local and utc timer and alarm callback function definition.
typedef CxxYZ::function<void (const DateTime &dateTime)> ClockChimeCallbackFunction;
//! Timer id definition.
typedef size_t Id;
//! Timer type definition.
typedef uint8_t Type;
//! Timer interval base class.
class Interval
{
protected:
Interval(const uint64_t intervalInMicroseconds) :
m_intervalInMicroseconds(intervalInMicroseconds) { }
public:
const uint64_t m_intervalInMicroseconds;
};
//! Timer interval helper.
template <typename T>
class IntervalHelper : public Interval
{
public:
explicit IntervalHelper(const uint64_t interval) :
Interval(T::MICROSECONDS_PER_UNIT * interval) {}
};
//! Supported timer intervals.
typedef IntervalHelper<TimeCommon::Microseconds> Microseconds;
typedef IntervalHelper<TimeCommon::Milliseconds> Milliseconds;
typedef IntervalHelper<TimeCommon::Seconds> Seconds;
typedef IntervalHelper<TimeCommon::Minutes> Minutes;
typedef IntervalHelper<TimeCommon::Hours> Hours;
typedef IntervalHelper<TimeCommon::Days> Days;
//! Default constructor.
ACTIV_FRAMEWORK_API
Timer();
//! Move constructor.
Timer(ACTIV_BOOST_RV_REF(Timer) rhs) :
m_pManagedEndPoint(rhs.m_pManagedEndPoint),
m_type(rhs.m_type),
m_interval(rhs.m_interval),
m_id(rhs.m_id)
{
rhs.m_id = Boost::none;
}
//! Move assignment.
Timer &operator=(ACTIV_BOOST_RV_REF(Timer) rhs)
{
if (this != CxxYZ::addressof(rhs))
{
Cancel();
m_pManagedEndPoint = rhs.m_pManagedEndPoint;
m_type = rhs.m_type;
m_interval = rhs.m_interval;
m_id = rhs.m_id;
rhs.m_id = Boost::none;
}
return *this;
}
//! Destructor.
ACTIV_FRAMEWORK_API
~Timer();
//! Is set.
bool IsSet() const;
//! Cancel.
ACTIV_FRAMEWORK_API
void Cancel();
/**
* @brief Create tick chime (repeating).
*
* @param managedEndPoint
* @param name (optional)
* @param interval, one of Microseconds, Milliseconds, Seconds, Minutes, Hours or Days.
* @param callbackFunction
*
* @throw std::exception
* @throw StatusCodeException
*/
ACTIV_FRAMEWORK_API
static Timer CreateTickChime(ManagedEndPoint &managedEndPoint, const String &name, const Interval interval,
const TickChimeCallbackFunction &callbackFunction);
/**
* @brief Create countdown (one shot).
*
* @param managedEndPoint
* @param name (optional)
* @param interval, one of Microseconds, Milliseconds, Seconds, Minutes, Hours or Days.
* @param callbackFunction
*
* @throw std::exception
* @throw StatusCodeException
*/
ACTIV_FRAMEWORK_API
static Timer CreateCountdown(ManagedEndPoint &managedEndPoint, const String &name, const Interval interval,
const TickChimeCallbackFunction &callbackFunction);
/**
* @brief Create local clock chime (repeating).
*
* @param managedEndPoint
* @param name (optional)
* @param startDateTime date and time of first tick.
* @param interval, one of Microseconds, Milliseconds, Seconds, Minutes, Hours or Days.
* @param callbackFunction
*
* @throw std::exception
* @throw StatusCodeException
*/
ACTIV_FRAMEWORK_API
static Timer CreateLocalClockChime(ManagedEndPoint &managedEndPoint, const String &name, const DateTime &startDateTime,
const Interval interval, const ClockChimeCallbackFunction &callbackFunction);
/**
* @brief Create daily chime (repeating).
*
* @param managedEndPoint
* @param name (optional)
* @param localTime local time of daily chime.
* @param callbackFunction
*
* @throw std::exception
* @throw StatusCodeException
*/
ACTIV_FRAMEWORK_API
static Timer CreateDailyChime(ManagedEndPoint &managedEndPoint, const String &name, const Time &localTime,
const ClockChimeCallbackFunction &callbackFunction);
/**
* @brief Create utc clock chime (repeating).
*
* @param managedEndPoint
* @param name (optional)
* @param startDateTime date and time of first tick.
* @param interval, one of Microseconds, Milliseconds, Seconds, Minutes, Hours or Days.
* @param callbackFunction
*
* @throw std::exception
* @throw StatusCodeException
*/
ACTIV_FRAMEWORK_API
static Timer CreateUtcClockChime(ManagedEndPoint &managedEndPoint, const String &name, const DateTime &startDateTime,
const Interval interval, const ClockChimeCallbackFunction &callbackFunction);
/**
* @brief Create local clock alarm (one shot).
*
* @param managedEndPoint
* @param name (optional)
* @param alarmDateTime date and time of the alarm.
* @param callbackFunction
*
* @throw std::exception
* @throw StatusCodeException
*/
ACTIV_FRAMEWORK_API
static Timer CreateLocalClockAlarm(ManagedEndPoint &managedEndPoint, const String &name, const DateTime &alarmDateTime,
const ClockChimeCallbackFunction &callbackFunction);
/**
* @brief Create utc clock alarm (one shot).
*
* @param managedEndPoint
* @param name (optional)
* @param alarmDateTime date and time of the alarm.
* @param callbackFunction
*
* @throw std::exception
* @throw StatusCodeException
*/
ACTIV_FRAMEWORK_API
static Timer CreateUtcClockAlarm(ManagedEndPoint &managedEndPoint, const String &name, const DateTime &alarmDateTime,
const ClockChimeCallbackFunction &callbackFunction);
private:
ACTIV_BOOST_MOVABLE_BUT_NOT_COPYABLE(Timer)
//! Constructor.
ACTIV_FRAMEWORK_API
Timer(ManagedEndPoint &managedEndPoint, const Type type, const Interval timerInterval);
ManagedEndPoint * m_pManagedEndPoint;
Type m_type; ///< The type of this timer.
uint64_t m_interval; ///< Timer interval in microseconds.
Boost::optional<Id> m_id; ///< The timer service id for this timer.
friend class ManagedEndPointTimerService;
};
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool Timer::IsSet() const
{
return bool(m_id);
}
} // namespace Activ
#endif // (!defined ACTIV_FRAMEWORK_TIMER_H)