cxxtools::Timer Class Reference

Notifies clients in constant intervals. More...

#include <cxxtools/timer.h>

Public Member Functions

 Timer (SelectorBase *selector=0)
 Default constructor.
 ~Timer ()
 Destructor.
SelectorBaseselector ()
void setSelector (SelectorBase *s)
bool active () const
 Returs true if timer is active.
const Timespaninterval () const
 Returns the current timer interval.
void start (const Milliseconds &interval)
 Starts the timer.
void start (const DateTime &startTime, const Milliseconds &interval)
 Starts the timer.
void after (const Milliseconds &interval)
 Starts the timer.
void at (const DateTime &tickTime)
 Starts the timer.
void stop ()
 Stops the timer.
bool update ()
 Update the timer.
bool update (const Milliseconds &now)
Timespan finished () const
 Returns the timespan since epoch when the timer expires.

Public Attributes

Signal timeout
 Notifies about interval timeouts.
Signal< DateTimetimeoutts
 Notifies about interval timeouts.

Detailed Description

Notifies clients in constant intervals.

Timers can be used to be notified if a time interval expires. It usually works with a Selector or event loop, where the Timer needs to be registered. Timers send the timeout signal in given intervals, to which the interested clients connect. The interval can be changed at any time and timers can switch between an active and inactive state. The following code calls the function onTimer every second:

void onTimer()
{
std::cerr << "Time out!\n";
}
int main()
{
cxxtools::Timer timer(&loop);
connect(timer.timeout, onTimer);
timer.start(1000);
loop.run();
return 0;
}

Note that a timer can process just one interval at once. When one of the methods is called while the timer is active, the previous interval will be stopped.

Constructor & Destructor Documentation

cxxtools::Timer::Timer ( SelectorBase selector = 0)
explicit

Default constructor.

Constructs an inactive timer and optionally sets the selector, which must be done anyway to run the timer.

cxxtools::Timer::~Timer ( )

Destructor.

The destructor sends the destroyed signal.

Member Function Documentation

bool cxxtools::Timer::active ( ) const

Returs true if timer is active.

void cxxtools::Timer::after ( const Milliseconds interval)

Starts the timer.

Start a timer. The timer will tick once after the specified time span.

The Timer needs to be registered with a Selector or event loop, otherwise the timeout signal will not be sent.

Parameters
intervalTimeout interval as a cxxtools::Timespan
void cxxtools::Timer::at ( const DateTime tickTime)

Starts the timer.

Start a timer. The timer will tick once at the specified point in time.

Parameters
tickTimeThe time, when the timer should tick.
Timespan cxxtools::Timer::finished ( ) const
inline

Returns the timespan since epoch when the timer expires.

const Timespan& cxxtools::Timer::interval ( ) const

Returns the current timer interval.

Returns the current interval of the timer in milliseconds.

SelectorBase* cxxtools::Timer::selector ( )
inline
void cxxtools::Timer::setSelector ( SelectorBase s)
void cxxtools::Timer::start ( const Milliseconds interval)

Starts the timer.

Start a timer from the moment this method is called. The Timer needs to be registered with a Selector or event loop, otherwise the timeout signal will not be sent.

Parameters
intervalTimeout interval as cxxtools::Timespan
void cxxtools::Timer::start ( const DateTime startTime,
const Milliseconds interval 
)

Starts the timer.

Start a timer. The start time is passed as a cxxtools::DateTime. If the start time is in the past, the timer starts on the next cycle. Past timer ticks are not catched up.

The Timer needs to be registered with a Selector or event loop, otherwise the timeout signal will not be sent.

Parameters
intervalTimeout interval as a cxxtools::Timespan
void cxxtools::Timer::stop ( )

Stops the timer.

If the Timer is registered with a Selector or an event loop, the timout signal will not be sent anymore.

bool cxxtools::Timer::update ( )

Update the timer.

This method is supposed to be called by the Selector or an event loop. If the interval timeout is passed the Timer will send the timeout signal and return true, otherwise internal times are updated and false is returned.

bool cxxtools::Timer::update ( const Milliseconds now)

Member Data Documentation

Signal cxxtools::Timer::timeout

Notifies about interval timeouts.

This signal is sent if the interval time has expired.

Signal<DateTime> cxxtools::Timer::timeoutts

Notifies about interval timeouts.

This signal is sent if the interval time has expired. As a parameter the due time is passed.


The documentation for this class was generated from the following file: