cxxtools::Timer Class Reference

Notifies clients in constant intervals. More...

#include <cxxtools/timer.h>

Public Member Functions

 Timer ()
 Default constructor.
 ~Timer ()
 Destructor.
SelectorBaseselector ()
void setSelector (SelectorBase *s)
bool active () const
 Returs true if timer is active.
std::size_t interval () const
 Returns the current timer interval.
void start (std::size_t interval)
 Starts the timer.
void stop ()
 Stops the timer.
bool update ()
 Update the timer.
bool update (const Timespan &now)
const Timespanfinished () const

Public Attributes

Signal timeout
 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()
{
connect(timer.timeout, onTimer);
loop.add(timer);
timer.start(1000);
loop.run();
return 0;
}

Constructor & Destructor Documentation

cxxtools::Timer::Timer ( )

Default constructor.

Constructs an inactive 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.

const Timespan& cxxtools::Timer::finished ( ) const
inline
std::size_t 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 ( std::size_t  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 in milliseconds
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 Timespan now)

Member Data Documentation

Signal cxxtools::Timer::timeout

Notifies about interval timeouts.

This signal is sent if the interval time has expired.


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