diff options
author | yexo <yexo@openttd.org> | 2010-07-31 09:32:44 +0000 |
---|---|---|
committer | yexo <yexo@openttd.org> | 2010-07-31 09:32:44 +0000 |
commit | 1ca16aa979fab1d7a60fb40def0374433362d265 (patch) | |
tree | 6897f2e35c723031aedf1c44c99c01731b8efa1e | |
parent | 24df43633cb1f647f71867512ce4cbc9f9672561 (diff) | |
download | openttd-1ca16aa979fab1d7a60fb40def0374433362d265.tar.xz |
(svn r20248) -Codechange: use a copy-constructor instead of a separate function co clone GRFConfig/GRFError
-rw-r--r-- | src/newgrf_config.cpp | 75 | ||||
-rw-r--r-- | src/newgrf_config.h | 3 | ||||
-rw-r--r-- | src/newgrf_gui.cpp | 5 |
3 files changed, 52 insertions, 31 deletions
diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index a77a537eb..20d3301b7 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -20,11 +20,37 @@ #include "fileio_func.h" #include "fios.h" +/** + * Create a new GRFConfig. + * @param filename Set the filename of this GRFConfig to filename. The argument + * is copied so the original string isn't needed after the constructor. + */ GRFConfig::GRFConfig(const char *filename) { if (filename != NULL) this->filename = strdup(filename); } +/** + * Create a new GRFConfig that is a deep copy of an existing config. + * @param config The GRFConfig object to make a copy of. + */ +GRFConfig::GRFConfig(const GRFConfig &config) : + ident(config.ident), + flags(config.flags & ~GCF_COPY), + status(config.status), + grf_bugs(config.grf_bugs), + num_params(config.num_params), + windows_paletted(config.windows_paletted) +{ + MemCpyT<uint8>(this->original_md5sum, config.original_md5sum, lengthof(this->original_md5sum)); + MemCpyT<uint32>(this->param, config.param, lengthof(this->param)); + if (config.filename != NULL) this->filename = strdup(config.filename); + if (config.name != NULL) this->name = strdup(config.name); + if (config.info != NULL) this->info = strdup(config.info); + if (config.error != NULL) this->error = new GRFError(*config.error); +} + +/** Cleanup a GRFConfig object. */ GRFConfig::~GRFConfig() { /* GCF_COPY as in NOT strdupped/alloced the filename, name and info */ @@ -61,12 +87,33 @@ GRFConfig *_grfconfig; GRFConfig *_grfconfig_newgame; GRFConfig *_grfconfig_static; +/** + * Construct a new GRFError. + * @param severity The severity of this error. + * @param message The actual error-string. + */ GRFError::GRFError(StringID severity, StringID message) : message(message), severity(severity) { } +/** + * 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), + num_params(error.num_params) +{ + if (error.custom_message != NULL) this->custom_message = strdup(error.custom_message); + if (error.data != NULL) this->data = strdup(error.data); + memcpy(this->param_value, error.param_value, sizeof(this->param_value)); +} + GRFError::~GRFError() { free(this->custom_message); @@ -162,32 +209,6 @@ void ClearGRFConfigList(GRFConfig **config) } -/** - * Make a deep copy of a GRFConfig. - * @param c the grfconfig to copy - * @return A pointer to a new grfconfig that's a copy of the original - */ -GRFConfig *DuplicateGRFConfig(const GRFConfig *c) -{ - GRFConfig *config = new GRFConfig(); - *config = *c; - - if (c->filename != NULL) config->filename = strdup(c->filename); - if (c->name != NULL) config->name = strdup(c->name); - if (c->info != NULL) config->info = strdup(c->info); - if (c->error != NULL) { - config->error = new GRFError(c->error->severity, c->error->message); - config->error->num_params = c->error->num_params; - memcpy(config->error->param_value, c->error->param_value, sizeof(config->error->param_value)); - if (c->error->data != NULL) config->error->data = strdup(c->error->data); - if (c->error->custom_message != NULL) config->error->custom_message = strdup(c->error->custom_message); - } - - ClrBit(config->flags, GCF_COPY); - - return config; -} - /** Copy a GRF Config list * @param dst pointer to destination list * @param src pointer to source list values @@ -198,7 +219,7 @@ GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_o /* Clear destination as it will be overwritten */ ClearGRFConfigList(dst); for (; src != NULL; src = src->next) { - GRFConfig *c = DuplicateGRFConfig(src); + GRFConfig *c = new GRFConfig(*src); ClrBit(c->flags, GCF_INIT_ONLY); if (init_only) SetBit(c->flags, GCF_INIT_ONLY); diff --git a/src/newgrf_config.h b/src/newgrf_config.h index bdac1868a..0a52f281c 100644 --- a/src/newgrf_config.h +++ b/src/newgrf_config.h @@ -70,6 +70,7 @@ struct GRFIdentifier { /** Information about why GRF had problems during initialisation */ struct GRFError : ZeroedMemoryAllocator { GRFError(StringID severity, StringID message = 0); + GRFError(const GRFError &error); ~GRFError(); char *custom_message; ///< Custom message (if present) @@ -83,6 +84,7 @@ struct GRFError : ZeroedMemoryAllocator { /** Information about GRF, used in the game and (part of it) in savegames */ struct GRFConfig : ZeroedMemoryAllocator { GRFConfig(const char *filename = NULL); + GRFConfig(const GRFConfig &config); ~GRFConfig(); GRFIdentifier ident; ///< grfid and md5sum to uniquely identify newgrfs @@ -123,7 +125,6 @@ void ResetGRFConfig(bool defaults); GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig); bool FillGRFDetails(GRFConfig *config, bool is_static); char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last); -GRFConfig *DuplicateGRFConfig(const GRFConfig *c); /* In newgrf_gui.cpp */ void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config); diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index 195893db7..77bcedb63 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -548,8 +548,7 @@ struct NewGRFWindow : public QueryStringBaseWindow { } } - GRFConfig *c = DuplicateGRFConfig(this->avail_sel); // Copy GRF details from scanned list. - c->next = NULL; + GRFConfig *c = new GRFConfig(*this->avail_sel); // Copy GRF details from scanned list. *list = c; // Append GRF config to configuration list. /* Select next (or previous, if last one) item in the list. */ @@ -720,7 +719,7 @@ struct NewGRFWindow : public QueryStringBaseWindow { const GRFConfig *f = FindGRFConfig(c->ident.grfid, compatible ? c->original_md5sum : c->ident.md5sum); if (f == NULL) continue; - *l = DuplicateGRFConfig(f); + *l = new GRFConfig(*f); (*l)->next = c->next; if (active_sel == c) active_sel = *l; |