[Orxonox-commit 2524] r7229 - code/branches/consolecommands3/src/libraries/core/command
landauf at orxonox.net
landauf at orxonox.net
Fri Aug 27 14:53:06 CEST 2010
Author: landauf
Date: 2010-08-27 14:53:06 +0200 (Fri, 27 Aug 2010)
New Revision: 7229
Modified:
code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.cc
code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.h
code/branches/consolecommands3/src/libraries/core/command/Shell.cc
code/branches/consolecommands3/src/libraries/core/command/Shell.h
Log:
implemented command cache in CommandExecutor
Modified: code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.cc
===================================================================
--- code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.cc 2010-08-27 12:41:03 UTC (rev 7228)
+++ code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.cc 2010-08-27 12:53:06 UTC (rev 7229)
@@ -60,7 +60,21 @@
if (useTcl)
return TclBind::eval(command, error);
else
- return CommandExecutor::evaluate(command).query(error);
+ {
+ CommandEvaluation evaluation;
+ if (!CommandExecutor::getInstance().getCached(command, evaluation))
+ {
+COUT(0) << "evaluate" << std::endl;
+ evaluation = CommandExecutor::evaluate(command);
+ CommandExecutor::getInstance().cache(command, evaluation);
+ }
+ else
+ {
+COUT(0) << "cached" << std::endl;
+ }
+
+ return evaluation.query(error);
+ }
}
/* static */ std::string CommandExecutor::query(const std::string& command, int* error, bool useTcl)
@@ -96,4 +110,46 @@
return evaluation;
}
+
+ bool CommandExecutor::getCached(const std::string& command, CommandEvaluation& evaluation)
+ {
+ if (Shell::getCacheSize() == 0)
+ return false;
+
+ std::map<std::string, CacheEntry>::iterator it = this->cache_.find(command);
+ if (it != this->cache_.end())
+ {
+ // update ordered list of cached commands (move it to the front)
+ this->cachelist_.erase(it->second.iterator_);
+ this->cachelist_.push_front(command);
+ it->second.iterator_ = this->cachelist_.begin();
+
+ // assign the cached evaluation
+ evaluation = it->second.evaluation_;
+ return true;
+ }
+ return false;
+ }
+
+ void CommandExecutor::cache(const std::string& command, const CommandEvaluation& evaluation)
+ {
+ if (Shell::getCacheSize() == 0)
+ return;
+
+ // push command to the front of the ordered list
+ this->cachelist_.push_front(command);
+
+ // create a cache entry and store it in the cache
+ CacheEntry entry;
+ entry.evaluation_ = evaluation;
+ entry.iterator_ = this->cachelist_.begin();
+ this->cache_[command] = entry;
+
+ // remove the last command in the ordered list from the cache if it exceeds the maximum size of the cache
+ if (this->cachelist_.size() > Shell::getCacheSize())
+ {
+ this->cache_.erase(this->cachelist_.back());
+ this->cachelist_.pop_back();
+ }
+ }
}
Modified: code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.h
===================================================================
--- code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.h 2010-08-27 12:41:03 UTC (rev 7228)
+++ code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.h 2010-08-27 12:53:06 UTC (rev 7229)
@@ -31,6 +31,8 @@
#include "core/CorePrereqs.h"
+#include <map>
+#include <list>
#include <string>
#include "util/MultiType.h"
@@ -64,6 +66,18 @@
~CommandExecutor() {}
static CommandExecutor& getInstance();
+
+ bool getCached(const std::string& command, CommandEvaluation& evaluation);
+ void cache(const std::string& command, const CommandEvaluation& evaluation);
+
+ struct CacheEntry
+ {
+ CommandEvaluation evaluation_;
+ std::list<std::string>::iterator iterator_;
+ };
+
+ std::map<std::string, CacheEntry> cache_;
+ std::list<std::string> cachelist_;
}; // tolua_export
} // tolua_export
Modified: code/branches/consolecommands3/src/libraries/core/command/Shell.cc
===================================================================
--- code/branches/consolecommands3/src/libraries/core/command/Shell.cc 2010-08-27 12:41:03 UTC (rev 7228)
+++ code/branches/consolecommands3/src/libraries/core/command/Shell.cc 2010-08-27 12:53:06 UTC (rev 7229)
@@ -45,6 +45,8 @@
_SetConsoleCommand("info", OutputHandler::info );
_SetConsoleCommand("debug", OutputHandler::debug );
+ unsigned int Shell::cacheSize_s;
+
Shell::Shell(const std::string& consoleName, bool bScrollable)
: OutputListener(consoleName)
, inputBuffer_(new InputBuffer())
@@ -98,6 +100,7 @@
SetConfigValue(historyOffset_, 0)
.callback(this, &Shell::commandHistoryOffsetChanged);
setConfigValueGeneric(this, &commandHistory_, ConfigFileType::CommandHistory, "Shell", "commandHistory_", std::vector<std::string>());
+ SetConfigValue(cacheSize_s, 32);
#ifdef ORXONOX_RELEASE
const unsigned int defaultLevel = 1;
Modified: code/branches/consolecommands3/src/libraries/core/command/Shell.h
===================================================================
--- code/branches/consolecommands3/src/libraries/core/command/Shell.h 2010-08-27 12:41:03 UTC (rev 7228)
+++ code/branches/consolecommands3/src/libraries/core/command/Shell.h 2010-08-27 12:53:06 UTC (rev 7229)
@@ -112,6 +112,9 @@
inline const std::string& getPromptPrefix() const { return this->promptPrefix_; }
void setPromptPrefix(const std::string& str);
+ static inline unsigned int getCacheSize()
+ { return Shell::cacheSize_s; }
+
private:
Shell(const Shell& other);
@@ -166,6 +169,7 @@
unsigned int historyOffset_;
std::vector<std::string> commandHistory_;
int softDebugLevel_;
+ static unsigned int cacheSize_s;
};
}
More information about the Orxonox-commit
mailing list