bindings: implement Key and Command in terms of std::tuple

This commit is contained in:
Andrzej Rybczak
2015-05-09 19:42:57 +02:00
parent 11735b42b3
commit 078fc099f3

View File

@@ -33,42 +33,23 @@ struct Key
{ {
enum Type { Standard, NCurses }; enum Type { Standard, NCurses };
Key(wchar_t ch, Type ct) : m_char(ch), m_type(ct) { } Key(wchar_t ch, Type ct) : m_impl(ch, ct) { }
wchar_t getChar() const { wchar_t getChar() const { return std::get<0>(m_impl); }
return m_char; Type getType() const { return std::get<1>(m_impl); }
}
Type getType() const {
return m_type;
}
# define KEYS_DEFINE_OPERATOR(CMP) \ bool operator< (const Key &rhs) const { return m_impl < rhs.m_impl; }
bool operator CMP (const Key &k) const { \ bool operator<=(const Key &rhs) const { return m_impl <= rhs.m_impl; }
if (m_char CMP k.m_char) \ bool operator> (const Key &rhs) const { return m_impl > rhs.m_impl; }
return true; \ bool operator>=(const Key &rhs) const { return m_impl >= rhs.m_impl; }
if (m_char != k.m_char) \ bool operator==(const Key &rhs) const { return m_impl == rhs.m_impl; }
return false; \ bool operator!=(const Key &rhs) const { return m_impl != rhs.m_impl; }
return m_type CMP k.m_type; \
}
KEYS_DEFINE_OPERATOR(<);
KEYS_DEFINE_OPERATOR(<=);
KEYS_DEFINE_OPERATOR(>);
KEYS_DEFINE_OPERATOR(>=);
# undef KEYS_DEFINE_OPERATOR
bool operator==(const Key &k) const {
return m_char == k.m_char && m_type == k.m_type;
}
bool operator!=(const Key &k) const {
return !(*this == k);
}
static Key read(NC::Window &w); static Key read(NC::Window &w);
static Key noOp; static Key noOp;
private: private:
wchar_t m_char; std::tuple<wchar_t, Type> m_impl;
Type m_type;
}; };
/// Represents either single action or chain of actions bound to a certain key /// Represents either single action or chain of actions bound to a certain key
@@ -108,14 +89,13 @@ struct Command
{ {
template <typename ArgT> template <typename ArgT>
Command(ArgT &&binding_, bool immediate_) Command(ArgT &&binding_, bool immediate_)
: m_binding(std::forward<ArgT>(binding_)), m_immediate(immediate_) { } : m_impl(std::forward<ArgT>(binding_), immediate_) { }
const Binding &binding() const { return m_binding; } const Binding &binding() const { return std::get<0>(m_impl); }
bool immediate() const { return m_immediate; } bool immediate() const { return std::get<1>(m_impl); }
private: private:
Binding m_binding; std::tuple<Binding, bool> m_impl;
bool m_immediate;
}; };
/// Keybindings configuration /// Keybindings configuration