mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Use std::array for GRF(File|Config) parameters.
This simplifies comparison, copying and assignment operations.pull/10844/head
parent
c23aae96a2
commit
6b87fe6540
|
@ -659,7 +659,7 @@ void Gamelog::GRFUpdate(const GRFConfig *oldc, const GRFConfig *newc)
|
|||
this->GRFCompatible(&nl[n]->ident);
|
||||
}
|
||||
|
||||
if (og->num_params != ng->num_params || memcmp(og->param, ng->param, og->num_params * sizeof(og->param[0])) != 0) {
|
||||
if (og->num_params != ng->num_params || og->param == ng->param) {
|
||||
this->GRFParameters(ol[o]->ident.grfid);
|
||||
}
|
||||
|
||||
|
|
|
@ -8090,7 +8090,7 @@ static bool ChangeGRFNumUsedParams(size_t len, ByteReader *buf)
|
|||
GrfMsg(2, "StaticGRFInfo: expected only 1 byte for 'INFO'->'NPAR' but got {}, ignoring this field", len);
|
||||
buf->Skip(len);
|
||||
} else {
|
||||
_cur.grfconfig->num_valid_params = std::min<byte>(buf->ReadByte(), lengthof(_cur.grfconfig->param));
|
||||
_cur.grfconfig->num_valid_params = std::min(buf->ReadByte(), ClampTo<uint8_t>(_cur.grfconfig->param.size()));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -8239,7 +8239,7 @@ static bool ChangeGRFParamMask(size_t len, ByteReader *buf)
|
|||
buf->Skip(len);
|
||||
} else {
|
||||
byte param_nr = buf->ReadByte();
|
||||
if (param_nr >= lengthof(_cur.grfconfig->param)) {
|
||||
if (param_nr >= _cur.grfconfig->param.size()) {
|
||||
GrfMsg(2, "StaticGRFInfo: invalid parameter number in 'INFO'->'PARA'->'MASK', param {}, ignoring this field", param_nr);
|
||||
buf->Skip(len - 1);
|
||||
} else {
|
||||
|
@ -8926,13 +8926,8 @@ GRFFile::GRFFile(const GRFConfig *config)
|
|||
|
||||
/* Copy the initial parameter list
|
||||
* 'Uninitialised' parameters are zeroed as that is their default value when dynamically creating them. */
|
||||
static_assert(lengthof(this->param) == lengthof(config->param) && lengthof(this->param) == 0x80);
|
||||
|
||||
assert(config->num_params <= lengthof(config->param));
|
||||
this->param = config->param;
|
||||
this->param_end = config->num_params;
|
||||
if (this->param_end > 0) {
|
||||
MemCpyT(this->param, config->param, this->param_end);
|
||||
}
|
||||
}
|
||||
|
||||
GRFFile::~GRFFile()
|
||||
|
|
|
@ -121,7 +121,7 @@ struct GRFFile : ZeroedMemoryAllocator {
|
|||
std::vector<std::unique_ptr<struct AirportTileSpec>> airtspec;
|
||||
std::vector<std::unique_ptr<struct RoadStopSpec>> roadstops;
|
||||
|
||||
uint32 param[0x80];
|
||||
std::array<uint32_t, 0x80> param;
|
||||
uint param_end; ///< one more than the highest set parameter
|
||||
|
||||
std::vector<GRFLabel> labels; ///< List of labels
|
||||
|
@ -154,9 +154,9 @@ struct GRFFile : ZeroedMemoryAllocator {
|
|||
/** Get GRF Parameter with range checking */
|
||||
uint32 GetParam(uint number) const
|
||||
{
|
||||
/* Note: We implicitly test for number < lengthof(this->param) and return 0 for invalid parameters.
|
||||
/* Note: We implicitly test for number < this->param.size() and return 0 for invalid parameters.
|
||||
* In fact this is the more important test, as param is zeroed anyway. */
|
||||
assert(this->param_end <= lengthof(this->param));
|
||||
assert(this->param_end <= this->param.size());
|
||||
return (number < this->param_end) ? this->param[number] : 0;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
* @param filename Set the filename of this GRFConfig to filename.
|
||||
*/
|
||||
GRFConfig::GRFConfig(const std::string &filename) :
|
||||
filename(filename), num_valid_params(lengthof(param))
|
||||
filename(filename), num_valid_params(ClampTo<uint8_t>(GRFConfig::param.size()))
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -56,13 +56,13 @@ GRFConfig::GRFConfig(const GRFConfig &config) :
|
|||
flags(config.flags & ~(1 << GCF_COPY)),
|
||||
status(config.status),
|
||||
grf_bugs(config.grf_bugs),
|
||||
param(config.param),
|
||||
num_params(config.num_params),
|
||||
num_valid_params(config.num_valid_params),
|
||||
palette(config.palette),
|
||||
param_info(config.param_info),
|
||||
has_param_defaults(config.has_param_defaults)
|
||||
{
|
||||
MemCpyT<uint32>(this->param, config.param, lengthof(this->param));
|
||||
if (config.error != nullptr) this->error = std::make_unique<GRFError>(*config.error);
|
||||
}
|
||||
|
||||
|
@ -74,7 +74,7 @@ void GRFConfig::CopyParams(const GRFConfig &src)
|
|||
{
|
||||
this->num_params = src.num_params;
|
||||
this->num_valid_params = src.num_valid_params;
|
||||
MemCpyT<uint32>(this->param, src.param, lengthof(this->param));
|
||||
this->param = src.param;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -110,7 +110,7 @@ const char *GRFConfig::GetURL() const
|
|||
void GRFConfig::SetParameterDefaults()
|
||||
{
|
||||
this->num_params = 0;
|
||||
MemSetT<uint32>(this->param, 0, lengthof(this->param));
|
||||
this->param = {};
|
||||
|
||||
if (!this->has_param_defaults) return;
|
||||
|
||||
|
|
|
@ -164,7 +164,7 @@ struct GRFConfig : ZeroedMemoryAllocator {
|
|||
uint8 flags; ///< NOSAVE: GCF_Flags, bitset
|
||||
GRFStatus status; ///< NOSAVE: GRFStatus, enum
|
||||
uint32 grf_bugs; ///< NOSAVE: bugs in this GRF in this run, @see enum GRFBugs
|
||||
uint32 param[0x80]; ///< GRF parameters
|
||||
std::array<uint32_t, 0x80> param; ///< GRF parameters
|
||||
uint8 num_params; ///< Number of used parameters
|
||||
uint8 num_valid_params; ///< NOSAVE: Number of valid parameters (action 0x14)
|
||||
uint8 palette; ///< GRFPalette, bitset
|
||||
|
|
|
@ -169,7 +169,7 @@ struct NewGRFParametersWindow : public Window {
|
|||
clicked_row(UINT_MAX),
|
||||
editable(editable)
|
||||
{
|
||||
this->action14present = (c->num_valid_params != lengthof(c->param) || c->param_info.size() != 0);
|
||||
this->action14present = (c->num_valid_params != c->param.size() || c->param_info.size() != 0);
|
||||
|
||||
this->CreateNestedTree();
|
||||
this->vscroll = this->GetScrollbar(WID_NP_SCROLLBAR);
|
||||
|
@ -225,7 +225,7 @@ struct NewGRFParametersWindow : public Window {
|
|||
}
|
||||
|
||||
case WID_NP_NUMPAR: {
|
||||
SetDParamMaxValue(0, lengthof(this->grf_config->param));
|
||||
SetDParamMaxValue(0, this->grf_config->param.size());
|
||||
Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
|
||||
d.width += padding.width;
|
||||
d.height += padding.height;
|
||||
|
|
|
@ -227,9 +227,9 @@ static size_t LookupManyOfMany(const std::vector<std::string> &many, const char
|
|||
* @return returns the number of items found, or -1 on an error
|
||||
*/
|
||||
template<typename T>
|
||||
static int ParseIntList(const char *p, T *items, int maxitems)
|
||||
static int ParseIntList(const char *p, T *items, size_t maxitems)
|
||||
{
|
||||
int n = 0; // number of items read so far
|
||||
size_t n = 0; // number of items read so far
|
||||
bool comma = false; // do we accept comma?
|
||||
|
||||
while (*p != '\0') {
|
||||
|
@ -262,7 +262,7 @@ static int ParseIntList(const char *p, T *items, int maxitems)
|
|||
* We have read comma when (n != 0) and comma is not allowed */
|
||||
if (n != 0 && !comma) return -1;
|
||||
|
||||
return n;
|
||||
return ClampTo<int>(n);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -996,7 +996,7 @@ static GRFConfig *GRFLoadConfig(IniFile &ini, const char *grpname, bool is_stati
|
|||
|
||||
/* Parse parameters */
|
||||
if (item->value.has_value() && !item->value->empty()) {
|
||||
int count = ParseIntList(item->value->c_str(), c->param, lengthof(c->param));
|
||||
int count = ParseIntList(item->value->c_str(), c->param.data(), c->param.size());
|
||||
if (count < 0) {
|
||||
SetDParamStr(0, filename);
|
||||
ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_ARRAY, WL_CRITICAL);
|
||||
|
|
Loading…
Reference in New Issue