OpenTTD Source: StationCargoList Class Reference

OpenTTD Source 15.0-RC2

Public Member Functions | Static Public Member Functions | Protected Types | Protected Attributes | Friends
StationCargoList Class Reference

CargoList that is used for stations. More...

`#include <cargopacket.h>`

Inheritance diagram for StationCargoList:

## Public Member Functions
template<class Taction >
bool | ShiftCargo (Taction &action, StationID next)
| Shifts cargo from the front of the packet list for a specific station and applies some action to it.
template<class Taction >
uint | ShiftCargo (Taction action, std::span< const StationID > next, bool include_invalid)
| Shifts cargo from the front of the packet list for a specific station and and optional also from the list for "any station", then applies some action to it.
void | Append (CargoPacket *cp, StationID next)
| Appends the given cargo packet to the range of packets with the same next station.
bool | HasCargoFor (std::span< const StationID > next) const
| Check for cargo headed for a specific station.
StationID | GetFirstStation () const
| Returns first station of the first cargo packet in this list.
uint | AvailableCount () const
| Returns sum of cargo still available for loading at the station.
uint | ReservedCount () const
| Returns sum of cargo reserved for loading onto vehicles.
uint | TotalCount () const
| Returns total count of cargo at the station, including cargo which is already reserved for loading.
uint | Reserve (uint max_move, VehicleCargoList *dest, std::span< const StationID > next, TileIndex current_tile)
| Reserves cargo for loading onto the vehicle.
uint | Load (uint max_move, VehicleCargoList *dest, std::span< const StationID > next, TileIndex current_tile)
| Loads cargo onto a vehicle.
uint | Truncate (uint max_move=UINT_MAX, StationCargoAmountMap *cargo_per_source=nullptr)
| Truncates where each destination loses roughly the same percentage of its cargo.
uint | Reroute (uint max_move, StationCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge)
| Routes packets with station "avoid" as next hop to a different place.
Public Member Functions inherited from CargoList< StationCargoList, StationCargoPacketMap >
| CargoList ()
| Create the cargo list.
| ~CargoList ()
| Destroy the cargolist ("frees" all cargo packets).
void | OnCleanPool ()
| Empty the cargo list, but don't free the cargo packets; the cargo packets are cleaned by CargoPacket's CleanPool.
const StationCargoPacketMap * | Packets () const
| Returns a pointer to the cargo packet list (so you can iterate over it etc).
uint | PeriodsInTransit () const
| Returns average number of cargo aging periods in transit for a cargo entity.
void | InvalidateCache ()
| Invalidates the cached data and rebuilds it.

## Static Public Member Functions
static void | InvalidateAllFrom (Source src)
static bool | AreMergable (const CargoPacket *cp1, const CargoPacket *cp2)
| Are the two CargoPackets mergeable in the context of a list of CargoPackets for a Station?

## Protected Types
typedef CargoList< StationCargoList, StationCargoPacketMap > | Parent
| The (direct) parent of this class.

## Protected Attributes
uint | reserved_count
| Amount of cargo being reserved for loading.
Protected Attributes inherited from CargoList< StationCargoList, StationCargoPacketMap >
uint | count
| Cache for the number of cargo entities.
uint64_t | cargo_periods_in_transit
| Cache for the sum of number of cargo aging periods in transit of each entity; comparable to man-hours.
StationCargoPacketMap | packets
| The cargo packets in this list.

## Friends
class | CargoList< StationCargoList, StationCargoPacketMap >
| The super class ought to know what it's doing.
class | SlStationGoods
class | CargoLoad
class | CargoTransfer
template<class Tsource >
class | CargoRemoval
class | CargoReservation
class | CargoReturn
class | StationCargoReroute

## Additional Inherited Members
Public Types inherited from CargoList< StationCargoList, StationCargoPacketMap >
enum | MoveToAction
| Kind of actions that could be done with packets on move. More...
typedef Tcont::iterator | Iterator
| The iterator for our container.
typedef Tcont::reverse_iterator | ReverseIterator
| The reverse iterator for our container.
typedef Tcont::const_iterator | ConstIterator
| The const iterator for our container.
typedef Tcont::const_reverse_iterator | ConstReverseIterator
| The const reverse iterator for our container.
Protected Member Functions inherited from CargoList< StationCargoList, StationCargoPacketMap >
void | AddToCache (const CargoPacket *cp)
| Update the cache to reflect adding of this packet.
void | RemoveFromCache (const CargoPacket *cp, uint count)
| Update the cached values to reflect the removal of this packet or part of it.
Static Protected Member Functions inherited from CargoList< StationCargoList, StationCargoPacketMap >
static bool | TryMerge (CargoPacket *cp, CargoPacket *icp)
| Tries to merge the second packet into the first and return if that was successful.

## Detailed Description

CargoList that is used for stations.

Definition at line 519 of file cargopacket.h.

## Member Typedef Documentation

## ◆ Parent

typedef CargoList<StationCargoList, StationCargoPacketMap> StationCargoList::Parent
| protected

The (direct) parent of this class.

Definition at line 522 of file cargopacket.h.

