HIDParser: Use std::array where applicable

Same thing, but strongly enforces the type of the array. This also
allows removing the <type_traits> include, since we can just query the
size of the array.
This commit is contained in:
Lioncash 2019-08-24 20:36:48 -04:00
parent b0c246abc7
commit 14369a9853
1 changed files with 266 additions and 250 deletions

View File

@ -1,8 +1,8 @@
#include "boo/inputdev/HIDParser.hpp" #include "boo/inputdev/HIDParser.hpp"
#include <algorithm> #include <algorithm>
#include <array>
#include <map> #include <map>
#include <type_traits>
#undef min #undef min
#undef max #undef max
@ -13,251 +13,257 @@ namespace boo {
* http://www.usb.org/developers/hidpage/HID1_11.pdf * http://www.usb.org/developers/hidpage/HID1_11.pdf
*/ */
static const char* UsagePageNames[] = {"Undefined", "Generic Desktop", "Simulation", "VR", "Sport", constexpr std::array<const char*, 14> UsagePageNames{
"Game Controls", "Generic Device", "Keyboard", "LEDs", "Button", "Undefined", "Generic Desktop", "Simulation", "VR", "Sport",
"Ordinal", "Telephony", "Consumer", "Digitizer"}; "Game Controls", "Generic Device", "Keyboard", "LEDs", "Button",
"Ordinal", "Telephony", "Consumer", "Digitizer",
};
static const char* GenericDesktopUsages[] = {"Undefined", constexpr std::array<const char*, 182> GenericDesktopUsages{
"Pointer", "Undefined",
"Mouse", "Pointer",
"Reserved", "Mouse",
"Joystick", "Reserved",
"Game Pad", "Joystick",
"Keyboard", "Game Pad",
"Keypad", "Keyboard",
"Multi-axis Controller", "Keypad",
"Tablet PC System Controls", "Multi-axis Controller",
nullptr, "Tablet PC System Controls",
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
"X", nullptr,
"Y", "X",
"Z", "Y",
"Rx", "Z",
"Ry", "Rx",
"Rz", "Ry",
"Slider", "Rz",
"Dial", "Slider",
"Wheel", "Dial",
"Hat Switch", "Wheel",
"Counted Buffer", "Hat Switch",
"Byte Count", "Counted Buffer",
"Motion Wakeup", "Byte Count",
"Start", "Motion Wakeup",
"Select", "Start",
"Reserved", "Select",
"Vx", "Reserved",
"Vy", "Vx",
"Vz", "Vy",
"Vbrx", "Vz",
"Vbry", "Vbrx",
"Vbrz", "Vbry",
"Vno", "Vbrz",
"Feature Notification", "Vno",
"Resolution Multiplier", "Feature Notification",
nullptr, "Resolution Multiplier",
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
"System Control", nullptr,
"System Power Down", "System Control",
"System Sleep", "System Power Down",
"System Wake Up", "System Sleep",
"System Context Menu", "System Wake Up",
"System Main Menu", "System Context Menu",
"System App Menu", "System Main Menu",
"System Menu Help", "System App Menu",
"System Menu Exit", "System Menu Help",
"System Menu Select", "System Menu Exit",
"System Menu Right", "System Menu Select",
"System Menu Left", "System Menu Right",
"System Menu Up", "System Menu Left",
"System Menu Down", "System Menu Up",
"System Cold Restart", "System Menu Down",
"System Warm Restart", "System Cold Restart",
"D-pad Up", "System Warm Restart",
"D-pad Down", "D-pad Up",
"D-pad Right", "D-pad Down",
"D-pad Left", "D-pad Right",
nullptr, "D-pad Left",
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
"System Dock", nullptr,
"System Undock", "System Dock",
"System Setup", "System Undock",
"System Break", "System Setup",
"System Debugger Break", "System Break",
"Application Break", "System Debugger Break",
"Application Debugger Break", "Application Break",
"System Speaker Mute", "Application Debugger Break",
"System Hibernate", "System Speaker Mute",
nullptr, "System Hibernate",
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
"System Display Invert", nullptr,
"System Display Internal", "System Display Invert",
"System Display External", "System Display Internal",
"System Display Both", "System Display External",
"System Display Dual", "System Display Both",
"System Display Toggle Int/Ext"}; "System Display Dual",
"System Display Toggle Int/Ext",
};
static const char* GameUsages[] = {"Undefined", constexpr std::array<const char*, 58> GameUsages{
"3D Game Controller", "Undefined",
"Pinball Device", "3D Game Controller",
"Gun Device", "Pinball Device",
nullptr, "Gun Device",
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
"Point of View", nullptr,
"Turn Right/Left", "Point of View",
"Pitch Forward/Backward", "Turn Right/Left",
"Roll Right/Left", "Pitch Forward/Backward",
"Move Right/Left", "Roll Right/Left",
"Move Forward/Backward", "Move Right/Left",
"Move Up/Down", "Move Forward/Backward",
"Lean Right/Left", "Move Up/Down",
"Lean Forward/Backward", "Lean Right/Left",
"Height of POV", "Lean Forward/Backward",
"Flipper", "Height of POV",
"Secondary Flipper", "Flipper",
"Bump", "Secondary Flipper",
"New Game", "Bump",
"Shoot Ball", "New Game",
"Player", "Shoot Ball",
"Gun Bolt", "Player",
"Gun Clip", "Gun Bolt",
"Gun Selector", "Gun Clip",
"Gun Single Shot", "Gun Selector",
"Gun Burst", "Gun Single Shot",
"Gun Automatic", "Gun Burst",
"Gun Safety", "Gun Automatic",
"Gemepad Fire/Jump", "Gun Safety",
nullptr, "Gemepad Fire/Jump",
"Gamepad Trigger"}; nullptr,
"Gamepad Trigger",
};
enum class HIDCollectionType : uint8_t { enum class HIDCollectionType : uint8_t {
Physical, Physical,
@ -382,21 +388,31 @@ HIDMainItem::HIDMainItem(uint32_t flags, HIDUsagePage usagePage, HIDUsage usage,
, m_reportSize(reportSize) {} , m_reportSize(reportSize) {}
const char* HIDMainItem::GetUsagePageName() const { const char* HIDMainItem::GetUsagePageName() const {
if (int(m_usagePage) >= std::extent<decltype(UsagePageNames)>::value) const auto index = size_t(m_usagePage);
if (index >= UsagePageNames.size()) {
return nullptr; return nullptr;
return UsagePageNames[int(m_usagePage)]; }
return UsagePageNames[index];
} }
const char* HIDMainItem::GetUsageName() const { const char* HIDMainItem::GetUsageName() const {
const auto index = size_t(m_usage);
switch (m_usagePage) { switch (m_usagePage) {
case HIDUsagePage::GenericDesktop: case HIDUsagePage::GenericDesktop:
if (int(m_usage) >= std::extent<decltype(GenericDesktopUsages)>::value) if (index >= GenericDesktopUsages.size()) {
return nullptr; return nullptr;
return GenericDesktopUsages[int(m_usage)]; }
return GenericDesktopUsages[index];
case HIDUsagePage::Game: case HIDUsagePage::Game:
if (int(m_usage) >= std::extent<decltype(GameUsages)>::value) if (index >= GameUsages.size()) {
return nullptr; return nullptr;
return GameUsages[int(m_usage)]; }
return GameUsages[index];
default: default:
return nullptr; return nullptr;
} }