From c09e825e0b0c1acd8809b86bb16bf9c9fb37d1ee Mon Sep 17 00:00:00 2001 From: frosch Date: Fri, 18 Apr 2025 19:57:07 +0200 Subject: [PATCH] Codechange: Change SetDateCallback into a std::function, so there is no need for void* user data. --- src/date_gui.cpp | 15 ++++++--------- src/date_gui.h | 4 ++-- src/timetable_gui.cpp | 18 +++++------------- 3 files changed, 13 insertions(+), 24 deletions(-) diff --git a/src/date_gui.cpp b/src/date_gui.cpp index 557816fb0c..ec451c1986 100644 --- a/src/date_gui.cpp +++ b/src/date_gui.cpp @@ -26,8 +26,7 @@ /** Window to select a date graphically by using dropdowns */ struct SetDateWindow : Window { - SetDateCallback *callback = nullptr; ///< Callback to call when a date has been selected - void *callback_data = nullptr; ///< Callback data pointer. + SetDateCallback callback; ///< Callback to call when a date has been selected TimerGameEconomy::YearMonthDay date{}; ///< The currently selected date TimerGameEconomy::Year min_year{}; ///< The minimum year in the year dropdown TimerGameEconomy::Year max_year{}; ///< The maximum year (inclusive) in the year dropdown @@ -42,10 +41,9 @@ struct SetDateWindow : Window { * @param max_year the maximum year (inclusive) to show in the year dropdown * @param callback the callback to call once a date has been selected */ - SetDateWindow(WindowDesc &desc, WindowNumber window_number, Window *parent, TimerGameEconomy::Date initial_date, TimerGameEconomy::Year min_year, TimerGameEconomy::Year max_year, SetDateCallback *callback, void *callback_data) : + SetDateWindow(WindowDesc &desc, WindowNumber window_number, Window *parent, TimerGameEconomy::Date initial_date, TimerGameEconomy::Year min_year, TimerGameEconomy::Year max_year, SetDateCallback &&callback) : Window(desc), - callback(callback), - callback_data(callback_data), + callback(std::move(callback)), min_year(std::max(EconomyTime::MIN_YEAR, min_year)), max_year(std::min(EconomyTime::MAX_YEAR, max_year)) { @@ -149,7 +147,7 @@ struct SetDateWindow : Window { break; case WID_SD_SET_DATE: - if (this->callback != nullptr) this->callback(this, TimerGameEconomy::ConvertYMDToDate(this->date.year, this->date.month, this->date.day), this->callback_data); + this->callback(this, TimerGameEconomy::ConvertYMDToDate(this->date.year, this->date.month, this->date.day)); this->Close(); break; } @@ -212,10 +210,9 @@ static WindowDesc _set_date_desc( * @param min_year the minimum year to show in the year dropdown * @param max_year the maximum year (inclusive) to show in the year dropdown * @param callback the callback to call once a date has been selected - * @param callback_data extra callback data */ -void ShowSetDateWindow(Window *parent, int window_number, TimerGameEconomy::Date initial_date, TimerGameEconomy::Year min_year, TimerGameEconomy::Year max_year, SetDateCallback *callback, void *callback_data) +void ShowSetDateWindow(Window *parent, int window_number, TimerGameEconomy::Date initial_date, TimerGameEconomy::Year min_year, TimerGameEconomy::Year max_year, SetDateCallback &&callback) { CloseWindowByClass(WC_SET_DATE); - new SetDateWindow(_set_date_desc, window_number, parent, initial_date, min_year, max_year, callback, callback_data); + new SetDateWindow(_set_date_desc, window_number, parent, initial_date, min_year, max_year, std::move(callback)); } diff --git a/src/date_gui.h b/src/date_gui.h index 4733f50600..631b0301a2 100644 --- a/src/date_gui.h +++ b/src/date_gui.h @@ -18,8 +18,8 @@ * @param w the window that sends the callback * @param date the date that has been chosen */ -typedef void SetDateCallback(const Window *w, TimerGameEconomy::Date date, void *data); +using SetDateCallback = std::function; -void ShowSetDateWindow(Window *parent, int window_number, TimerGameEconomy::Date initial_date, TimerGameEconomy::Year min_year, TimerGameEconomy::Year max_year, SetDateCallback *callback, void *callback_data); +void ShowSetDateWindow(Window *parent, int window_number, TimerGameEconomy::Date initial_date, TimerGameEconomy::Year min_year, TimerGameEconomy::Year max_year, SetDateCallback &&callback); #endif /* DATE_GUI_H */ diff --git a/src/timetable_gui.cpp b/src/timetable_gui.cpp index ac46927b2d..f07f7cf19e 100644 --- a/src/timetable_gui.cpp +++ b/src/timetable_gui.cpp @@ -176,18 +176,6 @@ static void FillTimetableArrivalDepartureTable(const Vehicle *v, VehicleOrderID } } - -/** - * Callback for when a time has been chosen to start the time table - * @param w the window related to the setting of the date - * @param date the actually chosen date - */ -static void ChangeTimetableStartCallback(const Window *w, TimerGameEconomy::Date date, void *data) -{ - Command::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, w->window_number, reinterpret_cast(data) != 0, GetStartTickFromDate(date)); -} - - struct TimetableWindow : Window { int sel_index = -1; VehicleTimetableWidgets query_widget{}; ///< Which button was clicked to open the query text input? @@ -661,7 +649,11 @@ struct TimetableWindow : Window { this->change_timetable_all = _ctrl_pressed; ShowQueryString({}, STR_TIMETABLE_START_SECONDS_QUERY, 6, this, CS_NUMERAL, QueryStringFlag::AcceptUnchanged); } else { - ShowSetDateWindow(this, v->index.base(), TimerGameEconomy::date, TimerGameEconomy::year, TimerGameEconomy::year + MAX_TIMETABLE_START_YEARS, ChangeTimetableStartCallback, reinterpret_cast(static_cast(_ctrl_pressed))); + ShowSetDateWindow(this, v->index.base(), TimerGameEconomy::date, TimerGameEconomy::year, TimerGameEconomy::year + MAX_TIMETABLE_START_YEARS, + [ctrl=_ctrl_pressed](const Window *w, TimerGameEconomy::Date date) { + Command::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, w->window_number, ctrl, GetStartTickFromDate(date)); + } + ); } break;