## Member Function Documentation

## ◆ Append()

void StationCargoList::Append | ( | CargoPacket * | cp,
|  | StationID | next
| ) |  |

Appends the given cargo packet to the range of packets with the same next station.

Warning
  After appending this packet may not exist anymore!

Note
  Do not use the cargo packet anymore after it has been appended to this CargoList!

Parameters

  next | the next hop
  cp | the cargo packet to add

Precondition
  cp != nullptr

Definition at line 684 of file cargopacket.cpp.

References CargoList< StationCargoList, StationCargoPacketMap >::AddToCache(), CargoList< StationCargoList, StationCargoPacketMap >::packets, and CargoList< StationCargoList, StationCargoPacketMap >::TryMerge().

Referenced by CargoTransfer::operator()(), and CargoReturn::operator()().

## ◆ AreMergable()

static bool StationCargoList::AreMergable | ( | const CargoPacket * | cp1,
|  | const CargoPacket * | cp2
| ) |  |
| inlinestatic

Are the two CargoPackets mergeable in the context of a list of CargoPackets for a Station?

Parameters

  cp1 | First CargoPacket.
  cp2 | Second CargoPacket.

Returns
  True if they are mergeable.

Definition at line 618 of file cargopacket.h.

References CargoPacket::first_station, CargoPacket::periods_in_transit, CargoPacket::source, and CargoPacket::source_xy.

## ◆ AvailableCount()

uint StationCargoList::AvailableCount | ( |  | ) | const
| inline

Returns sum of cargo still available for loading at the station.

(i.e. not counting cargo which is already reserved for loading)

Returns
  Cargo on board the vehicle.

Definition at line 578 of file cargopacket.h.

References CargoList< StationCargoList, StationCargoPacketMap >::count.

Referenced by LoadUnloadVehicle(), and UpdateStationRating().

## ◆ GetFirstStation()

StationID StationCargoList::GetFirstStation | ( |  | ) | const
| inline

Returns first station of the first cargo packet in this list.

Returns
  The before mentioned station.

Definition at line 568 of file cargopacket.h.

References CargoList< StationCargoList, StationCargoPacketMap >::count, and CargoList< StationCargoList, StationCargoPacketMap >::packets.

## ◆ HasCargoFor()

bool StationCargoList::HasCargoFor | ( | std::span< const StationID > | next | ) | const
| inline

Check for cargo headed for a specific station.

Parameters

  next | Station the cargo is headed for.

Returns
  If there is any cargo for that station.

Definition at line 555 of file cargopacket.h.

References CargoList< StationCargoList, StationCargoPacketMap >::packets.

## ◆ Load()

uint StationCargoList::Load | ( | uint | max_move,
|  | VehicleCargoList * | dest,
|  | std::span< const StationID > | next_station,
|  | TileIndex | current_tile
| ) |  |

Loads cargo onto a vehicle.

If the vehicle has reserved cargo load that. Otherwise load cargo from the station.

Parameters

  max_move | Amount of cargo to load.
  dest | Vehicle cargo list where the cargo resides.
  next_station | Next station(s) the loading vehicle will visit.
  current_tile | Current tile the cargo handling is happening on.

Returns
  Amount of cargo actually loaded.

Note
  Vehicles may or may not reserve, depending on their orders. The two modes of loading are exclusive, though. If cargo is reserved we don't need to load unreserved cargo.

Definition at line 834 of file cargopacket.cpp.

References VehicleCargoList::ActionCount(), CargoList< VehicleCargoList, CargoPacketList >::MTA_KEEP, CargoList< VehicleCargoList, CargoPacketList >::MTA_LOAD, VehicleCargoList::Reassign(), reserved_count, and ShiftCargo().

Referenced by LoadUnloadVehicle().

## ◆ Reroute()

uint StationCargoList::Reroute | ( | uint | max_move,
|  | StationCargoList * | dest,
|  | StationID | avoid,
|  | StationID | avoid2,
|  | const GoodsEntry * | ge
| ) |  |

Routes packets with station "avoid" as next hop to a different place.

Parameters

  max_move | Maximum amount of cargo to move.
  dest | List to append the cargo to.
  avoid | Station to exclude from routing and current next hop of packets to reroute.
  avoid2 | Additional station to exclude from routing.
  ge | GoodsEntry to get the routing info from.

Definition at line 854 of file cargopacket.cpp.

References ShiftCargo().

Referenced by RerouteCargo().

## ◆ Reserve()

uint StationCargoList::Reserve | ( | uint | max_move,
|  | VehicleCargoList * | dest,
|  | std::span< const StationID > | next_station,
|  | TileIndex | current_tile
| ) |  |

Reserves cargo for loading onto the vehicle.

Parameters

  max_move | Maximum amount of cargo to reserve.
  dest | VehicleCargoList to reserve for.
  next_station | Next station(s) the loading vehicle will visit.
  current_tile | Current tile the cargo handling is happening on.

Returns
  Amount of cargo actually reserved.

Definition at line 817 of file cargopacket.cpp.

References ShiftCargo().

## ◆ ReservedCount()

