(svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.

This commit is contained in:
rubidium 2007-08-02 12:22:40 +00:00
parent cab6275511
commit dd666a80d5
3 changed files with 52 additions and 83 deletions

View File

@ -188,7 +188,7 @@ struct PoolItem {
* @param size the size of the variable (unused) * @param size the size of the variable (unused)
* @return the memory that is 'allocated' * @return the memory that is 'allocated'
*/ */
void *operator new (size_t size) void *operator new(size_t size)
{ {
return AllocateRaw(); return AllocateRaw();
} }
@ -208,7 +208,7 @@ struct PoolItem {
* @param index the index of the object * @param index the index of the object
* @return the memory that is 'allocated' * @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()); if (!Tpool->AddBlockIfNeeded(index)) error("%s: failed loading savegame: too many %s", Tpool->GetName(), Tpool->GetName());

View File

@ -26,50 +26,14 @@
#include "newgrf.h" #include "newgrf.h"
#include "string.h" #include "string.h"
#include "strings.h" #include "strings.h"
#include "misc/autoptr.hpp"
enum { enum {
MAX_WAYPOINTS_PER_TOWN = 64, MAX_WAYPOINTS_PER_TOWN = 64,
}; };
/** DEFINE_OLD_POOL_GENERIC(Waypoint, Waypoint)
* 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;
/* 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 * Update the sign for the waypoint
@ -105,19 +69,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 * Set the default name for a waypoint
* @param wp Waypoint to work on * @param wp Waypoint to work on
@ -206,6 +157,7 @@ void AfterLoadWaypoints()
CommandCost CmdBuildTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) CommandCost CmdBuildTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{ {
Waypoint *wp; Waypoint *wp;
AutoPtrT<Waypoint> wp_auto_delete;
Slope tileh; Slope tileh;
Axis axis; 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. */ /* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */
wp = FindDeletedWaypointCloseTo(tile); wp = FindDeletedWaypointCloseTo(tile);
if (wp == NULL) { if (wp == NULL) {
wp = AllocateWaypoint(); wp = new Waypoint(tile);
if (wp == NULL) return CMD_ERROR; if (wp == NULL) return CMD_ERROR;
wp_auto_delete = wp;
wp->town_index = 0; wp->town_index = 0;
wp->string = STR_NULL; wp->string = STR_NULL;
wp->town_cn = 0; wp->town_cn = 0;
@ -264,7 +218,6 @@ CommandCost CmdBuildTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint3
} }
wp->deleted = 0; wp->deleted = 0;
wp->xy = tile;
wp->build_date = _date; wp->build_date = _date;
if (wp->town_index == 0) MakeDefaultWaypointName(wp); if (wp->town_index == 0) MakeDefaultWaypointName(wp);
@ -272,6 +225,7 @@ CommandCost CmdBuildTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint3
UpdateWaypointSign(wp); UpdateWaypointSign(wp);
RedrawWaypointSign(wp); RedrawWaypointSign(wp);
YapfNotifyTrackLayoutChange(tile, AxisToTrack(axis)); YapfNotifyTrackLayoutChange(tile, AxisToTrack(axis));
wp_auto_delete.Detach();
} }
return CommandCost(_price.build_train_depot); 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 * Fix savegames which stored waypoints in their old format
*/ */
@ -463,8 +443,8 @@ void FixOldWaypoints()
void InitializeWaypoints() void InitializeWaypoints()
{ {
CleanPool(&_Waypoint_pool); _Waypoint_pool.CleanPool();
AddBlockToPool(&_Waypoint_pool); _Waypoint_pool.AddBlockToPool();
} }
static const SaveLoad _waypoint_desc[] = { static const SaveLoad _waypoint_desc[] = {
@ -498,12 +478,7 @@ static void Load_WAYP()
int index; int index;
while ((index = SlIterateArray()) != -1) { while ((index = SlIterateArray()) != -1) {
Waypoint *wp; Waypoint *wp = new (index) Waypoint();
if (!AddBlockIfNeeded(&_Waypoint_pool, index))
error("Waypoints: failed loading savegame: too many waypoints");
wp = GetWaypoint(index);
SlObject(wp, _waypoint_desc); SlObject(wp, _waypoint_desc);
} }
} }

View File

@ -8,9 +8,11 @@
#include "oldpool.h" #include "oldpool.h"
#include "rail_map.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 TileIndex xy; ///< Tile of waypoint
WaypointID index; ///< Index of waypoint
TownID town_index; ///< Town associated with the waypoint TownID town_index; ///< Town associated with the waypoint
byte town_cn; ///< The Nth waypoint for this town (consecutive number) 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 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. byte deleted; ///< Delete counter. If greater than 0 then it is decremented until it reaches 0; the waypoint is then is deleted.
Waypoint(TileIndex tile = 0);
~Waypoint();
void QuickFree();
bool IsValid() const;
}; };
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;
}
static inline bool IsValidWaypointID(WaypointID index) 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) static inline void DeleteWaypoint(Waypoint *wp)
{ {
DestroyWaypoint(wp); wp->~Waypoint();
wp->xy = 0;
} }
#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) #define FOR_ALL_WAYPOINTS(wp) FOR_ALL_WAYPOINTS_FROM(wp, 0)