From faaceb5b5b100b6911611cc5792966e47a609423 Mon Sep 17 00:00:00 2001 From: kimci86 Date: Sun, 17 Dec 2023 18:38:54 +0100 Subject: [PATCH] Define KeyCount and ScancodeCount out of enums Those values are not valid Key or Scancode values, so it doesn't make sense for them to have Key or Scancode type. Moving them out of their enum makes it possible to write exhaustive switch case statement without having to write a case for those values. Making them unsigned int allows to use them as array size without having to do a static_cast. --- include/SFML/Window/Keyboard.hpp | 18 ++++++++++++++++-- src/SFML/Window/Unix/KeyboardImpl.cpp | 4 ++-- src/SFML/Window/Win32/InputImpl.cpp | 10 +++++----- src/SFML/Window/macOS/HIDInputManager.hpp | 8 ++++---- src/SFML/Window/macOS/HIDInputManager.mm | 2 +- 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/include/SFML/Window/Keyboard.hpp b/include/SFML/Window/Keyboard.hpp index b64abe38f..98afc3f16 100644 --- a/include/SFML/Window/Keyboard.hpp +++ b/include/SFML/Window/Keyboard.hpp @@ -153,9 +153,16 @@ enum Key F15, //!< The F15 key Pause, //!< The Pause key - KeyCount, //!< Keep last -- the total number of keyboard keys + Last = Pause, //!< Keep equal to last enumerator -- used to define KeyCount }; +//////////////////////////////////////////////////////////// +/// \brief The total number of keyboard keys, ignoring Key::Unknown +/// +//////////////////////////////////////////////////////////// +// NOLINTNEXTLINE(readability-identifier-naming) +static constexpr unsigned int KeyCount{static_cast(Key::Last) + 1}; + //////////////////////////////////////////////////////////// /// \brief Scancodes /// @@ -320,11 +327,18 @@ enum class Scan LaunchMail, //!< Keyboard Launch Mail key LaunchMediaSelect, //!< Keyboard Launch Media Select key - ScancodeCount //!< Keep last -- the total number of scancodes + Last = LaunchMediaSelect, //!< Keep equal to last enumerator -- used to define ScancodeCount }; using Scancode = Scan; +//////////////////////////////////////////////////////////// +/// \brief The total number of scancodes, ignoring Scan::Unknown +/// +//////////////////////////////////////////////////////////// +// NOLINTNEXTLINE(readability-identifier-naming) +static constexpr unsigned int ScancodeCount{static_cast(Scan::Last) + 1}; + //////////////////////////////////////////////////////////// /// \brief Check if a key is pressed /// diff --git a/src/SFML/Window/Unix/KeyboardImpl.cpp b/src/SFML/Window/Unix/KeyboardImpl.cpp index a45d12da4..8f2ae4add 100644 --- a/src/SFML/Window/Unix/KeyboardImpl.cpp +++ b/src/SFML/Window/Unix/KeyboardImpl.cpp @@ -48,8 +48,8 @@ namespace const KeyCode nullKeyCode = 0; const int maxKeyCode = 256; -KeyCode scancodeToKeycode[static_cast(sf::Keyboard::Scan::ScancodeCount)]; ///< Mapping of SFML scancode to X11 KeyCode -sf::Keyboard::Scancode keycodeToScancode[maxKeyCode]; ///< Mapping of X11 KeyCode to SFML scancode +KeyCode scancodeToKeycode[sf::Keyboard::ScancodeCount]; ///< Mapping of SFML scancode to X11 KeyCode +sf::Keyboard::Scancode keycodeToScancode[maxKeyCode]; ///< Mapping of X11 KeyCode to SFML scancode //////////////////////////////////////////////////////////// bool isValidKeycode(KeyCode keycode) diff --git a/src/SFML/Window/Win32/InputImpl.cpp b/src/SFML/Window/Win32/InputImpl.cpp index e9f5ea05c..c8c9cb5fb 100644 --- a/src/SFML/Window/Win32/InputImpl.cpp +++ b/src/SFML/Window/Win32/InputImpl.cpp @@ -33,8 +33,8 @@ namespace { -sf::Keyboard::Scancode keyToScancodeMapping[sf::Keyboard::KeyCount]; ///< Mapping from Key to Scancode -sf::Keyboard::Key scancodeToKeyMapping[static_cast(sf::Keyboard::Scan::ScancodeCount)]; ///< Mapping from Scancode to Key +sf::Keyboard::Scancode keyToScancodeMapping[sf::Keyboard::KeyCount]; ///< Mapping from Key to Scancode +sf::Keyboard::Key scancodeToKeyMapping[sf::Keyboard::ScancodeCount]; ///< Mapping from Scancode to Key sf::Keyboard::Key virtualKeyToSfKey(UINT virtualKey) { @@ -527,7 +527,7 @@ void ensureMappings() key = sf::Keyboard::Unknown; // Phase 2: Translate scancode to virtual code to key names - for (int i = 0; i < static_cast(sf::Keyboard::Scan::ScancodeCount); ++i) + for (unsigned int i = 0; i < sf::Keyboard::ScancodeCount; ++i) { const auto scan = static_cast(i); const UINT virtualKey = sfScanToVirtualKey(scan); @@ -542,12 +542,12 @@ void ensureMappings() bool isValidScancode(sf::Keyboard::Scancode code) { - return code > sf::Keyboard::Scan::Unknown && code < sf::Keyboard::Scan::ScancodeCount; + return code > sf::Keyboard::Scan::Unknown && static_cast(code) < sf::Keyboard::ScancodeCount; } bool isValidKey(sf::Keyboard::Key key) { - return key > sf::Keyboard::Unknown && key < sf::Keyboard::KeyCount; + return key > sf::Keyboard::Unknown && static_cast(key) < sf::Keyboard::KeyCount; } } // namespace diff --git a/src/SFML/Window/macOS/HIDInputManager.hpp b/src/SFML/Window/macOS/HIDInputManager.hpp index cb0efbf9c..5b70d902f 100644 --- a/src/SFML/Window/macOS/HIDInputManager.hpp +++ b/src/SFML/Window/macOS/HIDInputManager.hpp @@ -286,10 +286,10 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - IOHIDManagerRef m_manager{}; ///< Underlying HID Manager - IOHIDElements m_keys[static_cast(Keyboard::Scan::ScancodeCount)]; ///< All the keys on any connected keyboard - Keyboard::Scancode m_keyToScancodeMapping[Keyboard::KeyCount]{}; ///< Mapping from Key to Scancode - Keyboard::Key m_scancodeToKeyMapping[static_cast(Keyboard::Scan::ScancodeCount)]{}; ///< Mapping from Scancode to Key + IOHIDManagerRef m_manager{}; ///< Underlying HID Manager + IOHIDElements m_keys[Keyboard::ScancodeCount]; ///< All the keys on any connected keyboard + Keyboard::Scancode m_keyToScancodeMapping[Keyboard::KeyCount]{}; ///< Mapping from Key to Scancode + Keyboard::Key m_scancodeToKeyMapping[Keyboard::ScancodeCount]{}; ///< Mapping from Scancode to Key //////////////////////////////////////////////////////////// /// m_keys' index corresponds to sf::Keyboard::Scancode enum. diff --git a/src/SFML/Window/macOS/HIDInputManager.mm b/src/SFML/Window/macOS/HIDInputManager.mm index 1f15308a8..a3a425bbb 100644 --- a/src/SFML/Window/macOS/HIDInputManager.mm +++ b/src/SFML/Window/macOS/HIDInputManager.mm @@ -824,7 +824,7 @@ void HIDInputManager::buildMappings() // For each scancode having a IOHIDElement, we translate the corresponding // virtual code to a localized Key. - for (int i = 0; i < static_cast(Keyboard::Scan::ScancodeCount); ++i) + for (unsigned int i = 0; i < Keyboard::ScancodeCount; ++i) { const auto scan = static_cast(i); const std::uint8_t virtualCode = scanToVirtualCode(scan);