uint StationCargoList::ReservedCount | ( |  | ) | const
| inline

Returns sum of cargo reserved for loading onto vehicles.

Returns
  Cargo reserved for loading.

Definition at line 587 of file cargopacket.h.

References reserved_count.

Referenced by StationViewWindow::BuildCargoList(), and StationViewWindow::DrawEntries().

## ◆ ShiftCargo() [1/2]

template<class Taction >

bool StationCargoList::ShiftCargo | ( | Taction & | action,
|  | StationID | next
| ) |  |

Shifts cargo from the front of the packet list for a specific station and applies some action to it.

Template Parameters

  Taction | Action class or function to be used. It should define "bool operator()(CargoPacket *)". If true is returned the cargo packet will be removed from the list. Otherwise it will be kept and the loop will be aborted.

Parameters

  action | Action instance to be applied.
  next | Next hop the cargo wants to visit.

Returns
  True if all packets with the given next hop have been removed, False otherwise.

Definition at line 712 of file cargopacket.cpp.

References MultiMap< Tkey, Tvalue, Tcompare >::equal_range(), MultiMap< Tkey, Tvalue, Tcompare >::erase(), and CargoList< StationCargoList, StationCargoPacketMap >::packets.

Referenced by Load(), Reroute(), Reserve(), and ShiftCargo().

## ◆ ShiftCargo() [2/2]

template<class Taction >

uint StationCargoList::ShiftCargo | ( | Taction | action,
|  | std::span< const StationID > | next,
|  | bool | include_invalid
| ) |  |

Shifts cargo from the front of the packet list for a specific station and and optional also from the list for "any station", then applies some action to it.

Template Parameters

  Taction | Action class or function to be used. It should define "bool operator()(CargoPacket *)". If true is returned the cargo packet will be removed from the list. Otherwise it will be kept and the loop will be aborted.

Parameters

  action | Action instance to be applied.
  next | Next hop the cargo wants to visit.
  include_invalid | If cargo from the StationID::Invalid() list should be used if necessary.

Returns
  Amount of cargo actually moved.

Definition at line 742 of file cargopacket.cpp.

References ShiftCargo().

## ◆ TotalCount()

uint StationCargoList::TotalCount | ( |  | ) | const
| inline

Returns total count of cargo at the station, including cargo which is already reserved for loading.

Returns
  Total cargo count.

Definition at line 597 of file cargopacket.h.

References CargoList< StationCargoList, StationCargoPacketMap >::count, and reserved_count.

Referenced by CheckCaches(), LoadUnloadVehicle(), and StationResolverObject::ResolveReal().

## ◆ Truncate()

uint StationCargoList::Truncate | ( | uint | max_move = `UINT_MAX`,
|  | StationCargoAmountMap * | cargo_per_source = `nullptr`
| ) |  |

Truncates where each destination loses roughly the same percentage of its cargo.

This is done by randomizing the selection of packets to be removed. Optionally count the cargo by origin station.

Parameters

  max_move | Maximum amount of cargo to remove.
  cargo_per_source | Container for counting the cargo by origin.

Returns
  Amount of cargo actually moved.

Definition at line 763 of file cargopacket.cpp.

References CargoPacket::count, CargoList< StationCargoList, StationCargoPacketMap >::count, MultiMap< Tkey, Tvalue, Tcompare >::erase(), CargoPacket::first_station, CargoList< StationCargoList, StationCargoPacketMap >::packets, RandomRange(), CargoPacket::Reduce(), and CargoList< StationCargoList, StationCargoPacketMap >::RemoveFromCache().

Referenced by TruncateCargo().

## Friends And Related Symbol Documentation

## ◆ CargoList< StationCargoList, StationCargoPacketMap >

friend class CargoList< StationCargoList, StationCargoPacketMap >
| friend

The super class ought to know what it's doing.

Definition at line 524 of file cargopacket.h.

## ◆ CargoLoad

friend class CargoLoad
| friend

Definition at line 532 of file cargopacket.h.

## ◆ CargoRemoval

template<class Tsource >

friend class CargoRemoval
| friend

Definition at line 535 of file cargopacket.h.

## ◆ CargoReservation

friend class CargoReservation
| friend

Definition at line 536 of file cargopacket.h.

## ◆ CargoReturn

friend class CargoReturn
| friend

Definition at line 537 of file cargopacket.h.

## ◆ CargoTransfer

friend class CargoTransfer
| friend

Definition at line 533 of file cargopacket.h.

## ◆ SlStationGoods

friend class SlStationGoods
| friend

Definition at line 530 of file cargopacket.h.

## ◆ StationCargoReroute

friend class StationCargoReroute
| friend

Definition at line 538 of file cargopacket.h.

## Field Documentation

## ◆ reserved_count

uint StationCargoList::reserved_count
| protected

Amount of cargo being reserved for loading.

Definition at line 524 of file cargopacket.h.

Referenced by Load(), CargoReservation::operator()(), CargoReturn::operator()(), ReservedCount(), and TotalCount().

The documentation for this class was generated from the following files:

- src/cargopacket.h
- src/cargopacket.cpp