Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

made ConsoleCommandManager a singleton.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.