[Orxonox-commit 5824] r10484 - in code/branches/core7/src/libraries/core: . command
landauf at orxonox.net
landauf at orxonox.net
Mon May 25 22:56:27 CEST 2015
Author: landauf
Date: 2015-05-25 22:56:26 +0200 (Mon, 25 May 2015)
New Revision: 10484
Modified:
code/branches/core7/src/libraries/core/Core.cc
code/branches/core7/src/libraries/core/command/ArgumentCompletionFunctions.cc
code/branches/core7/src/libraries/core/command/CommandEvaluation.cc
code/branches/core7/src/libraries/core/command/CommandExecutor.cc
code/branches/core7/src/libraries/core/command/ConsoleCommand.cc
code/branches/core7/src/libraries/core/command/ConsoleCommandIncludes.cc
code/branches/core7/src/libraries/core/command/ConsoleCommandIncludes.h
code/branches/core7/src/libraries/core/command/ConsoleCommandManager.cc
code/branches/core7/src/libraries/core/command/ConsoleCommandManager.h
Log:
made ConsoleCommandManager a singleton.
Modified: code/branches/core7/src/libraries/core/Core.cc
===================================================================
--- code/branches/core7/src/libraries/core/Core.cc 2015-05-25 20:36:28 UTC (rev 10483)
+++ code/branches/core7/src/libraries/core/Core.cc 2015-05-25 20:56:26 UTC (rev 10484)
@@ -249,7 +249,7 @@
safeObjectDelete(&config_);
safeObjectDelete(&languageInstance_);
safeObjectDelete(&configFileManager_);
- ConsoleCommandManager::destroyAll();
+ ConsoleCommandManager::getInstance().destroyAll();
Context::setRootContext(NULL);
IdentifierManager::getInstance().destroyAllIdentifiers();
safeObjectDelete(&signalHandler_);
Modified: code/branches/core7/src/libraries/core/command/ArgumentCompletionFunctions.cc
===================================================================
--- code/branches/core7/src/libraries/core/command/ArgumentCompletionFunctions.cc 2015-05-25 20:36:28 UTC (rev 10483)
+++ code/branches/core7/src/libraries/core/command/ArgumentCompletionFunctions.cc 2015-05-25 20:56:26 UTC (rev 10484)
@@ -98,7 +98,7 @@
std::string fragmentLC = getLowercase(fragment);
// get all the groups that are visible (except the shortcut group "")
- const std::map<std::string, std::map<std::string, ConsoleCommand*> >& commands = ConsoleCommandManager::getCommands();
+ const std::map<std::string, std::map<std::string, ConsoleCommand*> >& commands = ConsoleCommandManager::getInstance().getCommands();
for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = commands.begin(); it_group != commands.end(); ++it_group)
if (groupIsVisible(it_group->second, bOnlyShowHidden) && it_group->first != "" && (fragmentLC == "" || getLowercase(it_group->first).find(fragmentLC) == 0))
groupList.push_back(ArgumentCompletionListElement(it_group->first, getLowercase(it_group->first)));
@@ -137,13 +137,13 @@
std::string groupLC = getLowercase(group);
// find the iterator of the given group
- std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getCommands().begin();
- for ( ; it_group != ConsoleCommandManager::getCommands().end(); ++it_group)
+ std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getInstance().getCommands().begin();
+ for ( ; it_group != ConsoleCommandManager::getInstance().getCommands().end(); ++it_group)
if (getLowercase(it_group->first) == groupLC)
break;
// add all commands in the group to the list
- if (it_group != ConsoleCommandManager::getCommands().end())
+ if (it_group != ConsoleCommandManager::getInstance().getCommands().end())
{
for (std::map<std::string, ConsoleCommand*>::const_iterator it_command = it_group->second.begin(); it_command != it_group->second.end(); ++it_command)
if (it_command->second->isActive() && it_command->second->hasAccess() && (!it_command->second->isHidden())^bOnlyShowHidden)
@@ -206,19 +206,19 @@
if (tokens.size() == 0)
return detail::_groupsandcommands(fragment, true);
- if (ConsoleCommandManager::getCommandLC(getLowercase(tokens[0])))
+ if (ConsoleCommandManager::getInstance().getCommandLC(getLowercase(tokens[0])))
return ARGUMENT_COMPLETION_FUNCTION_CALL(command)(fragment);
if (tokens.size() == 1)
{
- std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getCommands().find(tokens[0]);
- if (it_group != ConsoleCommandManager::getCommands().end())
+ std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getInstance().getCommands().find(tokens[0]);
+ if (it_group != ConsoleCommandManager::getInstance().getCommands().end())
return detail::_subcommands(fragment, tokens[0], true);
else
return detail::_groupsandcommands(fragment, true);
}
- if (ConsoleCommandManager::getCommandLC(getLowercase(tokens[0]), getLowercase(tokens[1])))
+ if (ConsoleCommandManager::getInstance().getCommandLC(getLowercase(tokens[0]), getLowercase(tokens[1])))
return ARGUMENT_COMPLETION_FUNCTION_CALL(command)(fragment);
return ArgumentCompletionList();
Modified: code/branches/core7/src/libraries/core/command/CommandEvaluation.cc
===================================================================
--- code/branches/core7/src/libraries/core/command/CommandEvaluation.cc 2015-05-25 20:36:28 UTC (rev 10483)
+++ code/branches/core7/src/libraries/core/command/CommandEvaluation.cc 2015-05-25 20:56:26 UTC (rev 10484)
@@ -305,7 +305,7 @@
{
// the user typed 1-2 arguments, check what he tried to type and print a suitable error
std::string groupLC = getLowercase(this->getToken(0));
- for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getCommandsLC().begin(); it_group != ConsoleCommandManager::getCommandsLC().end(); ++it_group)
+ for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getInstance().getCommandsLC().begin(); it_group != ConsoleCommandManager::getInstance().getCommandsLC().end(); ++it_group)
if (it_group->first == groupLC)
return std::string("Error: There is no command in group \"") + this->getToken(0) + "\" starting with \"" + this->getToken(1) + "\".";
@@ -327,7 +327,7 @@
unsigned int nearestDistance = (unsigned int)-1;
// iterate through all groups and their commands and calculate the distance to the current command. keep the best.
- for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getCommandsLC().begin(); it_group != ConsoleCommandManager::getCommandsLC().end(); ++it_group)
+ for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getInstance().getCommandsLC().begin(); it_group != ConsoleCommandManager::getInstance().getCommandsLC().end(); ++it_group)
{
if (it_group->first != "")
{
@@ -345,8 +345,8 @@
}
// now also iterate through all shortcuts and keep the best if it's better than the one found above.
- std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getCommandsLC().find("");
- if (it_group != ConsoleCommandManager::getCommandsLC().end())
+ std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getInstance().getCommandsLC().find("");
+ if (it_group != ConsoleCommandManager::getInstance().getCommandsLC().end())
{
for (std::map<std::string, ConsoleCommand*>::const_iterator it_name = it_group->second.begin(); it_name != it_group->second.end(); ++it_name)
{
Modified: code/branches/core7/src/libraries/core/command/CommandExecutor.cc
===================================================================
--- code/branches/core7/src/libraries/core/command/CommandExecutor.cc 2015-05-25 20:36:28 UTC (rev 10483)
+++ code/branches/core7/src/libraries/core/command/CommandExecutor.cc 2015-05-25 20:56:26 UTC (rev 10484)
@@ -154,19 +154,19 @@
evaluation.initialize(command);
// assign the fallback-command to get hints about the possible commands and groups
- evaluation.hintCommand_ = ConsoleCommandManager::getCommand(__CC_CommandExecutor_name, __CC_autocomplete_name);
+ evaluation.hintCommand_ = ConsoleCommandManager::getInstance().getCommand(__CC_CommandExecutor_name, __CC_autocomplete_name);
// check if there's at least one argument
if (evaluation.getNumberOfArguments() >= 1)
{
// try to get a command from the first token
- evaluation.execCommand_ = ConsoleCommandManager::getCommandLC(evaluation.getToken(0));
+ evaluation.execCommand_ = ConsoleCommandManager::getInstance().getCommandLC(evaluation.getToken(0));
if (evaluation.execCommand_)
evaluation.execArgumentsOffset_ = 1;
else if (evaluation.getNumberOfArguments() >= 2)
{
// try to get a command from the first two tokens
- evaluation.execCommand_ = ConsoleCommandManager::getCommandLC(evaluation.getToken(0), evaluation.getToken(1));
+ evaluation.execCommand_ = ConsoleCommandManager::getInstance().getCommandLC(evaluation.getToken(0), evaluation.getToken(1));
if (evaluation.execCommand_)
evaluation.execArgumentsOffset_ = 2;
}
@@ -287,7 +287,7 @@
SubString tokens(alias, " ");
// check if the alias already exists - print an error and return if it does
- if ((tokens.size() == 1 && ConsoleCommandManager::getCommand(tokens[0])) || (tokens.size() == 2 && ConsoleCommandManager::getCommand(tokens[0], tokens[1])))
+ if ((tokens.size() == 1 && ConsoleCommandManager::getInstance().getCommand(tokens[0])) || (tokens.size() == 2 && ConsoleCommandManager::getInstance().getCommand(tokens[0], tokens[1])))
{
orxout(user_error) << "A command with name \"" << alias << "\" already exists." << endl;
return;
@@ -295,9 +295,9 @@
// create a new console command with the given alias as its name
if (tokens.size() == 1)
- ConsoleCommandManager::registerCommand(new ConsoleCommand(tokens[0], executor));
+ ConsoleCommandManager::getInstance().registerCommand(new ConsoleCommand(tokens[0], executor));
else if (tokens.size() == 2)
- ConsoleCommandManager::registerCommand(new ConsoleCommand(tokens[0], tokens[1], executor));
+ ConsoleCommandManager::getInstance().registerCommand(new ConsoleCommand(tokens[0], tokens[1], executor));
else
orxout(user_error) << "\"" << alias << "\" is not a valid alias name (must have one or two words)." << endl;
}
Modified: code/branches/core7/src/libraries/core/command/ConsoleCommand.cc
===================================================================
--- code/branches/core7/src/libraries/core/command/ConsoleCommand.cc 2015-05-25 20:36:28 UTC (rev 10483)
+++ code/branches/core7/src/libraries/core/command/ConsoleCommand.cc 2015-05-25 20:56:26 UTC (rev 10484)
@@ -91,7 +91,7 @@
*/
ConsoleCommand::~ConsoleCommand()
{
- ConsoleCommandManager::unregisterCommand(this);
+ ConsoleCommandManager::getInstance().unregisterCommand(this);
}
/**
Modified: code/branches/core7/src/libraries/core/command/ConsoleCommandIncludes.cc
===================================================================
--- code/branches/core7/src/libraries/core/command/ConsoleCommandIncludes.cc 2015-05-25 20:36:28 UTC (rev 10483)
+++ code/branches/core7/src/libraries/core/command/ConsoleCommandIncludes.cc 2015-05-25 20:56:26 UTC (rev 10484)
@@ -34,11 +34,11 @@
{
void StaticallyInitializedConsoleCommand::load()
{
- ConsoleCommandManager::registerCommand(this->command_);
+ ConsoleCommandManager::getInstance().registerCommand(this->command_);
}
void StaticallyInitializedConsoleCommand::unload()
{
- ConsoleCommandManager::unregisterCommand(this->command_);
+ ConsoleCommandManager::getInstance().unregisterCommand(this->command_);
}
}
Modified: code/branches/core7/src/libraries/core/command/ConsoleCommandIncludes.h
===================================================================
--- code/branches/core7/src/libraries/core/command/ConsoleCommandIncludes.h 2015-05-25 20:36:28 UTC (rev 10483)
+++ code/branches/core7/src/libraries/core/command/ConsoleCommandIncludes.h 2015-05-25 20:56:26 UTC (rev 10484)
@@ -331,7 +331,7 @@
check internally if the command exists.
*/
inline ConsoleCommand::ConsoleCommandManipulator ModifyConsoleCommand(const std::string& name)
- { return ConsoleCommandManager::getCommand(name, true); }
+ { return ConsoleCommandManager::getInstance().getCommand(name, true); }
/**
@brief Returns a manipulator for a command with the given group and name.
@@ -340,7 +340,7 @@
check internally if the command exists.
*/
inline ConsoleCommand::ConsoleCommandManipulator ModifyConsoleCommand(const std::string& group, const std::string& name)
- { return ConsoleCommandManager::getCommand(group, name, true); }
+ { return ConsoleCommandManager::getInstance().getCommand(group, name, true); }
}
#endif /* _ConsoleCommandIncludes_H__ */
Modified: code/branches/core7/src/libraries/core/command/ConsoleCommandManager.cc
===================================================================
--- code/branches/core7/src/libraries/core/command/ConsoleCommandManager.cc 2015-05-25 20:36:28 UTC (rev 10483)
+++ code/branches/core7/src/libraries/core/command/ConsoleCommandManager.cc 2015-05-25 20:56:26 UTC (rev 10484)
@@ -38,17 +38,23 @@
namespace orxonox
{
+ /* static */ ConsoleCommandManager& ConsoleCommandManager::getInstance()
+ {
+ static ConsoleCommandManager instance;
+ return instance;
+ }
+
/**
@brief Returns the command with given group an name.
@param group The group of the requested command
@param name The group of the requested command
@param bPrintError If true, an error is printed if the command doesn't exist
*/
- /* static */ ConsoleCommand* ConsoleCommandManager::getCommand(const std::string& group, const std::string& name, bool bPrintError)
+ ConsoleCommand* ConsoleCommandManager::getCommand(const std::string& group, const std::string& name, bool bPrintError)
{
// find the group
- std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getCommandMap().find(group);
- if (it_group != ConsoleCommandManager::getCommandMap().end())
+ std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = this->commandMap_.find(group);
+ if (it_group != this->commandMap_.end())
{
// find the name
std::map<std::string, ConsoleCommand*>::const_iterator it_name = it_group->second.find(name);
@@ -74,14 +80,14 @@
@param name The group of the requested command in lowercase
@param bPrintError If true, an error is printed if the command doesn't exist
*/
- /* static */ ConsoleCommand* ConsoleCommandManager::getCommandLC(const std::string& group, const std::string& name, bool bPrintError)
+ ConsoleCommand* ConsoleCommandManager::getCommandLC(const std::string& group, const std::string& name, bool bPrintError)
{
std::string groupLC = getLowercase(group);
std::string nameLC = getLowercase(name);
// find the group
- std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getCommandMapLC().find(groupLC);
- if (it_group != ConsoleCommandManager::getCommandMapLC().end())
+ std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = this->commandMapLC_.find(groupLC);
+ if (it_group != this->commandMapLC_.end())
{
// find the name
std::map<std::string, ConsoleCommand*>::const_iterator it_name = it_group->second.find(nameLC);
@@ -102,45 +108,27 @@
}
/**
- @brief Returns the static map that stores all console commands.
- */
- /* static */ std::map<std::string, std::map<std::string, ConsoleCommand*> >& ConsoleCommandManager::getCommandMap()
- {
- static std::map<std::string, std::map<std::string, ConsoleCommand*> > commandMap;
- return commandMap;
- }
-
- /**
- @brief Returns the static map that stores all console commands in lowercase.
- */
- /* static */ std::map<std::string, std::map<std::string, ConsoleCommand*> >& ConsoleCommandManager::getCommandMapLC()
- {
- static std::map<std::string, std::map<std::string, ConsoleCommand*> > commandMapLC;
- return commandMapLC;
- }
-
- /**
@brief Registers a new command with the groups and names that are defined by ConsoleCommand::getNames().
*/
- /* static */ void ConsoleCommandManager::registerCommand(ConsoleCommand* command)
+ void ConsoleCommandManager::registerCommand(ConsoleCommand* command)
{
for (size_t i = 0; i < command->getNames().size(); ++i)
{
const ConsoleCommand::CommandName& name = command->getNames()[i];
- ConsoleCommandManager::registerCommand(name.group_, name.name_, command);
+ this->registerCommand(name.group_, name.name_, command);
}
}
/**
@brief Registers a new command with given group an name by adding it to the command map.
*/
- /* static */ void ConsoleCommandManager::registerCommand(const std::string& group, const std::string& name, ConsoleCommand* command)
+ void ConsoleCommandManager::registerCommand(const std::string& group, const std::string& name, ConsoleCommand* command)
{
if (name == "")
return;
// check if a command with this name already exists
- if (ConsoleCommandManager::getCommand(group, name) != 0)
+ if (this->getCommand(group, name) != 0)
{
if (group == "")
orxout(internal_warning, context::commands) << "A console command with shortcut \"" << name << "\" already exists." << endl;
@@ -150,18 +138,18 @@
else
{
// add the command to the map
- ConsoleCommandManager::getCommandMap()[group][name] = command;
- ConsoleCommandManager::getCommandMapLC()[getLowercase(group)][getLowercase(name)] = command;
+ this->commandMap_[group][name] = command;
+ this->commandMapLC_[getLowercase(group)][getLowercase(name)] = command;
}
}
/**
@brief Removes the command from the command map.
*/
- /* static */ void ConsoleCommandManager::unregisterCommand(ConsoleCommand* command)
+ void ConsoleCommandManager::unregisterCommand(ConsoleCommand* command)
{
// iterate through all groups
- for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::iterator it_group = ConsoleCommandManager::getCommandMap().begin(); it_group != ConsoleCommandManager::getCommandMap().end(); )
+ for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::iterator it_group = this->commandMap_.begin(); it_group != this->commandMap_.end(); )
{
// iterate through all commands of each group
for (std::map<std::string, ConsoleCommand*>::iterator it_name = it_group->second.begin(); it_name != it_group->second.end(); )
@@ -175,7 +163,7 @@
// erase the group if it is empty now
if (it_group->second.empty())
- ConsoleCommandManager::getCommandMap().erase(it_group++);
+ this->commandMap_.erase(it_group++);
else
++it_group;
}
@@ -183,7 +171,7 @@
// now the same for the lowercase-map:
// iterate through all groups
- for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::iterator it_group = ConsoleCommandManager::getCommandMapLC().begin(); it_group != ConsoleCommandManager::getCommandMapLC().end(); )
+ for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::iterator it_group = this->commandMapLC_.begin(); it_group != this->commandMapLC_.end(); )
{
// iterate through all commands of each group
for (std::map<std::string, ConsoleCommand*>::iterator it_name = it_group->second.begin(); it_name != it_group->second.end(); )
@@ -197,7 +185,7 @@
// erase the group if it is empty now
if (it_group->second.empty())
- ConsoleCommandManager::getCommandMapLC().erase(it_group++);
+ this->commandMapLC_.erase(it_group++);
else
++it_group;
}
@@ -206,10 +194,10 @@
/**
@brief Deletes all commands
*/
- /* static */ void ConsoleCommandManager::destroyAll()
+ void ConsoleCommandManager::destroyAll()
{
// delete entries until the map is empty
- while (!ConsoleCommandManager::getCommandMap().empty() && !ConsoleCommandManager::getCommandMap().begin()->second.empty())
- delete ConsoleCommandManager::getCommandMap().begin()->second.begin()->second;
+ while (!this->commandMap_.empty() && !this->commandMap_.begin()->second.empty())
+ delete this->commandMap_.begin()->second.begin()->second;
}
}
Modified: code/branches/core7/src/libraries/core/command/ConsoleCommandManager.h
===================================================================
--- code/branches/core7/src/libraries/core/command/ConsoleCommandManager.h 2015-05-25 20:36:28 UTC (rev 10483)
+++ code/branches/core7/src/libraries/core/command/ConsoleCommandManager.h 2015-05-25 20:56:26 UTC (rev 10484)
@@ -39,37 +39,39 @@
namespace orxonox
{
/**
- * A static class that stores all existing ConsoleCommands.
+ * A singleton that stores all existing ConsoleCommands.
*/
class _CoreExport ConsoleCommandManager
{
public:
- static void registerCommand(ConsoleCommand* command);
- static void registerCommand(const std::string& group, const std::string& name, ConsoleCommand* command);
- static void unregisterCommand(ConsoleCommand* command);
+ static ConsoleCommandManager& getInstance();
+ void registerCommand(ConsoleCommand* command);
+ void registerCommand(const std::string& group, const std::string& name, ConsoleCommand* command);
+ void unregisterCommand(ConsoleCommand* command);
+
/// Returns the map with all groups and commands.
- static inline const std::map<std::string, std::map<std::string, ConsoleCommand*> >& getCommands()
- { return ConsoleCommandManager::getCommandMap(); }
+ inline const std::map<std::string, std::map<std::string, ConsoleCommand*> >& getCommands()
+ { return this->commandMap_; }
/// Returns the map with all groups and commands in lowercase.
- static inline const std::map<std::string, std::map<std::string, ConsoleCommand*> >& getCommandsLC()
- { return ConsoleCommandManager::getCommandMapLC(); }
+ inline const std::map<std::string, std::map<std::string, ConsoleCommand*> >& getCommandsLC()
+ { return this->commandMapLC_; }
/// Returns a command (shortcut) with given name. @param name The name of the command shortcut @param bPrintError If true, an error is printed if the command doesn't exist
- static inline ConsoleCommand* getCommand(const std::string& name, bool bPrintError = false)
- { return ConsoleCommandManager::getCommand("", name, bPrintError); }
+ inline ConsoleCommand* getCommand(const std::string& name, bool bPrintError = false)
+ { return this->getCommand("", name, bPrintError); }
/// Returns a command (shortcut) with given name in lowercase. @param name The lowercase name of the command shortcut @param bPrintError If true, an error is printed if the command doesn't exist
- static inline ConsoleCommand* getCommandLC(const std::string& name, bool bPrintError = false)
- { return ConsoleCommandManager::getCommandLC("", name, bPrintError); }
+ inline ConsoleCommand* getCommandLC(const std::string& name, bool bPrintError = false)
+ { return this->getCommandLC("", name, bPrintError); }
- static ConsoleCommand* getCommand(const std::string& group, const std::string& name, bool bPrintError = false);
- static ConsoleCommand* getCommandLC(const std::string& group, const std::string& name, bool bPrintError = false);
+ ConsoleCommand* getCommand(const std::string& group, const std::string& name, bool bPrintError = false);
+ ConsoleCommand* getCommandLC(const std::string& group, const std::string& name, bool bPrintError = false);
- static void destroyAll();
+ void destroyAll();
private:
- static std::map<std::string, std::map<std::string, ConsoleCommand*> >& getCommandMap();
- static std::map<std::string, std::map<std::string, ConsoleCommand*> >& getCommandMapLC();
+ std::map<std::string, std::map<std::string, ConsoleCommand*> > commandMap_;
+ std::map<std::string, std::map<std::string, ConsoleCommand*> > commandMapLC_;
};
}
More information about the Orxonox-commit
mailing list