Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 5, 2011, 6:30:06 PM (14 years ago)
Author:
landauf
Message:

delayed commands now return a handle which allows to kill them selectively

File:
1 edited

Legend:

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

    r7401 r8020  
    3636#include <set>
    3737
     38#include <boost/bimap.hpp>
     39
    3840#include "util/Clock.h"
    3941#include "core/CoreIncludes.h"
     
    4547{
    4648    SetConsoleCommand("delay", &delay).argumentCompleter(1, autocompletion::command());
     49    SetConsoleCommand("killdelay", &killdelay);
    4750    SetConsoleCommand("killdelays", &killdelays);
    4851
    49     static std::set<Timer*> delaytimerset;
     52    static boost::bimap<unsigned int, Timer*> delaytimers;
     53    static unsigned int delayHandleCounter = 0;
    5054
    5155    /**
     
    5357        @param delay The delay in seconds
    5458        @param command The console command
     59        @return The handle of the delayed command, can be used as argument for killdelay()
    5560    */
    56     void delay(float delay, const std::string& command)
     61    unsigned int delay(float delay, const std::string& command)
    5762    {
    5863        Timer* delaytimer = new Timer();
    59         delaytimerset.insert(delaytimer);
     64        delaytimers.insert(boost::bimap<unsigned int, Timer*>::value_type(++delayHandleCounter, delaytimer));
    6065
    6166        const ExecutorStaticPtr& delayexecutor = createExecutor(createFunctor(&executeDelayedCommand));
    6267        delayexecutor->setDefaultValues(delaytimer, command);
    6368        delaytimer->setTimer(delay, false, delayexecutor);
     69
     70        return delayHandleCounter;
    6471    }
    6572
     
    7380        CommandExecutor::execute(command);
    7481        timer->destroy();
    75         delaytimerset.erase(timer);
     82        delaytimers.right.erase(timer);
    7683    }
    7784
     
    8188    void killdelays()
    8289    {
    83         for (std::set<Timer*>::iterator it = delaytimerset.begin(); it != delaytimerset.end(); ++it)
    84             (*it)->destroy();
     90        for (boost::bimap<unsigned int, Timer*>::left_map::iterator it = delaytimers.left.begin(); it != delaytimers.left.end(); ++it)
     91            it->second->destroy();
    8592
    86         delaytimerset.clear();
     93        delaytimers.clear();
     94    }
     95
     96    /**
     97        @brief Console-command: Kills a delayed command with given handle.
     98    */
     99    void killdelay(unsigned int handle)
     100    {
     101        boost::bimap<unsigned int, Timer*>::left_map::iterator it = delaytimers.left.find(handle);
     102        if (it != delaytimers.left.end())
     103        {
     104            it->second->destroy();
     105            delaytimers.left.erase(it);
     106        }
    87107    }
    88108
Note: See TracChangeset for help on using the changeset viewer.