Changeset - r27780:26377b959bf0
[Not reviewed]
master
0 2 0
Patric Stout - 10 months ago 2023-08-11 12:55:55
truebrain@openttd.org
Codechange: simplify the type of loaded_at_xy / next_station (#11182)

It was TileOrStationID, most likely to make sure both types in
the union are identical. But as TileIndex is a StrongTypeDef
that becomes a bit weird. So instead, still make sure they are
of equal size, but define their individual types better.
2 files changed with 13 insertions and 8 deletions:
0 comments (0 inline, 0 general)
src/cargopacket.cpp
Show inline comments
 
@@ -75,7 +75,7 @@ CargoPacket::CargoPacket(uint16_t count,
 
		source_id(source_id),
 
		source(source),
 
		source_xy(source_xy),
 
		loaded_at_xy(loaded_at_xy.value)
 
		loaded_at_xy(loaded_at_xy)
 
{
 
	assert(count != 0);
 
	this->source_type = source_type;
 
@@ -553,7 +553,7 @@ void VehicleCargoList::InvalidateCache()
 
 * @return Amount of cargo actually reassigned.
 
 */
 
template<VehicleCargoList::MoveToAction Tfrom, VehicleCargoList::MoveToAction Tto>
 
uint VehicleCargoList::Reassign(uint max_move, TileOrStationID)
 
uint VehicleCargoList::Reassign(uint max_move, StationID)
 
{
 
	static_assert(Tfrom != MTA_TRANSFER && Tto != MTA_TRANSFER);
 
	static_assert(Tfrom - Tto == 1 || Tto - Tfrom == 1);
 
@@ -571,7 +571,7 @@ uint VehicleCargoList::Reassign(uint max
 
 * @return Amount of cargo actually reassigned.
 
 */
 
template<>
 
uint VehicleCargoList::Reassign<VehicleCargoList::MTA_DELIVER, VehicleCargoList::MTA_TRANSFER>(uint max_move, TileOrStationID next_station)
 
uint VehicleCargoList::Reassign<VehicleCargoList::MTA_DELIVER, VehicleCargoList::MTA_TRANSFER>(uint max_move, StationID next_station)
 
{
 
	max_move = std::min(this->action_counts[MTA_DELIVER], max_move);
 

	
 
@@ -866,4 +866,4 @@ uint StationCargoList::Reroute(uint max_
 
 */
 
template class CargoList<VehicleCargoList, CargoPacketList>;
 
template class CargoList<StationCargoList, StationCargoPacketMap>;
 
template uint VehicleCargoList::Reassign<VehicleCargoList::MTA_DELIVER, VehicleCargoList::MTA_KEEP>(uint, TileOrStationID);
 
template uint VehicleCargoList::Reassign<VehicleCargoList::MTA_DELIVER, VehicleCargoList::MTA_KEEP>(uint, StationID);
src/cargopacket.h
Show inline comments
 
@@ -34,7 +34,12 @@ template <class Tinst, class Tcont> clas
 
class StationCargoList; // forward-declare, so we can use it in VehicleCargoList.
 
extern SaveLoadTable GetCargoPacketDesc();
 

	
 
typedef uint32_t TileOrStationID;
 
/**
 
 * To make alignment in the union in CargoPacket a bit easier, create a new type
 
 * that is a StationID, but stored as 32bit.
 
 */
 
typedef uint32_t StationID_32bit;
 
static_assert(sizeof(TileIndex) == sizeof(StationID_32bit));
 

	
 
/**
 
 * Container for cargo from the same location and time.
 
@@ -49,8 +54,8 @@ private:
 
	StationID source;       ///< The station where the cargo came from first.
 
	TileIndex source_xy;    ///< The origin of the cargo (first station in feeder chain).
 
	union {
 
		TileOrStationID loaded_at_xy; ///< Location where this cargo has been loaded into the vehicle.
 
		TileOrStationID next_station; ///< Station where the cargo wants to go next.
 
		TileIndex loaded_at_xy;       ///< Location where this cargo has been loaded into the vehicle.
 
		StationID_32bit next_station; ///< Station where the cargo wants to go next.
 
	};
 

	
 
	/** The CargoList caches, thus needs to know about it. */
 
@@ -416,7 +421,7 @@ public:
 
	 * applicable), return value is amount of cargo actually moved. */
 

	
 
	template<MoveToAction Tfrom, MoveToAction Tto>
 
	uint Reassign(uint max_move, TileOrStationID update = INVALID_TILE);
 
	uint Reassign(uint max_move, StationID update = INVALID_STATION);
 
	uint Return(uint max_move, StationCargoList *dest, StationID next_station);
 
	uint Unload(uint max_move, StationCargoList *dest, CargoPayment *payment);
 
	uint Shift(uint max_move, VehicleCargoList *dest);
0 comments (0 inline, 0 general)