Skip to content

Commit

Permalink
Codechange: use std::optional<std::string> over char * for text query…
Browse files Browse the repository at this point in the history
… results
  • Loading branch information
rubidium42 committed Jun 29, 2024
1 parent 3819ab2 commit 1420021
Show file tree
Hide file tree
Showing 22 changed files with 127 additions and 127 deletions.
6 changes: 3 additions & 3 deletions src/build_vehicle_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1853,11 +1853,11 @@ struct BuildVehicleWindow : Window {
}
}

void OnQueryTextFinished(char *str) override
void OnQueryTextFinished(std::optional<std::string> str) override
{
if (str == nullptr) return;
if (!str.has_value()) return;

Command<CMD_RENAME_ENGINE>::Post(STR_ERROR_CAN_T_RENAME_TRAIN_TYPE + this->vehicle_type, this->rename_engine, str);
Command<CMD_RENAME_ENGINE>::Post(STR_ERROR_CAN_T_RENAME_TRAIN_TYPE + this->vehicle_type, this->rename_engine, *str);
}

void OnDropdownSelect(WidgetID widget, int index) override
Expand Down
6 changes: 3 additions & 3 deletions src/cheat_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,14 +404,14 @@ struct CheatWindow : Window {
this->SetDirty();
}

void OnQueryTextFinished(char *str) override
void OnQueryTextFinished(std::optional<std::string> str) override
{
/* Was 'cancel' pressed or nothing entered? */
if (str == nullptr || StrEmpty(str)) return;
if (!str.has_value() || str->empty()) return;

const CheatEntry *ce = &_cheats_ui[clicked_widget];
int oldvalue = (int32_t)ReadValue(ce->variable, ce->type);
int value = atoi(str);
int value = atoi(str->c_str());
*ce->been_used = true;
value = ce->proc(value, value - oldvalue);

Expand Down
18 changes: 9 additions & 9 deletions src/company_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1686,12 +1686,12 @@ class SelectCompanyManagerFaceWindow : public Window
}
}

