(svn r20248) -Codechange: use a copy-constructor instead of a separate function co clone GRFConfig/GRFError

replace/41b28d7194a279bdc17475d4fbe2ea6ec885a466
yexo 14 years ago
parent 24df43633c
commit 1ca16aa979

@ -20,11 +20,37 @@
#include "fileio_func.h" #include "fileio_func.h"
#include "fios.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) GRFConfig::GRFConfig(const char *filename)
{ {
if (filename != NULL) this->filename = strdup(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() GRFConfig::~GRFConfig()
{ {
/* GCF_COPY as in NOT strdupped/alloced the filename, name and info */ /* GCF_COPY as in NOT strdupped/alloced the filename, name and info */
@ -61,12 +87,33 @@ GRFConfig *_grfconfig;
GRFConfig *_grfconfig_newgame; GRFConfig *_grfconfig_newgame;
GRFConfig *_grfconfig_static; 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) : GRFError::GRFError(StringID severity, StringID message) :
message(message), message(message),
severity(severity) 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() GRFError::~GRFError()
{ {
free(this->custom_message); 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 /** Copy a GRF Config list
* @param dst pointer to destination list * @param dst pointer to destination list
* @param src pointer to source list values * @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 */ /* Clear destination as it will be overwritten */
ClearGRFConfigList(dst); ClearGRFConfigList(dst);
for (; src != NULL; src = src->next) { for (; src != NULL; src = src->next) {
GRFConfig *c = DuplicateGRFConfig(src); GRFConfig *c = new GRFConfig(*src);
ClrBit(c->flags, GCF_INIT_ONLY); ClrBit(c->flags, GCF_INIT_ONLY);
if (init_only) SetBit(c->flags, GCF_INIT_ONLY); if (init_only) SetBit(c->flags, GCF_INIT_ONLY);

@ -70,6 +70,7 @@ struct GRFIdentifier {
/** Information about why GRF had problems during initialisation */ /** Information about why GRF had problems during initialisation */
struct GRFError : ZeroedMemoryAllocator { struct GRFError : ZeroedMemoryAllocator {
GRFError(StringID severity, StringID message = 0); GRFError(StringID severity, StringID message = 0);
GRFError(const GRFError &error);
~GRFError(); ~GRFError();
char *custom_message; ///< Custom message (if present) 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 */ /** Information about GRF, used in the game and (part of it) in savegames */
struct GRFConfig : ZeroedMemoryAllocator { struct GRFConfig : ZeroedMemoryAllocator {
GRFConfig(const char *filename = NULL); GRFConfig(const char *filename = NULL);
GRFConfig(const GRFConfig &config);
~GRFConfig(); ~GRFConfig();
GRFIdentifier ident; ///< grfid and md5sum to uniquely identify newgrfs GRFIdentifier ident; ///< grfid and md5sum to uniquely identify newgrfs
@ -123,7 +125,6 @@ void ResetGRFConfig(bool defaults);
GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig); GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig);
bool FillGRFDetails(GRFConfig *config, bool is_static); bool FillGRFDetails(GRFConfig *config, bool is_static);
char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last); char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last);
GRFConfig *DuplicateGRFConfig(const GRFConfig *c);
/* In newgrf_gui.cpp */ /* In newgrf_gui.cpp */
void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config); void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config);

@ -548,8 +548,7 @@ struct NewGRFWindow : public QueryStringBaseWindow {
} }
} }
GRFConfig *c = DuplicateGRFConfig(this->avail_sel); // Copy GRF details from scanned list. GRFConfig *c = new GRFConfig(*this->avail_sel); // Copy GRF details from scanned list.
c->next = NULL;
*list = c; // Append GRF config to configuration list. *list = c; // Append GRF config to configuration list.
/* Select next (or previous, if last one) item in the 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); const GRFConfig *f = FindGRFConfig(c->ident.grfid, compatible ? c->original_md5sum : c->ident.md5sum);
if (f == NULL) continue; if (f == NULL) continue;
*l = DuplicateGRFConfig(f); *l = new GRFConfig(*f);
(*l)->next = c->next; (*l)->next = c->next;
if (active_sel == c) active_sel = *l; if (active_sel == c) active_sel = *l;

Loading…
Cancel
Save