1
0
Fork 0

Codechange: use std::string as script API return type

pull/10801/head
Rubidium 2023-05-05 23:19:35 +02:00 committed by rubidium42
parent b24a6bb8f3
commit bbcb55ebc9
39 changed files with 83 additions and 83 deletions

View File

@ -26,9 +26,9 @@
return st != nullptr && (st->owner == ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity() || st->owner == OWNER_NONE); return st != nullptr && (st->owner == ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity() || st->owner == OWNER_NONE);
} }
/* static */ char *ScriptBaseStation::GetName(StationID station_id) /* static */ std::optional<std::string> ScriptBaseStation::GetName(StationID station_id)
{ {
if (!IsValidBaseStation(station_id)) return nullptr; if (!IsValidBaseStation(station_id)) return std::nullopt;
::SetDParam(0, station_id); ::SetDParam(0, station_id);
return GetString(::Station::IsValidID(station_id) ? STR_STATION_NAME : STR_WAYPOINT_NAME); return GetString(::Station::IsValidID(station_id) ? STR_STATION_NAME : STR_WAYPOINT_NAME);

View File

@ -43,7 +43,7 @@ public:
* @pre IsValidBaseStation(station_id). * @pre IsValidBaseStation(station_id).
* @return The name of the station. * @return The name of the station.
*/ */
static char *GetName(StationID station_id); static std::optional<std::string> GetName(StationID station_id);
/** /**
* Set the name this basestation. * Set the name this basestation.

View File

@ -129,10 +129,10 @@ static void _DoCommandReturnBuildBridge1(class ScriptInstance *instance)
return ScriptObject::Command<CMD_LANDSCAPE_CLEAR>::Do(tile); return ScriptObject::Command<CMD_LANDSCAPE_CLEAR>::Do(tile);
} }
/* static */ char *ScriptBridge::GetName(BridgeID bridge_id, ScriptVehicle::VehicleType vehicle_type) /* static */ std::optional<std::string> ScriptBridge::GetName(BridgeID bridge_id, ScriptVehicle::VehicleType vehicle_type)
{ {
EnforcePrecondition(nullptr, vehicle_type == ScriptVehicle::VT_ROAD || vehicle_type == ScriptVehicle::VT_RAIL || vehicle_type == ScriptVehicle::VT_WATER); EnforcePrecondition(std::nullopt, vehicle_type == ScriptVehicle::VT_ROAD || vehicle_type == ScriptVehicle::VT_RAIL || vehicle_type == ScriptVehicle::VT_WATER);
if (!IsValidBridge(bridge_id)) return nullptr; if (!IsValidBridge(bridge_id)) return std::nullopt;
return GetString(vehicle_type == ScriptVehicle::VT_WATER ? STR_LAI_BRIDGE_DESCRIPTION_AQUEDUCT : ::GetBridgeSpec(bridge_id)->transport_name[vehicle_type]); return GetString(vehicle_type == ScriptVehicle::VT_WATER ? STR_LAI_BRIDGE_DESCRIPTION_AQUEDUCT : ::GetBridgeSpec(bridge_id)->transport_name[vehicle_type]);
} }

View File

@ -69,7 +69,7 @@ public:
* @pre vehicle_type == ScriptVehicle::VT_ROAD || vehicle_type == ScriptVehicle::VT_RAIL || vehicle_type == ScriptVehicle::VT_WATER * @pre vehicle_type == ScriptVehicle::VT_ROAD || vehicle_type == ScriptVehicle::VT_RAIL || vehicle_type == ScriptVehicle::VT_WATER
* @return The name the bridge has. * @return The name the bridge has.
*/ */
static char *GetName(BridgeID bridge_id, ScriptVehicle::VehicleType vehicle_type); static std::optional<std::string> GetName(BridgeID bridge_id, ScriptVehicle::VehicleType vehicle_type);
/** /**
* Get the maximum speed of a bridge. * Get the maximum speed of a bridge.

View File

@ -28,26 +28,25 @@
return (towneffect_type >= (TownEffect)TE_BEGIN && towneffect_type < (TownEffect)TE_END); return (towneffect_type >= (TownEffect)TE_BEGIN && towneffect_type < (TownEffect)TE_END);
} }
/* static */ char *ScriptCargo::GetName(CargoID cargo_type) /* static */ std::optional<std::string> ScriptCargo::GetName(CargoID cargo_type)
{ {
if (!IsValidCargo(cargo_type)) return nullptr; if (!IsValidCargo(cargo_type)) return std::nullopt;
::SetDParam(0, 1ULL << cargo_type); ::SetDParam(0, 1ULL << cargo_type);
return GetString(STR_JUST_CARGO_LIST); return GetString(STR_JUST_CARGO_LIST);
} }
/* static */ char *ScriptCargo::GetCargoLabel(CargoID cargo_type) /* static */ std::optional<std::string> ScriptCargo::GetCargoLabel(CargoID cargo_type)
{ {
if (!IsValidCargo(cargo_type)) return nullptr; if (!IsValidCargo(cargo_type)) return std::nullopt;
const CargoSpec *cargo = ::CargoSpec::Get(cargo_type); const CargoSpec *cargo = ::CargoSpec::Get(cargo_type);
/* cargo->label is a uint32 packing a 4 character non-terminated string, /* cargo->label is a uint32 packing a 4 character non-terminated string,
* like "PASS", "COAL", "OIL_". New ones can be defined by NewGRFs */ * like "PASS", "COAL", "OIL_". New ones can be defined by NewGRFs */
char *cargo_label = MallocT<char>(sizeof(cargo->label) + 1); std::string cargo_label;
for (uint i = 0; i < sizeof(cargo->label); i++) { for (uint i = 0; i < sizeof(cargo->label); i++) {
cargo_label[i] = GB(cargo->label, (uint8)(sizeof(cargo->label) - i - 1) * 8, 8); cargo_label.push_back(GB(cargo->label, (uint8)(sizeof(cargo->label) - i - 1) * 8, 8));
} }
cargo_label[sizeof(cargo->label)] = '\0';
return cargo_label; return cargo_label;
} }

View File

@ -90,7 +90,7 @@ public:
* @pre IsValidCargo(cargo_type). * @pre IsValidCargo(cargo_type).
* @return The name of the cargo type. * @return The name of the cargo type.
*/ */
static char *GetName(CargoID cargo_type); static std::optional<std::string> GetName(CargoID cargo_type);
/** /**
* Gets the string representation of the cargo label. * Gets the string representation of the cargo label.
@ -107,7 +107,7 @@ public:
* - In other words: Only use the cargo label, if you know more about the behaviour * - In other words: Only use the cargo label, if you know more about the behaviour
* of a specific cargo from a specific industry set, than the API methods can tell you. * of a specific cargo from a specific industry set, than the API methods can tell you.
*/ */
static char *GetCargoLabel(CargoID cargo_type); static std::optional<std::string> GetCargoLabel(CargoID cargo_type);
/** /**
* Checks whether the give cargo is a freight or not. * Checks whether the give cargo is a freight or not.

View File

@ -32,11 +32,11 @@ static NetworkClientInfo *FindClientInfo(ScriptClient::ClientID client)
return (FindClientInfo(client) == nullptr ? ScriptClient::CLIENT_INVALID : client); return (FindClientInfo(client) == nullptr ? ScriptClient::CLIENT_INVALID : client);
} }
/* static */ char *ScriptClient::GetName(ScriptClient::ClientID client) /* static */ std::optional<std::string> ScriptClient::GetName(ScriptClient::ClientID client)
{ {
NetworkClientInfo *ci = FindClientInfo(client); NetworkClientInfo *ci = FindClientInfo(client);
if (ci == nullptr) return nullptr; if (ci == nullptr) return std::nullopt;
return stredup(ci->client_name.c_str()); return ci->client_name;
} }
/* static */ ScriptCompany::CompanyID ScriptClient::GetCompany(ScriptClient::ClientID client) /* static */ ScriptCompany::CompanyID ScriptClient::GetCompany(ScriptClient::ClientID client)

View File

@ -45,7 +45,7 @@ public:
* @pre ResolveClientID(client) != CLIENT_INVALID. * @pre ResolveClientID(client) != CLIENT_INVALID.
* @return The name of the given client. * @return The name of the given client.
*/ */
static char *GetName(ClientID client); static std::optional<std::string> GetName(ClientID client);
/** /**
* Get the company in which the given client is playing. * Get the company in which the given client is playing.

View File

@ -57,10 +57,10 @@
return ScriptObject::Command<CMD_RENAME_COMPANY>::Do(text); return ScriptObject::Command<CMD_RENAME_COMPANY>::Do(text);
} }
/* static */ char *ScriptCompany::GetName(ScriptCompany::CompanyID company) /* static */ std::optional<std::string> ScriptCompany::GetName(ScriptCompany::CompanyID company)
{ {
company = ResolveCompanyID(company); company = ResolveCompanyID(company);
if (company == COMPANY_INVALID) return nullptr; if (company == COMPANY_INVALID) return std::nullopt;
::SetDParam(0, company); ::SetDParam(0, company);
return GetString(STR_COMPANY_NAME); return GetString(STR_COMPANY_NAME);
@ -79,20 +79,13 @@
return ScriptObject::Command<CMD_RENAME_PRESIDENT>::Do(text); return ScriptObject::Command<CMD_RENAME_PRESIDENT>::Do(text);
} }
/* static */ char *ScriptCompany::GetPresidentName(ScriptCompany::CompanyID company) /* static */ std::optional<std::string> ScriptCompany::GetPresidentName(ScriptCompany::CompanyID company)
{ {
company = ResolveCompanyID(company); company = ResolveCompanyID(company);
if (company == COMPANY_INVALID) return std::nullopt;
static const int len = 64; ::SetDParam(0, company);
char *president_name = MallocT<char>(len); return GetString(STR_PRESIDENT_NAME);
if (company != COMPANY_INVALID) {
::SetDParam(0, company);
::GetString(president_name, STR_PRESIDENT_NAME, &president_name[len - 1]);
} else {
*president_name = '\0';
}
return president_name;
} }
/* static */ bool ScriptCompany::SetPresidentGender(Gender gender) /* static */ bool ScriptCompany::SetPresidentGender(Gender gender)

View File

@ -150,7 +150,7 @@ public:
* @pre ResolveCompanyID(company) != COMPANY_INVALID. * @pre ResolveCompanyID(company) != COMPANY_INVALID.
* @return The name of the given company. * @return The name of the given company.
*/ */
static char *GetName(CompanyID company); static std::optional<std::string> GetName(CompanyID company);
/** /**
* Set the name of your president. * Set the name of your president.
@ -168,7 +168,7 @@ public:
* @pre ResolveCompanyID(company) != COMPANY_INVALID. * @pre ResolveCompanyID(company) != COMPANY_INVALID.
* @return The name of the president of the given company. * @return The name of the president of the given company.
*/ */
static char *GetPresidentName(CompanyID company); static std::optional<std::string> GetPresidentName(CompanyID company);
/** /**
* Set the gender of the president of your company. * Set the gender of the president of your company.

View File

@ -41,9 +41,9 @@
return e != nullptr && ::IsEngineBuildable(engine_id, e->type, ScriptObject::GetCompany()); return e != nullptr && ::IsEngineBuildable(engine_id, e->type, ScriptObject::GetCompany());
} }
/* static */ char *ScriptEngine::GetName(EngineID engine_id) /* static */ std::optional<std::string> ScriptEngine::GetName(EngineID engine_id)
{ {
if (!IsValidEngine(engine_id)) return nullptr; if (!IsValidEngine(engine_id)) return std::nullopt;
::SetDParam(0, engine_id); ::SetDParam(0, engine_id);
return GetString(STR_ENGINE_NAME); return GetString(STR_ENGINE_NAME);

View File

@ -44,7 +44,7 @@ public:
* @pre IsValidEngine(engine_id). * @pre IsValidEngine(engine_id).
* @return The name the engine has. * @return The name the engine has.
*/ */
static char *GetName(EngineID engine_id); static std::optional<std::string> GetName(EngineID engine_id);
/** /**
* Get the cargo-type of an engine. In case it can transport multiple cargoes, it * Get the cargo-type of an engine. In case it can transport multiple cargoes, it

View File

@ -23,9 +23,9 @@ ScriptError::ScriptErrorMapString ScriptError::error_map_string = ScriptError::S
return ScriptObject::GetLastError(); return ScriptObject::GetLastError();
} }
/* static */ char *ScriptError::GetLastErrorString() /* static */ std::optional<std::string> ScriptError::GetLastErrorString()
{ {
return stredup((*error_map_string.find(ScriptError::GetLastError())).second); return (*error_map_string.find(ScriptError::GetLastError())).second;
} }
/* static */ ScriptErrorType ScriptError::StringToError(StringID internal_string_id) /* static */ ScriptErrorType ScriptError::StringToError(StringID internal_string_id)

View File

@ -193,7 +193,7 @@ public:
* Get the last error in string format (for human readability). * Get the last error in string format (for human readability).
* @return An ErrorMessage enum item, as string. * @return An ErrorMessage enum item, as string.
*/ */
static char *GetLastErrorString(); static std::optional<std::string> GetLastErrorString();
/** /**
* Get the error based on the OpenTTD StringID. * Get the error based on the OpenTTD StringID.

View File

@ -28,9 +28,9 @@ bool ScriptEventEnginePreview::IsEngineValid() const
return e != nullptr && e->IsEnabled(); return e != nullptr && e->IsEnabled();
} }
char *ScriptEventEnginePreview::GetName() std::optional<std::string> ScriptEventEnginePreview::GetName()
{ {
if (!this->IsEngineValid()) return nullptr; if (!this->IsEngineValid()) return std::nullopt;
::SetDParam(0, this->engine); ::SetDParam(0, this->engine);
return GetString(STR_ENGINE_NAME); return GetString(STR_ENGINE_NAME);

View File

@ -239,7 +239,7 @@ public:
* Get the name of the offered engine. * Get the name of the offered engine.
* @return The name the engine has. * @return The name the engine has.
*/ */
char *GetName(); std::optional<std::string> GetName();
/** /**
* Get the cargo-type of the offered engine. In case it can transport multiple cargoes, it * Get the cargo-type of the offered engine. In case it can transport multiple cargoes, it

View File

@ -68,9 +68,9 @@
return ScriptObject::Command<CMD_ALTER_GROUP>::Do(AlterGroupMode::Rename, group_id, 0, text); return ScriptObject::Command<CMD_ALTER_GROUP>::Do(AlterGroupMode::Rename, group_id, 0, text);
} }
/* static */ char *ScriptGroup::GetName(GroupID group_id) /* static */ std::optional<std::string> ScriptGroup::GetName(GroupID group_id)
{ {
if (!IsValidGroup(group_id)) return nullptr; if (!IsValidGroup(group_id)) return std::nullopt;
::SetDParam(0, group_id); ::SetDParam(0, group_id);
return GetString(STR_GROUP_NAME); return GetString(STR_GROUP_NAME);

View File

@ -84,7 +84,7 @@ public:
* @pre IsValidGroup(group_id). * @pre IsValidGroup(group_id).
* @return The name the group has. * @return The name the group has.
*/ */
static char *GetName(GroupID group_id); static std::optional<std::string> GetName(GroupID group_id);
/** /**
* Set parent group of a group. * Set parent group of a group.

View File

@ -42,9 +42,9 @@
return ::GetIndustryIndex(tile); return ::GetIndustryIndex(tile);
} }
/* static */ char *ScriptIndustry::GetName(IndustryID industry_id) /* static */ std::optional<std::string> ScriptIndustry::GetName(IndustryID industry_id)
{ {
if (!IsValidIndustry(industry_id)) return nullptr; if (!IsValidIndustry(industry_id)) return std::nullopt;
::SetDParam(0, industry_id); ::SetDParam(0, industry_id);
return GetString(STR_INDUSTRY_NAME); return GetString(STR_INDUSTRY_NAME);

View File

@ -79,7 +79,7 @@ public:
* @pre IsValidIndustry(industry_id). * @pre IsValidIndustry(industry_id).
* @return The name of the industry. * @return The name of the industry.
*/ */
static char *GetName(IndustryID industry_id); static std::optional<std::string> GetName(IndustryID industry_id);
/** /**
* Set the custom text of an industry, shown in the GUI. * Set the custom text of an industry, shown in the GUI.

View File

@ -57,9 +57,9 @@
return ::GetIndustrySpec(industry_type)->GetConstructionCost(); return ::GetIndustrySpec(industry_type)->GetConstructionCost();
} }
/* static */ char *ScriptIndustryType::GetName(IndustryType industry_type) /* static */ std::optional<std::string> ScriptIndustryType::GetName(IndustryType industry_type)
{ {
if (!IsValidIndustryType(industry_type)) return nullptr; if (!IsValidIndustryType(industry_type)) return std::nullopt;
return GetString(::GetIndustrySpec(industry_type)->name); return GetString(::GetIndustrySpec(industry_type)->name);
} }

View File

@ -39,7 +39,7 @@ public:
* @pre IsValidIndustryType(industry_type). * @pre IsValidIndustryType(industry_type).
* @return The name of an industry. * @return The name of an industry.
*/ */
static char *GetName(IndustryType industry_type); static std::optional<std::string> GetName(IndustryType industry_type);
/** /**
* Get a list of CargoID possible produced by this industry-type. * Get a list of CargoID possible produced by this industry-type.

View File

@ -50,15 +50,15 @@ ScriptNewGRFList::ScriptNewGRFList()
return 0; return 0;
} }
/* static */ char *ScriptNewGRF::GetName(SQInteger grfid) /* static */ std::optional<std::string> ScriptNewGRF::GetName(SQInteger grfid)
{ {
grfid = BSWAP32(GB(grfid, 0, 32)); // Match people's expectations. grfid = BSWAP32(GB(grfid, 0, 32)); // Match people's expectations.
for (auto c = _grfconfig; c != nullptr; c = c->next) { for (auto c = _grfconfig; c != nullptr; c = c->next) {
if (!HasBit(c->flags, GCF_STATIC) && c->ident.grfid == grfid) { if (!HasBit(c->flags, GCF_STATIC) && c->ident.grfid == grfid) {
return ::stredup(c->GetName()); return c->GetName();
} }
} }
return nullptr; return std::nullopt;
} }

View File

@ -50,7 +50,7 @@ public:
* @pre ScriptNewGRF::IsLoaded(grfid). * @pre ScriptNewGRF::IsLoaded(grfid).
* @return The name of the NewGRF or null if no name is defined. * @return The name of the NewGRF or null if no name is defined.
*/ */
static char *GetName(SQInteger grfid); static std::optional<std::string> GetName(SQInteger grfid);
}; };
#endif /* SCRIPT_NEWGRF_HPP */ #endif /* SCRIPT_NEWGRF_HPP */

View File

@ -218,12 +218,9 @@ ScriptObject::ActiveInstance::~ActiveInstance()
return GetStorage()->log_data; return GetStorage()->log_data;
} }
/* static */ char *ScriptObject::GetString(StringID string) /* static */ std::string ScriptObject::GetString(StringID string)
{ {
char buffer[64]; return ::StrMakeValid(::GetString(string));
::GetString(buffer, string, lastof(buffer));
::StrMakeValidInPlace(buffer, lastof(buffer), SVS_NONE);
return ::stredup(buffer);
} }
/* static */ void ScriptObject::SetCallbackVariable(int index, int value) /* static */ void ScriptObject::SetCallbackVariable(int index, int value)

View File

@ -282,7 +282,7 @@ protected:
/** /**
* Get an allocated string with all control codes stripped off. * Get an allocated string with all control codes stripped off.
*/ */
static char *GetString(StringID string); static std::string GetString(StringID string);
private: private:
/* Helper functions for DoCommand. */ /* Helper functions for DoCommand. */

View File

@ -23,9 +23,9 @@
return ObjectSpec::Get(object_type)->IsEverAvailable(); return ObjectSpec::Get(object_type)->IsEverAvailable();
} }
/* static */ char *ScriptObjectType::GetName(ObjectType object_type) /* static */ std::optional<std::string> ScriptObjectType::GetName(ObjectType object_type)
{ {
EnforcePrecondition(nullptr, IsValidObjectType(object_type)); EnforcePrecondition(std::nullopt, IsValidObjectType(object_type));
return GetString(ObjectSpec::Get(object_type)->name); return GetString(ObjectSpec::Get(object_type)->name);
} }

View File

@ -33,7 +33,7 @@ public:
* @pre IsValidObjectType(object_type). * @pre IsValidObjectType(object_type).
* @return The name of an object. * @return The name of an object.
*/ */
static char *GetName(ObjectType object_type); static std::optional<std::string> GetName(ObjectType object_type);
/** /**
* Get the number of views for an object-type. * Get the number of views for an object-type.

View File

@ -24,9 +24,9 @@
#include "../../safeguards.h" #include "../../safeguards.h"
/* static */ char *ScriptRail::GetName(RailType rail_type) /* static */ std::optional<std::string> ScriptRail::GetName(RailType rail_type)
{ {
if (!IsRailTypeAvailable(rail_type)) return nullptr; if (!IsRailTypeAvailable(rail_type)) return std::nullopt;
return GetString(GetRailTypeInfo((::RailType)rail_type)->strings.menu_text); return GetString(GetRailTypeInfo((::RailType)rail_type)->strings.menu_text);
} }

View File

@ -101,7 +101,7 @@ public:
* means that the name could be something like "Maglev construction" instead * means that the name could be something like "Maglev construction" instead
* of just "Maglev". * of just "Maglev".
*/ */
static char *GetName(RailType rail_type); static std::optional<std::string> GetName(RailType rail_type);
/** /**
* Checks whether the given tile is actually a tile with rail that can be * Checks whether the given tile is actually a tile with rail that can be

View File

@ -25,9 +25,9 @@
return ScriptCargo::HasCargoClass(cargo_type, ScriptCargo::CC_PASSENGERS) ? ROADVEHTYPE_BUS : ROADVEHTYPE_TRUCK; return ScriptCargo::HasCargoClass(cargo_type, ScriptCargo::CC_PASSENGERS) ? ROADVEHTYPE_BUS : ROADVEHTYPE_TRUCK;
} }
/* static */ char *ScriptRoad::GetName(RoadType road_type) /* static */ std::optional<std::string> ScriptRoad::GetName(RoadType road_type)
{ {
if (!IsRoadTypeAvailable(road_type)) return nullptr; if (!IsRoadTypeAvailable(road_type)) return std::nullopt;
return GetString(GetRoadTypeInfo((::RoadType)road_type)->strings.name); return GetString(GetRoadTypeInfo((::RoadType)road_type)->strings.name);
} }

View File

@ -90,7 +90,7 @@ public:
* @pre IsRoadTypeAvailable(road_type). * @pre IsRoadTypeAvailable(road_type).
* @return The name the road type has. * @return The name the road type has.
*/ */
static char *GetName(RoadType road_type); static std::optional<std::string> GetName(RoadType road_type);
/** /**
* Determines whether a busstop or a truckstop is needed to transport a certain cargo. * Determines whether a busstop or a truckstop is needed to transport a certain cargo.

View File

@ -47,9 +47,9 @@
return ScriptObject::Command<CMD_RENAME_SIGN>::Do(sign_id, text); return ScriptObject::Command<CMD_RENAME_SIGN>::Do(sign_id, text);
} }
/* static */ char *ScriptSign::GetName(SignID sign_id) /* static */ std::optional<std::string> ScriptSign::GetName(SignID sign_id)
{ {
if (!IsValidSign(sign_id)) return nullptr; if (!IsValidSign(sign_id)) return std::nullopt;
::SetDParam(0, sign_id); ::SetDParam(0, sign_id);
return GetString(STR_SIGN_NAME); return GetString(STR_SIGN_NAME);

View File

@ -55,7 +55,7 @@ public:
* @pre IsValidSign(sign_id). * @pre IsValidSign(sign_id).
* @return The name of the sign. * @return The name of the sign.
*/ */
static char *GetName(SignID sign_id); static std::optional<std::string> GetName(SignID sign_id);
/** /**
* Get the owner of a sign. * Get the owner of a sign.

View File

@ -32,9 +32,9 @@
return ::Town::IsValidID(town_id); return ::Town::IsValidID(town_id);
} }
/* static */ char *ScriptTown::GetName(TownID town_id) /* static */ std::optional<std::string> ScriptTown::GetName(TownID town_id)
{ {
if (!IsValidTown(town_id)) return nullptr; if (!IsValidTown(town_id)) return std::nullopt;
::SetDParam(0, town_id); ::SetDParam(0, town_id);
return GetString(STR_TOWN_NAME); return GetString(STR_TOWN_NAME);

View File

@ -142,7 +142,7 @@ public:
* @pre IsValidTown(town_id). * @pre IsValidTown(town_id).
* @return The name of the town. * @return The name of the town.
*/ */
static char *GetName(TownID town_id); static std::optional<std::string> GetName(TownID town_id);
/** /**
* Rename a town. * Rename a town.

View File

@ -299,9 +299,9 @@
return ::Vehicle::Get(vehicle_id)->unitnumber; return ::Vehicle::Get(vehicle_id)->unitnumber;
} }
/* static */ char *ScriptVehicle::GetName(VehicleID vehicle_id) /* static */ std::optional<std::string> ScriptVehicle::GetName(VehicleID vehicle_id)
{ {
if (!IsPrimaryVehicle(vehicle_id)) return nullptr; if (!IsPrimaryVehicle(vehicle_id)) return std::nullopt;
::SetDParam(0, vehicle_id); ::SetDParam(0, vehicle_id);
return GetString(STR_VEHICLE_NAME); return GetString(STR_VEHICLE_NAME);

View File

@ -137,7 +137,7 @@ public:
* @pre IsPrimaryVehicle(vehicle_id). * @pre IsPrimaryVehicle(vehicle_id).
* @return The name the vehicle has. * @return The name the vehicle has.
*/ */
static char *GetName(VehicleID vehicle_id); static std::optional<std::string> GetName(VehicleID vehicle_id);
/** /**
* Get the owner of a vehicle. * Get the owner of a vehicle.

View File

@ -52,11 +52,22 @@ namespace SQConvert {
template <> struct Return<Money> { static inline int Set(HSQUIRRELVM vm, Money res) { sq_pushinteger(vm, res); return 1; } }; template <> struct Return<Money> { static inline int Set(HSQUIRRELVM vm, Money res) { sq_pushinteger(vm, res); return 1; } };
template <> struct Return<TileIndex> { static inline int Set(HSQUIRRELVM vm, TileIndex res) { sq_pushinteger(vm, (int32)res.value); return 1; } }; template <> struct Return<TileIndex> { static inline int Set(HSQUIRRELVM vm, TileIndex res) { sq_pushinteger(vm, (int32)res.value); return 1; } };
template <> struct Return<bool> { static inline int Set(HSQUIRRELVM vm, bool res) { sq_pushbool (vm, res); return 1; } }; template <> struct Return<bool> { static inline int Set(HSQUIRRELVM vm, bool res) { sq_pushbool (vm, res); return 1; } };
template <> struct Return<char *> { static inline int Set(HSQUIRRELVM vm, char *res) { if (res == nullptr) sq_pushnull(vm); else { sq_pushstring(vm, res, -1); free(res); } return 1; } }; template <> struct Return<char *> { /* Do not use char *, use std::optional<std::string> instead. */ };
template <> struct Return<const char *> { static inline int Set(HSQUIRRELVM vm, const char *res) { if (res == nullptr) sq_pushnull(vm); else { sq_pushstring(vm, res, -1); } return 1; } }; template <> struct Return<const char *> { /* Do not use const char *, use std::optional<std::string> instead. */ };
template <> struct Return<void *> { static inline int Set(HSQUIRRELVM vm, void *res) { sq_pushuserpointer(vm, res); return 1; } }; template <> struct Return<void *> { static inline int Set(HSQUIRRELVM vm, void *res) { sq_pushuserpointer(vm, res); return 1; } };
template <> struct Return<HSQOBJECT> { static inline int Set(HSQUIRRELVM vm, HSQOBJECT res) { sq_pushobject(vm, res); return 1; } }; template <> struct Return<HSQOBJECT> { static inline int Set(HSQUIRRELVM vm, HSQOBJECT res) { sq_pushobject(vm, res); return 1; } };
template <> struct Return<std::optional<std::string>> {
static inline int Set(HSQUIRRELVM vm, std::optional<std::string> res) {
if (res.has_value()) {
sq_pushstring(vm, res.value(), -1);
} else {
sq_pushnull(vm);
}
return 1;
}
};
/** /**
* To get a param from squirrel, we use this helper class. It converts to the right format. * To get a param from squirrel, we use this helper class. It converts to the right format.
* We use a class instead of a plain function to allow us to use partial template specializations. * We use a class instead of a plain function to allow us to use partial template specializations.