From e7c0d2943fdb1d04512e7010116b1df680cabe7f Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Wed, 2 Nov 2022 07:34:30 +0000 Subject: [PATCH] KeyMapper: Use forward_list instead of vector Similar to how we do this for Padmapper. --- Source/diablo.cpp | 1 + Source/options.cpp | 23 ++++++++++++++--------- Source/options.h | 6 ++++-- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/Source/diablo.cpp b/Source/diablo.cpp index 459fb9a64..c96479aed 100644 --- a/Source/diablo.cpp +++ b/Source/diablo.cpp @@ -1761,6 +1761,7 @@ void InitKeymapActions() DebugToggle = !DebugToggle; }); #endif + sgOptions.Keymapper.CommitActions(); } void InitPadmapActions() diff --git a/Source/options.cpp b/Source/options.cpp index fe0482a2f..2773adf70 100644 --- a/Source/options.cpp +++ b/Source/options.cpp @@ -1249,8 +1249,8 @@ KeymapperOptions::KeymapperOptions() std::vector KeymapperOptions::GetEntries() { std::vector entries; - for (auto &action : actions) { - entries.push_back(action.get()); + for (Action &action : actions) { + entries.push_back(&action); } return entries; } @@ -1359,7 +1359,12 @@ bool KeymapperOptions::Action::SetValue(int value) void KeymapperOptions::AddAction(string_view key, const char *name, const char *description, uint32_t defaultKey, std::function actionPressed, std::function actionReleased, std::function enable, unsigned index) { - actions.push_back(std::unique_ptr(new Action(key, name, description, defaultKey, std::move(actionPressed), std::move(actionReleased), std::move(enable), index))); + actions.emplace_front(key, name, description, defaultKey, std::move(actionPressed), std::move(actionReleased), std::move(enable), index); +} + +void KeymapperOptions::CommitActions() +{ + actions.reverse(); } void KeymapperOptions::KeyPressed(uint32_t key) const @@ -1400,9 +1405,9 @@ void KeymapperOptions::KeyReleased(uint32_t key) const string_view KeymapperOptions::KeyNameForAction(string_view actionName) const { - for (const auto &action : actions) { - if (action->key == actionName && action->boundKey != SDLK_UNKNOWN) { - return action->GetValueDescription(); + for (const Action &action : actions) { + if (action.key == actionName && action.boundKey != SDLK_UNKNOWN) { + return action.GetValueDescription(); } } return ""; @@ -1410,9 +1415,9 @@ string_view KeymapperOptions::KeyNameForAction(string_view actionName) const uint32_t KeymapperOptions::KeyForAction(string_view actionName) const { - for (const auto &action : actions) { - if (action->key == actionName && action->boundKey != SDLK_UNKNOWN) { - return action->boundKey; + for (const Action &action : actions) { + if (action.key == actionName && action.boundKey != SDLK_UNKNOWN) { + return action.boundKey; } } return SDLK_UNKNOWN; diff --git a/Source/options.h b/Source/options.h index 16d678015..6a2d05bd3 100644 --- a/Source/options.h +++ b/Source/options.h @@ -638,6 +638,8 @@ struct KeymapperOptions : OptionCategoryBase { // The implicit copy constructor would copy that reference instead of referencing the copy. Action(const Action &) = delete; + Action(string_view key, const char *name, const char *description, uint32_t defaultKey, std::function actionPressed, std::function actionReleased, std::function enable, unsigned index); + [[nodiscard]] string_view GetName() const override; [[nodiscard]] OptionEntryType GetType() const override { @@ -652,7 +654,6 @@ struct KeymapperOptions : OptionCategoryBase { bool SetValue(int value); private: - Action(string_view key, const char *name, const char *description, uint32_t defaultKey, std::function actionPressed, std::function actionReleased, std::function enable, unsigned index); uint32_t defaultKey; std::function actionPressed; std::function actionReleased; @@ -674,13 +675,14 @@ struct KeymapperOptions : OptionCategoryBase { std::function actionReleased = nullptr, std::function enable = nullptr, unsigned index = 0); + void CommitActions(); void KeyPressed(uint32_t key) const; void KeyReleased(uint32_t key) const; string_view KeyNameForAction(string_view actionName) const; uint32_t KeyForAction(string_view actionName) const; private: - std::vector> actions; + std::forward_list actions; std::unordered_map> keyIDToAction; std::unordered_map keyIDToKeyName; std::unordered_map keyNameToKeyID;