List of all members | Public Member Functions
thor::CallbackTimer Class Reference

Advanced timer with the ability to trigger function calls. More...

Inheritance diagram for thor::CallbackTimer:
Inheritance graph

Public Member Functions

 CallbackTimer ()
 Default constructor: Creates a callback timer that is initially expired. More...
 
virtual void reset (sf::Time timeLimit)
 Resets the timer's remaining time to the given limit and stops it. More...
 
virtual void restart (sf::Time timeLimit)
 Resets the timer's remaining time to the given limit and starts it again. More...
 
void update ()
 Trigger listeners, if expired. More...
 
Connection connect (std::function< void(CallbackTimer &)> unaryListener)
 Registers a unary function which will be called when the time reaches zero. More...
 
Connection connect0 (std::function< void()> nullaryListener)
 Registers a nullary function which will be called when the time reaches zero. More...
 
void clearConnections ()
 Removes all currently associated timer listeners. More...
 
sf::Time getRemainingTime () const
 Returns the remaining time. More...
 
bool isRunning () const
 Checks if the timer is currently running. More...
 
bool isExpired () const
 Checks if the timer has expired yet. More...
 
void start ()
 Starts or continues the timer. More...
 
void stop ()
 Pauses the timer. More...
 

Detailed Description

Advanced timer with the ability to trigger function calls.

Clock class that counts time down. As an extension of Timer, this class is able to register functions that are called at expiration time. When you use this class, make sure you invoke the update() method every frame. Possible usage:

#include <iostream>
void listener(thor::CallbackTimer& trigger)
{
std::cout << "expired" << std::endl;
trigger.restart(sf::seconds(1.f));
}
int main()
{
timer.connect(&listener);
timer.restart(sf::seconds(1.f));
for (;;)
timer.update();
}

Constructor & Destructor Documentation

thor::CallbackTimer::CallbackTimer ( )

Default constructor: Creates a callback timer that is initially expired.

You have to call reset() or restart() before you can use the countdown functionality.

Member Function Documentation

void thor::CallbackTimer::clearConnections ( )

Removes all currently associated timer listeners.

This also invalidates all connections to those listeners.

Connection thor::CallbackTimer::connect ( std::function< void(CallbackTimer &)>  unaryListener)

Registers a unary function which will be called when the time reaches zero.

Make sure to call update() each frame to invoke potential listeners. Use this function if your callback should get the timer as a parameter.

Parameters
unaryListenerThe function you want to associate with the timer expiration. The first parameter is a reference to the CallbackTimer instance that just expired.
Returns
Connection that can be used to disconnect the listener.
Warning
Inside a callback, you may not modify the connections of the timer that invokes it. Insertion and removal of callbacks need to be postponed after the update() call.
Connection thor::CallbackTimer::connect0 ( std::function< void()>  nullaryListener)

Registers a nullary function which will be called when the time reaches zero.

Make sure to call update() each frame to invoke potential listeners. Use this function if you don't care about the timer that fired the callback, and thus don't need a parameter for it in your callback.

Parameters
nullaryListenerThe function you want to associate with the timer expiration.
Returns
Connection that can be used to disconnect the listener.
Warning
Inside a callback, you may not modify the connections of the timer that invokes it. Insertion and removal of callbacks need to be postponed after the update() call.
sf::Time thor::Timer::getRemainingTime ( ) const
inherited

Returns the remaining time.

If the timer has expired, sf::Time::Zero is returned.

bool thor::Timer::isExpired ( ) const
inherited

Checks if the timer has expired yet.

Timers expire when their remaining time reaches zero.

bool thor::Timer::isRunning ( ) const
inherited

Checks if the timer is currently running.

As soon as the timer expires, it stops running.

virtual void thor::CallbackTimer::reset ( sf::Time  timeLimit)
virtual

Resets the timer's remaining time to the given limit and stops it.

Parameters
timeLimitThe new time limit (must be greater than zero).

In contrast to restart(), the timer is not running after the call.

Reimplemented from thor::Timer.

virtual void thor::CallbackTimer::restart ( sf::Time  timeLimit)
virtual

Resets the timer's remaining time to the given limit and starts it again.

Parameters
timeLimitThe new time limit (must be greater than zero).

The behavior is equivalent to reset(timeLimit) followed by start().

Reimplemented from thor::Timer.

void thor::Timer::start ( )
inherited

Starts or continues the timer.

If it is already running, nothing happens.

void thor::Timer::stop ( )
inherited

Pauses the timer.

If it is already paused, nothing happens.

void thor::CallbackTimer::update ( )

Trigger listeners, if expired.

This is the most important function of this class. You should call update() every frame (or, even better, let this an automated routine do) to assure that all associated listeners are invoked when the timer expires. If you restart the timer, call update() before restarting; otherwise the listeners won't fire.


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