Convert list of commands to a map

This commit is contained in:
David Capello 2017-11-30 12:33:08 -03:00
parent eb3143ce21
commit a75c8fb1b8
2 changed files with 19 additions and 12 deletions

View File

@ -35,7 +35,7 @@ Commands::Commands()
#undef FOR_EACH_COMMAND
#define FOR_EACH_COMMAND(Name) \
m_commands.push_back(CommandFactory::create##Name##Command());
add(CommandFactory::create##Name##Command());
#include "app/commands/commands_list.h"
#undef FOR_EACH_COMMAND
@ -45,8 +45,10 @@ Commands::~Commands()
{
ASSERT(m_instance == this);
for (Command* cmd : m_commands)
delete cmd;
for (auto& it : m_commands) {
Command* command = it.second;
delete command;
}
m_commands.clear();
m_instance = NULL;
@ -63,13 +65,15 @@ Command* Commands::byId(const char* id)
if (!id)
return nullptr;
std::string lid = base::string_to_lower(id);
for (Command* cmd : m_commands) {
if (base::utf8_icmp(cmd->id(), lid) == 0)
return cmd;
}
auto lid = base::string_to_lower(id);
auto it = m_commands.find(lid);
return (it != m_commands.end() ? it->second: nullptr);
}
return nullptr;
void Commands::add(Command* command)
{
auto lid = base::string_to_lower(command->id());
m_commands[lid] = command;
}
} // namespace app

View File

@ -10,7 +10,8 @@
#include "ui/base.h"
#include <vector>
#include <map>
#include <string>
namespace app {
@ -23,11 +24,9 @@ namespace app {
};
class Command;
typedef std::vector<Command*> CommandsList;
class Commands {
static Commands* m_instance;
CommandsList m_commands;
public:
Commands();
@ -36,6 +35,10 @@ namespace app {
static Commands* instance();
Command* byId(const char* id);
void add(Command* command);
private:
std::map<std::string, Command*> m_commands;
};
} // namespace app