(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 "group.h"
#include "string.h" #include "string.h"
#include "strings.h" #include "strings.h"
#include "misc/autoptr.hpp"
EngineInfo _engine_info[TOTAL_NUM_ENGINES]; EngineInfo _engine_info[TOTAL_NUM_ENGINES];
RailVehicleInfo _rail_vehicle_info[NUM_TRAIN_ENGINES]; RailVehicleInfo _rail_vehicle_info[NUM_TRAIN_ENGINES];
@ -479,43 +480,7 @@ CargoID GetEngineCargoType(EngineID engine)
* Engine Replacement stuff * Engine Replacement stuff
************************************************************************/ ************************************************************************/
static void EngineRenewPoolNewBlock(uint start_item); DEFINE_OLD_POOL_GENERIC(EngineRenew, EngineRenew)
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;
}
/** /**
* Retrieves the EngineRenew that specifies the replacement of the given * Retrieves the EngineRenew that specifies the replacement of the given
@ -536,9 +501,9 @@ void RemoveAllEngineReplacement(EngineRenewList *erl)
EngineRenew *er = (EngineRenew *)(*erl); EngineRenew *er = (EngineRenew *)(*erl);
EngineRenew *next; EngineRenew *next;
while (er) { while (er != NULL) {
next = er->next; next = er->next;
DeleteEngineRenew(er); delete er;
er = next; er = next;
} }
*erl = NULL; // Empty list *erl = NULL; // Empty list
@ -561,17 +526,19 @@ CommandCost AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, Engi
return CommandCost(); return CommandCost();
} }
er = AllocateEngineRenew(); er = new EngineRenew(old_engine, new_engine);
if (er == NULL) return CMD_ERROR; if (er == NULL) return CMD_ERROR;
AutoPtrT<EngineRenew> er_auto_delete = er;
if (flags & DC_EXEC) { if (flags & DC_EXEC) {
er->from = old_engine;
er->to = new_engine;
er->group_id = group; er->group_id = group;
/* Insert before the first element */ /* Insert before the first element */
er->next = (EngineRenew *)(*erl); er->next = (EngineRenew *)(*erl);
*erl = (EngineRenewList)er; *erl = (EngineRenewList)er;
er_auto_delete.Detach();
} }
return CommandCost(); return CommandCost();
@ -593,7 +560,7 @@ CommandCost RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, Group
/* Cut this element out */ /* Cut this element out */
prev->next = er->next; prev->next = er->next;
} }
DeleteEngineRenew(er); delete er;
} }
return CommandCost(); return CommandCost();
} }
@ -628,12 +595,7 @@ static void Load_ERNW()
int index; int index;
while ((index = SlIterateArray()) != -1) { while ((index = SlIterateArray()) != -1) {
EngineRenew *er; EngineRenew *er = new (index) EngineRenew();
if (!AddBlockIfNeeded(&_EngineRenew_pool, index))
error("EngineRenews: failed loading savegame: too many EngineRenews");
er = GetEngineRenew(index);
SlObject(er, _engine_renew_desc); SlObject(er, _engine_renew_desc);
/* Advanced vehicle lists, ungrouped vehicles got added */ /* Advanced vehicle lists, ungrouped vehicles got added */
@ -704,6 +666,6 @@ extern const ChunkHandler _engine_chunk_handlers[] = {
void InitializeEngines() void InitializeEngines()
{ {
/* Clean the engine renew pool and create 1 block in it */ /* Clean the engine renew pool and create 1 block in it */
CleanPool(&_EngineRenew_pool); _EngineRenew_pool.CleanPool();
AddBlockToPool(&_EngineRenew_pool); _EngineRenew_pool.AddBlockToPool();
} }

View File

@ -262,19 +262,7 @@ static inline const RoadVehicleInfo* RoadVehInfo(EngineID e)
* Engine Replacement stuff * Engine Replacement stuff
************************************************************************/ ************************************************************************/
/** struct EngineRenew;
* 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;
};
/** /**
* Memory pool for engine renew elements. DO NOT USE outside of engine.c. Is * 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 * 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) 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) struct EngineRenew : PoolItem<EngineRenew, EngineRenewID, &_EngineRenew_pool> {
{ EngineID from;
return er->from != INVALID_ENGINE; EngineID to;
} EngineRenew *next;
GroupID group_id;
static inline void DeleteEngineRenew(EngineRenew *er) EngineRenew(EngineID from = INVALID_ENGINE, EngineID to = INVALID_ENGINE) : from(from), to(to), next(NULL) {}
{ ~EngineRenew() { this->from = INVALID_ENGINE; }
er->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) #define FOR_ALL_ENGINE_RENEWS(er) FOR_ALL_ENGINE_RENEWS_FROM(er, 0)