summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cargopacket.cpp67
-rw-r--r--src/cargopacket.h46
2 files changed, 79 insertions, 34 deletions
diff --git a/src/cargopacket.cpp b/src/cargopacket.cpp
index 6c93fc1c9..08c504bc9 100644
--- a/src/cargopacket.cpp
+++ b/src/cargopacket.cpp
@@ -96,7 +96,6 @@ template <class Tinst>
void CargoList<Tinst>::RemoveFromCache(const CargoPacket *cp)
{
this->count -= cp->count;
- this->feeder_share -= cp->feeder_share;
this->cargo_days_in_transit -= cp->days_in_transit * cp->count;
}
@@ -104,21 +103,9 @@ template <class Tinst>
void CargoList<Tinst>::AddToCache(const CargoPacket *cp)
{
this->count += cp->count;
- this->feeder_share += cp->feeder_share;
this->cargo_days_in_transit += cp->days_in_transit * cp->count;
}
-void VehicleCargoList::AgeCargo()
-{
- for (List::const_iterator it = this->packets.begin(); it != this->packets.end(); it++) {
- /* If we're at the maximum, then we can't increase no more. */
- if ((*it)->days_in_transit == 0xFF) continue;
-
- (*it)->days_in_transit++;
- this->cargo_days_in_transit += (*it)->count;
- }
-}
-
template <class Tinst>
void CargoList<Tinst>::Append(CargoPacket *cp)
{
@@ -130,7 +117,7 @@ void CargoList<Tinst>::Append(CargoPacket *cp)
icp->count += cp->count;
icp->feeder_share += cp->feeder_share;
- this->AddToCache(cp);
+ static_cast<Tinst *>(this)->AddToCache(cp);
delete cp;
return;
}
@@ -138,7 +125,7 @@ void CargoList<Tinst>::Append(CargoPacket *cp)
/* The packet could not be merged with another one */
this->packets.push_back(cp);
- this->AddToCache(cp);
+ static_cast<Tinst *>(this)->AddToCache(cp);
}
@@ -150,7 +137,7 @@ void CargoList<Tinst>::Truncate(uint max_remaining)
if (max_remaining == 0) {
/* Nothing should remain, just remove the packets. */
packets.erase(it++);
- this->RemoveFromCache(cp);
+ static_cast<Tinst *>(this)->RemoveFromCache(cp);
delete cp;
continue;
}
@@ -189,7 +176,7 @@ bool CargoList<Tinst>::MoveTo(Tother_inst *dest, uint max_move, MoveToAction mta
/* Can move the complete packet */
max_move -= cp->count;
this->packets.erase(it++);
- this->RemoveFromCache(cp);
+ static_cast<Tinst *>(this)->RemoveFromCache(cp);
switch(mta) {
case MTA_FINAL_DELIVERY:
payment->PayFinalDelivery(cp, cp->count);
@@ -215,20 +202,24 @@ bool CargoList<Tinst>::MoveTo(Tother_inst *dest, uint max_move, MoveToAction mta
if (mta == MTA_FINAL_DELIVERY) {
/* Final delivery doesn't need package splitting. */
payment->PayFinalDelivery(cp, max_move);
- this->count -= max_move;
- this->cargo_days_in_transit -= max_move * cp->days_in_transit;
+
+ /* Remove the delivered data from the cache */
+ uint left = cp->count - max_move;
+ cp->count = max_move;
+ static_cast<Tinst *>(this)->RemoveFromCache(cp);
/* Final delivery payment pays the feeder share, so we have to
* reset that so it is not 'shown' twice for partial unloads. */
- this->feeder_share -= cp->feeder_share;
cp->feeder_share = 0;
+ cp->count = left;
} else {
/* But... the rest needs package splitting. */
Money fs = cp->feeder_share * max_move / static_cast<uint>(cp->count);
cp->feeder_share -= fs;
+ cp->count -= max_move;
CargoPacket *cp_new = new CargoPacket(max_move, cp->days_in_transit, cp->source, cp->source_xy, (mta == MTA_CARGO_LOAD) ? data : cp->loaded_at_xy, fs, cp->source_type, cp->source_id);
- this->RemoveFromCache(cp_new); // this reflects the changes in cp.
+ static_cast<Tinst *>(this)->RemoveFromCache(cp_new); // this reflects the changes in cp.
if (mta == MTA_TRANSFER) {
/* Add the feeder share before inserting in dest. */
@@ -237,7 +228,6 @@ bool CargoList<Tinst>::MoveTo(Tother_inst *dest, uint max_move, MoveToAction mta
dest->Append(cp_new);
}
- cp->count -= max_move;
max_move = 0;
}
@@ -249,14 +239,43 @@ template <class Tinst>
void CargoList<Tinst>::InvalidateCache()
{
this->count = 0;
- this->feeder_share = 0;
this->cargo_days_in_transit = 0;
for (List::const_iterator it = this->packets.begin(); it != this->packets.end(); it++) {
- this->AddToCache(*it);
+ static_cast<Tinst *>(this)->AddToCache(*it);
}
}
+
+void VehicleCargoList::RemoveFromCache(const CargoPacket *cp)
+{
+ this->feeder_share -= cp->feeder_share;
+ this->Parent::RemoveFromCache(cp);
+}
+
+void VehicleCargoList::AddToCache(const CargoPacket *cp)
+{
+ this->feeder_share += cp->feeder_share;
+ this->Parent::AddToCache(cp);
+}
+
+void VehicleCargoList::AgeCargo()
+{
+ for (List::const_iterator it = this->packets.begin(); it != this->packets.end(); it++) {
+ /* If we're at the maximum, then we can't increase no more. */
+ if ((*it)->days_in_transit == 0xFF) continue;
+
+ (*it)->days_in_transit++;
+ this->cargo_days_in_transit += (*it)->count;
+ }
+}
+
+void VehicleCargoList::InvalidateCache()
+{
+ this->feeder_share = 0;
+ this->Parent::InvalidateCache();
+}
+
/*
* We have to instantiate everything we want to be usable.
*/
diff --git a/src/cargopacket.h b/src/cargopacket.h
index 2d5c824e9..90081855e 100644
--- a/src/cargopacket.h
+++ b/src/cargopacket.h
@@ -197,7 +197,6 @@ public:
};
protected:
- Money feeder_share; ///< Cache for the feeder share
uint count; ///< Cache for the number of cargo entities
uint cargo_days_in_transit; ///< Cache for the sum of number of days in transit of each entity; comparable to man-hours
@@ -251,15 +250,6 @@ public:
}
/**
- * Returns total sum of the feeder share for all packets
- * @return the before mentioned number
- */
- FORCEINLINE Money FeederShare() const
- {
- return this->feeder_share;
- }
-
- /**
* Returns source of the first cargo packet in this list
* @return the before mentioned source
*/
@@ -326,15 +316,49 @@ public:
* CargoList that is used for vehicles.
*/
class VehicleCargoList : public CargoList<VehicleCargoList> {
+protected:
+ /** The (direct) parent of this class */
+ typedef CargoList<VehicleCargoList> Parent;
+
+ Money feeder_share; ///< Cache for the feeder share
+
+ /**
+ * Update the cache to reflect adding of this packet.
+ * Increases count, feeder share and days_in_transit
+ * @param cp a new packet to be inserted
+ */
+ void AddToCache(const CargoPacket *cp);
+
+ /**
+ * Update the cached values to reflect the removal of this packet.
+ * Decreases count, feeder share and days_in_transit
+ * @param cp Packet to be removed from cache
+ */
+ void RemoveFromCache(const CargoPacket *cp);
+
public:
+ /** The super class ought to know what it's doing */
+ friend class CargoList<VehicleCargoList>;
/** The vehicles have a cargo list (and we want that saved). */
friend const struct SaveLoad *GetVehicleDescription(VehicleType vt);
/**
+ * Returns total sum of the feeder share for all packets
+ * @return the before mentioned number
+ */
+ FORCEINLINE Money FeederShare() const
+ {
+ return this->feeder_share;
+ }
+
+ /**
* Ages the all cargo in this list
*/
void AgeCargo();
+ /** Invalidates the cached data and rebuild it */
+ void InvalidateCache();
+
/**
* Are two the two CargoPackets mergeable in the context of
* a list of CargoPackets for a Vehicle?
@@ -357,6 +381,8 @@ public:
*/
class StationCargoList : public CargoList<StationCargoList> {
public:
+ /** The super class ought to know what it's doing */
+ friend class CargoList<StationCargoList>;
/** The stations, via GoodsEntry, have a CargoList. */
friend const struct SaveLoad *GetGoodsDesc();