summaryrefslogtreecommitdiff
path: root/src/station.cpp
diff options
context:
space:
mode:
authorcelestar <celestar@openttd.org>2007-01-17 11:15:51 +0000
committercelestar <celestar@openttd.org>2007-01-17 11:15:51 +0000
commit8c547930686ae662b26dbe93362c05e6e4227ac1 (patch)
tree3335268e36d18ec8067bc2aac90872caadf1709a /src/station.cpp
parenta973e457193b41afbfaa468be1c2fe22c484c845 (diff)
downloadopenttd-8c547930686ae662b26dbe93362c05e6e4227ac1.tar.xz
(svn r8185) -Codechange: Equipped Roadstops with new/delete operators and gave them proper constructors/destructors (Thanks to KUDr for a nice interactive C++ lesson)
Diffstat (limited to 'src/station.cpp')
-rw-r--r--src/station.cpp97
1 files changed, 97 insertions, 0 deletions
diff --git a/src/station.cpp b/src/station.cpp
index 69f34f20f..e23439eb0 100644
--- a/src/station.cpp
+++ b/src/station.cpp
@@ -312,3 +312,100 @@ StationRect& StationRect::operator = (Rect src)
return *this;
}
+
+/************************************************************************/
+/* RoadStop implementation */
+/************************************************************************/
+
+/** Allocates a new RoadStop onto the pool, or recycles an unsed one
+ * @return a pointer to the new roadstop
+ */
+void *RoadStop::operator new(size_t size)
+{
+ RoadStop *rs = AllocateRaw();
+ return rs;
+}
+
+/** Gets a RoadStop with a given index and allocates it when needed
+ * @return a pointer to the roadstop
+ */
+void *RoadStop::operator new(size_t size, int index)
+{
+ if (!AddBlockIfNeeded(&_RoadStop_pool, index)) {
+ error("RoadStops: failed loading savegame: too many RoadStops");
+ }
+
+ RoadStop *rs = GetRoadStop(index);
+ return rs;
+}
+
+void RoadStop::operator delete(void *p)
+{
+}
+
+void RoadStop::operator delete(void *p, int index)
+{
+}
+
+/** Initializes a RoadStop */
+RoadStop::RoadStop(TileIndex tile, StationID index)
+{
+ DEBUG(ms, cDebugCtorLevel, "I+%3d at %d[0x%x]", index, tile, tile);
+ xy = tile;
+ used = true;
+ status = 3; //stop is free
+ next = NULL;
+ prev = NULL;
+ station = index;
+ num_vehicles = 0;
+}
+
+/** De-Initializes a RoadStops. This includes clearing all slots that vehicles might
+ * have and unlinks it from the linked list of road stops at the given station
+ */
+RoadStop::~RoadStop()
+{
+ Vehicle *v;
+
+ /* Clear the slot assignment of all vehicles heading for this road stop */
+ if (num_vehicles != 0) {
+ FOR_ALL_VEHICLES(v) {
+ if (v->type == VEH_Road && v->u.road.slot == this) ClearSlot(v);
+ }
+ }
+ assert(num_vehicles == 0);
+
+ if (prev != NULL) prev->next = next;
+ if (next != NULL) next->prev = prev;
+
+ used = false;
+ DEBUG(ms, cDebugCtorLevel , "I- at %3d%d[0x%x]", station, xy, xy);
+
+ xy = INVALID_TILE;
+ station = INVALID_STATION;
+}
+
+
+/** Low-level function for allocating a RoadStop on the pool */
+RoadStop *RoadStop::AllocateRaw( void )
+{
+ RoadStop *rs;
+
+ /* 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 (rs = GetRoadStop(0); rs != NULL; rs = (rs->index + 1U < GetRoadStopPoolSize()) ? GetRoadStop(rs->index + 1U) : NULL) {
+ if (!IsValidRoadStop(rs)) {
+ RoadStopID index = rs->index;
+
+ memset(rs, 0, sizeof(*rs));
+ rs->index = index;
+
+ return rs;
+ }
+ }
+
+ /* Check if we can add a block to the pool */
+ if (AddBlockToPool(&_RoadStop_pool)) return AllocateRaw();
+
+ return NULL;
+}