Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 5, 2015, 8:25:11 PM (9 years ago)
Author:
landauf
Message:

moved static contents from ConsoleCommand to ConsoleCommandManager

File:
1 edited

Legend:

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

    r9348 r10346  
    3535
    3636#include "util/Convert.h"
    37 #include "util/StringUtils.h"
    3837#include "core/Language.h"
    3938#include "core/GameMode.h"
     
    6867            this->executor_ = executor;
    6968
    70         ConsoleCommand::registerCommand(group, name, this);
     69        ConsoleCommandManager::registerCommand(group, name, this);
    7170    }
    7271
     
    7675    ConsoleCommand::~ConsoleCommand()
    7776    {
    78         ConsoleCommand::unregisterCommand(this);
     77        ConsoleCommandManager::unregisterCommand(this);
    7978    }
    8079
     
    8483    ConsoleCommand& ConsoleCommand::addShortcut()
    8584    {
    86         ConsoleCommand::registerCommand("", this->baseName_, this);
     85        ConsoleCommandManager::registerCommand("", this->baseName_, this);
    8786        return *this;
    8887    }
     
    9392    ConsoleCommand& ConsoleCommand::addShortcut(const std::string&  name)
    9493    {
    95         ConsoleCommand::registerCommand("", name, this);
     94        ConsoleCommandManager::registerCommand("", name, this);
    9695        return *this;
    9796    }
     
    102101    ConsoleCommand& ConsoleCommand::addGroup(const std::string& group)
    103102    {
    104         ConsoleCommand::registerCommand(group, this->baseName_, this);
     103        ConsoleCommandManager::registerCommand(group, this->baseName_, this);
    105104        return *this;
    106105    }
     
    111110    ConsoleCommand& ConsoleCommand::addGroup(const std::string& group, const std::string&  name)
    112111    {
    113         ConsoleCommand::registerCommand(group, name, this);
     112        ConsoleCommandManager::registerCommand(group, name, this);
    114113        return *this;
    115114    }
     
    587586        return *this;
    588587    }
    589 
    590     /**
    591         @brief Returns the command with given group an name.
    592         @param group The group of the requested command
    593         @param name The group of the requested command
    594         @param bPrintError If true, an error is printed if the command doesn't exist
    595     */
    596     /* static */ ConsoleCommand* ConsoleCommand::getCommand(const std::string& group, const std::string& name, bool bPrintError)
    597     {
    598         // find the group
    599         std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommand::getCommandMap().find(group);
    600         if (it_group != ConsoleCommand::getCommandMap().end())
    601         {
    602             // find the name
    603             std::map<std::string, ConsoleCommand*>::const_iterator it_name = it_group->second.find(name);
    604             if (it_name != it_group->second.end())
    605             {
    606                 // return the pointer
    607                 return it_name->second;
    608             }
    609         }
    610         if (bPrintError)
    611         {
    612             if (group == "")
    613                 orxout(internal_error, context::commands) << "Couldn't find console command with shortcut \"" << name << "\"" << endl;
    614             else
    615                 orxout(internal_error, context::commands) << "Couldn't find console command with group \"" << group << "\" and name \"" << name << "\"" << endl;
    616         }
    617         return 0;
    618     }
    619 
    620     /**
    621         @brief Returns the command with given group an name in lowercase.
    622         @param group The group of the requested command in lowercase
    623         @param name The group of the requested command in lowercase
    624         @param bPrintError If true, an error is printed if the command doesn't exist
    625     */
    626     /* static */ ConsoleCommand* ConsoleCommand::getCommandLC(const std::string& group, const std::string& name, bool bPrintError)
    627     {
    628         std::string groupLC = getLowercase(group);
    629         std::string nameLC = getLowercase(name);
    630 
    631         // find the group
    632         std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommand::getCommandMapLC().find(groupLC);
    633         if (it_group != ConsoleCommand::getCommandMapLC().end())
    634         {
    635             // find the name
    636             std::map<std::string, ConsoleCommand*>::const_iterator it_name = it_group->second.find(nameLC);
    637             if (it_name != it_group->second.end())
    638             {
    639                 // return the pointer
    640                 return it_name->second;
    641             }
    642         }
    643         if (bPrintError)
    644         {
    645             if (group == "")
    646                 orxout(internal_error, context::commands) << "Couldn't find console command with shortcut \"" << name << "\"" << endl;
    647             else
    648                 orxout(internal_error, context::commands) << "Couldn't find console command with group \"" << group << "\" and name \"" << name << "\"" << endl;
    649         }
    650         return 0;
    651     }
    652 
    653     /**
    654         @brief Returns the static map that stores all console commands.
    655     */
    656     /* static */ std::map<std::string, std::map<std::string, ConsoleCommand*> >& ConsoleCommand::getCommandMap()
    657     {
    658         static std::map<std::string, std::map<std::string, ConsoleCommand*> > commandMap;
    659         return commandMap;
    660     }
    661 
    662     /**
    663         @brief Returns the static map that stores all console commands in lowercase.
    664     */
    665     /* static */ std::map<std::string, std::map<std::string, ConsoleCommand*> >& ConsoleCommand::getCommandMapLC()
    666     {
    667         static std::map<std::string, std::map<std::string, ConsoleCommand*> > commandMapLC;
    668         return commandMapLC;
    669     }
    670 
    671     /**
    672         @brief Registers a new command with given group an name by adding it to the command map.
    673     */
    674     /* static */ void ConsoleCommand::registerCommand(const std::string& group, const std::string& name, ConsoleCommand* command)
    675     {
    676         if (name == "")
    677             return;
    678 
    679         // check if a command with this name already exists
    680         if (ConsoleCommand::getCommand(group, name) != 0)
    681         {
    682             if (group == "")
    683                 orxout(internal_warning, context::commands) << "A console command with shortcut \"" << name << "\" already exists." << endl;
    684             else
    685                 orxout(internal_warning, context::commands) << "A console command with name \"" << name << "\" already exists in group \"" << group << "\"." << endl;
    686         }
    687         else
    688         {
    689             // add the command to the map
    690             ConsoleCommand::getCommandMap()[group][name] = command;
    691             ConsoleCommand::getCommandMapLC()[getLowercase(group)][getLowercase(name)] = command;
    692         }
    693     }
    694 
    695     /**
    696         @brief Removes the command from the command map.
    697     */
    698     /* static */ void ConsoleCommand::unregisterCommand(ConsoleCommand* command)
    699     {
    700         // iterate through all groups
    701         for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::iterator it_group = ConsoleCommand::getCommandMap().begin(); it_group != ConsoleCommand::getCommandMap().end(); )
    702         {
    703             // iterate through all commands of each group
    704             for (std::map<std::string, ConsoleCommand*>::iterator it_name = it_group->second.begin(); it_name != it_group->second.end(); )
    705             {
    706                 // erase the command
    707                 if (it_name->second == command)
    708                     it_group->second.erase(it_name++);
    709                 else
    710                     ++it_name;
    711             }
    712 
    713             // erase the group if it is empty now
    714             if (it_group->second.empty())
    715                 ConsoleCommand::getCommandMap().erase(it_group++);
    716             else
    717                 ++it_group;
    718         }
    719 
    720         // now the same for the lowercase-map:
    721 
    722         // iterate through all groups
    723         for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::iterator it_group = ConsoleCommand::getCommandMapLC().begin(); it_group != ConsoleCommand::getCommandMapLC().end(); )
    724         {
    725             // iterate through all commands of each group
    726             for (std::map<std::string, ConsoleCommand*>::iterator it_name = it_group->second.begin(); it_name != it_group->second.end(); )
    727             {
    728                 // erase the command
    729                 if (it_name->second == command)
    730                     it_group->second.erase(it_name++);
    731                 else
    732                     ++it_name;
    733             }
    734 
    735             // erase the group if it is empty now
    736             if (it_group->second.empty())
    737                 ConsoleCommand::getCommandMapLC().erase(it_group++);
    738             else
    739                 ++it_group;
    740         }
    741     }
    742 
    743     /**
    744         @brief Deletes all commands
    745     */
    746     /* static */ void ConsoleCommand::destroyAll()
    747     {
    748         // delete entries until the map is empty
    749         while (!ConsoleCommand::getCommandMap().empty() && !ConsoleCommand::getCommandMap().begin()->second.empty())
    750             delete ConsoleCommand::getCommandMap().begin()->second.begin()->second;
    751     }
    752588}
Note: See TracChangeset for help on using the changeset viewer.