Codechange: Use vector for NewGRF spec overrides.

This replaces C-style memory management.
pull/480/head
Peter Nelson 1 year ago committed by Michael Lutz
parent 07940726d3
commit eedb786872

@ -44,21 +44,10 @@ OverrideManagerBase::OverrideManagerBase(uint16 offset, uint16 maximum, uint16 i
max_new_entities = maximum;
invalid_ID = invalid;
mapping_ID = CallocT<EntityIDMapping>(max_new_entities);
entity_overrides = MallocT<uint16>(max_offset);
for (size_t i = 0; i < max_offset; i++) entity_overrides[i] = invalid;
grfid_overrides = CallocT<uint32>(max_offset);
}
/**
* Destructor of the generic class.
* Frees allocated memory of constructor
*/
OverrideManagerBase::~OverrideManagerBase()
{
free(mapping_ID);
free(entity_overrides);
free(grfid_overrides);
this->mapping_ID.resize(this->max_new_entities);
this->entity_overrides.resize(this->max_offset);
std::fill(this->entity_overrides.begin(), this->entity_overrides.end(), this->invalid_ID);
this->grfid_overrides.resize(this->max_offset);
}
/**
@ -81,16 +70,14 @@ void OverrideManagerBase::Add(uint8 local_id, uint32 grfid, uint entity_type)
/** Resets the mapping, which is used while initializing game */
void OverrideManagerBase::ResetMapping()
{
memset(mapping_ID, 0, (max_new_entities - 1) * sizeof(EntityIDMapping));
std::fill(this->mapping_ID.begin(), this->mapping_ID.end(), EntityIDMapping{});
}
/** Resets the override, which is used while initializing game */
void OverrideManagerBase::ResetOverride()
{
for (uint16 i = 0; i < max_offset; i++) {
entity_overrides[i] = invalid_ID;
grfid_overrides[i] = 0;
}
std::fill(this->entity_overrides.begin(), this->entity_overrides.end(), this->invalid_ID);
std::fill(this->grfid_overrides.begin(), this->grfid_overrides.end(), uint32());
}
/**

@ -191,8 +191,8 @@ struct EntityIDMapping {
class OverrideManagerBase {
protected:
uint16 *entity_overrides;
uint32 *grfid_overrides;
std::vector<uint16> entity_overrides;
std::vector<uint32> grfid_overrides;
uint16 max_offset; ///< what is the length of the original entity's array of specs
uint16 max_new_entities; ///< what is the amount of entities, old and new summed
@ -201,10 +201,10 @@ protected:
virtual bool CheckValidNewID(uint16 testid) { return true; }
public:
EntityIDMapping *mapping_ID; ///< mapping of ids from grf files. Public out of convenience
std::vector<EntityIDMapping> mapping_ID; ///< mapping of ids from grf files. Public out of convenience
OverrideManagerBase(uint16 offset, uint16 maximum, uint16 invalid);
virtual ~OverrideManagerBase();
virtual ~OverrideManagerBase() {}
void ResetOverride();
void ResetMapping();
@ -267,7 +267,7 @@ public:
struct AirportTileSpec;
class AirportTileOverrideManager : public OverrideManagerBase {
protected:
virtual bool CheckValidNewID(uint16 testid) { return testid != 0xFF; }
virtual bool CheckValidNewID(uint16 testid) override { return testid != 0xFF; }
public:
AirportTileOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
OverrideManagerBase(offset, maximum, invalid) {}
@ -278,7 +278,7 @@ public:
struct ObjectSpec;
class ObjectOverrideManager : public OverrideManagerBase {
protected:
virtual bool CheckValidNewID(uint16 testid) { return testid != 0xFF; }
virtual bool CheckValidNewID(uint16 testid) override { return testid != 0xFF; }
public:
ObjectOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
OverrideManagerBase(offset, maximum, invalid) {}

Loading…
Cancel
Save