1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-15 19:13:07 +01:00

Remove use of std::variant

This commit is contained in:
Ted John
2020-04-20 23:30:37 +01:00
parent 940cd6db73
commit e1c08defc8
9 changed files with 70 additions and 20 deletions

View File

@@ -1824,7 +1824,7 @@ static void window_ride_construction_construct(rct_window* w)
// Used by some functions
if (res->Error != GA_ERROR::OK)
{
if (auto error = std::get_if<rct_string_id>(&res->ErrorMessage))
if (auto error = res->ErrorMessage.AsStringId())
gGameCommandErrorText = *error;
else
gGameCommandErrorText = STR_NONE;

View File

@@ -1947,7 +1947,7 @@ static void window_top_toolbar_scenery_tool_down(int16_t x, int16_t y, rct_windo
break;
}
if (auto message = std::get_if<rct_string_id>(&res->ErrorMessage))
if (auto message = res->ErrorMessage.AsStringId())
{
if (*message == STR_NOT_ENOUGH_CASH_REQUIRES || *message == STR_CAN_ONLY_BUILD_THIS_ON_WATER)
{
@@ -2000,7 +2000,7 @@ static void window_top_toolbar_scenery_tool_down(int16_t x, int16_t y, rct_windo
break;
}
if (auto message = std::get_if<rct_string_id>(&res->ErrorMessage))
if (auto message = res->ErrorMessage.AsStringId())
{
if (*message == STR_NOT_ENOUGH_CASH_REQUIRES || *message == STR_CAN_ONLY_BUILD_THIS_ON_WATER)
{

View File

@@ -53,13 +53,13 @@ GameActionResult::GameActionResult(GA_ERROR error, rct_string_id title, rct_stri
std::string GameActionResult::GetErrorTitle() const
{
std::string titlez;
if (auto title = std::get_if<std::string>(&ErrorTitle))
if (auto title = ErrorTitle.AsString())
{
titlez = *title;
}
else
{
titlez = format_string(std::get<rct_string_id>(ErrorTitle), nullptr);
titlez = format_string(ErrorTitle.GetStringId(), nullptr);
}
return titlez;
}
@@ -67,13 +67,13 @@ std::string GameActionResult::GetErrorTitle() const
std::string GameActionResult::GetErrorMessage() const
{
std::string messagez;
if (auto message = std::get_if<std::string>(&ErrorMessage))
if (auto message = ErrorMessage.AsString())
{
messagez = *message;
}
else
{
messagez = format_string(std::get<rct_string_id>(ErrorMessage), ErrorMessageArgs.data());
messagez = format_string(ErrorMessage.GetStringId(), ErrorMessageArgs.data());
}
return messagez;
}

View File

@@ -20,7 +20,6 @@
#include <functional>
#include <memory>
#include <utility>
#include <variant>
/**
* Common error codes for game actions.
@@ -61,6 +60,59 @@ namespace GA_FLAGS
# pragma GCC diagnostic ignored "-Wsuggest-final-types"
#endif
class StringVariant
{
private:
rct_string_id StringId = STR_NONE;
std::string String;
public:
StringVariant() = default;
StringVariant(rct_string_id stringId)
: StringId(stringId)
{
}
StringVariant(const std::string& s)
: String(s)
{
}
StringVariant(std::string&& s)
: String(s)
{
}
StringVariant(const char* s)
: String(s)
{
}
const std::string* AsString() const
{
if (!String.empty())
{
return &String;
}
return {};
}
const rct_string_id* AsStringId() const
{
if (String.empty())
{
return &StringId;
}
return {};
}
rct_string_id GetStringId() const
{
return String.empty() ? StringId : STR_NONE;
}
};
/**
* Represents the result of a game action query or execution.
*/
@@ -70,8 +122,8 @@ public:
using Ptr = std::unique_ptr<GameActionResult>;
GA_ERROR Error = GA_ERROR::OK;
std::variant<rct_string_id, std::string> ErrorTitle = STR_NONE;
std::variant<rct_string_id, std::string> ErrorMessage = STR_NONE;
StringVariant ErrorTitle;
StringVariant ErrorMessage;
std::array<uint8_t, 32> ErrorMessageArgs;
CoordsXYZ Position = { LOCATION_NULL, LOCATION_NULL, LOCATION_NULL };
money32 Cost = 0;

View File

@@ -140,11 +140,11 @@ money32 maze_set_track(
// NOTE: ride_construction_tooldown_construct requires them to be set.
// Refactor result type once theres no C code referencing this function.
if (auto title = std::get_if<rct_string_id>(&res->ErrorTitle))
if (auto title = res->ErrorTitle.AsStringId())
gGameCommandErrorTitle = *title;
else
gGameCommandErrorTitle = STR_NONE;
if (auto message = std::get_if<rct_string_id>(&res->ErrorMessage))
if (auto message = res->ErrorMessage.AsStringId())
gGameCommandErrorText = *message;
else
gGameCommandErrorText = STR_NONE;

View File

@@ -642,7 +642,7 @@ private:
}
default:
log_error("Invalid map selection %u", _selectionType);
return MakeResult(GA_ERROR::INVALID_PARAMETERS, std::get<rct_string_id>(res->ErrorTitle));
return MakeResult(GA_ERROR::INVALID_PARAMETERS, res->ErrorTitle.GetStringId());
} // switch selectionType
// Raise / lower the land tool selection area

View File

@@ -93,8 +93,7 @@ public:
{ _loc.ToTileStart(), baseHeight, clearanceHeight }, &map_place_non_scenery_clear_func, { 0b1111, 0 },
GetFlags(), &clearCost, CREATE_CROSSING_MODE_NONE))
{
return MakeResult(
GA_ERROR::NO_CLEARANCE, std::get<rct_string_id>(res->ErrorTitle), gGameCommandErrorText, gCommonFormatArgs);
return MakeResult(GA_ERROR::NO_CLEARANCE, res->ErrorTitle.GetStringId(), gGameCommandErrorText, gCommonFormatArgs);
}
if (gMapGroundFlags & ELEMENT_IS_UNDERWATER)
@@ -163,8 +162,7 @@ public:
{ _loc.ToTileStart(), baseHeight, clearanceHeight }, &map_place_non_scenery_clear_func, { 0b1111, 0 },
GetFlags() | GAME_COMMAND_FLAG_APPLY, &clearCost, CREATE_CROSSING_MODE_NONE))
{
return MakeResult(
GA_ERROR::NO_CLEARANCE, std::get<rct_string_id>(res->ErrorTitle), gGameCommandErrorText, gCommonFormatArgs);
return MakeResult(GA_ERROR::NO_CLEARANCE, res->ErrorTitle.GetStringId(), gGameCommandErrorText, gCommonFormatArgs);
}
money32 price = (((RideTrackCosts[ride->type].track_price * TrackPricing[TRACK_ELEM_MAZE]) >> 16));

View File

@@ -137,7 +137,7 @@ public:
if (!map_can_construct_at({ _loc.ToTileStart(), baseHeight, clearanceHeight }, { 0b1111, 0 }))
{
return MakeResult(
GA_ERROR::NO_CLEARANCE, std::get<rct_string_id>(res->ErrorTitle), gGameCommandErrorText, gCommonFormatArgs);
GA_ERROR::NO_CLEARANCE, res->ErrorTitle.GetStringId(), gGameCommandErrorText, gCommonFormatArgs);
}
if (gMapGroundFlags & ELEMENT_IS_UNDERWATER)

View File

@@ -1324,7 +1324,7 @@ static GameActionResult::Ptr map_can_construct_with_clear_at(
if (tileElement == nullptr)
{
res->Error = GA_ERROR::UNKNOWN;
res->ErrorMessage = 0;
res->ErrorMessage = STR_NONE;
return res;
}
do
@@ -1481,7 +1481,7 @@ bool map_can_construct_with_clear_at(
uint8_t crossingMode)
{
GameActionResult::Ptr res = map_can_construct_with_clear_at(pos, clearFunc, quarterTile, flags, crossingMode);
if (auto message = std::get_if<rct_string_id>(&res->ErrorMessage))
if (auto message = res->ErrorMessage.AsStringId())
gGameCommandErrorText = *message;
else
gGameCommandErrorText = STR_NONE;