Hotkeys: refactored how hotkey_commands are stored and managed (WML hotkeys only) #6728
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This encompasses two closely related changes. First, we now store hotkey_commands in a map instead of
a vector with accompanying index map. Most code did not rely on the command container being contiguous,
and since the commonly used get_hotkey_command was already performing a lookup in the index map, there
wasn't much need to use a separate container. Associative storage makes the most sense here.
The one place that did rely on the command container being contiguous was remove_wml_hotkey. I realized,
however, that it would be much cleaner to eliminate the manual bookkeeping. To this end, I added a new
wml_hotkey_record RAII class. It registers a hotkey_command when created and removes it on destruction.
Using a map for the commands made this even easier, since it doesn't invalidate other iterators or
require other commands to be moved to keep the container contiguous. In addition, removing the secondary
index map means we no longer need to recreate that every time a wml hotkey is removed!
Switching to this RAII-based system means delete_all_wml_hotkeys could also be removed, since all WML
hotkeys should now be handled by wml_hotkey_record and clean themselves up on destruction.
clear_hotkey_commands was also removed, since it was both misleading and now unnecessary. It only cleared
the index map, not the actual list of hotkey_commands! Given that init_hotkey_commands actually set up
the command list, this was doubly confusing.