diff --git a/src/airport_gui.cpp b/src/airport_gui.cpp index dff11d0f8b..c67132126a 100644 --- a/src/airport_gui.cpp +++ b/src/airport_gui.cpp @@ -183,7 +183,6 @@ static EventState AirportToolbarGlobalHotkeys(int hotkey) static Hotkey airtoolbar_hotkeys[] = { Hotkey('1', "airport", WID_AT_AIRPORT), Hotkey('2', "demolish", WID_AT_DEMOLISH), - HOTKEY_LIST_END }; HotkeyList BuildAirToolbarWindow::hotkeys("airtoolbar", airtoolbar_hotkeys, AirportToolbarGlobalHotkeys); diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index bf2527a205..98c6e3f5da 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -2285,7 +2285,6 @@ struct BuildVehicleWindow : BuildVehicleWindowBase { static Hotkey buildvehicle_hotkeys[] = { Hotkey('F', "focus_filter_box", BVHK_FOCUS_FILTER_BOX), - HOTKEY_LIST_END }; HotkeyList BuildVehicleWindow::hotkeys("buildvehicle", buildvehicle_hotkeys); diff --git a/src/dock_gui.cpp b/src/dock_gui.cpp index 05f7334ca6..e669d4f9bc 100644 --- a/src/dock_gui.cpp +++ b/src/dock_gui.cpp @@ -318,7 +318,6 @@ static Hotkey dockstoolbar_hotkeys[] = { Hotkey('6', "buoy", WID_DT_BUOY), Hotkey('7', "river", WID_DT_RIVER), Hotkey(_dockstoolbar_aqueduct_keys, "aqueduct", WID_DT_BUILD_AQUEDUCT), - HOTKEY_LIST_END }; HotkeyList BuildDocksToolbarWindow::hotkeys("dockstoolbar", dockstoolbar_hotkeys, DockToolbarGlobalHotkeys); diff --git a/src/hotkeys.cpp b/src/hotkeys.cpp index 2473c29eb8..d2436f3bfb 100644 --- a/src/hotkeys.cpp +++ b/src/hotkeys.cpp @@ -152,14 +152,14 @@ static uint16_t ParseKeycode(const char *start, const char *end) * @param hotkey The hotkey object to add the keycodes to * @param value The string to parse */ -static void ParseHotkeys(Hotkey *hotkey, const char *value) +static void ParseHotkeys(Hotkey &hotkey, const char *value) { const char *start = value; while (*start != '\0') { const char *end = start; while (*end != '\0' && *end != ',') end++; uint16_t keycode = ParseKeycode(start, end); - if (keycode != 0) hotkey->AddKeycode(keycode); + if (keycode != 0) hotkey.AddKeycode(keycode); start = (*end == ',') ? end + 1: end; } } @@ -213,10 +213,10 @@ static std::string KeycodeToString(uint16_t keycode) * @param hotkey The keycodes of this hotkey need to be converted to a string. * @return A string representation of all keycodes. */ -std::string SaveKeycodes(const Hotkey *hotkey) +std::string SaveKeycodes(const Hotkey &hotkey) { std::string str; - for (auto keycode : hotkey->keycodes) { + for (auto keycode : hotkey.keycodes) { if (!str.empty()) str += ","; str += KeycodeToString(keycode); } @@ -263,13 +263,21 @@ void Hotkey::AddKeycode(uint16_t keycode) this->keycodes.insert(keycode); } -HotkeyList::HotkeyList(const char *ini_group, Hotkey *items, GlobalHotkeyHandlerFunc global_hotkey_handler) : - global_hotkey_handler(global_hotkey_handler), ini_group(ini_group), items(items) +HotkeyList::HotkeyList(const char *ini_group, std::vector items, GlobalHotkeyHandlerFunc global_hotkey_handler) : + global_hotkey_handler(global_hotkey_handler), ini_group(ini_group), items(std::move(items)) { if (_hotkey_lists == nullptr) _hotkey_lists = new std::vector(); _hotkey_lists->push_back(this); } +HotkeyList::HotkeyList(const char *ini_group, std::span items, GlobalHotkeyHandlerFunc global_hotkey_handler) : + global_hotkey_handler(global_hotkey_handler), ini_group(ini_group) +{ + this->items.assign(items.begin(), items.end()); + if (_hotkey_lists == nullptr) _hotkey_lists = new std::vector(); + _hotkey_lists->push_back(this); +} + HotkeyList::~HotkeyList() { _hotkey_lists->erase(std::find(_hotkey_lists->begin(), _hotkey_lists->end(), this)); @@ -283,10 +291,10 @@ void HotkeyList::Load(const IniFile &ini) { const IniGroup *group = ini.GetGroup(this->ini_group); if (group == nullptr) return; - for (Hotkey *hotkey = this->items; hotkey->name != nullptr; ++hotkey) { - const IniItem *item = group->GetItem(hotkey->name); + for (Hotkey &hotkey : this->items) { + const IniItem *item = group->GetItem(hotkey.name); if (item != nullptr) { - hotkey->keycodes.clear(); + hotkey.keycodes.clear(); if (item->value.has_value()) ParseHotkeys(hotkey, item->value->c_str()); } } @@ -299,8 +307,8 @@ void HotkeyList::Load(const IniFile &ini) void HotkeyList::Save(IniFile &ini) const { IniGroup &group = ini.GetOrCreateGroup(this->ini_group); - for (const Hotkey *hotkey = this->items; hotkey->name != nullptr; ++hotkey) { - IniItem &item = group.GetOrCreateItem(hotkey->name); + for (const Hotkey &hotkey : this->items) { + IniItem &item = group.GetOrCreateItem(hotkey.name); item.SetValue(SaveKeycodes(hotkey)); } } @@ -313,11 +321,11 @@ void HotkeyList::Save(IniFile &ini) const */ int HotkeyList::CheckMatch(uint16_t keycode, bool global_only) const { - for (const Hotkey *list = this->items; list->name != nullptr; ++list) { - auto begin = list->keycodes.begin(); - auto end = list->keycodes.end(); + for (const Hotkey &list : this->items) { + auto begin = list.keycodes.begin(); + auto end = list.keycodes.end(); if (std::find(begin, end, keycode | WKC_GLOBAL_HOTKEY) != end || (!global_only && std::find(begin, end, keycode) != end)) { - return list->num; + return list.num; } } return -1; diff --git a/src/hotkeys.h b/src/hotkeys.h index 38d855cab9..46dae01166 100644 --- a/src/hotkeys.h +++ b/src/hotkeys.h @@ -30,8 +30,6 @@ struct Hotkey { btree::btree_set keycodes; }; -#define HOTKEY_LIST_END Hotkey((uint16_t)0, nullptr, -1) - struct IniFile; /** @@ -40,7 +38,8 @@ struct IniFile; struct HotkeyList { typedef EventState (*GlobalHotkeyHandlerFunc)(int hotkey); - HotkeyList(const char *ini_group, Hotkey *items, GlobalHotkeyHandlerFunc global_hotkey_handler = nullptr); + HotkeyList(const char *ini_group, std::vector items, GlobalHotkeyHandlerFunc global_hotkey_handler = nullptr); + HotkeyList(const char *ini_group, std::span items, GlobalHotkeyHandlerFunc global_hotkey_handler = nullptr); ~HotkeyList(); void Load(const IniFile &ini); @@ -51,7 +50,7 @@ struct HotkeyList { GlobalHotkeyHandlerFunc global_hotkey_handler; private: const char *ini_group; - Hotkey *items; + std::vector items; /** * Dummy private copy constructor to prevent compilers from diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index f1e274872f..441b88f5f7 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -1959,7 +1959,6 @@ public: static Hotkey industrydirectory_hotkeys[] = { Hotkey('F', "focus_filter_box", IDHK_FOCUS_FILTER_BOX), - HOTKEY_LIST_END }; HotkeyList IndustryDirectoryWindow::hotkeys("industrydirectory", industrydirectory_hotkeys); diff --git a/src/main_gui.cpp b/src/main_gui.cpp index c74da1bdce..d9e1c1e288 100644 --- a/src/main_gui.cpp +++ b/src/main_gui.cpp @@ -609,7 +609,6 @@ static Hotkey global_hotkeys[] = { Hotkey((uint16_t)0, "switch_viewport_route_overlay_mode", GHK_SWITCH_VIEWPORT_ROUTE_OVERLAY_MODE), Hotkey((uint16_t)0, "switch_viewport_map_slope_mode", GHK_SWITCH_VIEWPORT_MAP_SLOPE_MODE), Hotkey((uint16_t)0, "switch_viewport_map_height_mode", GHK_SWITCH_VIEWPORT_MAP_HEIGHT_MODE), - HOTKEY_LIST_END }; HotkeyList MainWindow::hotkeys("global", global_hotkeys); diff --git a/src/object_gui.cpp b/src/object_gui.cpp index cf19751cdd..a469ab01d6 100644 --- a/src/object_gui.cpp +++ b/src/object_gui.cpp @@ -690,7 +690,6 @@ static EventState BuildObjectGlobalHotkeys(int hotkey) static Hotkey buildobject_hotkeys[] = { Hotkey('F', "focus_filter_box", BOHK_FOCUS_FILTER_BOX), - HOTKEY_LIST_END }; HotkeyList BuildObjectWindow::hotkeys("buildobject", buildobject_hotkeys, BuildObjectGlobalHotkeys); diff --git a/src/order_gui.cpp b/src/order_gui.cpp index 069c713a6c..0b31ce457d 100644 --- a/src/order_gui.cpp +++ b/src/order_gui.cpp @@ -3816,7 +3816,6 @@ static Hotkey order_hotkeys[] = { Hotkey((uint16_t)0, "duplicate", OHK_DUPLICATE), Hotkey((uint16_t)0, "retarget_jump", OHK_RETARGET_JUMP), Hotkey((uint16_t)0, "close", OHK_CLOSE), - HOTKEY_LIST_END }; HotkeyList OrdersWindow::hotkeys("order", order_hotkeys); diff --git a/src/rail_gui.cpp b/src/rail_gui.cpp index 60605ec086..1a7c4de27d 100644 --- a/src/rail_gui.cpp +++ b/src/rail_gui.cpp @@ -1048,7 +1048,6 @@ static Hotkey railtoolbar_hotkeys[] = { Hotkey('R', "remove", WID_RAT_REMOVE), Hotkey('C', "convert", WID_RAT_CONVERT_RAIL), Hotkey(WKC_CTRL | 'C', "convert_track", WID_RAT_CONVERT_RAIL_TRACK), - HOTKEY_LIST_END }; HotkeyList BuildRailToolbarWindow::hotkeys("railtoolbar", railtoolbar_hotkeys, RailToolbarGlobalHotkeys); @@ -1781,7 +1780,6 @@ static EventState BuildRailStationGlobalHotkeys(int hotkey) static Hotkey buildrailstation_hotkeys[] = { Hotkey('F', "focus_filter_box", BRASHK_FOCUS_FILTER_BOX), - HOTKEY_LIST_END }; HotkeyList BuildRailStationWindow::hotkeys("buildrailstation", buildrailstation_hotkeys, BuildRailStationGlobalHotkeys); @@ -2293,7 +2291,6 @@ static Hotkey signaltoolbar_hotkeys[] = { Hotkey('H', "signal_pbs", WID_BS_ELECTRIC_PBS), Hotkey('J', "signal_pbs_oneway", WID_BS_ELECTRIC_PBS_OWAY), Hotkey((uint16_t)0, "signal_no_entry", WID_BS_ELECTRIC_NO_ENTRY), - HOTKEY_LIST_END }; HotkeyList BuildSignalWindow::hotkeys("signaltoolbar", signaltoolbar_hotkeys); diff --git a/src/road_gui.cpp b/src/road_gui.cpp index ca005f0019..d5ee464d29 100644 --- a/src/road_gui.cpp +++ b/src/road_gui.cpp @@ -917,7 +917,6 @@ static Hotkey roadtoolbar_hotkeys[] = { Hotkey('R', "remove", WID_ROT_REMOVE), Hotkey('C', "convert", WID_ROT_CONVERT_ROAD), Hotkey('9', "waypoint", WID_ROT_BUILD_WAYPOINT), - HOTKEY_LIST_END }; HotkeyList BuildRoadToolbarWindow::road_hotkeys("roadtoolbar", roadtoolbar_hotkeys, RoadToolbarGlobalHotkeys); @@ -934,7 +933,6 @@ static Hotkey tramtoolbar_hotkeys[] = { Hotkey('R', "remove", WID_ROT_REMOVE), Hotkey('C', "convert", WID_ROT_CONVERT_ROAD), Hotkey('9', "waypoint", WID_ROT_BUILD_WAYPOINT), - HOTKEY_LIST_END }; HotkeyList BuildRoadToolbarWindow::tram_hotkeys("tramtoolbar", tramtoolbar_hotkeys, TramToolbarGlobalHotkeys); @@ -1762,13 +1760,11 @@ public: static Hotkey buildroadstop_hotkeys[] = { Hotkey('F', "focus_filter_box", BROSHK_FOCUS_FILTER_BOX), - HOTKEY_LIST_END }; HotkeyList BuildRoadStationWindow::road_hotkeys("buildroadstop", buildroadstop_hotkeys); static Hotkey buildtramstop_hotkeys[] = { Hotkey('F', "focus_filter_box", BROSHK_FOCUS_FILTER_BOX), - HOTKEY_LIST_END }; HotkeyList BuildRoadStationWindow::tram_hotkeys("buildtramstop", buildtramstop_hotkeys); diff --git a/src/script/script_gui.cpp b/src/script/script_gui.cpp index 09047647a0..75fe62637a 100644 --- a/src/script/script_gui.cpp +++ b/src/script/script_gui.cpp @@ -1265,7 +1265,6 @@ static Hotkey scriptdebug_hotkeys[] = { Hotkey('F', "break_string", WID_SCRD_BREAK_STR_EDIT_BOX), Hotkey('C', "match_case", WID_SCRD_MATCH_CASE_BTN), Hotkey(WKC_RETURN, "continue", WID_SCRD_CONTINUE_BTN), - HOTKEY_LIST_END }; HotkeyList ScriptDebugWindow::hotkeys("aidebug", scriptdebug_hotkeys, ScriptDebugGlobalHotkeys); diff --git a/src/signs_gui.cpp b/src/signs_gui.cpp index d4d1165f20..0f05845ac4 100644 --- a/src/signs_gui.cpp +++ b/src/signs_gui.cpp @@ -350,7 +350,6 @@ static EventState SignListGlobalHotkeys(int hotkey) static Hotkey signlist_hotkeys[] = { Hotkey('F', "focus_filter_box", SLHK_FOCUS_FILTER_BOX), - HOTKEY_LIST_END }; HotkeyList SignListWindow::hotkeys("signlist", signlist_hotkeys, SignListGlobalHotkeys); diff --git a/src/terraform_gui.cpp b/src/terraform_gui.cpp index 38b4a7b97f..ae2a00c987 100644 --- a/src/terraform_gui.cpp +++ b/src/terraform_gui.cpp @@ -383,7 +383,6 @@ static Hotkey terraform_hotkeys[] = { Hotkey('R' | WKC_SHIFT, "ruler", WID_TT_MEASUREMENT_TOOL), Hotkey('O', "placesign", WID_TT_PLACE_SIGN), Hotkey('P', "placeobject", WID_TT_PLACE_OBJECT), - HOTKEY_LIST_END }; HotkeyList TerraformToolbarWindow::hotkeys("terraform", terraform_hotkeys, TerraformToolbarGlobalHotkeys); @@ -844,7 +843,6 @@ static Hotkey terraform_editor_hotkeys[] = { Hotkey('T', "desert", WID_ETT_PLACE_DESERT), Hotkey('O', "object", WID_ETT_PLACE_OBJECT), Hotkey('H', "house", WID_ETT_PLACE_HOUSE), - HOTKEY_LIST_END }; HotkeyList ScenarioEditorLandscapeGenerationWindow::hotkeys("terraform_editor", terraform_editor_hotkeys, TerraformToolbarEditorGlobalHotkeys); diff --git a/src/toolbar_gui.cpp b/src/toolbar_gui.cpp index c01781d826..37f28dad4d 100644 --- a/src/toolbar_gui.cpp +++ b/src/toolbar_gui.cpp @@ -2339,7 +2339,6 @@ static Hotkey maintoolbar_hotkeys[] = { Hotkey((uint16_t)0, "template_replacement", MTHK_TEMPLATE_REPLACEMENT), Hotkey((uint16_t)0, "train_slots", MTHK_TRAIN_SLOTS), Hotkey((uint16_t)0, "train_counters", MTHK_TRAIN_COUNTERS), - HOTKEY_LIST_END }; HotkeyList MainToolbarWindow::hotkeys("maintoolbar", maintoolbar_hotkeys); @@ -2724,7 +2723,6 @@ static Hotkey scenedit_maintoolbar_hotkeys[] = { Hotkey('L', "terraform", MTEHK_TERRAFORM), Hotkey('M', "smallmap", MTEHK_SMALLMAP), Hotkey('V', "extra_viewport", MTEHK_EXTRA_VIEWPORT), - HOTKEY_LIST_END }; HotkeyList ScenarioEditorToolbarWindow::hotkeys("scenedit_maintoolbar", scenedit_maintoolbar_hotkeys); diff --git a/src/town_gui.cpp b/src/town_gui.cpp index 6e2254348f..82894390e4 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -1253,7 +1253,6 @@ public: static Hotkey towndirectory_hotkeys[] = { Hotkey('F', "focus_filter_box", TDHK_FOCUS_FILTER_BOX), - HOTKEY_LIST_END }; HotkeyList TownDirectoryWindow::hotkeys("towndirectory", towndirectory_hotkeys); diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 266bd4404c..adcbffde0e 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -4473,7 +4473,6 @@ public: static Hotkey vehicleview_hotkeys[] = { Hotkey('H', "honk", WID_VV_HONK_HORN), - HOTKEY_LIST_END }; HotkeyList VehicleViewWindow::hotkeys("vehicleview", vehicleview_hotkeys);