Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Sep 28, 2009, 10:48:47 PM (15 years ago)
Author:
landauf
Message:

Realized Timer doesn't have to be a template, hence merged TimerBase, Timer and StaticTimer.
Removed the object pointer from Timer for memberfunctions, use createFunctor(f, object) instead.

Location:
code/branches/core5/src/libraries/tools
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core5/src/libraries/tools/Timer.cc

    r5798 r5831  
    4242    SetConsoleCommandShortcutExtern(killdelays);
    4343
    44     static std::set<StaticTimer*> delaytimerset;
     44    static std::set<Timer*> delaytimerset;
    4545
    4646    /**
     
    5151    void delay(float delay, const std::string& command)
    5252    {
    53         StaticTimer *delaytimer = new StaticTimer();
     53        Timer* delaytimer = new Timer();
    5454        delaytimerset.insert(delaytimer);
    5555
     
    6464        @param command The command to execute
    6565    */
    66     void executeDelayedCommand(StaticTimer* timer, const std::string& command)
     66    void executeDelayedCommand(Timer* timer, const std::string& command)
    6767    {
    6868        CommandExecutor::execute(command);
     
    7676    void killdelays()
    7777    {
    78         for (std::set<StaticTimer*>::iterator it = delaytimerset.begin(); it != delaytimerset.end(); ++it)
     78        for (std::set<Timer*>::iterator it = delaytimerset.begin(); it != delaytimerset.end(); ++it)
    7979            (*it)->destroy();
    8080
     
    8585        @brief Constructor: Sets the default-values.
    8686    */
    87     TimerBase::TimerBase()
     87    Timer::Timer()
     88    {
     89        this->init();
     90        RegisterObject(Timer);
     91    }
     92
     93    /**
     94        @brief Constructor: Initializes the Timer with given values.
     95        @param interval The timer-interval in seconds
     96        @param bLoop If true, the function gets called every 'interval' seconds
     97        @param exeuctor A executor of the function to call
     98    */
     99    Timer::Timer(float interval, bool bLoop, Executor* executor, bool bKillAfterCall)
     100    {
     101        this->init();
     102        RegisterObject(Timer);
     103
     104        this->setTimer(interval, bLoop, executor, bKillAfterCall);
     105    }
     106
     107    /**
     108        @brief Deletes the executor.
     109    */
     110    Timer::~Timer()
     111    {
     112        this->deleteExecutor();
     113    }
     114   
     115    /**
     116        @brief Initializes the Timer
     117    */
     118    void Timer::init()
    88119    {
    89120        this->executor_ = 0;
     
    94125
    95126        this->time_ = 0;
    96 
    97         RegisterObject(TimerBase);
    98     }
    99 
    100     /**
    101         @brief Deletes the executor.
    102     */
    103     TimerBase::~TimerBase()
    104     {
    105         this->deleteExecutor();
    106127    }
    107128
     
    109130        @brief Executes the executor.
    110131    */
    111     void TimerBase::run()
     132    void Timer::run()
    112133    {
    113134        bool temp = this->bKillAfterCall_; // to avoid errors with bKillAfterCall_=false and an exutors which destroy the timer
     
    122143        @brief Deletes the executor.
    123144    */
    124     void TimerBase::deleteExecutor()
     145    void Timer::deleteExecutor()
    125146    {
    126147      if (this->executor_)
     
    131152        @brief Updates the timer before the frames are rendered.
    132153    */
    133     void TimerBase::tick(const Clock& time)
     154    void Timer::tick(const Clock& time)
    134155    {
    135156        if (this->bActive_)
  • code/branches/core5/src/libraries/tools/Timer.h

    r5798 r5831  
    4040                ClassName();
    4141                void functionName();
    42                 Timer<ClassName> myTimer;
     42                Timer myTimer;
    4343        };
    4444
     
    4848        ClassName::ClassName()
    4949        {
    50             myTimer.setTimer(interval_in_seconds, bLoop, this, createExecutor(createFunctor(&ClassName::functionName)));
     50            myTimer.setTimer(interval_in_seconds, bLoop, createExecutor(createFunctor(&ClassName::functionName, this)));
    5151        }
    5252
     
    6969namespace orxonox
    7070{
    71     class StaticTimer;
    7271    void delay(float delay, const std::string& command);
    7372    void killdelays();
    74     void executeDelayedCommand(StaticTimer* timer, const std::string& command);
     73    void executeDelayedCommand(Timer* timer, const std::string& command);
    7574
    76     //! TimerBase is the parent of the Timer class.
    77     class _ToolsExport TimerBase : public TimeFactorListener
     75    //! The Timer is a callback-object, calling a given function after a given time-interval.
     76    class _ToolsExport Timer : public TimeFactorListener
    7877    {
    7978        public:
    80             ~TimerBase();
     79            Timer();
     80            ~Timer();
     81
     82            Timer(float interval, bool bLoop, Executor* executor, bool bKillAfterCall = false);
     83
     84            /**
     85                @brief Initializes the Timer with given values.
     86                @param interval The timer-interval in seconds
     87                @param bLoop If true, the function gets called every 'interval' seconds
     88                @param object The object owning the timer and the function
     89                @param executor A executor of the function to call
     90            */
     91            void setTimer(float interval, bool bLoop, Executor* executor, bool bKillAfterCall = false)
     92            {
     93                this->deleteExecutor();
     94
     95                this->setInterval(interval);
     96                this->bLoop_ = bLoop;
     97                this->executor_ = executor;
     98                this->bActive_ = true;
     99
     100                this->time_ = this->interval_;
     101                this->bKillAfterCall_ = bKillAfterCall;
     102            }
    81103
    82104            void run();
     
    116138            void tick(const Clock& time);
    117139
    118         protected:
    119             TimerBase();
    120 
     140        private:
     141            void init();
     142       
    121143            Executor* executor_;  //!< The executor of the function that should be called when the time expires
    122144
     
    128150            long long time_;      //!< Internal variable, counting the time till the next function-call
    129151    };
    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 
    212152}
    213153
  • code/branches/core5/src/libraries/tools/ToolsPrereqs.h

    r5738 r5831  
    7777    class ParticleInterface;
    7878    class Shader;
    79     template <class T>
    8079    class Timer;
    81     class StaticTimer;
    8280}
    8381
Note: See TracChangeset for help on using the changeset viewer.