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

pull/562/head
Rubidium 1 year ago committed by rubidium42
parent b24a6bb8f3
commit bbcb55ebc9

@ -26,9 +26,9 @@
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);
return GetString(::Station::IsValidID(station_id) ? STR_STATION_NAME : STR_WAYPOINT_NAME);

@ -43,7 +43,7 @@ public:
* @pre IsValidBaseStation(station_id).
* @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.

@ -129,10 +129,10 @@ static void _DoCommandReturnBuildBridge1(class ScriptInstance *instance)
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);
if (!IsValidBridge(bridge_id)) return nullptr;
EnforcePrecondition(std::nullopt, vehicle_type == ScriptVehicle::VT_ROAD || vehicle_type == ScriptVehicle::VT_RAIL || vehicle_type == ScriptVehicle::VT_WATER);
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]);
}

@ -69,7 +69,7 @@ public:
* @pre vehicle_type == ScriptVehicle::VT_ROAD || vehicle_type == ScriptVehicle::VT_RAIL || vehicle_type == ScriptVehicle::VT_WATER
* @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.

@ -28,26 +28,25 @@
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);
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);
/* cargo->label is a uint32 packing a 4 character non-terminated string,
* 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++) {
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;
}

@ -90,7 +90,7 @@ public:
* @pre IsValidCargo(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.
@ -107,7 +107,7 @@ public:
* - 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.
*/
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.

@ -32,11 +32,11 @@ static NetworkClientInfo *FindClientInfo(ScriptClient::ClientID 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);
if (ci == nullptr) return nullptr;
return stredup(ci->client_name.c_str());
if (ci == nullptr) return std::nullopt;
return ci->client_name;
}
/* static */ ScriptCompany::CompanyID ScriptClient::GetCompany(ScriptClient::ClientID client)

@ -45,7 +45,7 @@ public:
* @pre ResolveClientID(client) != CLIENT_INVALID.
* @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.

@ -57,10 +57,10 @@
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);
if (company == COMPANY_INVALID) return nullptr;
if (company == COMPANY_INVALID) return std::nullopt;
::SetDParam(0, company);
return GetString(STR_COMPANY_NAME);
@ -79,20 +79,13 @@
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);
if (company == COMPANY_INVALID) return std::nullopt;
static const int len = 64;
char *president_name = MallocT<char>(len);
if (company != COMPANY_INVALID) {
::SetDParam(0, company);
::GetString(president_name, STR_PRESIDENT_NAME, &president_name[len - 1]);
} else {
*president_name = '\0';
}
return president_name;
::SetDParam(0, company);
return GetString(STR_PRESIDENT_NAME);
}
/* static */ bool ScriptCompany::SetPresidentGender(Gender gender)

@ -150,7 +150,7 @@ public:
* @pre ResolveCompanyID(company) != COMPANY_INVALID.
* @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.
@ -168,7 +168,7 @@ public:
* @pre ResolveCompanyID(company) != COMPANY_INVALID.
* @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.

@ -41,9 +41,9 @@
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);
return GetString(STR_ENGINE_NAME);

@ -44,7 +44,7 @@ public:
* @pre IsValidEngine(engine_id).
* @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

@ -23,9 +23,9 @@ ScriptError::ScriptErrorMapString ScriptError::error_map_string = ScriptError::S
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)

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

@ -28,9 +28,9 @@ bool ScriptEventEnginePreview::IsEngineValid() const
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);
return GetString(STR_ENGINE_NAME);

@ -239,7 +239,7 @@ public:
* Get the name of the offered engine.
* @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

@ -68,9 +68,9 @@
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);
return GetString(STR_GROUP_NAME);

@ -84,7 +84,7 @@ public:
* @pre IsValidGroup(group_id).
* @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.

@ -42,9 +42,9 @@
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);
return GetString(STR_INDUSTRY_NAME);

@ -79,7 +79,7 @@ public:
* @pre IsValidIndustry(industry_id).
* @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.

@ -57,9 +57,9 @@
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);
}

@ -39,7 +39,7 @@ public:
* @pre IsValidIndustryType(industry_type).
* @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.

@ -50,15 +50,15 @@ ScriptNewGRFList::ScriptNewGRFList()
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.
for (auto c = _grfconfig; c != nullptr; c = c->next) {
if (!HasBit(c->flags, GCF_STATIC) && c->ident.grfid == grfid) {
return ::stredup(c->GetName());
return c->GetName();
}
}
return nullptr;
return std::nullopt;
}

@ -50,7 +50,7 @@ public:
* @pre ScriptNewGRF::IsLoaded(grfid).
* @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 */

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

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

@ -23,9 +23,9 @@
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);
}

@ -33,7 +33,7 @@ public:
* @pre IsValidObjectType(object_type).
* @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.

@ -24,9 +24,9 @@
#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);
}

@ -101,7 +101,7 @@ public:
* means that the name could be something like "Maglev construction" instead
* 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

@ -25,9 +25,9 @@
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);
}

@ -90,7 +90,7 @@ public:
* @pre IsRoadTypeAvailable(road_type).
* @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.

@ -47,9 +47,9 @@
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);
return GetString(STR_SIGN_NAME);

@ -55,7 +55,7 @@ public:
* @pre IsValidSign(sign_id).
* @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.

@ -32,9 +32,9 @@
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);
return GetString(STR_TOWN_NAME);

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

@ -299,9 +299,9 @@
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);
return GetString(STR_VEHICLE_NAME);

@ -137,7 +137,7 @@ public:
* @pre IsPrimaryVehicle(vehicle_id).
* @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.

@ -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<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<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<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<char *> { /* Do not use char *, use std::optional<std::string> instead. */ };
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<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.
* We use a class instead of a plain function to allow us to use partial template specializations.

Loading…
Cancel
Save