Add the ability to unregister commands and clarify who owns command objects

Originally committed to SVN as r5636.
This commit is contained in:
Thomas Goyne 2011-09-28 19:48:02 +00:00
parent 8aca250fd0
commit f970508e67
2 changed files with 16 additions and 3 deletions

View File

@ -34,7 +34,16 @@ namespace cmd {
} }
void reg(Command *cmd) { void reg(Command *cmd) {
cmd_map[cmd->name()] = cmd; std::string name = cmd->name();
if (cmd_map.count(name))
delete cmd_map[name];
cmd_map[name] = cmd;
}
void unreg(std::string const& name) {
iterator it = find_command(name);
delete it->second;
cmd_map.erase(it);
} }
Command *get(std::string const& name) { Command *get(std::string const& name) {

View File

@ -137,9 +137,13 @@ namespace cmd {
void init_builtin_commands(); void init_builtin_commands();
/// Register a command. /// Register a command.
/// @param cmd Command object. /// @param cmd Command object to register. The command system takes ownership of this object.
void reg(Command *cmd); void reg(Command *cmd);
/// Unregister a command.
/// @param cmd Command name to unregister. The associated command object is deleted.
void unreg(std::string const& name);
/// Call a command. /// Call a command.
/// @param name Name of the command to call. /// @param name Name of the command to call.
/// @param c Current Context. /// @param c Current Context.
@ -149,6 +153,6 @@ namespace cmd {
/// @param Command object. /// @param Command object.
Command* get(std::string const& name); Command* get(std::string const& name);
/// Unregister all commands /// Unregister and deletes all commands
void clear(); void clear();
} // namespace cmd } // namespace cmd