From 8769318653e803d06389bd9eb6ec9f481d204e34 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Thu, 2 May 2024 23:19:08 +0100 Subject: [PATCH] Codechange: Ensure SDLK mappings stay in the expected order. (#12608) Add a constexpr constructor that ensures at compile-time that the source SDLK range matches the target range. (cherry picked from commit 90029beb4919ea8d74b9d646e643beb7a3e3fcd0) # Conflicts: # src/video/sdl2_v.cpp # src/video/sdl_v.cpp --- src/video/sdl2_v.cpp | 24 +++++++++++++++--------- src/video/sdl_v.cpp | 18 ++++++++++++------ 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/video/sdl2_v.cpp b/src/video/sdl2_v.cpp index 1edfc45ec6..14af3e3f3f 100644 --- a/src/video/sdl2_v.cpp +++ b/src/video/sdl2_v.cpp @@ -518,18 +518,24 @@ std::vector VideoDriver_SDL_Base::GetListOfMonitorRefreshRates() } struct SDLVkMapping { - SDL_Keycode vk_from; - byte vk_count; - byte map_to; - bool unprintable; + const SDL_Keycode vk_from; + const uint8_t vk_count; + const uint8_t map_to; + const bool unprintable; + + constexpr SDLVkMapping(SDL_Keycode vk_first, SDL_Keycode vk_last, uint8_t map_first, [[maybe_unused]] uint8_t map_last, bool unprintable) + : vk_from(vk_first), vk_count(vk_first - vk_last + 1), map_to(map_first), unprintable(unprintable) + { + assert((vk_last - vk_first) == (map_last - map_first)); + } }; -#define AS(x, z) {x, 0, z, false} -#define AM(x, y, z, w) {x, (byte)(y - x), z, false} -#define AS_UP(x, z) {x, 0, z, true} -#define AM_UP(x, y, z, w) {x, (byte)(y - x), z, true} +#define AS(x, z) {x, x, z, z, false} +#define AM(x, y, z, w) {x, y, z, w, false} +#define AS_UP(x, z) {x, x, z, z, true} +#define AM_UP(x, y, z, w) {x, y, z, w, true} -static const SDLVkMapping _vk_mapping[] = { +static constexpr SDLVkMapping _vk_mapping[] = { /* Pageup stuff + up/down */ AS_UP(SDLK_PAGEUP, WKC_PAGEUP), AS_UP(SDLK_PAGEDOWN, WKC_PAGEDOWN), diff --git a/src/video/sdl_v.cpp b/src/video/sdl_v.cpp index 030251348e..1b0da21318 100644 --- a/src/video/sdl_v.cpp +++ b/src/video/sdl_v.cpp @@ -378,15 +378,21 @@ bool VideoDriver_SDL::ClaimMousePointer() } struct SDLVkMapping { - uint16_t vk_from; - byte vk_count; - byte map_to; + const uint16_t vk_from; + const uint8_t vk_count; + const uint8_t map_to; + + constexpr SDLVkMapping(SDL_Keycode vk_first, SDL_Keycode vk_last, uint8_t map_first, [[maybe_unused]] uint8_t map_last) + : vk_from(vk_first), vk_count(vk_first - vk_last + 1), map_to(map_first) + { + assert((vk_last - vk_first) == (map_last - map_first)); + } }; -#define AS(x, z) {x, 0, z} -#define AM(x, y, z, w) {x, (byte)(y - x), z} +#define AS(x, z) {x, x, z, z, false} +#define AM(x, y, z, w) {x, y, z, w, false} -static const SDLVkMapping _vk_mapping[] = { +static constexpr SDLVkMapping _vk_mapping[] = { /* Pageup stuff + up/down */ AM(SDLK_PAGEUP, SDLK_PAGEDOWN, WKC_PAGEUP, WKC_PAGEDOWN), AS(SDLK_UP, WKC_UP),