(svn r10755) -Codechange: make the town struct use the pool item class as super class.
authorrubidium
Thu, 02 Aug 2007 21:05:54 +0000
changeset 7386 93f6a042d1c3
parent 7385 69832b54e5af
child 7387 c81fff74048b
(svn r10755) -Codechange: make the town struct use the pool item class as super class.
src/misc_gui.cpp
src/strings.cpp
src/town.h
src/town_cmd.cpp
src/town_gui.cpp
--- a/src/misc_gui.cpp	Thu Aug 02 21:02:57 2007 +0000
+++ b/src/misc_gui.cpp	Thu Aug 02 21:05:54 2007 +0000
@@ -134,7 +134,7 @@
 	GetString(_landinfo_data[3], STR_LANDINFO_COORDS, lastof(_landinfo_data[3]));
 
 	SetDParam(0, STR_01A9_NONE);
-	if (t != NULL && IsValidTown(t)) {
+	if (t != NULL && t->IsValid()) {
 		SetDParam(0, STR_TOWN);
 		SetDParam(1, t->index);
 	}
--- a/src/strings.cpp	Thu Aug 02 21:02:57 2007 +0000
+++ b/src/strings.cpp	Thu Aug 02 21:05:54 2007 +0000
@@ -824,7 +824,7 @@
 				const Town* t = GetTown(GetInt32(&argv));
 				int64 temp[1];
 
-				assert(IsValidTown(t));
+				assert(t->IsValid());
 
 				temp[0] = t->townnameparts;
 				uint32 grfid = t->townnamegrfid;
--- a/src/town.h	Thu Aug 02 21:02:57 2007 +0000
+++ b/src/town.h	Thu Aug 02 21:05:54 2007 +0000
@@ -75,7 +75,10 @@
 	uint8 class_count[HOUSE_CLASS_MAX];
 };
 
