110 lines
3.1 KiB
C++
110 lines
3.1 KiB
C++
/**
|
|
* @file include/ActivMiddleware/ActivBase/IntervalTimer.h
|
|
*
|
|
* @brief Header file for the interval timer class.
|
|
*/
|
|
|
|
#if (!defined ACTIV_BASE_INTERVAL_TIMER_H)
|
|
#define ACTIV_BASE_INTERVAL_TIMER_H
|
|
|
|
#include "ActivMiddleware/ActivBase/External.h"
|
|
#include "ActivMiddleware/ActivBase/ActivBase.h"
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
//! Interval timer class
|
|
class IntervalTimer
|
|
{
|
|
public:
|
|
//! Helper class to measure time whilst in scope.
|
|
class Scoped : private Boost::noncopyable
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* "Starts" the timer.
|
|
*
|
|
* @param time reference to a double to receive the time this object is in scope, in seconds.
|
|
*/
|
|
Scoped(double &time);
|
|
|
|
/**
|
|
* @brief Destructor.
|
|
*
|
|
* "Stops" the timer and stores the time in the double provided to the constructor.
|
|
*/
|
|
~Scoped();
|
|
|
|
private:
|
|
double & m_time;
|
|
const uint64_t m_startTimeStamp;
|
|
};
|
|
|
|
//! Get the timer frequency.
|
|
ACTIV_BASE_API static uint64_t GetFrequency();
|
|
|
|
//! Scale a timestamp to a time in seconds.
|
|
ACTIV_BASE_API static double ScaleTimeStamp(const uint64_t time);
|
|
|
|
//! Get the current time stamp.
|
|
ACTIV_BASE_API static uint64_t GetTimeStamp();
|
|
|
|
//! Get the current time stamp in milliseconds.
|
|
ACTIV_BASE_API static uint64_t GetMillisecondTimeStamp();
|
|
|
|
//! Get the current time stamp in microseconds.
|
|
ACTIV_BASE_API static uint64_t GetMicrosecondTimeStamp();
|
|
|
|
//! Get the current time stamp in nanoseconds.
|
|
ACTIV_BASE_API static uint64_t GetNanosecondTimeStamp();
|
|
|
|
//! Get the number of milliseconds that have elapsed since the system was started.
|
|
ACTIV_BASE_API static uint32_t GetTickCount();
|
|
|
|
/**
|
|
* @brief Get the number of milliseconds that have elapsed since the system was started.
|
|
* This function will be emulated on Windows if a native GetTickCount64 is not
|
|
* available or if the native GetTickCount64 cannot provide the desired resolution.
|
|
* The emulated version keeps track of how many times the system tick count
|
|
* overflows a uint32_t and so must be called at least once every ~49 days.
|
|
*
|
|
* @return Time since system started, in ms.
|
|
*/
|
|
ACTIV_BASE_API static uint64_t GetTickCount64();
|
|
|
|
/**
|
|
* @brief Get the system clock resolution.
|
|
*
|
|
* On Windows, this returns the actual clock resolution if possible, or a default of 15600 if unable to determine.
|
|
*
|
|
* @return The clock resolution, in microseconds.
|
|
*/
|
|
ACTIV_BASE_API static size_t GetClockResolution();
|
|
|
|
private:
|
|
IntervalTimer();
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline IntervalTimer::Scoped::Scoped(double &time) :
|
|
m_time(time),
|
|
m_startTimeStamp(IntervalTimer::GetTimeStamp())
|
|
{
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline IntervalTimer::Scoped::~Scoped()
|
|
{
|
|
m_time = IntervalTimer::ScaleTimeStamp(IntervalTimer::GetTimeStamp() - m_startTimeStamp);
|
|
}
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_BASE_INTERVAL_TIMER_H)
|