- Timestamp:
- Aug 25, 2010, 7:07:08 PM (14 years ago)
- Location:
- code/branches/consolecommands3/src/libraries/core
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/consolecommands3/src/libraries/core/Identifier.cc
r7204 r7216 40 40 #include "ClassFactory.h" 41 41 #include "XMLPort.h" 42 #include "command/ConsoleCommand.h"43 42 44 43 namespace orxonox … … 64 63 65 64 this->bHasConfigValues_ = false; 66 this->bHasConsoleCommands_ = false;67 65 68 66 // Default network ID is the class ID … … 80 78 delete this->factory_; 81 79 82 for (std::map<std::string, ConsoleCommand*>::iterator it = this->consoleCommands_.begin(); it != this->consoleCommands_.end(); ++it)83 delete (it->second);84 80 for (std::map<std::string, ConfigValueContainer*>::iterator it = this->configValues_.begin(); it != this->configValues_.end(); ++it) 85 81 delete (it->second); … … 433 429 434 430 /** 435 @brief Adds a new console command of this class.436 @param executor The executor of the command437 @param bCreateShortcut If this is true a shortcut gets created so you don't have to add the classname to access this command438 @return The executor of the command439 */440 ConsoleCommand& Identifier::addConsoleCommand(ConsoleCommand* command, bool bCreateShortcut)441 {442 std::map<std::string, ConsoleCommand*>::const_iterator it = this->consoleCommands_.find(command->getName());443 if (it != this->consoleCommands_.end())444 {445 COUT(2) << "Warning: Overwriting console-command with name " << command->getName() << " in class " << this->getName() << '.' << std::endl;446 delete (it->second);447 }448 449 this->bHasConsoleCommands_ = true;450 this->consoleCommands_[command->getName()] = command;451 this->consoleCommands_LC_[getLowercase(command->getName())] = command;452 453 if (bCreateShortcut)454 CommandExecutor::addConsoleCommandShortcut(command);455 456 return (*command);457 }458 459 /**460 @brief Returns the executor of a console command with given name.461 @brief name The name of the requested console command462 @return The executor of the requested console command463 */464 ConsoleCommand* Identifier::getConsoleCommand(const std::string& name) const465 {466 std::map<std::string, ConsoleCommand*>::const_iterator it = this->consoleCommands_.find(name);467 if (it != this->consoleCommands_.end())468 return it->second;469 else470 return 0;471 }472 473 /**474 @brief Returns the executor of a console command with given name in lowercase.475 @brief name The name of the requested console command in lowercae476 @return The executor of the requested console command477 */478 ConsoleCommand* Identifier::getLowercaseConsoleCommand(const std::string& name) const479 {480 std::map<std::string, ConsoleCommand*>::const_iterator it = this->consoleCommands_LC_.find(name);481 if (it != this->consoleCommands_LC_.end())482 return it->second;483 else484 return 0;485 }486 487 /**488 431 @brief Returns a XMLPortParamContainer that loads a parameter of this class. 489 432 @param paramname The name of the parameter -
code/branches/consolecommands3/src/libraries/core/Identifier.h
r6536 r7216 202 202 void addConfigValueContainer(const std::string& varname, ConfigValueContainer* container); 203 203 ConfigValueContainer* getConfigValueContainer(const std::string& varname); 204 205 206 ////////////////////////////207 ///// Console Commands /////208 ////////////////////////////209 /** @brief Returns true if this class has at least one console command. @return True if this class has at least one console command */210 inline bool hasConsoleCommands() const { return this->bHasConsoleCommands_; }211 212 /** @brief Returns the map that stores all console commands. @return The const_iterator */213 inline const std::map<std::string, ConsoleCommand*>& getConsoleCommandMap() const { return this->consoleCommands_; }214 /** @brief Returns a const_iterator to the beginning of the map that stores all console commands. @return The const_iterator */215 inline std::map<std::string, ConsoleCommand*>::const_iterator getConsoleCommandMapBegin() const { return this->consoleCommands_.begin(); }216 /** @brief Returns a const_iterator to the end of the map that stores all console commands. @return The const_iterator */217 inline std::map<std::string, ConsoleCommand*>::const_iterator getConsoleCommandMapEnd() const { return this->consoleCommands_.end(); }218 219 /** @brief Returns the map that stores all console commands with their names in lowercase. @return The const_iterator */220 inline const std::map<std::string, ConsoleCommand*>& getLowercaseConsoleCommandMap() const { return this->consoleCommands_LC_; }221 /** @brief Returns a const_iterator to the beginning of the map that stores all console commands with their names in lowercase. @return The const_iterator */222 inline std::map<std::string, ConsoleCommand*>::const_iterator getLowercaseConsoleCommandMapBegin() const { return this->consoleCommands_LC_.begin(); }223 /** @brief Returns a const_iterator to the end of the map that stores all console commands with their names in lowercase. @return The const_iterator */224 inline std::map<std::string, ConsoleCommand*>::const_iterator getLowercaseConsoleCommandMapEnd() const { return this->consoleCommands_LC_.end(); }225 226 ConsoleCommand& addConsoleCommand(ConsoleCommand* command, bool bCreateShortcut);227 ConsoleCommand* getConsoleCommand(const std::string& name) const;228 ConsoleCommand* getLowercaseConsoleCommand(const std::string& name) const;229 204 230 205 … … 305 280 bool bHasConfigValues_; //!< True if this class has at least one assigned config value 306 281 std::map<std::string, ConfigValueContainer*> configValues_; //!< A map to link the string of configurable variables with their ConfigValueContainer 307 308 bool bHasConsoleCommands_; //!< True if this class has at least one assigned console command309 std::map<std::string, ConsoleCommand*> consoleCommands_; //!< All console commands of this class310 std::map<std::string, ConsoleCommand*> consoleCommands_LC_; //!< All console commands of this class with their names in lowercase311 282 312 283 std::map<std::string, XMLPortParamContainer*> xmlportParamContainers_; //!< All loadable parameters -
code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.cc
r7203 r7216 54 54 } 55 55 56 ConsoleCommand& CommandExecutor::addConsoleCommandShortcut(ConsoleCommand* command, bool bDeleteAtExit)57 {58 std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getInstance().consoleCommandShortcuts_.find(command->getName());59 if (it != CommandExecutor::getInstance().consoleCommandShortcuts_.end())60 {61 COUT(2) << "Warning: Overwriting console-command shortcut with name " << command->getName() << '.' << std::endl;62 }63 64 // Make sure we can also delete the external ConsoleCommands that don't belong to an Identifier65 if (command && bDeleteAtExit)66 {67 CommandExecutor::getInstance().consoleCommandExternals_.insert(command);68 }69 70 CommandExecutor::getInstance().consoleCommandShortcuts_[command->getName()] = command;71 CommandExecutor::getInstance().consoleCommandShortcuts_LC_[getLowercase(command->getName())] = command;72 return (*command);73 }74 75 /**76 @brief Returns the executor of a console command shortcut with given name.77 @brief name The name of the requested console command shortcut78 @return The executor of the requested console command shortcut79 */80 ConsoleCommand* CommandExecutor::getConsoleCommandShortcut(const std::string& name)81 {82 std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getInstance().consoleCommandShortcuts_.find(name);83 if (it != CommandExecutor::getInstance().consoleCommandShortcuts_.end())84 return it->second;85 else86 return 0;87 }88 89 /**90 @brief Returns the executor of a console command shortcut with given name in lowercase.91 @brief name The name of the requested console command shortcut in lowercase92 @return The executor of the requested console command shortcut93 */94 ConsoleCommand* CommandExecutor::getLowercaseConsoleCommandShortcut(const std::string& name)95 {96 std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getInstance().consoleCommandShortcuts_LC_.find(name);97 if (it != CommandExecutor::getInstance().consoleCommandShortcuts_LC_.end())98 return it->second;99 else100 return 0;101 }102 103 56 bool CommandExecutor::execute(const std::string& command, bool useTcl) 104 57 { -
code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.h
r7203 r7216 57 57 static const CommandEvaluation& getLastEvaluation(); 58 58 59 static ConsoleCommand& addConsoleCommandShortcut(ConsoleCommand* command, bool bDeleteAtExit = false);60 static ConsoleCommand* getConsoleCommandShortcut(const std::string& name);61 static ConsoleCommand* getLowercaseConsoleCommandShortcut(const std::string& name);62 63 /** @brief Returns the map that stores all console commands. @return The const_iterator */64 static inline const std::map<std::string, ConsoleCommand*>& getConsoleCommandShortcutMap() { return CommandExecutor::getInstance().consoleCommandShortcuts_; }65 /** @brief Returns a const_iterator to the beginning of the map that stores all console commands. @return The const_iterator */66 static inline std::map<std::string, ConsoleCommand*>::const_iterator getConsoleCommandShortcutMapBegin() { return CommandExecutor::getInstance().consoleCommandShortcuts_.begin(); }67 /** @brief Returns a const_iterator to the end of the map that stores all console commands. @return The const_iterator */68 static inline std::map<std::string, ConsoleCommand*>::const_iterator getConsoleCommandShortcutMapEnd() { return CommandExecutor::getInstance().consoleCommandShortcuts_.end(); }69 70 /** @brief Returns the map that stores all console commands with their names in lowercase. @return The const_iterator */71 static inline const std::map<std::string, ConsoleCommand*>& getLowercaseConsoleCommandShortcutMap() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_; }72 /** @brief Returns a const_iterator to the beginning of the map that stores all console commands with their names in lowercase. @return The const_iterator */73 static inline std::map<std::string, ConsoleCommand*>::const_iterator getLowercaseConsoleCommandShortcutMapBegin() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_.begin(); }74 /** @brief Returns a const_iterator to the end of the map that stores all console commands with their names in lowercase. @return The const_iterator */75 static inline std::map<std::string, ConsoleCommand*>::const_iterator getLowercaseConsoleCommandShortcutMapEnd() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_.end(); }76 77 static void destroyExternalCommands();78 79 59 private: 80 60 CommandExecutor() {} … … 107 87 108 88 CommandEvaluation evaluation_; 109 std::map<std::string, ConsoleCommand*> consoleCommandShortcuts_;110 std::map<std::string, ConsoleCommand*> consoleCommandShortcuts_LC_;111 std::set<ConsoleCommand*> consoleCommandExternals_;112 89 }; // tolua_export 113 90 } // tolua_export -
code/branches/consolecommands3/src/libraries/core/command/ConsoleCommand.cc
r7215 r7216 28 28 29 29 #include "ConsoleCommand.h" 30 #include <cassert>31 30 32 31 #include "util/Convert.h" 33 32 #include "core/Language.h" 34 35 namespace orxonox36 {37 ConsoleCommand::ConsoleCommand(const FunctorPtr& functor, const std::string& name) : Executor(functor, name)38 {39 this->accessLevel_ = AccessLevel::None;40 this->argumentCompleter_[0] = 0;41 this->argumentCompleter_[1] = 0;42 this->argumentCompleter_[2] = 0;43 this->argumentCompleter_[3] = 0;44 this->argumentCompleter_[4] = 0;45 46 this->keybindMode_ = KeybindMode::OnPress;47 this->inputConfiguredParam_ = -1;48 }49 50 ConsoleCommand& ConsoleCommand::argumentCompleter(unsigned int param, ArgumentCompleter* completer)51 {52 if (param < 5)53 this->argumentCompleter_[param] = completer;54 else55 {56 COUT(2) << "Warning: Couldn't add autocompletion-function for param " << param << ": index out of bound." << std::endl;57 }58 return (*this);59 }60 61 ArgumentCompleter* ConsoleCommand::getArgumentCompleter(unsigned int param) const62 {63 if (param < 5)64 return this->argumentCompleter_[param];65 else66 return 0;67 }68 69 void ConsoleCommand::createArgumentCompletionList(unsigned int param, const std::string& param1, const std::string& param2, const std::string& param3, const std::string& param4, const std::string& param5)70 {71 if (param < 5 && this->argumentCompleter_[param])72 this->argumentList_ = (*this->argumentCompleter_[param])(param1, param2, param3, param4, param5);73 else74 this->argumentList_.clear();75 }76 77 ConsoleCommand& ConsoleCommand::description(const std::string& description)78 {79 this->description_ = std::string("ConsoleCommandDescription::" + this->name_ + "::function");80 AddLanguageEntry(this->description_, description);81 return (*this);82 }83 84 const std::string& ConsoleCommand::getDescription() const85 {86 return GetLocalisation_noerror(this->description_);87 }88 89 ConsoleCommand& ConsoleCommand::descriptionParam(unsigned int param, const std::string& description)90 {91 if (param < MAX_FUNCTOR_ARGUMENTS)92 {93 this->descriptionParam_[param] = std::string("ConsoleCommandDescription::" + this->name_ + "::param" + multi_cast<std::string>(param));94 AddLanguageEntry(this->descriptionParam_[param], description);95 }96 return (*this);97 }98 99 const std::string& ConsoleCommand::getDescriptionParam(unsigned int param) const100 {101 if (param < MAX_FUNCTOR_ARGUMENTS)102 return GetLocalisation_noerror(this->descriptionParam_[param]);103 104 return this->descriptionParam_[0];105 }106 107 ConsoleCommand& ConsoleCommand::descriptionReturnvalue(const std::string& description)108 {109 this->descriptionReturnvalue_ = std::string("ConsoleCommandDescription::" + this->name_ + "::returnvalue");110 AddLanguageEntry(this->descriptionReturnvalue_, description);111 return (*this);112 }113 114 const std::string& ConsoleCommand::getDescriptionReturnvalue(int param) const115 {116 return GetLocalisation_noerror(this->descriptionReturnvalue_);117 }118 }119 120 33 #include "core/BaseObject.h" // remove this 121 34 … … 537 450 } 538 451 } 452 453 /* static */ void _ConsoleCommand::destroyAll() 454 { 455 while (!_ConsoleCommand::getCommandMap().empty() && !_ConsoleCommand::getCommandMap().begin().empty()) 456 _ConsoleCommand::getCommandMap().begin().erase(_ConsoleCommand::getCommandMap().begin().begin()); 457 } 539 458 } -
code/branches/consolecommands3/src/libraries/core/command/ConsoleCommand.h
r7215 r7216 37 37 38 38 #include "util/VA_NARGS.h" 39 #include "core/Identifier.h"40 39 #include "ArgumentCompletionFunctions.h" 41 #include "CommandExecutor.h"42 40 #include "Executor.h" 43 44 45 #define SetConsoleCommand(classname, function, bCreateShortcut) \46 SetConsoleCommandGeneric(classname, function, #function, bCreateShortcut)47 #define SetConsoleCommandAlias(classname, function, name, bCreateShortcut) \48 SetConsoleCommandGeneric(classname, function, name, bCreateShortcut)49 50 #define SetConsoleCommandGeneric(classname, function, name, bCreateShortcut) \51 orxonox::ConsoleCommand& BOOST_PP_CAT(classname##function##consolecommand__, __LINE__) = orxonox::ClassIdentifier<classname>::getIdentifier(#classname)->addConsoleCommand(orxonox::createConsoleCommand(orxonox::createFunctor(&classname::function), name), bCreateShortcut)52 53 54 #define SetConsoleCommandShortcut(classname, function) \55 SetConsoleCommandShortcutAliasGeneric(classname, function, #function)56 #define SetConsoleCommandShortcutAlias(classname, function, name) \57 SetConsoleCommandShortcutAliasGeneric(classname, function, name)58 #define SetConsoleCommandShortcutAliasGeneric(classname, function, name) \59 SetConsoleCommandShortcutGeneric(BOOST_PP_CAT(function##consolecommand__, __LINE__), orxonox::createConsoleCommand(orxonox::createFunctor(&classname::function), name))60 61 #define SetConsoleCommandShortcutExtern(function) \62 SetConsoleCommandShortcutExternAliasGeneric(function, #function)63 #define SetConsoleCommandShortcutExternAlias(function, name) \64 SetConsoleCommandShortcutExternAliasGeneric(function, name)65 #define SetConsoleCommandShortcutExternAliasGeneric(function, name) \66 SetConsoleCommandShortcutGeneric(BOOST_PP_CAT(function##consolecommand__, __LINE__), orxonox::createConsoleCommand(orxonox::createFunctor(&function), name))67 68 #define SetConsoleCommandShortcutGeneric(fakevariable, command) \69 orxonox::ConsoleCommand& fakevariable = orxonox::CommandExecutor::addConsoleCommandShortcut(command, true)70 71 72 namespace orxonox73 {74 namespace AccessLevel75 {76 enum Value77 {78 None,79 User,80 Admin,81 Offline,82 Debug,83 Disabled84 };85 }86 87 class _CoreExport ConsoleCommand : public Executor88 {89 public:90 ConsoleCommand(const FunctorPtr& functor, const std::string& name = "");91 92 ConsoleCommand& description(const std::string& description);93 const std::string& getDescription() const;94 95 ConsoleCommand& descriptionParam(unsigned int param, const std::string& description);96 const std::string& getDescriptionParam(unsigned int param) const;97 98 ConsoleCommand& descriptionReturnvalue(const std::string& description);99 const std::string& getDescriptionReturnvalue(int param) const;100 101 inline ConsoleCommand& defaultValues(const MultiType& param1)102 { this->Executor::setDefaultValues(param1); return (*this); }103 inline ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2)104 { this->Executor::setDefaultValues(param1, param2); return (*this); }105 inline ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3)106 { this->Executor::setDefaultValues(param1, param2, param3); return (*this); }107 inline ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4)108 { this->Executor::setDefaultValues(param1, param2, param3, param4); return (*this); }109 inline ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5)110 { this->Executor::setDefaultValues(param1, param2, param3, param4, param5); return (*this); }111 inline ConsoleCommand& defaultValue(unsigned int index, const MultiType& param)112 { this->Executor::setDefaultValue(index, param); return (*this); }113 114 inline ConsoleCommand& accessLevel(AccessLevel::Value level)115 { this->accessLevel_ = level; return (*this); }116 inline AccessLevel::Value getAccessLevel() const117 { return this->accessLevel_; }118 119 ConsoleCommand& argumentCompleter(unsigned int param, ArgumentCompleter* completer);120 ArgumentCompleter* getArgumentCompleter(unsigned int param) const;121 122 void createArgumentCompletionList(unsigned int param, const std::string& param1 = "", const std::string& param2 = "", const std::string& param3 = "", const std::string& param4 = "", const std::string& param5 = "");123 const ArgumentCompletionList& getArgumentCompletionList() const124 { return this->argumentList_; }125 ArgumentCompletionList::const_iterator getArgumentCompletionListBegin() const126 { return this->argumentList_.begin(); }127 ArgumentCompletionList::const_iterator getArgumentCompletionListEnd() const128 { return this->argumentList_.end(); }129 130 inline ConsoleCommand& setAsInputCommand()131 {132 this->keybindMode(KeybindMode::OnHold);133 this->defaultValue(0, Vector2(0.0f, 0.0f));134 this->inputConfiguredParam(0);135 return *this;136 }137 138 inline ConsoleCommand& keybindMode(KeybindMode::Value mode)139 { this->keybindMode_ = mode; return *this; }140 inline KeybindMode::Value getKeybindMode() const141 { return this->keybindMode_; }142 143 inline ConsoleCommand& inputConfiguredParam(int index)144 { this->inputConfiguredParam_ = index; return *this; }145 inline int getInputConfiguredParam_() const146 { return this->inputConfiguredParam_; }147 148 private:149 AccessLevel::Value accessLevel_;150 ArgumentCompleter* argumentCompleter_[5];151 ArgumentCompletionList argumentList_;152 153 KeybindMode::Value keybindMode_;154 int inputConfiguredParam_;155 156 LanguageEntryLabel description_;157 LanguageEntryLabel descriptionReturnvalue_;158 LanguageEntryLabel descriptionParam_[MAX_FUNCTOR_ARGUMENTS];159 };160 161 inline ConsoleCommand* createConsoleCommand(const FunctorPtr& functor, const std::string& name = "")162 {163 return new ConsoleCommand(functor, name);164 }165 }166 41 167 42 … … 452 327 static const _ConsoleCommand* getCommand(const std::string& group, const std::string& name, bool bPrintError = false); 453 328 329 static void destroyAll(); 330 454 331 private: 455 332 static std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommandMap();
Note: See TracChangeset
for help on using the changeset viewer.