josiasbb

InputController

Jun 1st, 2017
77
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class InputController
  2. {
  3.     class InputValidator
  4.     {
  5.     public:
  6.         virtual bool Invoke() const = 0;
  7.     };
  8.  
  9.     class GenericInputValidator : public InputValidator
  10.     {
  11.     public:
  12.         virtual bool Invoke() const { return this->Invoke(); }
  13.     private:
  14.         char m_storage[32];
  15.     };
  16.  
  17.     template<typename Device, typename Function, typename Button>
  18.     class InputValidatorWrapper : public InputValidator
  19.     {
  20.     public:
  21.         InputValidatorWrapper(Device * device, Function function, Button button)
  22.             : m_device(device)
  23.             , m_function(function)
  24.             , m_button(button)
  25.         {}
  26.  
  27.         virtual bool Invoke() const { return (m_device->*m_function)(m_button); };
  28.     private:
  29.         Device *    m_device;
  30.         Function    m_function;
  31.         Button      m_button;
  32.     };
  33. public:
  34.     InputController()
  35.         : m_index(0)
  36.     {
  37.     }
  38.  
  39.     template<typename Device, typename Function, typename Button>
  40.     void AddInput(Device * device, Function funct, Button button)
  41.     {
  42.         new (&m_validators[m_index]) InputValidatorWrapper<Device, Function, Button>(device, funct, button);
  43.  
  44.         m_index++;
  45.     }
  46.  
  47.     bool IsAnyTriggered()
  48.     {
  49.         bool result = NS_FALSE;
  50.  
  51.         for (size_t i = 0; i < m_index; ++i)
  52.         {
  53.             result |= m_validators[i].Invoke();
  54.         }
  55.  
  56.         return result;
  57.     }
  58.  
  59. private:
  60.     static constexpr size_t MAX_INPUTS = 16;
  61.  
  62.     GenericInputValidator m_validators[MAX_INPUTS];
  63.     size_t m_index;
  64. };
RAW Paste Data