Changeset 1052 for code/trunk/src/orxonox/tools
- Timestamp:
- Apr 14, 2008, 3:42:49 AM (17 years ago)
- Location:
- code/trunk/src/orxonox/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/orxonox/tools/Timer.cc
r1039 r1052 27 27 28 28 #include "OrxonoxStableHeaders.h" 29 #include "core/Executor.h" 30 #include "core/CoreIncludes.h" 31 #include "core/ConsoleCommand.h" 32 #include "core/CommandExecutor.h" 29 33 #include "Timer.h" 30 31 #include "core/CoreIncludes.h"32 34 33 35 namespace orxonox 34 36 { 37 ConsoleCommandShortcutExtern(delay, AccessLevel::None); 38 39 /** 40 @brief Calls a console command after 'delay' seconds. 41 @param delay The delay in seconds 42 @param command The console command 43 */ 44 void delay(float delay, const std::string& command) 45 { 46 StaticTimer *delaytimer = new StaticTimer(); 47 ExecutorStatic* delayexecutor = createExecutor(createFunctor(&executeDelayedCommand)); 48 delayexecutor->setDefaultValues(delaytimer, command); 49 delaytimer->setTimer(delay, false, delayexecutor); 50 } 51 52 /** 53 @brief Executes the command. 54 @param timer The timer to destroy after the command-execution 55 @param command The command to execute 56 */ 57 void executeDelayedCommand(StaticTimer* timer, const std::string& command) 58 { 59 CommandExecutor::execute(command); 60 delete timer; 61 } 62 35 63 /** 36 64 @brief Constructor: Sets the default-values. … … 40 68 RegisterRootObject(TimerBase); 41 69 70 this->executor_ = 0; 42 71 this->interval_ = 0; 43 72 this->bLoop_ = false; … … 45 74 46 75 this->time_ = 0; 76 } 77 78 /** 79 @brief Deletes the executor. 80 */ 81 TimerBase::~TimerBase() 82 { 83 delete this->executor_; 84 } 85 86 /** 87 @brief Executes the executor. 88 */ 89 void TimerBase::run() const 90 { 91 (*this->executor_)(); 47 92 } 48 93 -
code/trunk/src/orxonox/tools/Timer.h
r1039 r1052 43 43 44 44 source.cc: 45 include "core/Executor.h" 46 45 47 ClassName::ClassName() 46 48 { 47 myTimer.setTimer(interval_in_seconds, bLoop, this, &ClassName::functionName);49 myTimer.setTimer(interval_in_seconds, bLoop, this, createExecutor(createFunctor(&ClassName::functionName))); 48 50 } 49 51 … … 59 61 60 62 #include "OrxonoxPrereqs.h" 61 63 #include "core/CorePrereqs.h" 62 64 #include "core/Tickable.h" 63 65 64 66 namespace orxonox 65 67 { 68 class StaticTimer; 69 void delay(float delay, const std::string& command); 70 void executeDelayedCommand(StaticTimer* timer, const std::string& command); 71 66 72 //! TimerBase is the parent of the Timer class. 67 73 class _OrxonoxExport TimerBase : public Tickable 68 74 { 69 75 public: 70 TimerBase();76 ~TimerBase(); 71 77 72 v irtual void run() const = 0;78 void run() const; 73 79 74 80 /** @brief Starts the Timer: Function-call after 'interval' seconds. */ 75 inline void startTimer() { this->bActive_ = true; this->time_ = this->interval_; } 81 inline void startTimer() 82 { this->bActive_ = true; this->time_ = this->interval_; } 76 83 /** @brief Stops the Timer. */ 77 inline void stopTimer() { this->bActive_ = false; this->time_ = this->interval_; } 84 inline void stopTimer() 85 { this->bActive_ = false; this->time_ = this->interval_; } 78 86 /** @brief Pauses the Timer - it will continue with the actual state if you unpause it. */ 79 inline void pauseTimer() { this->bActive_ = false; } 87 inline void pauseTimer() 88 { this->bActive_ = false; } 80 89 /** @brief Unpauses the Timer - continues with the given state. */ 81 inline void unpauseTimer() { this->bActive_ = true; } 90 inline void unpauseTimer() 91 { this->bActive_ = true; } 82 92 /** @brief Returns true if the Timer is active (= not stoped, not paused). @return True = Time is active */ 83 inline bool isActive() const { return this->bActive_; } 93 inline bool isActive() const 94 { return this->bActive_; } 95 /** @brief Gives the Timer some extra time. @param time The amount of extra time in seconds */ 96 inline void addTime(float time) 97 { this->time_ += time; } 98 /** @brief Decreases the remaining time of the Timer. @param time The amount of time to remove */ 99 inline void removeTime(float time) 100 { this->time_ -= time; } 101 /** @brief Sets the interval of the Timer. @param interval The interval */ 102 inline void setInterval(float interval) 103 { this->interval_ = interval; } 104 /** @brief Sets bLoop to a given value. @param bLoop True = loop */ 105 inline void setLoop(bool bLoop) 106 { this->bLoop_ = bLoop; } 84 107 85 108 void tick(float dt); 86 109 87 110 protected: 88 float interval_; //!< The time-interval in seconds 89 bool bLoop_; //!< If true, the function gets called every 'interval' seconds 90 bool bActive_; //!< If true, the Timer ticks and calls the function if the time's up 111 TimerBase(); 91 112 92 float time_; //!< Internal variable, counting the time till the next function-call 113 Executor* executor_; //!< The executor of the function that should be called when the time expires 114 115 float interval_; //!< The time-interval in seconds 116 bool bLoop_; //!< If true, the function gets called every 'interval' seconds 117 bool bActive_; //!< If true, the Timer ticks and calls the function if the time's up 118 119 float time_; //!< Internal variable, counting the time till the next function-call 93 120 }; 94 121 … … 98 125 { 99 126 public: 100 /** @brief Constructor: Sets the default-values. */ 101 Timer() 102 { 103 this->timerFunction_ = 0; 104 this->object_ = 0; 105 } 127 Timer() {} 106 128 107 129 /** … … 110 132 @param bLoop If true, the function gets called every 'interval' seconds 111 133 @param object The object owning the timer and the function 112 @param timerFunction A function pointer tothe function to call134 @param exeuctor A executor of the function to call 113 135 */ 114 Timer(float interval, bool bLoop, T* object, void (T::*timerFunction)())136 Timer(float interval, bool bLoop, T* object, ExecutorMember<T>* exeuctor) 115 137 { 116 this->setTimer(interval, bLoop, timerFunction, object);138 this->setTimer(interval, bLoop, object, exeuctor); 117 139 } 118 140 … … 122 144 @param bLoop If true, the function gets called every 'interval' seconds 123 145 @param object The object owning the timer and the function 124 @param timerFunction A function pointer tothe function to call146 @param exeuctor A executor of the function to call 125 147 */ 126 void setTimer(float interval, bool bLoop, T* object, void (T::*timerFunction)())148 void setTimer(float interval, bool bLoop, T* object, ExecutorMember<T>* executor) 127 149 { 128 150 this->interval_ = interval; 129 151 this->bLoop_ = bLoop; 130 this->timerFunction_ = timerFunction;131 this-> object_ = object;152 executor->setObject(object); 153 this->executor_ = (Executor*)executor; 132 154 this->bActive_ = true; 133 155 134 156 this->time_ = interval; 135 157 } 158 }; 136 159 137 /** @brief Calls the given function of the given object. */ 138 void run() const 160 //! The StaticTimer is a callback-object, calling a static function after a given time-interval. 161 class StaticTimer : public TimerBase 162 { 163 public: 164 StaticTimer() {} 165 166 /** 167 @brief Constructor: Initializes the Timer with given values. 168 @param interval The timer-interval in seconds 169 @param bLoop If true, the function gets called every 'interval' seconds 170 @param exeuctor A executor of the function to call 171 */ 172 StaticTimer(float interval, bool bLoop, ExecutorStatic* executor) 139 173 { 140 ((*this->object_).*timerFunction_)();174 this->setTimer(interval, bLoop, executor); 141 175 } 142 176 143 private: 144 void (T::*timerFunction_)(); 145 T* object_; 177 /** 178 @brief Initializes the Timer with given values. 179 @param interval The timer-interval in seconds 180 @param bLoop If true, the function gets called every 'interval' seconds 181 @param object The object owning the timer and the function 182 @param executor A executor of the function to call 183 */ 184 void setTimer(float interval, bool bLoop, ExecutorStatic* executor) 185 { 186 this->interval_ = interval; 187 this->bLoop_ = bLoop; 188 this->executor_ = (Executor*)executor; 189 this->bActive_ = true; 190 191 this->time_ = interval; 192 } 146 193 }; 147 194
Note: See TracChangeset
for help on using the changeset viewer.