[Orxonox-commit 5686] r10346 - in code/branches/core7/src/libraries/core: . command
landauf at orxonox.net
landauf at orxonox.net
Sun Apr 5 20:25:11 CEST 2015
Author: landauf
Date: 2015-04-05 20:25:11 +0200 (Sun, 05 Apr 2015)
New Revision: 10346
Added:
code/branches/core7/src/libraries/core/command/ConsoleCommandManager.cc
code/branches/core7/src/libraries/core/command/ConsoleCommandManager.h
Modified:
code/branches/core7/src/libraries/core/Core.cc
code/branches/core7/src/libraries/core/CorePrereqs.h
code/branches/core7/src/libraries/core/command/ArgumentCompletionFunctions.cc
code/branches/core7/src/libraries/core/command/CMakeLists.txt
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/ConsoleCommand.h
Log:
moved static contents from ConsoleCommand to ConsoleCommandManager
Modified: code/branches/core7/src/libraries/core/Core.cc
===================================================================
--- code/branches/core7/src/libraries/core/Core.cc 2015-04-05 15:32:40 UTC (rev 10345)
+++ code/branches/core7/src/libraries/core/Core.cc 2015-04-05 18:25:11 UTC (rev 10346)
@@ -253,7 +253,7 @@
safeObjectDelete(&ioConsole_);
safeObjectDelete(&languageInstance_);
safeObjectDelete(&configFileManager_);
- ConsoleCommand::destroyAll();
+ ConsoleCommandManager::destroyAll();
Context::setRootContext(NULL);
IdentifierManager::getInstance().destroyAllIdentifiers();
safeObjectDelete(&signalHandler_);
Modified: code/branches/core7/src/libraries/core/CorePrereqs.h
===================================================================
--- code/branches/core7/src/libraries/core/CorePrereqs.h 2015-04-05 15:32:40 UTC (rev 10345)
+++ code/branches/core7/src/libraries/core/CorePrereqs.h 2015-04-05 18:25:11 UTC (rev 10346)
@@ -211,6 +211,7 @@
class ArgumentCompletionListElement;
class CommandEvaluation;
class ConsoleCommand;
+ class ConsoleCommandManager;
class Executor;
template <class T>
class ExecutorMember;
Modified: code/branches/core7/src/libraries/core/command/ArgumentCompletionFunctions.cc
===================================================================
--- code/branches/core7/src/libraries/core/command/ArgumentCompletionFunctions.cc 2015-04-05 15:32:40 UTC (rev 10345)
+++ code/branches/core7/src/libraries/core/command/ArgumentCompletionFunctions.cc 2015-04-05 18:25:11 UTC (rev 10346)
@@ -97,7 +97,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 = ConsoleCommand::getCommands();
+ const std::map<std::string, std::map<std::string, ConsoleCommand*> >& commands = ConsoleCommandManager::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)));
@@ -136,13 +136,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 = ConsoleCommand::getCommands().begin();
- for ( ; it_group != ConsoleCommand::getCommands().end(); ++it_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)
if (getLowercase(it_group->first) == groupLC)
break;
// add all commands in the group to the list
- if (it_group != ConsoleCommand::getCommands().end())
+ if (it_group != ConsoleCommandManager::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)
@@ -205,19 +205,19 @@
if (tokens.size() == 0)
return detail::_groupsandcommands(fragment, true);
- if (ConsoleCommand::getCommandLC(getLowercase(tokens[0])))
+ if (ConsoleCommandManager::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 = ConsoleCommand::getCommands().find(tokens[0]);
- if (it_group != ConsoleCommand::getCommands().end())
+ std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getCommands().find(tokens[0]);
+ if (it_group != ConsoleCommandManager::getCommands().end())
return detail::_subcommands(fragment, tokens[0], true);
else
return detail::_groupsandcommands(fragment, true);
}
- if (ConsoleCommand::getCommandLC(getLowercase(tokens[0]), getLowercase(tokens[1])))
+ if (ConsoleCommandManager::getCommandLC(getLowercase(tokens[0]), getLowercase(tokens[1])))
return ARGUMENT_COMPLETION_FUNCTION_CALL(command)(fragment);
return ArgumentCompletionList();
Modified: code/branches/core7/src/libraries/core/command/CMakeLists.txt
===================================================================
--- code/branches/core7/src/libraries/core/command/CMakeLists.txt 2015-04-05 15:32:40 UTC (rev 10345)
+++ code/branches/core7/src/libraries/core/command/CMakeLists.txt 2015-04-05 18:25:11 UTC (rev 10346)
@@ -3,6 +3,7 @@
CommandExecutor.cc
ConsoleCommand.cc
ConsoleCommandCompilation.cc
+ ConsoleCommandManager.cc
Executor.cc
IOConsole.cc
IRC.cc
Modified: code/branches/core7/src/libraries/core/command/CommandEvaluation.cc
===================================================================
--- code/branches/core7/src/libraries/core/command/CommandEvaluation.cc 2015-04-05 15:32:40 UTC (rev 10345)
+++ code/branches/core7/src/libraries/core/command/CommandEvaluation.cc 2015-04-05 18:25:11 UTC (rev 10346)
@@ -304,7 +304,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 = ConsoleCommand::getCommandsLC().begin(); it_group != ConsoleCommand::getCommandsLC().end(); ++it_group)
+ for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getCommandsLC().begin(); it_group != ConsoleCommandManager::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) + "\".";
@@ -326,7 +326,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 = ConsoleCommand::getCommandsLC().begin(); it_group != ConsoleCommand::getCommandsLC().end(); ++it_group)
+ for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getCommandsLC().begin(); it_group != ConsoleCommandManager::getCommandsLC().end(); ++it_group)
{
if (it_group->first != "")
{
@@ -344,8 +344,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 = ConsoleCommand::getCommandsLC().find("");
- if (it_group != ConsoleCommand::getCommandsLC().end())
+ std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getCommandsLC().find("");
+ if (it_group != ConsoleCommandManager::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-04-05 15:32:40 UTC (rev 10345)
+++ code/branches/core7/src/libraries/core/command/CommandExecutor.cc 2015-04-05 18:25:11 UTC (rev 10346)
@@ -154,19 +154,19 @@
evaluation.initialize(command);
// assign the fallback-command to get hints about the possible commands and groups
- evaluation.hintCommand_ = ConsoleCommand::getCommand(__CC_CommandExecutor_name, __CC_autocomplete_name);
+ evaluation.hintCommand_ = ConsoleCommandManager::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_ = ConsoleCommand::getCommandLC(evaluation.getToken(0));
+ evaluation.execCommand_ = ConsoleCommandManager::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_ = ConsoleCommand::getCommandLC(evaluation.getToken(0), evaluation.getToken(1));
+ evaluation.execCommand_ = ConsoleCommandManager::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 && ConsoleCommand::getCommand(tokens[0])) || (tokens.size() == 2 && ConsoleCommand::getCommand(tokens[0], tokens[1])))
+ if ((tokens.size() == 1 && ConsoleCommandManager::getCommand(tokens[0])) || (tokens.size() == 2 && ConsoleCommandManager::getCommand(tokens[0], tokens[1])))
{
orxout(user_error) << "A command with name \"" << alias << "\" already exists." << endl;
return;
Modified: code/branches/core7/src/libraries/core/command/ConsoleCommand.cc
===================================================================
--- code/branches/core7/src/libraries/core/command/ConsoleCommand.cc 2015-04-05 15:32:40 UTC (rev 10345)
+++ code/branches/core7/src/libraries/core/command/ConsoleCommand.cc 2015-04-05 18:25:11 UTC (rev 10346)
@@ -34,7 +34,6 @@
#include "ConsoleCommand.h"
#include "util/Convert.h"
-#include "util/StringUtils.h"
#include "core/Language.h"
#include "core/GameMode.h"
#include "core/input/KeyBinder.h"
@@ -67,7 +66,7 @@
if (bInitialized)
this->executor_ = executor;
- ConsoleCommand::registerCommand(group, name, this);
+ ConsoleCommandManager::registerCommand(group, name, this);
}
/**
@@ -75,7 +74,7 @@
*/
ConsoleCommand::~ConsoleCommand()
{
- ConsoleCommand::unregisterCommand(this);
+ ConsoleCommandManager::unregisterCommand(this);
}
/**
@@ -83,7 +82,7 @@
*/
ConsoleCommand& ConsoleCommand::addShortcut()
{
- ConsoleCommand::registerCommand("", this->baseName_, this);
+ ConsoleCommandManager::registerCommand("", this->baseName_, this);
return *this;
}
@@ -92,7 +91,7 @@
*/
ConsoleCommand& ConsoleCommand::addShortcut(const std::string& name)
{
- ConsoleCommand::registerCommand("", name, this);
+ ConsoleCommandManager::registerCommand("", name, this);
return *this;
}
@@ -101,7 +100,7 @@
*/
ConsoleCommand& ConsoleCommand::addGroup(const std::string& group)
{
- ConsoleCommand::registerCommand(group, this->baseName_, this);
+ ConsoleCommandManager::registerCommand(group, this->baseName_, this);
return *this;
}
@@ -110,7 +109,7 @@
*/
ConsoleCommand& ConsoleCommand::addGroup(const std::string& group, const std::string& name)
{
- ConsoleCommand::registerCommand(group, name, this);
+ ConsoleCommandManager::registerCommand(group, name, this);
return *this;
}
@@ -586,167 +585,4 @@
this->keybindMode(mode);
return *this;
}
-
- /**
- @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* ConsoleCommand::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 = ConsoleCommand::getCommandMap().find(group);
- if (it_group != ConsoleCommand::getCommandMap().end())
- {
- // find the name
- std::map<std::string, ConsoleCommand*>::const_iterator it_name = it_group->second.find(name);
- if (it_name != it_group->second.end())
- {
- // return the pointer
- return it_name->second;
- }
- }
- if (bPrintError)
- {
- if (group == "")
- orxout(internal_error, context::commands) << "Couldn't find console command with shortcut \"" << name << "\"" << endl;
- else
- orxout(internal_error, context::commands) << "Couldn't find console command with group \"" << group << "\" and name \"" << name << "\"" << endl;
- }
- return 0;
- }
-
- /**
- @brief Returns the command with given group an name in lowercase.
- @param group The group of the requested command in lowercase
- @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* ConsoleCommand::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 = ConsoleCommand::getCommandMapLC().find(groupLC);
- if (it_group != ConsoleCommand::getCommandMapLC().end())
- {
- // find the name
- std::map<std::string, ConsoleCommand*>::const_iterator it_name = it_group->second.find(nameLC);
- if (it_name != it_group->second.end())
- {
- // return the pointer
- return it_name->second;
- }
- }
- if (bPrintError)
- {
- if (group == "")
- orxout(internal_error, context::commands) << "Couldn't find console command with shortcut \"" << name << "\"" << endl;
- else
- orxout(internal_error, context::commands) << "Couldn't find console command with group \"" << group << "\" and name \"" << name << "\"" << endl;
- }
- return 0;
- }
-
- /**
- @brief Returns the static map that stores all console commands.
- */
- /* static */ std::map<std::string, std::map<std::string, ConsoleCommand*> >& ConsoleCommand::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*> >& ConsoleCommand::getCommandMapLC()
- {
- static std::map<std::string, std::map<std::string, ConsoleCommand*> > commandMapLC;
- return commandMapLC;
- }
-
- /**
- @brief Registers a new command with given group an name by adding it to the command map.
- */
- /* static */ void ConsoleCommand::registerCommand(const std::string& group, const std::string& name, ConsoleCommand* command)
- {
- if (name == "")
- return;
-
- // check if a command with this name already exists
- if (ConsoleCommand::getCommand(group, name) != 0)
- {
- if (group == "")
- orxout(internal_warning, context::commands) << "A console command with shortcut \"" << name << "\" already exists." << endl;
- else
- orxout(internal_warning, context::commands) << "A console command with name \"" << name << "\" already exists in group \"" << group << "\"." << endl;
- }
- else
- {
- // add the command to the map
- ConsoleCommand::getCommandMap()[group][name] = command;
- ConsoleCommand::getCommandMapLC()[getLowercase(group)][getLowercase(name)] = command;
- }
- }
-
- /**
- @brief Removes the command from the command map.
- */
- /* static */ void ConsoleCommand::unregisterCommand(ConsoleCommand* command)
- {
- // iterate through all groups
- for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::iterator it_group = ConsoleCommand::getCommandMap().begin(); it_group != ConsoleCommand::getCommandMap().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(); )
- {
- // erase the command
- if (it_name->second == command)
- it_group->second.erase(it_name++);
- else
- ++it_name;
- }
-
- // erase the group if it is empty now
- if (it_group->second.empty())
- ConsoleCommand::getCommandMap().erase(it_group++);
- else
- ++it_group;
- }
-
- // now the same for the lowercase-map:
-
- // iterate through all groups
- for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::iterator it_group = ConsoleCommand::getCommandMapLC().begin(); it_group != ConsoleCommand::getCommandMapLC().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(); )
- {
- // erase the command
- if (it_name->second == command)
- it_group->second.erase(it_name++);
- else
- ++it_name;
- }
-
- // erase the group if it is empty now
- if (it_group->second.empty())
- ConsoleCommand::getCommandMapLC().erase(it_group++);
- else
- ++it_group;
- }
- }
-
- /**
- @brief Deletes all commands
- */
- /* static */ void ConsoleCommand::destroyAll()
- {
- // delete entries until the map is empty
- while (!ConsoleCommand::getCommandMap().empty() && !ConsoleCommand::getCommandMap().begin()->second.empty())
- delete ConsoleCommand::getCommandMap().begin()->second.begin()->second;
- }
}
Modified: code/branches/core7/src/libraries/core/command/ConsoleCommand.h
===================================================================
--- code/branches/core7/src/libraries/core/command/ConsoleCommand.h 2015-04-05 15:32:40 UTC (rev 10345)
+++ code/branches/core7/src/libraries/core/command/ConsoleCommand.h 2015-04-05 18:25:11 UTC (rev 10346)
@@ -225,6 +225,7 @@
#include "util/VA_NARGS.h"
#include "ArgumentCompletionFunctions.h"
#include "Executor.h"
+#include "ConsoleCommandManager.h"
/**
@@ -654,33 +655,6 @@
LanguageEntryLabel description_; ///< The description of the command
LanguageEntryLabel descriptionReturnvalue_; ///< A description of the return-value
LanguageEntryLabel descriptionParam_[MAX_FUNCTOR_ARGUMENTS]; ///< A description for each argument
-
- public:
- /// Returns the map with all groups and commands.
- static inline const std::map<std::string, std::map<std::string, ConsoleCommand*> >& getCommands()
- { return ConsoleCommand::getCommandMap(); }
- /// Returns the map with all groups and commands in lowercase.
- static inline const std::map<std::string, std::map<std::string, ConsoleCommand*> >& getCommandsLC()
- { return ConsoleCommand::getCommandMapLC(); }
-
- /// 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 ConsoleCommand::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 ConsoleCommand::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);
-
- static 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();
-
- static void registerCommand(const std::string& group, const std::string& name, ConsoleCommand* command);
- static void unregisterCommand(ConsoleCommand* command);
};
/**
@@ -710,7 +684,7 @@
check internally if the command exists.
*/
inline ConsoleCommand::ConsoleCommandManipulator ModifyConsoleCommand(const std::string& name)
- { return ConsoleCommand::getCommand(name, true); }
+ { return ConsoleCommandManager::getCommand(name, true); }
/**
@brief Returns a manipulator for a command with the given group and name.
@@ -719,7 +693,7 @@
check internally if the command exists.
*/
inline ConsoleCommand::ConsoleCommandManipulator ModifyConsoleCommand(const std::string& group, const std::string& name)
- { return ConsoleCommand::getCommand(group, name, true); }
+ { return ConsoleCommandManager::getCommand(group, name, true); }
}
#endif /* _ConsoleCommand_H__ */
Added: code/branches/core7/src/libraries/core/command/ConsoleCommandManager.cc
===================================================================
--- code/branches/core7/src/libraries/core/command/ConsoleCommandManager.cc (rev 0)
+++ code/branches/core7/src/libraries/core/command/ConsoleCommandManager.cc 2015-04-05 18:25:11 UTC (rev 10346)
@@ -0,0 +1,203 @@
+/*
+ * ORXONOX - the hottest 3D action shooter ever to exist
+ * > www.orxonox.net <
+ *
+ *
+ * License notice:
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Author:
+ * Fabian 'x3n' Landau
+ * Co-authors:
+ * ...
+ *
+ */
+
+/**
+ @file
+ @brief Implementation of the ConsoleCommand class.
+*/
+
+#include "ConsoleCommandManager.h"
+
+#include "ConsoleCommand.h"
+#include "util/StringUtils.h"
+
+namespace orxonox
+{
+ /**
+ @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)
+ {
+ // 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())
+ {
+ // find the name
+ std::map<std::string, ConsoleCommand*>::const_iterator it_name = it_group->second.find(name);
+ if (it_name != it_group->second.end())
+ {
+ // return the pointer
+ return it_name->second;
+ }
+ }
+ if (bPrintError)
+ {
+ if (group == "")
+ orxout(internal_error, context::commands) << "Couldn't find console command with shortcut \"" << name << "\"" << endl;
+ else
+ orxout(internal_error, context::commands) << "Couldn't find console command with group \"" << group << "\" and name \"" << name << "\"" << endl;
+ }
+ return 0;
+ }
+
+ /**
+ @brief Returns the command with given group an name in lowercase.
+ @param group The group of the requested command in lowercase
+ @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)
+ {
+ 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())
+ {
+ // find the name
+ std::map<std::string, ConsoleCommand*>::const_iterator it_name = it_group->second.find(nameLC);
+ if (it_name != it_group->second.end())
+ {
+ // return the pointer
+ return it_name->second;
+ }
+ }
+ if (bPrintError)
+ {
+ if (group == "")
+ orxout(internal_error, context::commands) << "Couldn't find console command with shortcut \"" << name << "\"" << endl;
+ else
+ orxout(internal_error, context::commands) << "Couldn't find console command with group \"" << group << "\" and name \"" << name << "\"" << endl;
+ }
+ return 0;
+ }
+
+ /**
+ @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 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)
+ {
+ if (name == "")
+ return;
+
+ // check if a command with this name already exists
+ if (ConsoleCommandManager::getCommand(group, name) != 0)
+ {
+ if (group == "")
+ orxout(internal_warning, context::commands) << "A console command with shortcut \"" << name << "\" already exists." << endl;
+ else
+ orxout(internal_warning, context::commands) << "A console command with name \"" << name << "\" already exists in group \"" << group << "\"." << endl;
+ }
+ else
+ {
+ // add the command to the map
+ ConsoleCommandManager::getCommandMap()[group][name] = command;
+ ConsoleCommandManager::getCommandMapLC()[getLowercase(group)][getLowercase(name)] = command;
+ }
+ }
+
+ /**
+ @brief Removes the command from the command map.
+ */
+ /* static */ 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(); )
+ {
+ // 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(); )
+ {
+ // erase the command
+ if (it_name->second == command)
+ it_group->second.erase(it_name++);
+ else
+ ++it_name;
+ }
+
+ // erase the group if it is empty now
+ if (it_group->second.empty())
+ ConsoleCommandManager::getCommandMap().erase(it_group++);
+ else
+ ++it_group;
+ }
+
+ // 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(); )
+ {
+ // 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(); )
+ {
+ // erase the command
+ if (it_name->second == command)
+ it_group->second.erase(it_name++);
+ else
+ ++it_name;
+ }
+
+ // erase the group if it is empty now
+ if (it_group->second.empty())
+ ConsoleCommandManager::getCommandMapLC().erase(it_group++);
+ else
+ ++it_group;
+ }
+ }
+
+ /**
+ @brief Deletes all commands
+ */
+ /* static */ 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;
+ }
+}
Added: code/branches/core7/src/libraries/core/command/ConsoleCommandManager.h
===================================================================
--- code/branches/core7/src/libraries/core/command/ConsoleCommandManager.h (rev 0)
+++ code/branches/core7/src/libraries/core/command/ConsoleCommandManager.h 2015-04-05 18:25:11 UTC (rev 10346)
@@ -0,0 +1,75 @@
+/*
+ * ORXONOX - the hottest 3D action shooter ever to exist
+ * > www.orxonox.net <
+ *
+ *
+ * License notice:
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Author:
+ * Fabian 'x3n' Landau
+ * Co-authors:
+ * ...
+ *
+ */
+
+/**
+ @defgroup ConsoleCommand Console commands
+ @ingroup Command
+*/
+
+#ifndef _ConsoleCommandManager_H__
+#define _ConsoleCommandManager_H__
+
+#include "core/CorePrereqs.h"
+
+namespace orxonox
+{
+ /**
+ * A static class that stores all existing ConsoleCommands.
+ */
+ class _CoreExport ConsoleCommandManager
+ {
+ public:
+ static void registerCommand(const std::string& group, const std::string& name, ConsoleCommand* command);
+ static 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(); }
+ /// 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(); }
+
+ /// 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); }
+ /// 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); }
+
+ 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);
+
+ static 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();
+ };
+}
+
+#endif /* _ConsoleCommandManager_H__ */
Property changes on: code/branches/core7/src/libraries/core/command/ConsoleCommandManager.h
___________________________________________________________________
Added: svn:eol-style
+ native
More information about the Orxonox-commit
mailing list