summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2007-08-02 12:22:40 +0000
committerrubidium <rubidium@openttd.org>2007-08-02 12:22:40 +0000
commit53e6da1752c616388984c8906eb63673fb3b4050 (patch)
tree7b3d74bfcae924abd1c651969e8045c4b22c5cc9
parente7d2918c0cdb3d58aa066c1497f7e4946ef2cb6f (diff)
downloadopenttd-53e6da1752c616388984c8906eb63673fb3b4050.tar.xz
(svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
-rw-r--r--src/oldpool.h4
-rw-r--r--src/waypoint.cpp97
-rw-r--r--src/waypoint.h32
3 files changed, 51 insertions, 82 deletions
diff --git a/src/oldpool.h b/src/oldpool.h
index 6802cb63e..37154897e 100644
--- a/src/oldpool.h
+++ b/src/oldpool.h
@@ -188,7 +188,7 @@ struct PoolItem {
* @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 @@ struct PoolItem {
* @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());
diff --git a/src/waypoint.cpp b/src/waypoint.cpp
index d2980b3d9..425c7a503 100644
--- a/src/waypoint.cpp
+++ b/src/waypoint.cpp
@@ -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)
-
-/**
- * Create a new waypoint
- * @return a pointer to the newly created Waypoint */
-static Waypoint* AllocateWaypoint()
-{
- Waypoint *wp;
+DEFINE_OLD_POOL_GENERIC(Waypoint, Waypoint)
- /* 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 @@ void UpdateAllWaypointSigns()
}
/**
- * 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 @@ void AfterLoadWaypoints()
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 @@ CommandCost CmdBuildTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint3
/* 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 @@ CommandCost CmdBuildTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint3
}
wp->deleted = 0;
- wp->xy = tile;
wp->build_date = _date;
if (wp->town_index == 0) MakeDefaultWaypointName(wp);
@@ -272,6 +225,7 @@ CommandCost CmdBuildTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint3
UpdateWaypointSign(wp);
RedrawWaypointSign(wp);
YapfNotifyTrackLayoutChange(tile, AxisToTrack(axis));
+ wp_auto_delete.Detach();
}
return CommandCost(_price.build_train_depot);
@@ -443,6 +397,32 @@ void DrawWaypointSprite(int x, int y, int stat_id, RailType railtype)
}
}
+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 FixOldWaypoints()
void InitializeWaypoints()
{
- CleanPool(&_Waypoint_pool);
- AddBlockToPool(&_Waypoint_pool);
+ _Waypoint_pool.CleanPool();
+ _Waypoint_pool.AddBlockToPool();
}
static const SaveLoad _waypoint_desc[] = {
@@ -498,12 +478,7 @@ static void Load_WAYP()
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);
}
}
diff --git a/src/waypoint.h b/src/waypoint.h
index c3e239561..af24b1906 100644
--- a/src/waypoint.h
+++ b/src/waypoint.h
@@ -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 @@ struct Waypoint {
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)
+ Waypoint(TileIndex tile = 0);
+ ~Waypoint();
-/**
- * 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;
-}
+ 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)