-struct Town {
+struct Town;
+DECLARE_OLD_POOL(Town, Town, 3, 8000)
+
+struct Town : PoolItem<Town, TownID, &_Town_pool> {
 	TileIndex xy;
 
 	/* Current population of people and amount of houses. */
@@ -139,9 +142,6 @@
 	/* Fund road reconstruction in action? */
 	byte road_build_months;
 
-	/* Index in town array */
-	TownID index;
-
 	/* If this is a larger town, and should grow more quickly. */
 	bool larger_town;
 
@@ -150,6 +150,18 @@
 
 	/* NOSAVE: The number of each type of building in the town. */
 	BuildingCounts building_counts;
+
+	/**
+	 * Creates a new town
+	 */
+	Town(TileIndex tile = 0);
+
+	/** Destroy the town */
+	~Town();
+
+	bool IsValid() const { return this->xy != 0; }
+
+	void QuickFree();
 };
 
 struct HouseSpec {
@@ -270,8 +282,6 @@
 
 VARDEF const Town** _town_sort;
 
-DECLARE_OLD_POOL(Town, Town, 3, 8000)
-
 static inline HouseSpec *GetHouseSpecs(HouseID house_id)
 {
 	assert(house_id < HOUSE_MAX);
@@ -279,23 +289,13 @@
 }
 
 /**
- * Check if a Town really exists.
- * @param town to inquiry
- * @return true if it exists
- */
-static inline bool IsValidTown(const Town* town)
-{
-	return town->xy != 0;
-}
-
-/**
  * Check if a TownID is valid.
  * @param index to inquiry in the pool of town
  * @return true if it exists
  */
 static inline bool IsValidTownID(TownID index)
 {
-	return index < GetTownPoolSize() && IsValidTown(GetTown(index));
+	return index < GetTownPoolSize() && GetTown(index)->IsValid();
 }
 
 VARDEF uint _total_towns;
@@ -337,17 +337,9 @@
 	return GetTown(index);
 }
 
-void DestroyTown(Town *t);
-
-static inline void DeleteTown(Town *t)
-{
-	DestroyTown(t);
-	t->xy = 0;
-}
-
 Town* CalcClosestTownFromTile(TileIndex tile, uint threshold);
 
-#define FOR_ALL_TOWNS_FROM(t, start) for (t = GetTown(start); t != NULL; t = (t->index + 1U < GetTownPoolSize()) ? GetTown(t->index + 1U) : NULL) if (IsValidTown(t))
+#define FOR_ALL_TOWNS_FROM(t, start) for (t = GetTown(start); t != NULL; t = (t->index + 1U < GetTownPoolSize()) ? GetTown(t->index + 1U) : NULL) if (t->IsValid())
 #define FOR_ALL_TOWNS(t) FOR_ALL_TOWNS_FROM(t, 0)
 
 VARDEF bool _town_sort_dirty;
--- a/src/town_cmd.cpp	Thu Aug 02 21:02:57 2007 +0000
+++ b/src/town_cmd.cpp	Thu Aug 02 21:05:54 2007 +0000
@@ -40,52 +40,40 @@
 #include "newgrf_house.h"
 #include "newgrf_commons.h"
 #include "newgrf_townname.h"
-
-/**
- * Called if a new block is added to the town-pool
- */
-static void TownPoolNewBlock(uint start_item)
-{
-	Town *t;
-
-	/* 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 (t = GetTown(start_item); t != NULL; t = (t->index + 1U < GetTownPoolSize()) ? GetTown(t->index + 1U) : NULL) t->index = start_item++;
-}
+#include "misc/autoptr.hpp"
 
 /* Initialize the town-pool */
-DEFINE_OLD_POOL(Town, Town, TownPoolNewBlock, NULL)
-
-/**
- * Removes a specific town as well as all industries
- * under its "juridiction"
- * @param t Town to remove
- */
-void DestroyTown(Town *t)
+DEFINE_OLD_POOL_GENERIC(Town, Town)
+
+Town::Town(TileIndex tile)
+{
+	this->xy = tile;
+}
+
+Town::~Town()
 {
 	Industry *i;
-	TileIndex tile;
 
 	/* Delete town authority window
 	 * and remove from list of sorted towns */
-	DeleteWindowById(WC_TOWN_VIEW, t->index);
+	DeleteWindowById(WC_TOWN_VIEW, this->index);
 	_town_sort_dirty = true;
 	_total_towns--;
 
 	/* Delete all industries belonging to the town */
-	FOR_ALL_INDUSTRIES(i) if (i->town == t) DeleteIndustry(i);
+	FOR_ALL_INDUSTRIES(i) if (i->town == this) DeleteIndustry(i);
 
 	/* Go through all tiles and delete those belonging to the town */
-	for (tile = 0; tile < MapSize(); ++tile) {
+	for (TileIndex tile = 0; tile < MapSize(); ++tile) {
 		switch (GetTileType(tile)) {
 			case MP_HOUSE:
-				if (GetTownByTile(tile) == t) DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
+				if (GetTownByTile(tile) == this) DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
 				break;
 
 			case MP_ROAD:
 			case MP_TUNNELBRIDGE:
 				if (IsTileOwner(tile, OWNER_TOWN) &&
-						ClosestTownFromTile(tile, (uint)-1) == t)
+						ClosestTownFromTile(tile, (uint)-1) == this)
 					DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
 				break;
 
@@ -94,10 +82,17 @@
 		}
 	}
 
-	DeleteName(t->townnametype);
-	DeleteSubsidyWithTown(t->index);
+	DeleteSubsidyWithTown(this->index);
 
 	MarkWholeScreenDirty();
+
+	this->QuickFree();
+	this->xy = 0;
+}
+
+void Town::QuickFree()
+{
+	DeleteName(this->townnametype);
 }
 
 // Local
@@ -1359,10 +1354,6 @@
 	extern int _nb_orig_names;
 	int x, i;
 
-	/* clear the town struct */
-	i = t->index;
-	memset(t, 0, sizeof(Town));
-	t->index = i;
 	_total_towns++;
 
 	t->xy = tile;
@@ -1446,30 +1437,6 @@
 	UpdateTownMaxPass(t);
 }
 
-static Town *AllocateTown()
-{
-	Town *t;
-
-	/* 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 (t = GetTown(0); t != NULL; t = (t->index + 1U < GetTownPoolSize()) ? GetTown(t->index + 1U) : NULL) {
-		if (!IsValidTown(t)) {
-			TownID index = t->index;
-
-			memset(t, 0, sizeof(Town));
-			t->index = index;
-
-			return t;
-		}
-	}
-
-	/* Check if we can add a block to the pool */
-	if (AddBlockToPool(&_Town_pool))
-		return AllocateTown();
-
-	return NULL;
-}
-
 /** Create a new town.
  * This obviously only works in the scenario editor. Function not removed
  * as it might be possible in the future to fund your own town :)
@@ -1480,7 +1447,6 @@
  */
 CommandCost CmdBuildTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 {
-	Town *t;
 	uint32 townnameparts;
 
 	/* Only in the scenario editor */
@@ -1507,14 +1473,16 @@
 		return_cmd_error(STR_023A_TOO_MANY_TOWNS);
 
 	/* Allocate town struct */
-	t = AllocateTown();
+	Town *t = new Town(tile);
 	if (t == NULL) return_cmd_error(STR_023A_TOO_MANY_TOWNS);
+	AutoPtrT<Town> t_auto_delete = t;
 
 	/* Create the town */
 	if (flags & DC_EXEC) {
 		_generating_world = true;
 		DoCreateTown(t, tile, townnameparts, (TownSizeMode)p2, p1);
 		_generating_world = false;
+		t_auto_delete.Detach();
 	}
 	return CommandCost();
 }
@@ -1540,7 +1508,7 @@
 		if (!CreateTownName(&townnameparts)) break;
 
 		/* Allocate a town struct */
-		t = AllocateTown();
+		t = new Town(tile);
 		if (t == NULL) break;
 
 		DoCreateTown(t, tile, townnameparts, mode, size);
@@ -2476,12 +2444,7 @@
 	_total_towns = 0;
 
 	while ((index = SlIterateArray()) != -1) {
-		Town *t;
-
-		if (!AddBlockIfNeeded(&_Town_pool, index))
-			error("Towns: failed loading savegame: too many towns");
-
-		t = GetTown(index);
+		Town *t = new (index) Town();
 		SlObject(t, _town_desc);
 
 		_total_towns++;
--- a/src/town_gui.cpp	Thu Aug 02 21:02:57 2007 +0000
+++ b/src/town_gui.cpp	Thu Aug 02 21:05:54 2007 +0000
@@ -278,7 +278,7 @@
 				break;
 
 			case 10: /* delete town */
-				DeleteTown(t);
+				delete t;
 				break;
 		}
 		break;