From f78aa1e72061009752a0dabf0a5a46749a3e7a80 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Thu, 27 Apr 2023 20:28:09 +0200 Subject: [PATCH] Codechange: use std::unique_ptr to manager GRFErrors in GRFConfig --- src/newgrf.cpp | 20 +++++++------------- src/newgrf_config.cpp | 3 +-- src/newgrf_config.h | 2 +- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 4336e7b76f..3921208ef0 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -449,12 +449,11 @@ static GRFError *DisableGrf(StringID message = STR_NULL, GRFConfig *config = nul if (config == _cur.grfconfig) _cur.skip_sprites = -1; if (message != STR_NULL) { - delete config->error; - config->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL, message); + config->error = std::make_unique(STR_NEWGRF_ERROR_MSG_FATAL, message); if (config == _cur.grfconfig) config->error->param_value[0] = _cur.nfo_line; } - return config->error; + return config->error.get(); } /** @@ -7134,8 +7133,7 @@ static void GRFLoadError(ByteReader *buf) DisableGrf(); /* Make sure we show fatal errors, instead of silly infos from before */ - delete _cur.grfconfig->error; - _cur.grfconfig->error = nullptr; + _cur.grfconfig->error.reset(); } if (message_id >= lengthof(msgstr) && message_id != 0xFF) { @@ -7151,7 +7149,8 @@ static void GRFLoadError(ByteReader *buf) /* For now we can only show one message per newgrf file. */ if (_cur.grfconfig->error != nullptr) return; - GRFError *error = new GRFError(sevstr[severity]); + _cur.grfconfig->error = std::make_unique(sevstr[severity]); + GRFError *error = _cur.grfconfig->error.get(); if (message_id == 0xFF) { /* This is a custom error message. */ @@ -7181,8 +7180,6 @@ static void GRFLoadError(ByteReader *buf) uint param_number = buf->ReadByte(); error->param_value[i] = _cur.grffile->GetParam(param_number); } - - _cur.grfconfig->error = error; } /* Action 0x0C */ @@ -8723,10 +8720,7 @@ static void ResetNewGRF() static void ResetNewGRFErrors() { for (GRFConfig *c = _grfconfig; c != nullptr; c = c->next) { - if (!HasBit(c->flags, GCF_COPY) && c->error != nullptr) { - delete c->error; - c->error = nullptr; - } + c->error.reset(); } } @@ -10011,7 +10005,7 @@ void LoadNewGRF(uint load_index, uint num_baseset) if (num_non_static == NETWORK_MAX_GRF_COUNT) { Debug(grf, 0, "'{}' is not loaded as the maximum number of non-static GRFs has been reached", c->filename); c->status = GCS_DISABLED; - c->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_TOO_MANY_NEWGRFS_LOADED); + c->error = std::make_unique(STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_TOO_MANY_NEWGRFS_LOADED); continue; } num_non_static++; diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index d51bb48a5b..bb4ed080f2 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -64,7 +64,7 @@ GRFConfig::GRFConfig(const GRFConfig &config) : MemCpyT(this->original_md5sum, config.original_md5sum, lengthof(this->original_md5sum)); MemCpyT(this->param, config.param, lengthof(this->param)); if (config.filename != nullptr) this->filename = stredup(config.filename); - if (config.error != nullptr) this->error = new GRFError(*config.error); + if (config.error != nullptr) this->error = std::make_unique(*config.error); for (uint i = 0; i < config.param_info.size(); i++) { if (config.param_info[i] == nullptr) { this->param_info.push_back(nullptr); @@ -80,7 +80,6 @@ GRFConfig::~GRFConfig() /* GCF_COPY as in NOT stredupped/alloced the filename */ if (!HasBit(this->flags, GCF_COPY)) { free(this->filename); - delete this->error; } for (uint i = 0; i < this->param_info.size(); i++) delete this->param_info[i]; diff --git a/src/newgrf_config.h b/src/newgrf_config.h index 6f4b5c0028..5a089b7093 100644 --- a/src/newgrf_config.h +++ b/src/newgrf_config.h @@ -166,7 +166,7 @@ struct GRFConfig : ZeroedMemoryAllocator { GRFTextWrapper name; ///< NOSAVE: GRF name (Action 0x08) GRFTextWrapper info; ///< NOSAVE: GRF info (author, copyright, ...) (Action 0x08) GRFTextWrapper url; ///< NOSAVE: URL belonging to this GRF. - GRFError *error; ///< NOSAVE: Error/Warning during GRF loading (Action 0x0B) + std::unique_ptr error; ///< NOSAVE: Error/Warning during GRF loading (Action 0x0B) uint32 version; ///< NOSAVE: Version a NewGRF can set so only the newest NewGRF is shown uint32 min_loadable_version; ///< NOSAVE: Minimum compatible version a NewGRF can define