#include <Timer.h>
Inheritance diagram for GNE::Timer:
Public Types | |
typedef SmartPtr< Timer > | sptr |
typedef WeakPtr< Timer > | wptr |
Public Member Functions | |
virtual | ~Timer () |
Destructor. | |
SmartPtr< TimerCallback > | getCallback () const |
Returns the TimerCallback set in the constructor. | |
void | startTimer () |
Starts the timer running and calling the callback. | |
virtual void | shutDown () |
Equivalent to calling stopTimer with false. | |
void | stopTimer (bool waitForEnd) |
Stops the timer and stops calling the callback. | |
Static Public Member Functions | |
static void | stopAll () |
Stops all Timers. | |
static sptr | create (const SmartPtr< TimerCallback > &callback, int rate) |
Create a timer callback. | |
static Time | getCurrentTime () |
Returns the current time from some arbitrary point in the past. | |
static Time | getAbsoluteTime () |
Returns the current time from the system clock. | |
Protected Member Functions | |
Timer (const SmartPtr< TimerCallback > &callback, int rate) | |
void | run () |
This is the thread that will perform the callbacks. |
A timer object calls its listeners back every so often based on the time given.
All of the methods in this class are safe to call from multiple threads at the same time, and can also be called from the TimerCallback as well, with a few (some obvious) exceptions.
A Timer thread runs with a higher priority than the main thread, therefore its callbacks are suitable for short, quick tasks.
Timer::sptr GNE::Timer::create | ( | const SmartPtr< TimerCallback > & | callback, | |
int | rate | |||
) | [static] |
Create a timer callback.
The first call to the callback will occur after "rate" milliseconds, so this class is suitable for setting timeouts for your operations. Use the startTimer method to start this timer.
The callback is released when the Timer is stopped. This allows the callback to contain a reference to the Timer.
callback | A newly allocated object to perform callbacks on. | |
rate | the callback rate in milliseconds. |
Time GNE::Timer::getAbsoluteTime | ( | ) | [static] |
Returns the current time from the system clock.
The time returned is an absolute time, relative to midnight, Jan 1, 1970.
TimerCallback::sptr GNE::Timer::getCallback | ( | ) | const |
Returns the TimerCallback set in the constructor.
After the timer has stopped, an empty SmartPtr is returned since the listener is released when the timer stops.
Time GNE::Timer::getCurrentTime | ( | ) | [static] |
Returns the current time from some arbitrary point in the past.
This is usually a very high precision timer that likely provides microsecond or better resolution.
void GNE::Timer::startTimer | ( | ) |
void GNE::Timer::stopTimer | ( | bool | waitForEnd | ) |
Stops the timer and stops calling the callback.
The timer will likely be called one more time because the timer will actually stop at the end of its most recent cycle. If you want to wait until the callback is called for the last time, pass true into this function. Then this function will block for at most the time of the callback rate plus the time it takes for the callback to finish.
This timer's callback can call this function, but obviously it must not pass true to this function.
If a Timer is already stopped, this function will have no effect.