void OnQueryTextFinished(char *str) override
void OnQueryTextFinished(std::optional<std::string> str) override
{
if (str == nullptr) return;
if (!str.has_value()) return;
/* Set a new company manager face number */
if (!StrEmpty(str)) {
this->face = std::strtoul(str, nullptr, 10);
if (!str->empty()) {
this->face = std::strtoul(str->c_str(), nullptr, 10);
ScaleAllCompanyManagerFaceBits(this->face);
ShowErrorMessage(STR_FACE_FACECODE_SET, INVALID_STRING_ID, WL_INFO);
this->UpdateData();
Expand Down Expand Up @@ -2520,25 +2520,25 @@ struct CompanyWindow : Window
this->RaiseButtons();
}

void OnQueryTextFinished(char *str) override
void OnQueryTextFinished(std::optional<std::string> str) override
{
if (str == nullptr) return;
if (!str.has_value()) return;

switch (this->query_widget) {
default: NOT_REACHED();

case WID_C_GIVE_MONEY: {
Money money = std::strtoull(str, nullptr, 10) / GetCurrency().rate;
Money money = std::strtoull(str->c_str(), nullptr, 10) / GetCurrency().rate;
Command<CMD_GIVE_MONEY>::Post(STR_ERROR_CAN_T_GIVE_MONEY, money, (CompanyID)this->window_number);
break;
}

case WID_C_PRESIDENT_NAME:
Command<CMD_RENAME_PRESIDENT>::Post(STR_ERROR_CAN_T_CHANGE_PRESIDENT, str);
Command<CMD_RENAME_PRESIDENT>::Post(STR_ERROR_CAN_T_CHANGE_PRESIDENT, *str);
break;

case WID_C_COMPANY_NAME:
Command<CMD_RENAME_COMPANY>::Post(STR_ERROR_CAN_T_CHANGE_COMPANY_NAME, str);
Command<CMD_RENAME_COMPANY>::Post(STR_ERROR_CAN_T_CHANGE_COMPANY_NAME, *str);
break;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/depot_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -831,12 +831,12 @@ struct DepotWindow : Window {
}
}

void OnQueryTextFinished(char *str) override
void OnQueryTextFinished(std::optional<std::string> str) override
{
if (str == nullptr) return;
if (!str.has_value()) return;

/* Do depot renaming */
Command<CMD_RENAME_DEPOT>::Post(STR_ERROR_CAN_T_RENAME_DEPOT, this->GetDepotIndex(), str);
Command<CMD_RENAME_DEPOT>::Post(STR_ERROR_CAN_T_RENAME_DEPOT, this->GetDepotIndex(), *str);
}

bool OnRightClick([[maybe_unused]] Point pt, WidgetID widget) override
Expand Down
6 changes: 3 additions & 3 deletions src/game/game_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,10 @@ struct GSConfigWindow : public Window {
}
}

void OnQueryTextFinished(char *str) override
void OnQueryTextFinished(std::optional<std::string> str) override
{
if (StrEmpty(str)) return;
int32_t value = atoi(str);
if (!str.has_value() || str->empty()) return;
int32_t value = atoi(str->c_str());
SetValue(value);
}

Expand Down
38 changes: 19 additions & 19 deletions src/genworld_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -935,14 +935,14 @@ struct GenerateLandscapeWindow : public Window {
this->InvalidateData();
}

void OnQueryTextFinished(char *str) override
void OnQueryTextFinished(std::optional<std::string> str) override
{
/* Was 'cancel' pressed? */
if (str == nullptr) return;
if (!str.has_value()) return;

int32_t value;
if (!StrEmpty(str)) {
value = atoi(str);
if (!str->empty()) {
value = atoi(str->c_str());
} else {
/* An empty string means revert to the default */
switch (this->widget_id) {
Expand Down Expand Up @@ -1229,25 +1229,25 @@ struct CreateScenarioWindow : public Window
this->SetDirty();
}

void OnQueryTextFinished(char *str) override
void OnQueryTextFinished(std::optional<std::string> str) override
{
if (!StrEmpty(str)) {
int32_t value = atoi(str);
if (!str.has_value() || str->empty()) return;

switch (this->widget_id) {
case WID_CS_START_DATE_TEXT:
this->SetWidgetDirty(WID_CS_START_DATE_TEXT);
_settings_newgame.game_creation.starting_year = Clamp(TimerGameCalendar::Year(value), CalendarTime::MIN_YEAR, CalendarTime::MAX_YEAR);
break;

case WID_CS_FLAT_LAND_HEIGHT_TEXT:
this->SetWidgetDirty(WID_CS_FLAT_LAND_HEIGHT_TEXT);
_settings_newgame.game_creation.se_flat_world_height = Clamp(value, 0, GetMapHeightLimit());
break;
}
int32_t value = atoi(str->c_str());

this->SetDirty();
switch (this->widget_id) {
case WID_CS_START_DATE_TEXT:
this->SetWidgetDirty(WID_CS_START_DATE_TEXT);
_settings_newgame.game_creation.starting_year = Clamp(TimerGameCalendar::Year(value), CalendarTime::MIN_YEAR, CalendarTime::MAX_YEAR);
break;

case WID_CS_FLAT_LAND_HEIGHT_TEXT:
this->SetWidgetDirty(WID_CS_FLAT_LAND_HEIGHT_TEXT);
_settings_newgame.game_creation.se_flat_world_height = Clamp(value, 0, GetMapHeightLimit());
break;
}

this->SetDirty();
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/group_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -972,9 +972,9 @@ class VehicleGroupWindow : public BaseVehicleListWindow {
_cursor.vehchain = false;
}

void OnQueryTextFinished(char *str) override
void OnQueryTextFinished(std::optional<std::string> str) override
{
if (str != nullptr) Command<CMD_ALTER_GROUP>::Post(STR_ERROR_GROUP_CAN_T_RENAME, AlterGroupMode::Rename, this->group_rename, 0, str);
if (str.has_value()) Command<CMD_ALTER_GROUP>::Post(STR_ERROR_GROUP_CAN_T_RENAME, AlterGroupMode::Rename, this->group_rename, 0, *str);
this->group_rename = INVALID_GROUP;
}

Expand Down
6 changes: 3 additions & 3 deletions src/industry_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1138,12 +1138,12 @@ class IndustryViewWindow : public Window
}
}

void OnQueryTextFinished(char *str) override
void OnQueryTextFinished(std::optional<std::string> str) override
{
if (StrEmpty(str)) return;
if (!str.has_value() || str->empty()) return;

Industry *i = Industry::Get(this->window_number);
uint value = atoi(str);
uint value = atoi(str->c_str());
switch (this->editbox_line) {
case IL_NONE: NOT_REACHED();

Expand Down
2 changes: 1 addition & 1 deletion src/misc_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,7 @@ struct QueryStringWindow : public Window
if (!this->editbox.handled && this->parent != nullptr) {
Window *parent = this->parent;
this->parent = nullptr; // so parent doesn't try to close us again
parent->OnQueryTextFinished(nullptr);
parent->OnQueryTextFinished(std::nullopt);
}
this->Window::Close();
}
Expand Down
34 changes: 17 additions & 17 deletions src/network/network_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,13 +836,13 @@ class NetworkGameWindow : public Window {
}
}

void OnQueryTextFinished(char *str) override
void OnQueryTextFinished(std::optional<std::string> str) override
{
if (!StrEmpty(str)) {
_settings_client.network.connect_to_ip = str;
NetworkAddServer(str);
NetworkRebuildHostList();
}
if (!str.has_value() || str->empty()) return;

_settings_client.network.connect_to_ip = std::move(*str);
NetworkAddServer(_settings_client.network.connect_to_ip);
NetworkRebuildHostList();
}

void OnResize() override
Expand Down Expand Up @@ -1135,14 +1135,14 @@ struct NetworkStartServerWindow : public Window {
this->RaiseWidgetsWhenLowered(WID_NSS_CLIENTS_BTND, WID_NSS_CLIENTS_BTNU, WID_NSS_COMPANIES_BTND, WID_NSS_COMPANIES_BTNU);
}

void OnQueryTextFinished(char *str) override
void OnQueryTextFinished(std::optional<std::string> str) override
{
if (str == nullptr) return;
if (!str.has_value()) return;

if (this->widget_id == WID_NSS_SETPWD) {
_settings_client.network.server_password = str;
_settings_client.network.server_password = std::move(*str);
} else {
int32_t value = atoi(str);
int32_t value = atoi(str->c_str());
this->SetWidgetDirty(this->widget_id);
switch (this->widget_id) {
default: NOT_REACHED();
Expand Down Expand Up @@ -1868,23 +1868,23 @@ struct NetworkClientListWindow : Window {
this->SetDirty();
}

void OnQueryTextFinished(char *str) override
void OnQueryTextFinished(std::optional<std::string> str) override
{
if (str == nullptr) return;
if (!str.has_value()) return;

switch (this->query_widget) {
default: NOT_REACHED();

case WID_CL_SERVER_NAME_EDIT: {
if (!_network_server) break;

SetSettingValue(GetSettingFromName("network.server_name")->AsStringSetting(), str);
SetSettingValue(GetSettingFromName("network.server_name")->AsStringSetting(), *str);
this->InvalidateData();
break;
}

case WID_CL_CLIENT_NAME_EDIT: {
SetSettingValue(GetSettingFromName("network.client_name")->AsStringSetting(), str);
SetSettingValue(GetSettingFromName("network.client_name")->AsStringSetting(), *str);
this->InvalidateData();
break;
}
Expand Down Expand Up @@ -2169,14 +2169,14 @@ struct NetworkJoinStatusWindow : Window {
}
}

void OnQueryTextFinished(char *str) override
void OnQueryTextFinished(std::optional<std::string> str) override
{
if (StrEmpty(str) || this->request == nullptr) {
if (!str.has_value() || str->empty() || this->request == nullptr) {
NetworkDisconnect();
return;
}

this->request->Reply(str);
this->request->Reply(*str);
}
};

Expand Down
12 changes: 6 additions & 6 deletions src/newgrf_debug_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,11 +595,11 @@ struct NewGRFInspectWindow : Window {
}
}

void OnQueryTextFinished(char *str) override
void OnQueryTextFinished(std::optional<std::string> str) override
{
if (StrEmpty(str)) return;
if (!str.has_value() || str->empty()) return;

NewGRFInspectWindow::var60params[GetFeatureNum(this->window_number)][this->current_edit_param - 0x60] = std::strtol(str, nullptr, 16);
NewGRFInspectWindow::var60params[GetFeatureNum(this->window_number)][this->current_edit_param - 0x60] = std::strtol(str->c_str(), nullptr, 16);
this->SetDirty();
}

Expand Down Expand Up @@ -1074,11 +1074,11 @@ struct SpriteAlignerWindow : Window {
}
}

void OnQueryTextFinished(char *str) override
void OnQueryTextFinished(std::optional<std::string> str) override
{
if (StrEmpty(str)) return;
if (!str.has_value() || str->empty()) return;

this->current_sprite = atoi(str);
this->current_sprite = atoi(str->c_str());
if (this->current_sprite >= GetMaxSpriteID()) this->current_sprite = 0;
while (GetSpriteType(this->current_sprite) != SpriteType::Normal) {
this->current_sprite = (this->current_sprite + 1) % GetMaxSpriteID();
Expand Down
12 changes: 6 additions & 6 deletions src/newgrf_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,10 @@ struct NewGRFParametersWindow : public Window {
}
}

void OnQueryTextFinished(char *str) override
void OnQueryTextFinished(std::optional<std::string> str) override
{
if (StrEmpty(str)) return;
int32_t value = atoi(str);
if (!str.has_value() || str->empty()) return;
int32_t value = atoi(str->c_str());
GRFParameterInfo &par_info = this->GetParameterInfo(this->clicked_row);
uint32_t val = Clamp<uint32_t>(value, par_info.min_value, par_info.max_value);
par_info.SetValue(this->grf_config, val);
Expand Down Expand Up @@ -1195,11 +1195,11 @@ struct NewGRFWindow : public Window, NewGRFScanCallback {
this->InvalidateData(GOID_NEWGRF_CHANGES_MADE);
}

void OnQueryTextFinished(char *str) override
void OnQueryTextFinished(std::optional<std::string> str) override
{
if (str == nullptr) return;
if (!str.has_value()) return;

SaveGRFPresetToConfig(str, this->actives);
SaveGRFPresetToConfig(str->c_str(), this->actives);
this->grf_presets = GetGRFPresetList();

/* Switch to this preset */
Expand Down
32 changes: 16 additions & 16 deletions src/order_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1375,27 +1375,27 @@ struct OrdersWindow : public Window {
}
}

void OnQueryTextFinished(char *str) override
void OnQueryTextFinished(std::optional<std::string> str) override
{
if (!StrEmpty(str)) {
VehicleOrderID sel = this->OrderGetSel();
uint value = atoi(str);
if (!str.has_value() || str->empty()) return;

switch (this->vehicle->GetOrder(sel)->GetConditionVariable()) {
case OCV_MAX_SPEED:
value = ConvertDisplaySpeedToSpeed(value, this->vehicle->type);
break;
VehicleOrderID sel = this->OrderGetSel();
uint value = atoi(str->c_str());

case OCV_RELIABILITY:
case OCV_LOAD_PERCENTAGE:
value = Clamp(value, 0, 100);
break;
switch (this->vehicle->GetOrder(sel)->GetConditionVariable()) {
case OCV_MAX_SPEED:
value = ConvertDisplaySpeedToSpeed(value, this->vehicle->type);
break;

default:
break;
}
Command<CMD_MODIFY_ORDER>::Post(STR_ERROR_CAN_T_MODIFY_THIS_ORDER, this->vehicle->tile, this->vehicle->index, sel, MOF_COND_VALUE, Clamp(value, 0, 2047));
case OCV_RELIABILITY:
case OCV_LOAD_PERCENTAGE:
value = Clamp(value, 0, 100);
break;

default:
break;
}
Command<CMD_MODIFY_ORDER>::Post(STR_ERROR_CAN_T_MODIFY_THIS_ORDER, this->vehicle->tile, this->vehicle->index, sel, MOF_COND_VALUE, Clamp(value, 0, 2047));
}

void OnDropdownSelect(WidgetID widget, int index) override
Expand Down
Loading

0 comments on commit 1420021

Please sign in to comment.