Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 25, 2015, 10:56:26 PM (9 years ago)
Author:
landauf
Message:

made ConsoleCommandManager a singleton.

Location:
code/branches/core7/src/libraries/core/command
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core7/src/libraries/core/command/ArgumentCompletionFunctions.cc

    r10347 r10484  
    9999
    100100                // get all the groups that are visible (except the shortcut group "")
    101                 const std::map<std::string, std::map<std::string, ConsoleCommand*> >& commands = ConsoleCommandManager::getCommands();
     101                const std::map<std::string, std::map<std::string, ConsoleCommand*> >& commands = ConsoleCommandManager::getInstance().getCommands();
    102102                for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = commands.begin(); it_group != commands.end(); ++it_group)
    103103                    if (groupIsVisible(it_group->second, bOnlyShowHidden) && it_group->first != "" && (fragmentLC == "" || getLowercase(it_group->first).find(fragmentLC) == 0))
     
    138138
    139139                // find the iterator of the given group
    140                 std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getCommands().begin();
    141                 for ( ; it_group != ConsoleCommandManager::getCommands().end(); ++it_group)
     140                std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getInstance().getCommands().begin();
     141                for ( ; it_group != ConsoleCommandManager::getInstance().getCommands().end(); ++it_group)
    142142                    if (getLowercase(it_group->first) == groupLC)
    143143                        break;
    144144
    145145                // add all commands in the group to the list
    146                 if (it_group != ConsoleCommandManager::getCommands().end())
     146                if (it_group != ConsoleCommandManager::getInstance().getCommands().end())
    147147                {
    148148                    for (std::map<std::string, ConsoleCommand*>::const_iterator it_command = it_group->second.begin(); it_command != it_group->second.end(); ++it_command)
     
    207207                return detail::_groupsandcommands(fragment, true);
    208208
    209             if (ConsoleCommandManager::getCommandLC(getLowercase(tokens[0])))
     209            if (ConsoleCommandManager::getInstance().getCommandLC(getLowercase(tokens[0])))
    210210                return ARGUMENT_COMPLETION_FUNCTION_CALL(command)(fragment);
    211211
    212212            if (tokens.size() == 1)
    213213            {
    214                 std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getCommands().find(tokens[0]);
    215                 if (it_group != ConsoleCommandManager::getCommands().end())
     214                std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getInstance().getCommands().find(tokens[0]);
     215                if (it_group != ConsoleCommandManager::getInstance().getCommands().end())
    216216                    return detail::_subcommands(fragment, tokens[0], true);
    217217                else
     
    219219            }
    220220
    221             if (ConsoleCommandManager::getCommandLC(getLowercase(tokens[0]), getLowercase(tokens[1])))
     221            if (ConsoleCommandManager::getInstance().getCommandLC(getLowercase(tokens[0]), getLowercase(tokens[1])))
    222222                return ARGUMENT_COMPLETION_FUNCTION_CALL(command)(fragment);
    223223
  • code/branches/core7/src/libraries/core/command/CommandEvaluation.cc

    r10347 r10484  
    306306                // the user typed 1-2 arguments, check what he tried to type and print a suitable error
    307307                std::string groupLC = getLowercase(this->getToken(0));
    308                 for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getCommandsLC().begin(); it_group != ConsoleCommandManager::getCommandsLC().end(); ++it_group)
     308                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)
    309309                    if (it_group->first == groupLC)
    310310                        return std::string("Error: There is no command in group \"") + this->getToken(0) + "\" starting with \"" + this->getToken(1) + "\".";
     
    328328
    329329        // iterate through all groups and their commands and calculate the distance to the current command. keep the best.
    330         for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getCommandsLC().begin(); it_group != ConsoleCommandManager::getCommandsLC().end(); ++it_group)
     330        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)
    331331        {
    332332            if (it_group->first != "")
     
    346346
    347347        // now also iterate through all shortcuts and keep the best if it's better than the one found above.
    348         std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getCommandsLC().find("");
    349         if (it_group !=  ConsoleCommandManager::getCommandsLC().end())
     348        std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getInstance().getCommandsLC().find("");
     349        if (it_group !=  ConsoleCommandManager::getInstance().getCommandsLC().end())
    350350        {
    351351            for (std::map<std::string, ConsoleCommand*>::const_iterator it_name = it_group->second.begin(); it_name != it_group->second.end(); ++it_name)
  • code/branches/core7/src/libraries/core/command/CommandExecutor.cc

    r10355 r10484  
    155155
    156156        // assign the fallback-command to get hints about the possible commands and groups
    157         evaluation.hintCommand_ = ConsoleCommandManager::getCommand(__CC_CommandExecutor_name, __CC_autocomplete_name);
     157        evaluation.hintCommand_ = ConsoleCommandManager::getInstance().getCommand(__CC_CommandExecutor_name, __CC_autocomplete_name);
    158158
    159159        // check if there's at least one argument
     
    161161        {
    162162            // try to get a command from the first token
    163             evaluation.execCommand_ = ConsoleCommandManager::getCommandLC(evaluation.getToken(0));
     163            evaluation.execCommand_ = ConsoleCommandManager::getInstance().getCommandLC(evaluation.getToken(0));
    164164            if (evaluation.execCommand_)
    165165                evaluation.execArgumentsOffset_ = 1;
     
    167167            {
    168168                // try to get a command from the first two tokens
    169                 evaluation.execCommand_ = ConsoleCommandManager::getCommandLC(evaluation.getToken(0), evaluation.getToken(1));
     169                evaluation.execCommand_ = ConsoleCommandManager::getInstance().getCommandLC(evaluation.getToken(0), evaluation.getToken(1));
    170170                if (evaluation.execCommand_)
    171171                    evaluation.execArgumentsOffset_ = 2;
     
    288288
    289289            // check if the alias already exists - print an error and return if it does
    290             if ((tokens.size() == 1 && ConsoleCommandManager::getCommand(tokens[0])) || (tokens.size() == 2 && ConsoleCommandManager::getCommand(tokens[0], tokens[1])))
     290            if ((tokens.size() == 1 && ConsoleCommandManager::getInstance().getCommand(tokens[0])) || (tokens.size() == 2 && ConsoleCommandManager::getInstance().getCommand(tokens[0], tokens[1])))
    291291            {
    292292                orxout(user_error) << "A command with name \"" << alias << "\" already exists." << endl;
     
    296296            // create a new console command with the given alias as its name
    297297            if (tokens.size() == 1)
    298                 ConsoleCommandManager::registerCommand(new ConsoleCommand(tokens[0], executor));
     298                ConsoleCommandManager::getInstance().registerCommand(new ConsoleCommand(tokens[0], executor));
    299299            else if (tokens.size() == 2)
    300                 ConsoleCommandManager::registerCommand(new ConsoleCommand(tokens[0], tokens[1], executor));
     300                ConsoleCommandManager::getInstance().registerCommand(new ConsoleCommand(tokens[0], tokens[1], executor));
    301301            else
    302302                orxout(user_error) << "\"" << alias << "\" is not a valid alias name (must have one or two words)." << endl;
  • code/branches/core7/src/libraries/core/command/ConsoleCommand.cc

    r10352 r10484  
    9292    ConsoleCommand::~ConsoleCommand()
    9393    {
    94         ConsoleCommandManager::unregisterCommand(this);
     94        ConsoleCommandManager::getInstance().unregisterCommand(this);
    9595    }
    9696
  • code/branches/core7/src/libraries/core/command/ConsoleCommandIncludes.cc

    r10404 r10484  
    3535    void StaticallyInitializedConsoleCommand::load()
    3636    {
    37         ConsoleCommandManager::registerCommand(this->command_);
     37        ConsoleCommandManager::getInstance().registerCommand(this->command_);
    3838    }
    3939
    4040    void StaticallyInitializedConsoleCommand::unload()
    4141    {
    42         ConsoleCommandManager::unregisterCommand(this->command_);
     42        ConsoleCommandManager::getInstance().unregisterCommand(this->command_);
    4343    }
    4444}
  • code/branches/core7/src/libraries/core/command/ConsoleCommandIncludes.h

    r10404 r10484  
    332332    */
    333333    inline ConsoleCommand::ConsoleCommandManipulator ModifyConsoleCommand(const std::string& name)
    334         { return ConsoleCommandManager::getCommand(name, true); }
     334        { return ConsoleCommandManager::getInstance().getCommand(name, true); }
    335335    /**
    336336        @brief Returns a manipulator for a command with the given group and name.
     
    341341    */
    342342    inline ConsoleCommand::ConsoleCommandManipulator ModifyConsoleCommand(const std::string& group, const std::string& name)
    343         { return ConsoleCommandManager::getCommand(group, name, true); }
     343        { return ConsoleCommandManager::getInstance().getCommand(group, name, true); }
    344344}
    345345
  • code/branches/core7/src/libraries/core/command/ConsoleCommandManager.cc

    r10354 r10484  
    3939namespace orxonox
    4040{
     41    /* static */ ConsoleCommandManager& ConsoleCommandManager::getInstance()
     42    {
     43        static ConsoleCommandManager instance;
     44        return instance;
     45    }
     46
    4147    /**
    4248        @brief Returns the command with given group an name.
     
    4551        @param bPrintError If true, an error is printed if the command doesn't exist
    4652    */
    47     /* static */ ConsoleCommand* ConsoleCommandManager::getCommand(const std::string& group, const std::string& name, bool bPrintError)
     53    ConsoleCommand* ConsoleCommandManager::getCommand(const std::string& group, const std::string& name, bool bPrintError)
    4854    {
    4955        // find the group
    50         std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getCommandMap().find(group);
    51         if (it_group != ConsoleCommandManager::getCommandMap().end())
     56        std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = this->commandMap_.find(group);
     57        if (it_group != this->commandMap_.end())
    5258        {
    5359            // find the name
     
    7581        @param bPrintError If true, an error is printed if the command doesn't exist
    7682    */
    77     /* static */ ConsoleCommand* ConsoleCommandManager::getCommandLC(const std::string& group, const std::string& name, bool bPrintError)
     83    ConsoleCommand* ConsoleCommandManager::getCommandLC(const std::string& group, const std::string& name, bool bPrintError)
    7884    {
    7985        std::string groupLC = getLowercase(group);
     
    8187
    8288        // find the group
    83         std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getCommandMapLC().find(groupLC);
    84         if (it_group != ConsoleCommandManager::getCommandMapLC().end())
     89        std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = this->commandMapLC_.find(groupLC);
     90        if (it_group != this->commandMapLC_.end())
    8591        {
    8692            // find the name
     
    103109
    104110    /**
    105         @brief Returns the static map that stores all console commands.
    106     */
    107     /* static */ std::map<std::string, std::map<std::string, ConsoleCommand*> >& ConsoleCommandManager::getCommandMap()
    108     {
    109         static std::map<std::string, std::map<std::string, ConsoleCommand*> > commandMap;
    110         return commandMap;
    111     }
    112 
    113     /**
    114         @brief Returns the static map that stores all console commands in lowercase.
    115     */
    116     /* static */ std::map<std::string, std::map<std::string, ConsoleCommand*> >& ConsoleCommandManager::getCommandMapLC()
    117     {
    118         static std::map<std::string, std::map<std::string, ConsoleCommand*> > commandMapLC;
    119         return commandMapLC;
    120     }
    121 
    122     /**
    123111        @brief Registers a new command with the groups and names that are defined by ConsoleCommand::getNames().
    124112    */
    125     /* static */ void ConsoleCommandManager::registerCommand(ConsoleCommand* command)
     113    void ConsoleCommandManager::registerCommand(ConsoleCommand* command)
    126114    {
    127115        for (size_t i = 0; i < command->getNames().size(); ++i)
    128116        {
    129117            const ConsoleCommand::CommandName& name = command->getNames()[i];
    130             ConsoleCommandManager::registerCommand(name.group_, name.name_, command);
     118            this->registerCommand(name.group_, name.name_, command);
    131119        }
    132120    }
     
    135123        @brief Registers a new command with given group an name by adding it to the command map.
    136124    */
    137     /* static */ void ConsoleCommandManager::registerCommand(const std::string& group, const std::string& name, ConsoleCommand* command)
     125    void ConsoleCommandManager::registerCommand(const std::string& group, const std::string& name, ConsoleCommand* command)
    138126    {
    139127        if (name == "")
     
    141129
    142130        // check if a command with this name already exists
    143         if (ConsoleCommandManager::getCommand(group, name) != 0)
     131        if (this->getCommand(group, name) != 0)
    144132        {
    145133            if (group == "")
     
    151139        {
    152140            // add the command to the map
    153             ConsoleCommandManager::getCommandMap()[group][name] = command;
    154             ConsoleCommandManager::getCommandMapLC()[getLowercase(group)][getLowercase(name)] = command;
     141            this->commandMap_[group][name] = command;
     142            this->commandMapLC_[getLowercase(group)][getLowercase(name)] = command;
    155143        }
    156144    }
     
    159147        @brief Removes the command from the command map.
    160148    */
    161     /* static */ void ConsoleCommandManager::unregisterCommand(ConsoleCommand* command)
     149    void ConsoleCommandManager::unregisterCommand(ConsoleCommand* command)
    162150    {
    163151        // iterate through all groups
    164         for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::iterator it_group = ConsoleCommandManager::getCommandMap().begin(); it_group != ConsoleCommandManager::getCommandMap().end(); )
     152        for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::iterator it_group = this->commandMap_.begin(); it_group != this->commandMap_.end(); )
    165153        {
    166154            // iterate through all commands of each group
     
    176164            // erase the group if it is empty now
    177165            if (it_group->second.empty())
    178                 ConsoleCommandManager::getCommandMap().erase(it_group++);
     166                this->commandMap_.erase(it_group++);
    179167            else
    180168                ++it_group;
     
    184172
    185173        // iterate through all groups
    186         for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::iterator it_group = ConsoleCommandManager::getCommandMapLC().begin(); it_group != ConsoleCommandManager::getCommandMapLC().end(); )
     174        for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::iterator it_group = this->commandMapLC_.begin(); it_group != this->commandMapLC_.end(); )
    187175        {
    188176            // iterate through all commands of each group
     
    198186            // erase the group if it is empty now
    199187            if (it_group->second.empty())
    200                 ConsoleCommandManager::getCommandMapLC().erase(it_group++);
     188                this->commandMapLC_.erase(it_group++);
    201189            else
    202190                ++it_group;
     
    207195        @brief Deletes all commands
    208196    */
    209     /* static */ void ConsoleCommandManager::destroyAll()
     197    void ConsoleCommandManager::destroyAll()
    210198    {
    211199        // delete entries until the map is empty
    212         while (!ConsoleCommandManager::getCommandMap().empty() && !ConsoleCommandManager::getCommandMap().begin()->second.empty())
    213             delete ConsoleCommandManager::getCommandMap().begin()->second.begin()->second;
     200        while (!this->commandMap_.empty() && !this->commandMap_.begin()->second.empty())
     201            delete this->commandMap_.begin()->second.begin()->second;
    214202    }
    215203}
  • code/branches/core7/src/libraries/core/command/ConsoleCommandManager.h

    r10354 r10484  
    4040{
    4141    /**
    42      * A static class that stores all existing ConsoleCommands.
     42     * A singleton that stores all existing ConsoleCommands.
    4343     */
    4444    class _CoreExport ConsoleCommandManager
    4545    {
    4646        public:
    47             static void registerCommand(ConsoleCommand* command);
    48             static void registerCommand(const std::string& group, const std::string& name, ConsoleCommand* command);
    49             static void unregisterCommand(ConsoleCommand* command);
     47            static ConsoleCommandManager& getInstance();
     48
     49            void registerCommand(ConsoleCommand* command);
     50            void registerCommand(const std::string& group, const std::string& name, ConsoleCommand* command);
     51            void unregisterCommand(ConsoleCommand* command);
    5052
    5153            /// Returns the map with all groups and commands.
    52             static inline const std::map<std::string, std::map<std::string, ConsoleCommand*> >& getCommands()
    53                 { return ConsoleCommandManager::getCommandMap(); }
     54            inline const std::map<std::string, std::map<std::string, ConsoleCommand*> >& getCommands()
     55                { return this->commandMap_; }
    5456            /// Returns the map with all groups and commands in lowercase.
    55             static inline const std::map<std::string, std::map<std::string, ConsoleCommand*> >& getCommandsLC()
    56                 { return ConsoleCommandManager::getCommandMapLC(); }
     57            inline const std::map<std::string, std::map<std::string, ConsoleCommand*> >& getCommandsLC()
     58                { return this->commandMapLC_; }
    5759
    5860            /// 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
    59             static inline ConsoleCommand* getCommand(const std::string& name, bool bPrintError = false)
    60                 { return ConsoleCommandManager::getCommand("", name, bPrintError); }
     61            inline ConsoleCommand* getCommand(const std::string& name, bool bPrintError = false)
     62                { return this->getCommand("", name, bPrintError); }
    6163            /// 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
    62             static inline ConsoleCommand* getCommandLC(const std::string& name, bool bPrintError = false)
    63                 { return ConsoleCommandManager::getCommandLC("", name, bPrintError); }
     64            inline ConsoleCommand* getCommandLC(const std::string& name, bool bPrintError = false)
     65                { return this->getCommandLC("", name, bPrintError); }
    6466
    65             static ConsoleCommand* getCommand(const std::string& group, const std::string& name, bool bPrintError = false);
    66             static ConsoleCommand* getCommandLC(const std::string& group, const std::string& name, bool bPrintError = false);
     67            ConsoleCommand* getCommand(const std::string& group, const std::string& name, bool bPrintError = false);
     68            ConsoleCommand* getCommandLC(const std::string& group, const std::string& name, bool bPrintError = false);
    6769
    68             static void destroyAll();
     70            void destroyAll();
    6971
    7072        private:
    71             static std::map<std::string, std::map<std::string, ConsoleCommand*> >& getCommandMap();
    72             static std::map<std::string, std::map<std::string, ConsoleCommand*> >& getCommandMapLC();
     73            std::map<std::string, std::map<std::string, ConsoleCommand*> > commandMap_;
     74            std::map<std::string, std::map<std::string, ConsoleCommand*> > commandMapLC_;
    7375    };
    7476}
Note: See TracChangeset for help on using the changeset viewer.