comparison src/station.cpp @ 5708:3dc3a5375999 draft

(svn r8185) -Codechange: Equipped Roadstops with new/delete operators and gave them proper constructors/destructors (Thanks to KUDr for a nice interactive C++ lesson)
author celestar <celestar@openttd.org>
date Wed, 17 Jan 2007 11:15:51 +0000
parents 3d8596aefdc3
children ce6d8f33f06f
comparison
equal deleted inserted replaced
5707:ec270c5719b1 5708:3dc3a5375999
310 right = src.right; 310 right = src.right;
311 bottom = src.bottom; 311 bottom = src.bottom;
312 return *this; 312 return *this;
313 } 313 }
314 314
315
316 /************************************************************************/
317 /* RoadStop implementation */
318 /************************************************************************/
319
320 /** Allocates a new RoadStop onto the pool, or recycles an unsed one
321 * @return a pointer to the new roadstop
322 */
323 void *RoadStop::operator new(size_t size)
324 {
325 RoadStop *rs = AllocateRaw();
326 return rs;
327 }
328
329 /** Gets a RoadStop with a given index and allocates it when needed
330 * @return a pointer to the roadstop
331 */
332 void *RoadStop::operator new(size_t size, int index)
333 {
334 if (!AddBlockIfNeeded(&_RoadStop_pool, index)) {
335 error("RoadStops: failed loading savegame: too many RoadStops");
336 }
337
338 RoadStop *rs = GetRoadStop(index);
339 return rs;
340 }
341
342 void RoadStop::operator delete(void *p)
343 {
344 }
345
346 void RoadStop::operator delete(void *p, int index)
347 {
348 }
349
350 /** Initializes a RoadStop */
351 RoadStop::RoadStop(TileIndex tile, StationID index)
352 {
353 DEBUG(ms, cDebugCtorLevel, "I+%3d at %d[0x%x]", index, tile, tile);
354 xy = tile;
355 used = true;
356 status = 3; //stop is free
357 next = NULL;
358 prev = NULL;
359 station = index;
360 num_vehicles = 0;
361 }
362
363 /** De-Initializes a RoadStops. This includes clearing all slots that vehicles might
364 * have and unlinks it from the linked list of road stops at the given station
365 */
366 RoadStop::~RoadStop()
367 {
368 Vehicle *v;
369
370 /* Clear the slot assignment of all vehicles heading for this road stop */
371 if (num_vehicles != 0) {
372 FOR_ALL_VEHICLES(v) {
373 if (v->type == VEH_Road && v->u.road.slot == this) ClearSlot(v);
374 }
375 }
376 assert(num_vehicles == 0);
377
378 if (prev != NULL) prev->next = next;
379 if (next != NULL) next->prev = prev;
380
381 used = false;
382 DEBUG(ms, cDebugCtorLevel , "I- at %3d%d[0x%x]", station, xy, xy);
383
384 xy = INVALID_TILE;
385 station = INVALID_STATION;
386 }
387
388
389 /** Low-level function for allocating a RoadStop on the pool */
390 RoadStop *RoadStop::AllocateRaw( void )
391 {
392 RoadStop *rs;
393
394 /* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
395 * TODO - This is just a temporary stage, this will be removed. */
396 for (rs = GetRoadStop(0); rs != NULL; rs = (rs->index + 1U < GetRoadStopPoolSize()) ? GetRoadStop(rs->index + 1U) : NULL) {
397 if (!IsValidRoadStop(rs)) {
398 RoadStopID index = rs->index;
399
400 memset(rs, 0, sizeof(*rs));
401 rs->index = index;
402
403 return rs;
404 }
405 }
406
407 /* Check if we can add a block to the pool */
408 if (AddBlockToPool(&_RoadStop_pool)) return AllocateRaw();
409
410 return NULL;
411 }