| 1 | // Aseprite |
|---|---|
| 2 | // Copyright (C) 2020 Igara Studio S.A. |
| 3 | // Copyright (C) 2001-2017 David Capello |
| 4 | // |
| 5 | // This program is distributed under the terms of |
| 6 | // the End-User License Agreement for Aseprite. |
| 7 | |
| 8 | #ifdef HAVE_CONFIG_H |
| 9 | #include "config.h" |
| 10 | #endif |
| 11 | |
| 12 | #include "app/commands/commands.h" |
| 13 | |
| 14 | #include "app/commands/command.h" |
| 15 | #include "app/console.h" |
| 16 | #include "base/string.h" |
| 17 | #include "ui/ui.h" |
| 18 | |
| 19 | #include <cstring> |
| 20 | #include <exception> |
| 21 | |
| 22 | namespace app { |
| 23 | |
| 24 | Commands* Commands::m_instance = NULL; |
| 25 | |
| 26 | Commands::Commands() |
| 27 | { |
| 28 | ASSERT(m_instance == NULL); |
| 29 | m_instance = this; |
| 30 | |
| 31 | #undef FOR_EACH_COMMAND |
| 32 | #define FOR_EACH_COMMAND(Name) \ |
| 33 | add(CommandFactory::create##Name##Command()); |
| 34 | |
| 35 | #include "app/commands/commands_list.h" |
| 36 | #undef FOR_EACH_COMMAND |
| 37 | } |
| 38 | |
| 39 | Commands::~Commands() |
| 40 | { |
| 41 | ASSERT(m_instance == this); |
| 42 | |
| 43 | for (auto& it : m_commands) { |
| 44 | Command* command = it.second; |
| 45 | delete command; |
| 46 | } |
| 47 | |
| 48 | m_commands.clear(); |
| 49 | m_instance = NULL; |
| 50 | } |
| 51 | |
| 52 | Commands* Commands::instance() |
| 53 | { |
| 54 | ASSERT(m_instance != NULL); |
| 55 | return m_instance; |
| 56 | } |
| 57 | |
| 58 | Command* Commands::byId(const char* id) |
| 59 | { |
| 60 | if (!id) |
| 61 | return nullptr; |
| 62 | |
| 63 | auto lid = base::string_to_lower(id); |
| 64 | auto it = m_commands.find(lid); |
| 65 | return (it != m_commands.end() ? it->second: nullptr); |
| 66 | } |
| 67 | |
| 68 | Commands* Commands::add(Command* command) |
| 69 | { |
| 70 | auto lid = base::string_to_lower(command->id()); |
| 71 | m_commands[lid] = command; |
| 72 | return this; |
| 73 | } |
| 74 | |
| 75 | void Commands::remove(Command* command) |
| 76 | { |
| 77 | auto lid = base::string_to_lower(command->id()); |
| 78 | auto it = m_commands.find(lid); |
| 79 | ASSERT(it != m_commands.end()); |
| 80 | if (it != m_commands.end()) |
| 81 | m_commands.erase(it); |
| 82 | } |
| 83 | |
| 84 | void Commands::getAllIds(std::vector<std::string>& ids) |
| 85 | { |
| 86 | for (auto& it : m_commands) |
| 87 | ids.push_back(it.second->id()); |
| 88 | } |
| 89 | |
| 90 | } // namespace app |
| 91 |