Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 14, 2008, 3:42:49 AM (16 years ago)
Author:
landauf
Message:

merged core2 back to trunk
there might be some errors, wasn't able to test it yet due to some strange g++ and linker behaviour.

Location:
code/trunk/src/orxonox/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/orxonox/tools/Timer.cc

    r1039 r1052  
    2727
    2828#include "OrxonoxStableHeaders.h"
     29#include "core/Executor.h"
     30#include "core/CoreIncludes.h"
     31#include "core/ConsoleCommand.h"
     32#include "core/CommandExecutor.h"
    2933#include "Timer.h"
    30 
    31 #include "core/CoreIncludes.h"
    3234
    3335namespace orxonox
    3436{
     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
    3563    /**
    3664        @brief Constructor: Sets the default-values.
     
    4068        RegisterRootObject(TimerBase);
    4169
     70        this->executor_ = 0;
    4271        this->interval_ = 0;
    4372        this->bLoop_ = false;
     
    4574
    4675        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_)();
    4792    }
    4893
  • code/trunk/src/orxonox/tools/Timer.h

    r1039 r1052  
    4343
    4444    source.cc:
     45        include "core/Executor.h"
     46
    4547        ClassName::ClassName()
    4648        {
    47             myTimer.setTimer(interval_in_seconds, bLoop, this, &ClassName::functionName);
     49            myTimer.setTimer(interval_in_seconds, bLoop, this, createExecutor(createFunctor(&ClassName::functionName)));
    4850        }
    4951
     
    5961
    6062#include "OrxonoxPrereqs.h"
    61 
     63#include "core/CorePrereqs.h"
    6264#include "core/Tickable.h"
    6365
    6466namespace orxonox
    6567{
     68    class StaticTimer;
     69    void delay(float delay, const std::string& command);
     70    void executeDelayedCommand(StaticTimer* timer, const std::string& command);
     71
    6672    //! TimerBase is the parent of the Timer class.
    6773    class _OrxonoxExport TimerBase : public Tickable
    6874    {
    6975        public:
    70             TimerBase();
     76            ~TimerBase();
    7177
    72             virtual void run() const = 0;
     78            void run() const;
    7379
    7480            /** @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_; }
    7683            /** @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_; }
    7886            /** @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; }
    8089            /** @brief Unpauses the Timer - continues with the given state. */
    81             inline void unpauseTimer() { this->bActive_ = true; }
     90            inline void unpauseTimer()
     91                { this->bActive_ = true; }
    8292            /** @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; }
    84107
    85108            void tick(float dt);
    86109
    87110        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();
    91112
    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
    93120    };
    94121
     
    98125    {
    99126        public:
    100             /** @brief Constructor: Sets the default-values. */
    101             Timer()
    102             {
    103                 this->timerFunction_ = 0;
    104                 this->object_ = 0;
    105             }
     127            Timer() {}
    106128
    107129            /**
     
    110132                @param bLoop If true, the function gets called every 'interval' seconds
    111133                @param object The object owning the timer and the function
    112                 @param timerFunction A function pointer to the function to call
     134                @param exeuctor A executor of the function to call
    113135            */
    114             Timer(float interval, bool bLoop, T* object, void (T::*timerFunction)())
     136            Timer(float interval, bool bLoop, T* object, ExecutorMember<T>* exeuctor)
    115137            {
    116                 this->setTimer(interval, bLoop, timerFunction, object);
     138                this->setTimer(interval, bLoop, object, exeuctor);
    117139            }
    118140
     
    122144                @param bLoop If true, the function gets called every 'interval' seconds
    123145                @param object The object owning the timer and the function
    124                 @param timerFunction A function pointer to the function to call
     146                @param exeuctor A executor of the function to call
    125147            */
    126             void setTimer(float interval, bool bLoop, T* object, void (T::*timerFunction)())
     148            void setTimer(float interval, bool bLoop, T* object, ExecutorMember<T>* executor)
    127149            {
    128150                this->interval_ = interval;
    129151                this->bLoop_ = bLoop;
    130                 this->timerFunction_ = timerFunction;
    131                 this->object_ = object;
     152                executor->setObject(object);
     153                this->executor_ = (Executor*)executor;
    132154                this->bActive_ = true;
    133155
    134156                this->time_ = interval;
    135157            }
     158    };
    136159
    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)
    139173            {
    140                 ((*this->object_).*timerFunction_)();
     174                this->setTimer(interval, bLoop, executor);
    141175            }
    142176
    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            }
    146193    };
    147194
Note: See TracChangeset for help on using the changeset viewer.