1
0
Fork 0

Codechange: Use std::array instead of C array for automatic deep-copies.

pull/10844/head
Peter Nelson 2023-05-18 19:56:09 +01:00 committed by PeterN
parent acec34a0fe
commit c23aae96a2
4 changed files with 5 additions and 25 deletions

View File

@ -7191,7 +7191,7 @@ static void GRFLoadError(ByteReader *buf)
} }
/* Only two parameter numbers can be used in the string. */ /* Only two parameter numbers can be used in the string. */
for (uint i = 0; i < lengthof(error->param_value) && buf->HasData(); i++) { for (uint i = 0; i < error->param_value.size() && buf->HasData(); i++) {
uint param_number = buf->ReadByte(); uint param_number = buf->ReadByte();
error->param_value[i] = _cur.grffile->GetParam(param_number); error->param_value[i] = _cur.grffile->GetParam(param_number);
} }

View File

@ -158,26 +158,10 @@ uint _missing_extra_graphics = 0;
* @param severity The severity of this error. * @param severity The severity of this error.
* @param message The actual error-string. * @param message The actual error-string.
*/ */
GRFError::GRFError(StringID severity, StringID message) : GRFError::GRFError(StringID severity, StringID message) : message(message), severity(severity)
message(message),
severity(severity),
param_value()
{ {
} }
/**
* Create a new GRFError that is a deep copy of an existing error message.
* @param error The GRFError object to make a copy of.
*/
GRFError::GRFError(const GRFError &error) :
custom_message(error.custom_message),
data(error.data),
message(error.message),
severity(error.severity)
{
memcpy(this->param_value, error.param_value, sizeof(this->param_value));
}
/** /**
* Create a new empty GRFParameterInfo object. * Create a new empty GRFParameterInfo object.
* @param nr The newgrf parameter that is changed. * @param nr The newgrf parameter that is changed.

View File

@ -108,16 +108,12 @@ struct GRFIdentifier {
/** Information about why GRF had problems during initialisation */ /** Information about why GRF had problems during initialisation */
struct GRFError { struct GRFError {
GRFError(StringID severity, StringID message = 0); GRFError(StringID severity, StringID message = 0);
GRFError(const GRFError &error);
/* Remove the copy assignment, as the default implementation will not do the right thing. */
GRFError &operator=(GRFError &rhs) = delete;
std::string custom_message; ///< Custom message (if present) std::string custom_message; ///< Custom message (if present)
std::string data; ///< Additional data for message and custom_message std::string data; ///< Additional data for message and custom_message
StringID message; ///< Default message StringID message; ///< Default message
StringID severity; ///< Info / Warning / Error / Fatal StringID severity; ///< Info / Warning / Error / Fatal
uint32 param_value[2]; ///< Values of GRF parameters to show for message and custom_message std::array<uint32_t, 2> param_value; ///< Values of GRF parameters to show for message and custom_message
}; };
/** The possible types of a newgrf parameter. */ /** The possible types of a newgrf parameter. */

View File

@ -55,7 +55,7 @@ void ShowNewGRFError()
SetDParamStr(2, c->error->custom_message); SetDParamStr(2, c->error->custom_message);
SetDParamStr(3, c->filename); SetDParamStr(3, c->filename);
SetDParamStr(4, c->error->data); SetDParamStr(4, c->error->data);
for (uint i = 0; i < lengthof(c->error->param_value); i++) { for (uint i = 0; i < c->error->param_value.size(); i++) {
SetDParam(5 + i, c->error->param_value[i]); SetDParam(5 + i, c->error->param_value[i]);
} }
if (c->error->severity == STR_NEWGRF_ERROR_MSG_FATAL) { if (c->error->severity == STR_NEWGRF_ERROR_MSG_FATAL) {
@ -75,7 +75,7 @@ static void ShowNewGRFInfo(const GRFConfig *c, const Rect &r, bool show_params)
SetDParamStr(0, c->error->custom_message); // is skipped by built-in messages SetDParamStr(0, c->error->custom_message); // is skipped by built-in messages
SetDParamStr(1, c->filename); SetDParamStr(1, c->filename);
SetDParamStr(2, c->error->data); SetDParamStr(2, c->error->data);
for (uint i = 0; i < lengthof(c->error->param_value); i++) { for (uint i = 0; i < c->error->param_value.size(); i++) {
SetDParam(3 + i, c->error->param_value[i]); SetDParam(3 + i, c->error->param_value[i]);
} }
GetString(message, c->error->message != STR_NULL ? c->error->message : STR_JUST_RAW_STRING, lastof(message)); GetString(message, c->error->message != STR_NULL ? c->error->message : STR_JUST_RAW_STRING, lastof(message));