KeyboardManager: a class to manage the polling of a switch-matrix keyboard
Diff: kbd_mgr/KeyboardStateHandler.h
- Revision:
- 3:1310c57aca77
- Parent:
- 2:eb4cc53ff33d
diff -r eb4cc53ff33d -r 1310c57aca77 kbd_mgr/KeyboardStateHandler.h --- a/kbd_mgr/KeyboardStateHandler.h Sun Jan 23 23:15:36 2011 +0000 +++ b/kbd_mgr/KeyboardStateHandler.h Thu Feb 03 22:01:57 2011 +0000 @@ -1,19 +1,53 @@ -#ifndef KEYBOARD_STATE_HANDLER_H_ -#define KEYBOARD_STATE_HANDLER_H_ - -#include "kbd_mgr/KeyboardState.h" - -namespace kbd_mgr { - -/** - * @brief Interface used to report a keyboard state. - */ -class KeyboardStateHandler { -public: - virtual void operator()(const KeyboardState &newState) = 0; - virtual ~KeyboardStateHandler() { } -}; - -} // kbd_mgr - -#endif // KEYBOARD_STATE_HANDLER_H_ +#ifndef KEYBOARD_STATE_HANDLER_H_ +#define KEYBOARD_STATE_HANDLER_H_ + +#include "kbd_mgr/KeyboardState.h" + +namespace kbd_mgr { + +/** + * @brief Interface used to report a keyboard state. + */ +class KeyboardStateHandler { +public: + virtual void handleState(const KeyboardState &newState) = 0; + virtual ~KeyboardStateHandler() { } +}; + +template <class T> +class MemberKeyboardStateHandler : public KeyboardStateHandler { +public: + typedef void (T::*MemberFunction)(const KeyboardState &); + + MemberKeyboardStateHandler(T *obj, MemberFunction fn) : + object(obj), func(fn) + { } + + virtual void handleState(const KeyboardState &newState) { + (object->*func)(newState); + } + +private: + T *object; + MemberFunction func; +}; + +class FunctionKeyboardStateHandler : public KeyboardStateHandler { +public: + typedef void (*HandlerFunction)(const KeyboardState &); + + FunctionKeyboardStateHandler(HandlerFunction fn) : + func(fn) + { } + + virtual void handleState(const KeyboardState &newState) { + func(newState); + } + +private: + HandlerFunction func; +}; + +} // kbd_mgr + +#endif // KEYBOARD_STATE_HANDLER_H_