Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/libraries/tools/Timer.h @ 5791

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

Added destroy() function to OrxonoxClass.
Removed destroy() functions from some subclasses.
Expanded Timer to accept Functors of other classes too.

  • 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            template <class O>
146            Timer(float interval, bool bLoop, T* object, ExecutorMember<O>* exeuctor, bool bKillAfterCall = false)
147            {
148                this->setTimer(interval, bLoop, object, exeuctor, bKillAfterCall);
149            }
150
151            /**
152                @brief Initializes the Timer with given values.
153                @param interval The timer-interval in seconds
154                @param bLoop If true, the function gets called every 'interval' seconds
155                @param object The object owning the timer and the function
156                @param exeuctor A executor of the function to call
157            */
158            template <class O>
159            void setTimer(float interval, bool bLoop, T* object, ExecutorMember<O>* executor, bool bKillAfterCall = false)
160            {
161                this->deleteExecutor();
162
163                this->setInterval(interval);
164                this->bLoop_ = bLoop;
165                executor->setObject(object);
166                this->executor_ = static_cast<Executor*>(executor);
167                this->bActive_ = true;
168
169                this->time_ = this->interval_;
170                this->bKillAfterCall_ = bKillAfterCall;
171            }
172    };
173
174    //! The StaticTimer is a callback-object, calling a static function after a given time-interval.
175    class _ToolsExport StaticTimer : public TimerBase
176    {
177        public:
178            StaticTimer() {}
179
180            /**
181                @brief Constructor: Initializes the Timer with given values.
182                @param interval The timer-interval in seconds
183                @param bLoop If true, the function gets called every 'interval' seconds
184                @param exeuctor A executor of the function to call
185            */
186            StaticTimer(float interval, bool bLoop, ExecutorStatic* executor, bool bKillAfterCall = false)
187            {
188                this->setTimer(interval, bLoop, executor, bKillAfterCall);
189            }
190
191            /**
192                @brief Initializes the Timer with given values.
193                @param interval The timer-interval in seconds
194                @param bLoop If true, the function gets called every 'interval' seconds
195                @param object The object owning the timer and the function
196                @param executor A executor of the function to call
197            */
198            void setTimer(float interval, bool bLoop, ExecutorStatic* executor, bool bKillAfterCall = false)
199            {
200                this->deleteExecutor();
201
202                this->setInterval(interval);
203                this->bLoop_ = bLoop;
204                this->executor_ = executor;
205                this->bActive_ = true;
206
207                this->time_ = this->interval_;
208                this->bKillAfterCall_ = bKillAfterCall;
209            }
210    };
211
212}
213
214#endif /* _Timer_H__ */
Note: See TracBrowser for help on using the repository browser.