(svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
authorrubidium
Thu, 02 Aug 2007 12:22:40 +0000
changeset 7381 d7991ec3f19d
parent 7380 3cfd7b197ce8
child 7382 721b2d81ce91
(svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
src/oldpool.h
src/waypoint.cpp
src/waypoint.h
--- a/src/oldpool.h	Thu Aug 02 10:49:24 2007 +0000
+++ b/src/oldpool.h	Thu Aug 02 12:22:40 2007 +0000
@@ -188,7 +188,7 @@
 	 * @param size the size of the variable (unused)
 	 * @return the memory that is 'allocated'
 	 */
-	void *operator new (size_t size)
+	void *operator new(size_t size)
 	{
 		return AllocateRaw();
 	}
@@ -208,7 +208,7 @@
 	 * @param index the index of the object
 	 * @return the memory that is 'allocated'
 	 */
-	void *operator new (size_t size, int index)
+	void *operator new(size_t size, int index)
 	{
 		if (!Tpool->AddBlockIfNeeded(index)) error("%s: failed loading savegame: too many %s", Tpool->GetName(), Tpool->GetName());
 
--- a/src/waypoint.cpp	Thu Aug 02 10:49:24 2007 +0000
+++ b/src/waypoint.cpp	Thu Aug 02 12:22:40 2007 +0000
@@ -26,50 +26,14 @@
 #include "newgrf.h"
 #include "string.h"
 #include "strings.h"
+#include "misc/autoptr.hpp"
 
 enum {
 	MAX_WAYPOINTS_PER_TOWN = 64,
 };
 
-/**
- * Called if a new block is added to the waypoint-pool
- */
-static void WaypointPoolNewBlock(uint start_item)
-{
-	Waypoint *wp;
-
-	/* 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 (wp = GetWaypoint(start_item); wp != NULL; wp = (wp->index + 1U < GetWaypointPoolSize()) ? GetWaypoint(wp->index + 1U) : NULL) wp->index = start_item++;
-}
-
-DEFINE_OLD_POOL(Waypoint, Waypoint, WaypointPoolNewBlock, NULL)
+DEFINE_OLD_POOL_GENERIC(Waypoint, Waypoint)
 
-/**
- * Create a new waypoint
- * @return a pointer to the newly created Waypoint */
-static Waypoint* AllocateWaypoint()
-{
-	Waypoint *wp;
-
-	/* 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 (wp = GetWaypoint(0); wp != NULL; wp = (wp->index + 1U < GetWaypointPoolSize()) ? GetWaypoint(wp->index + 1U) : NULL) {
-		if (!IsValidWaypoint(wp)) {
-			uint index = wp->index;
-
-			memset(wp, 0, sizeof(*wp));
-			wp->index = index;
-
-			return wp;
-		}
-	}
-
-	/* Check if we can add a block to the pool */
-	if (AddBlockToPool(&_Waypoint_pool)) return AllocateWaypoint();
-
-	return NULL;
-}
 
 /**
  * Update the sign for the waypoint
@@ -106,19 +70,6 @@
 }
 
 /**
- * Internal handler to delete a waypoint
- * @param wp Waypoint to delete
- */
-void DestroyWaypoint(Waypoint *wp)
-{
-	RemoveOrderFromAllVehicles(OT_GOTO_WAYPOINT, wp->index);
-
-	if (wp->string != STR_NULL) DeleteName(wp->string);
-
-	RedrawWaypointSign(wp);
-}
-
-/**
  * Set the default name for a waypoint
  * @param wp Waypoint to work on
  */
@@ -206,6 +157,7 @@
 CommandCost CmdBuildTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 {
 	Waypoint *wp;
+	AutoPtrT<Waypoint> wp_auto_delete;
 	Slope tileh;
 	Axis axis;
 
@@ -236,9 +188,11 @@
 	/* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */
 	wp = FindDeletedWaypointCloseTo(tile);
 	if (wp == NULL) {
-		wp = AllocateWaypoint();
+		wp = new Waypoint(tile);
 		if (wp == NULL) return CMD_ERROR;
 
+		wp_auto_delete = wp;
+
 		wp->town_index = 0;
 		wp->string = STR_NULL;
 		wp->town_cn = 0;
@@ -264,7 +218,6 @@
 		}
 
 		wp->deleted = 0;
-		wp->xy = tile;
 		wp->build_date = _date;
 
 		if (wp->town_index == 0) MakeDefaultWaypointName(wp);
@@ -272,6 +225,7 @@
 		UpdateWaypointSign(wp);
 		RedrawWaypointSign(wp);
 		YapfNotifyTrackLayoutChange(tile, AxisToTrack(axis));
+		wp_auto_delete.Detach();
 	}
 
 	return CommandCost(_price.build_train_depot);
@@ -443,6 +397,32 @@
 	}
 }
 
+Waypoint::Waypoint(TileIndex tile)
+{
+	this->xy = tile;
+}
+
+Waypoint::~Waypoint()
+{
+	RemoveOrderFromAllVehicles(OT_GOTO_WAYPOINT, this->index);
+
+	RedrawWaypointSign(this);
+	this->xy = 0;
+
+	this->QuickFree();
+}
+
+void Waypoint::QuickFree()
+{
+	if (this->string != STR_NULL) DeleteName(this->string);
+}
+
+bool Waypoint::IsValid() const
+{
+	return this->xy != 0;
+}
+
+
 /**
  * Fix savegames which stored waypoints in their old format
  */
@@ -463,8 +443,8 @@
 
 void InitializeWaypoints()
 {
-	CleanPool(&_Waypoint_pool);
-	AddBlockToPool(&_Waypoint_pool);
+	_Waypoint_pool.CleanPool();
+	_Waypoint_pool.AddBlockToPool();
 }
 
 static const SaveLoad _waypoint_desc[] = {
@@ -498,12 +478,7 @@
 	int index;
 
 	while ((index = SlIterateArray()) != -1) {
-		Waypoint *wp;
-
-		if (!AddBlockIfNeeded(&_Waypoint_pool, index))
-			error("Waypoints: failed loading savegame: too many waypoints");
-
-		wp = GetWaypoint(index);
+		Waypoint *wp = new (index) Waypoint();
 		SlObject(wp, _waypoint_desc);
 	}
 }
--- a/src/waypoint.h	Thu Aug 02 10:49:24 2007 +0000
+++ b/src/waypoint.h	Thu Aug 02 12:22:40 2007 +0000
@@ -8,9 +8,11 @@
 #include "oldpool.h"
 #include "rail_map.h"
 
-struct Waypoint {
+struct Waypoint;
+DECLARE_OLD_POOL(Waypoint, Waypoint, 3, 8000)
+
+struct Waypoint : PoolItem<Waypoint, WaypointID, &_Waypoint_pool> {
 	TileIndex xy;      ///< Tile of waypoint
-	WaypointID index;  ///< Index of waypoint
 
 	TownID town_index; ///< Town associated with the waypoint
 	byte town_cn;      ///< The Nth waypoint for this town (consecutive number)
@@ -24,34 +26,26 @@
 	byte localidx;     ///< Index of station within GRF file
 
 	byte deleted;      ///< Delete counter. If greater than 0 then it is decremented until it reaches 0; the waypoint is then is deleted.
-};
-
-DECLARE_OLD_POOL(Waypoint, Waypoint, 3, 8000)
 
-/**
- * Check if a Waypoint really exists.
- * @param wp Waypoint to query
- * @return the validity of the waypoint
- */
-static inline bool IsValidWaypoint(const Waypoint *wp)
-{
-	return wp->xy != 0;
-}
+	Waypoint(TileIndex tile = 0);
+	~Waypoint();
+
+	void QuickFree();
+
+	bool IsValid() const;
+};
 
 static inline bool IsValidWaypointID(WaypointID index)
 {
-	return index < GetWaypointPoolSize() && IsValidWaypoint(GetWaypoint(index));
+	return index < GetWaypointPoolSize() && GetWaypoint(index)->IsValid();
 }
 
-void DestroyWaypoint(Waypoint *wp);
-
 static inline void DeleteWaypoint(Waypoint *wp)
 {
-	DestroyWaypoint(wp);
-	wp->xy = 0;
+	wp->~Waypoint();
 }
 
-#define FOR_ALL_WAYPOINTS_FROM(wp, start) for (wp = GetWaypoint(start); wp != NULL; wp = (wp->index + 1U < GetWaypointPoolSize()) ? GetWaypoint(wp->index + 1U) : NULL) if (IsValidWaypoint(wp))
+#define FOR_ALL_WAYPOINTS_FROM(wp, start) for (wp = GetWaypoint(start); wp != NULL; wp = (wp->index + 1U < GetWaypointPoolSize()) ? GetWaypoint(wp->index + 1U) : NULL) if (wp->IsValid())
 #define FOR_ALL_WAYPOINTS(wp) FOR_ALL_WAYPOINTS_FROM(wp, 0)