[Orxonox-commit 3332] r8020 - code/branches/usability/src/libraries/tools
landauf at orxonox.net
landauf at orxonox.net
Sat Mar 5 18:30:07 CET 2011
Author: landauf
Date: 2011-03-05 18:30:06 +0100 (Sat, 05 Mar 2011)
New Revision: 8020
Modified:
code/branches/usability/src/libraries/tools/Timer.cc
code/branches/usability/src/libraries/tools/Timer.h
Log:
delayed commands now return a handle which allows to kill them selectively
Modified: code/branches/usability/src/libraries/tools/Timer.cc
===================================================================
--- code/branches/usability/src/libraries/tools/Timer.cc 2011-03-05 16:59:17 UTC (rev 8019)
+++ code/branches/usability/src/libraries/tools/Timer.cc 2011-03-05 17:30:06 UTC (rev 8020)
@@ -35,6 +35,8 @@
#include <set>
+#include <boost/bimap.hpp>
+
#include "util/Clock.h"
#include "core/CoreIncludes.h"
#include "core/command/ConsoleCommand.h"
@@ -44,23 +46,28 @@
namespace orxonox
{
SetConsoleCommand("delay", &delay).argumentCompleter(1, autocompletion::command());
+ SetConsoleCommand("killdelay", &killdelay);
SetConsoleCommand("killdelays", &killdelays);
- static std::set<Timer*> delaytimerset;
+ static boost::bimap<unsigned int, Timer*> delaytimers;
+ static unsigned int delayHandleCounter = 0;
/**
@brief Console-command: Calls another console command after @a delay seconds.
@param delay The delay in seconds
@param command The console command
+ @return The handle of the delayed command, can be used as argument for killdelay()
*/
- void delay(float delay, const std::string& command)
+ unsigned int delay(float delay, const std::string& command)
{
Timer* delaytimer = new Timer();
- delaytimerset.insert(delaytimer);
+ delaytimers.insert(boost::bimap<unsigned int, Timer*>::value_type(++delayHandleCounter, delaytimer));
const ExecutorStaticPtr& delayexecutor = createExecutor(createFunctor(&executeDelayedCommand));
delayexecutor->setDefaultValues(delaytimer, command);
delaytimer->setTimer(delay, false, delayexecutor);
+
+ return delayHandleCounter;
}
/**
@@ -72,7 +79,7 @@
{
CommandExecutor::execute(command);
timer->destroy();
- delaytimerset.erase(timer);
+ delaytimers.right.erase(timer);
}
/**
@@ -80,13 +87,26 @@
*/
void killdelays()
{
- for (std::set<Timer*>::iterator it = delaytimerset.begin(); it != delaytimerset.end(); ++it)
- (*it)->destroy();
+ for (boost::bimap<unsigned int, Timer*>::left_map::iterator it = delaytimers.left.begin(); it != delaytimers.left.end(); ++it)
+ it->second->destroy();
- delaytimerset.clear();
+ delaytimers.clear();
}
/**
+ @brief Console-command: Kills a delayed command with given handle.
+ */
+ void killdelay(unsigned int handle)
+ {
+ boost::bimap<unsigned int, Timer*>::left_map::iterator it = delaytimers.left.find(handle);
+ if (it != delaytimers.left.end())
+ {
+ it->second->destroy();
+ delaytimers.left.erase(it);
+ }
+ }
+
+ /**
@brief Constructor: Sets the default-values.
*/
Timer::Timer()
Modified: code/branches/usability/src/libraries/tools/Timer.h
===================================================================
--- code/branches/usability/src/libraries/tools/Timer.h 2011-03-05 16:59:17 UTC (rev 8019)
+++ code/branches/usability/src/libraries/tools/Timer.h 2011-03-05 17:30:06 UTC (rev 8020)
@@ -84,7 +84,8 @@
namespace orxonox
{
- void delay(float delay, const std::string& command);
+ unsigned int delay(float delay, const std::string& command);
+ void killdelay(unsigned int handle);
void killdelays();
void executeDelayedCommand(Timer* timer, const std::string& command);
More information about the Orxonox-commit
mailing list