Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/usability/src/libraries/tools/Timer.h @ 8020

Last change on this file since 8020 was 8020, checked in by landauf, 13 years ago

delayed commands now return a handle which allows to kill them selectively

  • Property svn:eol-style set to native
File size: 6.1 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @defgroup Timer Timer
31    @ingroup Tools
32*/
33
34/**
35    @file
36    @ingroup Timer
37    @brief Declaration of the Timer class, used to call functions after a given time-interval.
38
39    @anchor TimerExample
40
41    Timer is a helper class that executes a function after a given amount of time.
42
43    Usage: <br>
44    header.h:
45    @code
46    class MyClass
47    {
48        public:
49            MyClass();
50            void functionName();
51
52        private:
53            Timer myTimer;
54    };
55    @endcode
56
57    source.cc:
58    @code
59    #include "core/command/Executor.h"
60
61    MyClass::MyClass()
62    {
63        myTimer.setTimer(3, false, createExecutor(createFunctor(&ClassName::myFunction, this)));
64    }
65
66    void MyClass::myFunction()
67    {
68        COUT(0) << "Hello World" << std::endl;
69    }
70    @endcode
71
72    The code in this example prints "Hello World" to the console, 3 seconds after creating
73    an instance of MyClass.
74*/
75
76#ifndef _Timer_H__
77#define _Timer_H__
78
79#include "tools/ToolsPrereqs.h"
80
81#include "core/OrxonoxClass.h"
82#include "core/command/Executor.h"
83#include "tools/interfaces/TimeFactorListener.h"
84
85namespace orxonox
86{
87    unsigned int delay(float delay, const std::string& command);
88    void killdelay(unsigned int handle);
89    void killdelays();
90    void executeDelayedCommand(Timer* timer, const std::string& command);
91
92    /**
93        @brief Timer is a helper class that executes a function after a given amount of time.
94
95        @see See @ref TimerExample "Timer.h" for an example.
96    */
97    class _ToolsExport Timer : public TimeFactorListener
98    {
99        public:
100            Timer();
101
102            Timer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall = false);
103
104            /**
105                @brief Initializes and starts the timer, which will call an executor after some time.
106                @param interval         The timer-interval in seconds
107                @param bLoop            If true, the executor gets called every @a interval seconds
108                @param executor         The executor that will be called
109                @param bKillAfterCall   If true, the timer will be deleted after the executor was called
110            */
111            void setTimer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall = false)
112            {
113                this->setInterval(interval);
114                this->bLoop_ = bLoop;
115                this->executor_ = executor;
116                this->bActive_ = true;
117
118                this->time_ = this->interval_;
119                this->bKillAfterCall_ = bKillAfterCall;
120
121                executor->getFunctor()->setSafeMode(true);
122            }
123
124            void run();
125
126            /// Re-starts the Timer: The executor will be called after @a interval seconds.
127            inline void startTimer()
128                { this->bActive_ = true; this->time_ = this->interval_; }
129            /// Stops the Timer.
130            inline void stopTimer()
131                { this->bActive_ = false; this->time_ = this->interval_; }
132            /// Pauses the Timer - it will continue with the actual state if you call unpauseTimer().
133            inline void pauseTimer()
134                { this->bActive_ = false; }
135            /// Unpauses the Timer - continues with the given state.
136            inline void unpauseTimer()
137                { this->bActive_ = true; }
138            /// Returns true if the Timer is active (neither stopped nor paused).
139            inline bool isActive() const
140                { return this->bActive_; }
141            /// Returns the remaining time until the Timer calls the executor.
142            inline float getRemainingTime() const
143                { return static_cast<float>(this->time_ / 1000000.0f); }
144            /// Increases the remaining time of the Timer by the given amount of time (in seconds).
145            inline void addTime(float time)
146                { if (time > 0.0f) this->time_ += static_cast<long long>(time * 1000000.0f); }
147            /// Decreases the remaining time of the Timer by the given amount of time (in seconds)
148            inline void removeTime(float time)
149                { if (time > 0.0f) this->time_ -= static_cast<long long>(time * 1000000.0f); }
150            /// Changes the calling interval.
151            inline void setInterval(float interval)
152                { this->interval_ = static_cast<long long>(interval * 1000000.0f); }
153            /// Defines if the timer call the executor every @a interval seconds or only once.
154            inline void setLoop(bool bLoop)
155                { this->bLoop_ = bLoop; }
156
157            void tick(const Clock& time);
158
159        private:
160            void init();
161
162            ExecutorPtr executor_;  //!< The executor of the function that will be called when the time expires
163
164            long long interval_;    //!< The time-interval in micro seconds
165            bool bLoop_;            //!< If true, the executor gets called every @a interval seconds
166            bool bActive_;          //!< If true, the Timer ticks and calls the executor if the time's up
167            bool bKillAfterCall_;   //!< If true the timer gets deleted after it expired and called the executor
168
169            long long time_;        //!< Internal variable, counting the time untill the next executor-call
170    };
171}
172
173#endif /* _Timer_H__ */
Note: See TracBrowser for help on using the repository browser.