summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2007-08-02 22:32:47 +0000
committerrubidium <rubidium@openttd.org>2007-08-02 22:32:47 +0000
commit5016f5497ccacd0837d37d5099433b3d7728838b (patch)
tree8f1dd8ff82a55b059fa648e4164794c687df1b47 /src
parentb15c0efaa951425d74e8a9ca4104da72a81fe50c (diff)
downloadopenttd-5016f5497ccacd0837d37d5099433b3d7728838b.tar.xz
(svn r10757) -Codechange: make the engine renew struct use the pool item class as super class.
Diffstat (limited to 'src')
-rw-r--r--src/engine.cpp64
-rw-r--r--src/engine.h36
2 files changed, 27 insertions, 73 deletions
diff --git a/src/engine.cpp b/src/engine.cpp
index f7dea0eae..144da2959 100644
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -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();
}
diff --git a/src/engine.h b/src/engine.h
index cd238b8a6..6ee7d18c4 100644
--- a/src/engine.h
+++ b/src/engine.h
@@ -262,40 +262,32 @@ static inline const RoadVehicleInfo* RoadVehInfo(EngineID e)
* Engine Replacement stuff
************************************************************************/
+struct EngineRenew;
/**
- * Struct to store engine replacements. 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
* it.
*/
-struct EngineRenew {
- EngineRenewID index;
- EngineID from;
- EngineID to;
- EngineRenew *next;
- GroupID group_id;
-};
+DECLARE_OLD_POOL(EngineRenew, EngineRenew, 3, 8000)
/**
- * Memory pool for engine renew elements. DO NOT USE outside of engine.c. Is
+ * 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.
*/
-DECLARE_OLD_POOL(EngineRenew, EngineRenew, 3, 8000)
+struct EngineRenew : PoolItem<EngineRenew, EngineRenewID, &_EngineRenew_pool> {
+ EngineID from;
+ EngineID to;
+ EngineRenew *next;
+ GroupID group_id;
-/**
- * Check if a EngineRenew really exists.
- */
-static inline bool IsValidEngineRenew(const EngineRenew *er)
-{
- return er->from != INVALID_ENGINE;
-}
+ EngineRenew(EngineID from = INVALID_ENGINE, EngineID to = INVALID_ENGINE) : from(from), to(to), next(NULL) {}
+ ~EngineRenew() { this->from = INVALID_ENGINE; }
-static inline void DeleteEngineRenew(EngineRenew *er)
-{
- er->from = INVALID_ENGINE;
-}
+ 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->from != INVALID_ENGINE) if (IsValidEngineRenew(er))
+#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)