(svn r10757) -Codechange: make the engine renew struct use the pool item class as super class.

This commit is contained in:
rubidium 2007-08-02 22:32:47 +00:00
parent b15c0efaa9
commit 5016f5497c
2 changed files with 28 additions and 74 deletions

View File

@ -23,6 +23,7 @@
#include "group.h"
#include "string.h"
#include "strings.h"
#include "misc/autoptr.hpp"
EngineInfo _engine_info[TOTAL_NUM_ENGINES];
RailVehicleInfo _rail_vehicle_info[NUM_TRAIN_ENGINES];
@ -479,43 +480,7 @@ CargoID GetEngineCargoType(EngineID engine)
* Engine Replacement stuff
************************************************************************/
static void EngineRenewPoolNewBlock(uint start_item);
DEFINE_OLD_POOL(EngineRenew, EngineRenew, EngineRenewPoolNewBlock, NULL)
static void EngineRenewPoolNewBlock(uint start_item)
{
EngineRenew *er;
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
* TODO - This is just a temporary stage, this will be removed. */
for (er = GetEngineRenew(start_item); er != NULL; er = (er->index + 1U < GetEngineRenewPoolSize()) ? GetEngineRenew(er->index + 1U) : NULL) {
er->index = start_item++;
er->from = INVALID_ENGINE;
}
}
static EngineRenew *AllocateEngineRenew()
{
EngineRenew *er;
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
* TODO - This is just a temporary stage, this will be removed. */
for (er = GetEngineRenew(0); er != NULL; er = (er->index + 1U < GetEngineRenewPoolSize()) ? GetEngineRenew(er->index + 1U) : NULL) {
if (IsValidEngineRenew(er)) continue;
er->to = INVALID_ENGINE;
er->next = NULL;
er->group_id = ALL_GROUP;
return er;
}
/* Check if we can add a block to the pool */
if (AddBlockToPool(&_EngineRenew_pool)) return AllocateEngineRenew();
return NULL;
}
DEFINE_OLD_POOL_GENERIC(EngineRenew, EngineRenew)
/**
* Retrieves the EngineRenew that specifies the replacement of the given
@ -536,9 +501,9 @@ void RemoveAllEngineReplacement(EngineRenewList *erl)
EngineRenew *er = (EngineRenew *)(*erl);
EngineRenew *next;
while (er) {
while (er != NULL) {
next = er->next;
DeleteEngineRenew(er);
delete er;
er = next;
}
*erl = NULL; // Empty list
@ -561,17 +526,19 @@ CommandCost AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, Engi
return CommandCost();
}
er = AllocateEngineRenew();
er = new EngineRenew(old_engine, new_engine);
if (er == NULL) return CMD_ERROR;
AutoPtrT<EngineRenew> er_auto_delete = er;
if (flags & DC_EXEC) {
er->from = old_engine;
er->to = new_engine;
er->group_id = group;
/* Insert before the first element */
er->next = (EngineRenew *)(*erl);
*erl = (EngineRenewList)er;
er_auto_delete.Detach();
}
return CommandCost();
@ -593,7 +560,7 @@ CommandCost RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, Group
/* Cut this element out */
prev->next = er->next;
}
DeleteEngineRenew(er);
delete er;
}
return CommandCost();
}
@ -628,12 +595,7 @@ static void Load_ERNW()
int index;
while ((index = SlIterateArray()) != -1) {
EngineRenew *er;
if (!AddBlockIfNeeded(&_EngineRenew_pool, index))
error("EngineRenews: failed loading savegame: too many EngineRenews");
er = GetEngineRenew(index);
EngineRenew *er = new (index) EngineRenew();
SlObject(er, _engine_renew_desc);
/* Advanced vehicle lists, ungrouped vehicles got added */
@ -704,6 +666,6 @@ extern const ChunkHandler _engine_chunk_handlers[] = {
void InitializeEngines()
{
/* Clean the engine renew pool and create 1 block in it */
CleanPool(&_EngineRenew_pool);
AddBlockToPool(&_EngineRenew_pool);
_EngineRenew_pool.CleanPool();
_EngineRenew_pool.AddBlockToPool();
}

View File

@ -262,19 +262,7 @@ static inline const RoadVehicleInfo* RoadVehInfo(EngineID e)
* Engine Replacement stuff
************************************************************************/
/**
* Struct to store engine replacements. DO NOT USE outside of engine.c. Is
* placed here so the only exception to this rule, the saveload code, can use
* it.
*/
struct EngineRenew {
EngineRenewID index;
EngineID from;
EngineID to;
EngineRenew *next;
GroupID group_id;
};
struct EngineRenew;
/**
* Memory pool for engine renew elements. DO NOT USE outside of engine.c. Is
* placed here so the only exception to this rule, the saveload code, can use
@ -283,19 +271,23 @@ struct EngineRenew {
DECLARE_OLD_POOL(EngineRenew, EngineRenew, 3, 8000)
/**
* Check if a EngineRenew really exists.
* Struct to store engine replacements. DO NOT USE outside of engine.c. Is
* placed here so the only exception to this rule, the saveload code, can use
* it.
*/
static inline bool IsValidEngineRenew(const EngineRenew *er)
{
return er->from != INVALID_ENGINE;
}
struct EngineRenew : PoolItem<EngineRenew, EngineRenewID, &_EngineRenew_pool> {
EngineID from;
EngineID to;
EngineRenew *next;
GroupID group_id;
static inline void DeleteEngineRenew(EngineRenew *er)
{
er->from = INVALID_ENGINE;
}
EngineRenew(EngineID from = INVALID_ENGINE, EngineID to = INVALID_ENGINE) : from(from), to(to), next(NULL) {}
~EngineRenew() { this->from = INVALID_ENGINE; }
#define FOR_ALL_ENGINE_RENEWS_FROM(er, start) for (er = GetEngineRenew(start); er != NULL; er = (er->index + 1U < GetEngineRenewPoolSize()) ? GetEngineRenew(er->index + 1U) : NULL) if (er->from != INVALID_ENGINE) if (IsValidEngineRenew(er))
bool IsValid() const { return this->from != INVALID_ENGINE; }
};
#define FOR_ALL_ENGINE_RENEWS_FROM(er, start) for (er = GetEngineRenew(start); er != NULL; er = (er->index + 1U < GetEngineRenewPoolSize()) ? GetEngineRenew(er->index + 1U) : NULL) if (er->IsValid())
#define FOR_ALL_ENGINE_RENEWS(er) FOR_ALL_ENGINE_RENEWS_FROM(er, 0)