Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

Timer Class Reference

Notifies clients in constant intervals. More...

#include <cxxtools/timer.h>

List of all members.

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

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;
            connect(timer.timeout, onTimer);

            cxxtools::EventLoop loop;
            loop.addTimer(timer);
            timer.start(1000);

            loop.run();
            return 0;
        }

Constructor & Destructor Documentation

Timer (  ) 

Contructs an inactive timer.

~Timer (  ) 

The destructor sends the destroyed signal.


Member Function Documentation

std::size_t interval (  )  const

Returns the current interval of the timer in milliseconds.

void start ( std::size_t  interval  ) 

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:
interval Timeout interval in milliseconds

void stop (  ) 

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

bool update (  ) 

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.


Member Data Documentation

Signal timeout

This signal is sent if the interval time has expired.

Copyright © 2008 The Tntnet Development Team
Tntnet 1.6