Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/tools/Timer.h @ 5759

Last change on this file since 5759 was 5738, checked in by landauf, 15 years ago

merged libraries2 back to trunk

  • Property svn:eol-style set to native
File size: 8.2 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    @file
31    @brief Definition and Implementation of the Timer class.
32
33    The Timer is a callback-object, calling a given function after a given time-interval.
34
35    Usage:
36    header.h:
37        class ClassName
38        {
39            public:
40                ClassName();
41                void functionName();
42                Timer<ClassName> myTimer;
43        };
44
45    source.cc:
46        include "core/Executor.h"
47
48        ClassName::ClassName()
49        {
50            myTimer.setTimer(interval_in_seconds, bLoop, this, createExecutor(createFunctor(&ClassName::functionName)));
51        }
52
53        void ClassName::functionName()
54        {
55            whateveryouwant();
56            something(else);
57        }
58*/
59
60#ifndef _Timer_H__
61#define _Timer_H__
62
63#include "tools/ToolsPrereqs.h"
64
65#include "core/Executor.h"
66#include "core/OrxonoxClass.h"
67#include "tools/interfaces/TimeFactorListener.h"
68
69namespace orxonox
70{
71    class StaticTimer;
72    void delay(float delay, const std::string& command);
73    void killdelays();
74    void executeDelayedCommand(StaticTimer* timer, const std::string& command);
75
76    //! TimerBase is the parent of the Timer class.
77    class _ToolsExport TimerBase : public TimeFactorListener
78    {
79        public:
80            ~TimerBase();
81
82            void run() const;
83            void deleteExecutor();
84
85            /** @brief Starts the Timer: Function-call after 'interval' seconds. */
86            inline void startTimer()
87                { this->bActive_ = true; this->time_ = this->interval_; }
88            /** @brief Stops the Timer. */
89            inline void stopTimer()
90                { this->bActive_ = false; this->time_ = this->interval_; }
91            /** @brief Pauses the Timer - it will continue with the actual state if you unpause it. */
92            inline void pauseTimer()
93                { this->bActive_ = false; }
94            /** @brief Unpauses the Timer - continues with the given state. */
95            inline void unpauseTimer()
96                { this->bActive_ = true; }
97            /** @brief Returns true if the Timer is active (= not stoped, not paused). @return True = Time is active */
98            inline bool isActive() const
99                { return this->bActive_; }
100            /** @brief Returns the remaining time until the Timer calls the function. @return The remaining time */
101            inline float getRemainingTime() const
102                { return static_cast<float>(this->time_ / 1000000.0f); }
103            /** @brief Gives the Timer some extra time. @param time The amount of extra time in seconds */
104            inline void addTime(float time)
105                { if (time > 0.0f) this->time_ += static_cast<long long>(time * 1000000.0f); }
106            /** @brief Decreases the remaining time of the Timer. @param time The amount of time to remove */
107            inline void removeTime(float time)
108                { if (time > 0.0f) this->time_ -= static_cast<long long>(time * 1000000.0f); }
109            /** @brief Sets the interval of the Timer. @param interval The interval */
110            inline void setInterval(float interval)
111                { this->interval_ = static_cast<long long>(interval * 1000000.0f); }
112            /** @brief Sets bLoop to a given value. @param bLoop True = loop */
113            inline void setLoop(bool bLoop)
114                { this->bLoop_ = bLoop; }
115
116            void tick(const Clock& time);
117
118        protected:
119            TimerBase();
120
121            Executor* executor_;  //!< The executor of the function that should be called when the time expires
122
123            long long interval_;  //!< The time-interval in micro seconds
124            bool bLoop_;          //!< If true, the function gets called every 'interval' seconds
125            bool bActive_;        //!< If true, the Timer ticks and calls the function if the time's up
126            bool bKillAfterCall_; //!< If true the timer gets deleted after it called the function
127
128            long long time_;      //!< Internal variable, counting the time till the next function-call
129    };
130
131    //! The Timer is a callback-object, calling a given function after a given time-interval.
132    template <class T = BaseObject>
133    class Timer : public TimerBase
134    {
135        public:
136            Timer() {}
137
138            /**
139                @brief Constructor: Initializes the Timer with given values.
140                @param interval The timer-interval in seconds
141                @param bLoop If true, the function gets called every 'interval' seconds
142                @param object The object owning the timer and the function
143                @param exeuctor A executor of the function to call
144            */
145            Timer(float interval, bool bLoop, T* object, ExecutorMember<T>* exeuctor, bool bKillAfterCall = false)
146            {
147                this->setTimer(interval, bLoop, object, exeuctor, bKillAfterCall);
148            }
149
150            /**
151                @brief Initializes the Timer with given values.
152                @param interval The timer-interval in seconds
153                @param bLoop If true, the function gets called every 'interval' seconds
154                @param object The object owning the timer and the function
155                @param exeuctor A executor of the function to call
156            */
157            void setTimer(float interval, bool bLoop, T* object, ExecutorMember<T>* executor, bool bKillAfterCall = false)
158            {
159                this->deleteExecutor();
160
161                this->setInterval(interval);
162                this->bLoop_ = bLoop;
163                executor->setObject(object);
164                this->executor_ = static_cast<Executor*>(executor);
165                this->bActive_ = true;
166
167                this->time_ = this->interval_;
168                this->bKillAfterCall_ = bKillAfterCall;
169            }
170    };
171
172    //! The StaticTimer is a callback-object, calling a static function after a given time-interval.
173    class _ToolsExport StaticTimer : public TimerBase
174    {
175        public:
176            StaticTimer() {}
177
178            /**
179                @brief Constructor: Initializes the Timer with given values.
180                @param interval The timer-interval in seconds
181                @param bLoop If true, the function gets called every 'interval' seconds
182                @param exeuctor A executor of the function to call
183            */
184            StaticTimer(float interval, bool bLoop, ExecutorStatic* executor, bool bKillAfterCall = false)
185            {
186                this->setTimer(interval, bLoop, executor, bKillAfterCall);
187            }
188
189            /**
190                @brief Initializes the Timer with given values.
191                @param interval The timer-interval in seconds
192                @param bLoop If true, the function gets called every 'interval' seconds
193                @param object The object owning the timer and the function
194                @param executor A executor of the function to call
195            */
196            void setTimer(float interval, bool bLoop, ExecutorStatic* executor, bool bKillAfterCall = false)
197            {
198                this->deleteExecutor();
199
200                this->setInterval(interval);
201                this->bLoop_ = bLoop;
202                this->executor_ = executor;
203                this->bActive_ = true;
204
205                this->time_ = this->interval_;
206                this->bKillAfterCall_ = bKillAfterCall;
207            }
208    };
209
210}
211
212#endif /* _Timer_H__ */
Note: See TracBrowser for help on using the repository browser.