diff options
author | Andrzej Rybczak <electricityispower@gmail.com> | 2012-09-20 04:32:51 +0200 |
---|---|---|
committer | Andrzej Rybczak <electricityispower@gmail.com> | 2012-09-20 04:32:51 +0200 |
commit | 07fc58015ea6e322b13968e782f96cde63a64f3a (patch) | |
tree | 8f65fab4f350538b67e2f43f70973580b04ea3d2 /src/bindings.h | |
parent | ba0a47668a1939414281fa74c3beb4202318815c (diff) |
bindings: add support for defining and executing commands
Diffstat (limited to 'src/bindings.h')
-rw-r--r-- | src/bindings.h | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/src/bindings.h b/src/bindings.h index c6ee340d..c62784bb 100644 --- a/src/bindings.h +++ b/src/bindings.h @@ -22,6 +22,7 @@ #define _BINDINGS_H #include <cassert> +#include <unordered_map> #include "actions.h" #include "macro_utilities.h" @@ -85,6 +86,20 @@ struct Binding } } + bool execute() const { + bool result = false; + if (m_is_single) { + assert(m_action); + result = m_action->Execute(); + } else { + for (auto it = m_chain->begin(); it != m_chain->end(); ++it) + if (!(*it)->Execute()) + break; + result = true; + } + return result; + } + bool isSingle() const { return m_is_single; } @@ -105,16 +120,41 @@ private: }; }; +/// Represents executable command +struct Command +{ + Command(const Binding &binding_, bool immediate_) + : m_binding(binding_), m_immediate(immediate_) { } + + const Binding &binding() const { return m_binding; } + bool immediate() const { return m_immediate; } + +private: + Binding m_binding; + bool m_immediate; +}; + /// Keybindings configuration -struct BindingsConfiguration +class BindingsConfiguration { + typedef std::unordered_map<std::string, Command> CommandsSet; typedef std::multimap<Key, Binding> BindingsMap; + +public: typedef BindingsMap::iterator BindingIterator; typedef BindingsMap::const_iterator ConstBindingIterator; bool read(const std::string &file); void generateDefaults(); + const Command *findCommand(const std::string &name) { + const Command *ptr = 0; + auto it = m_commands.find(name); + if (it != m_commands.end()) + ptr = &it->second; + return ptr; + } + std::pair<BindingIterator, BindingIterator> get(const Key &k) { return m_bindings.equal_range(k); } @@ -132,6 +172,7 @@ private: } BindingsMap m_bindings; + CommandsSet m_commands; }; extern BindingsConfiguration Bindings; |