gui/auxiliary/timer.hpp

Go to the documentation of this file.
00001 /* $Id: timer.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2009 - 2012 by Mark de Wever <koraq@xs4all.nl>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 /**
00017  * @file
00018  * Contains the gui2 timer routines.
00019  *
00020  * This code avoids the following problems with the sdl timers:
00021  * - the callback must be a C function with a fixed signature.
00022  * - the callback needs to push an event in the event queue, between the
00023  *   pushing and execution of that event the timer can't be stopped. (Makes
00024  *   sense since the timer has expired, but not what the user wants.)
00025  *
00026  * With these functions it's possible to remove the event between pushing in
00027  * the queue and the actual execution. Since the callback is a boost::function
00028  * object it's possible to make the callback as fancy as wanted.
00029  */
00030 
00031 #ifndef GUI_WIDGETS_AUXILIARY_TIMER_HPP_INCLUDED
00032 #define GUI_WIDGETS_AUXILIARY_TIMER_HPP_INCLUDED
00033 
00034 #include <boost/function.hpp>
00035 
00036 #include <SDL_types.h>
00037 
00038 namespace gui2 {
00039 
00040 /**
00041  * Adds a new timer.
00042  *
00043  * @param interval                The timer interval in ms.
00044  * @param callback                The function to call when the timer expires,
00045  *                                the id send as parameter is the id of the
00046  *                                timer.
00047  * @param repeat                  If true the timer will restart after it
00048  *                                expires.
00049  *
00050  * @returns                       The id of the timer.
00051  * @retval [0]                    Failed to create a timer.
00052  */
00053 unsigned long
00054 add_timer(const Uint32 interval
00055         , const boost::function<void(unsigned long id)>& callback
00056         , const bool repeat = false);
00057 
00058 /**
00059  * Removes a timer.
00060  *
00061  * It's save to remove a timer in its own callback, only the value returned
00062  * might not be accurate. The destruction is postponed until the execution is
00063  * finished  and the return value is whether the postponing was successful.
00064  *
00065  * @param id                      The id of the timer to remove, this is the id
00066  *                                returned by add_timer.
00067  *
00068  * @returns                       Status, false if the timer couldn't be
00069  *                                removed.
00070  */
00071 bool
00072 remove_timer(const unsigned long id);
00073 
00074 /**
00075  * Executes a timer.
00076  *
00077  * @note this function is only meant to be executed by the event handling
00078  * system.
00079  *
00080  * @param id                      The id of the timer to execute, this is the
00081  *                                id returned by add_timer.
00082  *
00083  * @returns                       Status, false if the timer couldn't be
00084  *                                executed.
00085  */
00086 bool
00087 execute_timer(const unsigned long id);
00088 
00089 } //namespace gui2
00090 
00091 #endif
00092 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:02:54 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs