Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8043 was 7851, checked in by landauf, 14 years ago

added "safe mode" feature to FunctorMember: if enabled, the functor acts like a WeakPtr and sets the object-pointer to NULL if the object is deleted, hence the functor can not be executed anymore and instead prints an error message. safe mode is _disabled_ by default.

enabled safe mode for timers

To implement safe mode, FunctorMember<void> (= FunctorStatic) was spezialized and implemented differently. Additionally functors now work only with polymorphic objects due to a dynamic cast, hence XMLPortVariableHelperClass and Magazine got a virtual destructor.

  • Property svn:eol-style set to native
File size: 6.1 KB
RevLine 
[1505]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
[7401]29/**
30    @defgroup Timer Timer
31    @ingroup Tools
32*/
33
34/**
[2171]35    @file
[7401]36    @ingroup Timer
37    @brief Declaration of the Timer class, used to call functions after a given time-interval.
[1505]38
[7401]39    @anchor TimerExample
[1505]40
[7401]41    Timer is a helper class that executes a function after a given amount of time.
42
43    Usage: <br>
[1505]44    header.h:
[7401]45    @code
46    class MyClass
47    {
48        public:
49            MyClass();
50            void functionName();
[1505]51
[7401]52        private:
53            Timer myTimer;
54    };
55    @endcode
56
[1505]57    source.cc:
[7401]58    @code
59    #include "core/command/Executor.h"
[1505]60
[7401]61    MyClass::MyClass()
62    {
63        myTimer.setTimer(3, false, createExecutor(createFunctor(&ClassName::myFunction, this)));
64    }
[1505]65
[7401]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.
[1505]74*/
75
76#ifndef _Timer_H__
77#define _Timer_H__
78
[5693]79#include "tools/ToolsPrereqs.h"
[3196]80
[1755]81#include "core/OrxonoxClass.h"
[7284]82#include "core/command/Executor.h"
[5693]83#include "tools/interfaces/TimeFactorListener.h"
[1505]84
85namespace orxonox
86{
87    void delay(float delay, const std::string& command);
88    void killdelays();
[5929]89    void executeDelayedCommand(Timer* timer, const std::string& command);
[1505]90
[7401]91    /**
92        @brief Timer is a helper class that executes a function after a given amount of time.
93
94        @see See @ref TimerExample "Timer.h" for an example.
95    */
[5929]96    class _ToolsExport Timer : public TimeFactorListener
[1505]97    {
98        public:
[5929]99            Timer();
[1505]100
[7284]101            Timer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall = false);
[5929]102
103            /**
[7401]104                @brief Initializes and starts the timer, which will call an executor after some time.
105                @param interval         The timer-interval in seconds
106                @param bLoop            If true, the executor gets called every @a interval seconds
107                @param executor         The executor that will be called
108                @param bKillAfterCall   If true, the timer will be deleted after the executor was called
[5929]109            */
[7284]110            void setTimer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall = false)
[5929]111            {
112                this->setInterval(interval);
113                this->bLoop_ = bLoop;
114                this->executor_ = executor;
115                this->bActive_ = true;
116
117                this->time_ = this->interval_;
118                this->bKillAfterCall_ = bKillAfterCall;
[7851]119
120                executor->getFunctor()->setSafeMode(true);
[5929]121            }
122
123            void run();
[1505]124
[7401]125            /// Re-starts the Timer: The executor will be called after @a interval seconds.
[1505]126            inline void startTimer()
127                { this->bActive_ = true; this->time_ = this->interval_; }
[7401]128            /// Stops the Timer.
[1505]129            inline void stopTimer()
130                { this->bActive_ = false; this->time_ = this->interval_; }
[7401]131            /// Pauses the Timer - it will continue with the actual state if you call unpauseTimer().
[1505]132            inline void pauseTimer()
133                { this->bActive_ = false; }
[7401]134            /// Unpauses the Timer - continues with the given state.
[1505]135            inline void unpauseTimer()
136                { this->bActive_ = true; }
[7545]137            /// Returns true if the Timer is active (neither stopped nor paused).
[1505]138            inline bool isActive() const
139                { return this->bActive_; }
[7401]140            /// Returns the remaining time until the Timer calls the executor.
[1608]141            inline float getRemainingTime() const
[3300]142                { return static_cast<float>(this->time_ / 1000000.0f); }
[7401]143            /// Increases the remaining time of the Timer by the given amount of time (in seconds).
[1505]144            inline void addTime(float time)
[3301]145                { if (time > 0.0f) this->time_ += static_cast<long long>(time * 1000000.0f); }
[7401]146            /// Decreases the remaining time of the Timer by the given amount of time (in seconds)
[1505]147            inline void removeTime(float time)
[3301]148                { if (time > 0.0f) this->time_ -= static_cast<long long>(time * 1000000.0f); }
[7401]149            /// Changes the calling interval.
[1505]150            inline void setInterval(float interval)
[3301]151                { this->interval_ = static_cast<long long>(interval * 1000000.0f); }
[7401]152            /// Defines if the timer call the executor every @a interval seconds or only once.
[1505]153            inline void setLoop(bool bLoop)
154                { this->bLoop_ = bLoop; }
155
[1755]156            void tick(const Clock& time);
[1505]157
[5929]158        private:
159            void init();
[6417]160
[7401]161            ExecutorPtr executor_;  //!< The executor of the function that will be called when the time expires
[1505]162
[7284]163            long long interval_;    //!< The time-interval in micro seconds
[7401]164            bool bLoop_;            //!< If true, the executor gets called every @a interval seconds
165            bool bActive_;          //!< If true, the Timer ticks and calls the executor if the time's up
166            bool bKillAfterCall_;   //!< If true the timer gets deleted after it expired and called the executor
[1505]167
[7401]168            long long time_;        //!< Internal variable, counting the time untill the next executor-call
[1505]169    };
170}
171
172#endif /* _Timer_H__ */
Note: See TracBrowser for help on using the repository browser.