Console: Use emplace in registerCommand()

Allows avoiding a redundant initial element. We can also remove the
const from the std::function rvalue to allow a std::move to take effect.

We can also use the std::string_view constructor for std::string to
avoid an unnecessary strlen() call.
This commit is contained in:
Lioncash 2019-10-21 01:03:27 -04:00
parent cbd40382bf
commit 5d2987588b
2 changed files with 10 additions and 5 deletions

View File

@ -81,7 +81,7 @@ private:
public:
Console(CVarManager*);
void registerCommand(std::string_view name, std::string_view helpText, std::string_view usage,
const std::function<void(Console*, const std::vector<std::string>&)>&& func,
std::function<void(Console*, const std::vector<std::string>&)>&& func,
SConsoleCommand::ECommandFlags cmdFlags = SConsoleCommand::ECommandFlags::Normal);
void unregisterCommand(std::string_view name);

View File

@ -40,12 +40,17 @@ Console::Console(CVarManager* cvarMgr) : m_cvarMgr(cvarMgr), m_overwrite(false),
}
void Console::registerCommand(std::string_view name, std::string_view helpText, std::string_view usage,
const std::function<void(Console*, const std::vector<std::string>&)>&& func,
std::function<void(Console*, const std::vector<std::string>&)>&& func,
SConsoleCommand::ECommandFlags cmdFlags) {
std::string lowName = name.data();
std::string lowName{name};
athena::utility::tolower(lowName);
if (m_commands.find(lowName) == m_commands.end())
m_commands[lowName] = SConsoleCommand{name.data(), helpText.data(), usage.data(), std::move(func), cmdFlags};
if (m_commands.find(lowName) != m_commands.end()) {
return;
}
m_commands.emplace(std::move(lowName), SConsoleCommand{std::string{name}, std::string{helpText}, std::string{usage},
std::move(func), cmdFlags});
}
void Console::unregisterCommand(std::string_view name) {