(svn r17800) -Codechange: first steps into making CargoList a template

pull/155/head
rubidium 15 years ago
parent aa75552957
commit e553983e39

@ -119,7 +119,7 @@ static void TransferCargo(Vehicle *old_veh, Vehicle *new_head, bool part_of_chai
uint amount = min(src->cargo.Count(), dest->cargo_cap - dest->cargo.Count());
if (amount <= 0) continue;
src->cargo.MoveTo(&dest->cargo, amount, CargoList::MTA_UNLOAD, NULL);
src->cargo.MoveTo(&dest->cargo, amount, VehicleCargoList::MTA_UNLOAD, NULL);
}
}

@ -68,7 +68,8 @@ CargoPacket::CargoPacket(uint16 count, byte days_in_transit, Money feeder_share,
*
*/
CargoList::~CargoList()
template <class Tinst>
CargoList<Tinst>::~CargoList()
{
while (!this->packets.empty()) {
delete this->packets.front();
@ -76,14 +77,16 @@ CargoList::~CargoList()
}
}
void CargoList::RemoveFromCache(const CargoPacket *cp)
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;
}
void CargoList::AddToCache(const CargoPacket *cp)
template <class Tinst>
void CargoList<Tinst>::AddToCache(const CargoPacket *cp)
{
this->count += cp->count;
this->feeder_share += cp->feeder_share;
@ -101,7 +104,8 @@ void VehicleCargoList::AgeCargo()
}
}
void CargoList::Append(CargoPacket *cp)
template <class Tinst>
void CargoList<Tinst>::Append(CargoPacket *cp)
{
assert(cp != NULL);
@ -123,7 +127,8 @@ void CargoList::Append(CargoPacket *cp)
}
void CargoList::Truncate(uint max_remaining)
template <class Tinst>
void CargoList<Tinst>::Truncate(uint max_remaining)
{
for (List::iterator it = packets.begin(); it != packets.end(); /* done during loop*/) {
CargoPacket *cp = *it;
@ -149,7 +154,9 @@ void CargoList::Truncate(uint max_remaining)
}
}
bool CargoList::MoveTo(CargoList *dest, uint max_move, CargoList::MoveToAction mta, CargoPayment *payment, uint data)
template <class Tinst>
template <class Tother_inst>
bool CargoList<Tinst>::MoveTo(Tother_inst *dest, uint max_move, CargoList::MoveToAction mta, CargoPayment *payment, uint data)
{
assert(mta == MTA_FINAL_DELIVERY || dest != NULL);
assert(mta == MTA_UNLOAD || mta == MTA_CARGO_LOAD || payment != NULL);
@ -228,7 +235,8 @@ bool CargoList::MoveTo(CargoList *dest, uint max_move, CargoList::MoveToAction m
return it != packets.end();
}
void CargoList::InvalidateCache()
template <class Tinst>
void CargoList<Tinst>::InvalidateCache()
{
this->count = 0;
this->feeder_share = 0;
@ -238,3 +246,16 @@ void CargoList::InvalidateCache()
this->AddToCache(*it);
}
}
/*
* We have to instantiate everything we want to be usable.
*/
template class CargoList<VehicleCargoList>;
template class CargoList<StationCargoList>;
/** Autoreplace Vehicle -> Vehicle 'transfer' */
template bool CargoList<VehicleCargoList>::MoveTo(VehicleCargoList *, uint max_move, MoveToAction mta, CargoPayment *payment, uint data);
/** Cargo unloading at a station */
template bool CargoList<VehicleCargoList>::MoveTo(StationCargoList *, uint max_move, MoveToAction mta, CargoPayment *payment, uint data);
/** Cargo loading at a station */
template bool CargoList<StationCargoList>::MoveTo(VehicleCargoList *, uint max_move, MoveToAction mta, CargoPayment *payment, uint data);

@ -29,7 +29,7 @@ typedef Pool<CargoPacket, CargoPacketID, 1024, 1048576> CargoPacketPool;
/** The actual pool with cargo packets */
extern CargoPacketPool _cargopacket_pool;
class CargoList;
template <class Tinst> class CargoList;
extern const struct SaveLoad *GetCargoPacketDesc();
/**
@ -44,7 +44,7 @@ private:
byte days_in_transit; ///< Amount of days this packet has been in transit
/** The CargoList caches, thus needs to know about it. */
friend class CargoList;
template <class Tinst> friend class CargoList;
friend class VehicleCargoList;
friend class StationCargoList;
/** We want this to be saved, right? */
@ -147,7 +147,9 @@ public:
/**
* Simple collection class for a list of cargo packets
* @tparam Tinst The actual instantation of this cargo list
*/
template <class Tinst>
class CargoList {
public:
/** List of cargo packets */
@ -192,16 +194,11 @@ public:
* Returns a pointer to the cargo packet list (so you can iterate over it etc).
* @return pointer to the packet list
*/
FORCEINLINE const CargoList::List *Packets() const
FORCEINLINE const List *Packets() const
{
return &this->packets;
}
/**
* Ages the all cargo in this list
*/
void AgeCargo();
/**
* Checks whether this list is empty
* @return true if and only if the list is empty
@ -285,7 +282,8 @@ public:
* @pre mta == MTA_UNLOAD || mta == MTA_CARGO_LOAD || payment != NULL
* @return true if there are still packets that might be moved from this cargo list
*/
bool MoveTo(CargoList *dest, uint count, CargoList::MoveToAction mta, CargoPayment *payment, uint data = 0);
template <class Tother_inst>
bool MoveTo(Tother_inst *dest, uint count, CargoList::MoveToAction mta, CargoPayment *payment, uint data = 0);
/** Invalidates the cached data and rebuild it */
void InvalidateCache();
@ -294,7 +292,7 @@ public:
/**
* CargoList that is used for vehicles.
*/
class VehicleCargoList : public CargoList {
class VehicleCargoList : public CargoList<VehicleCargoList> {
public:
/** The vehicles have a cargo list (and we want that saved). */
friend const struct SaveLoad *GetVehicleDescription(VehicleType vt);
@ -308,7 +306,7 @@ public:
/**
* CargoList that is used for stations.
*/
class StationCargoList : public CargoList {
class StationCargoList : public CargoList<StationCargoList> {
public:
/** The stations, via GoodsEntry, have a CargoList. */
friend const struct SaveLoad *GetGoodsDesc();

@ -1162,7 +1162,7 @@ static void LoadUnloadVehicle(Vehicle *v, int *cargo_left)
if (HasBit(ge->acceptance_pickup, GoodsEntry::ACCEPTANCE) && !(u->current_order.GetUnloadType() & OUFB_TRANSFER)) {
/* The cargo has reached it's final destination, the packets may now be destroyed */
remaining = v->cargo.MoveTo(NULL, amount_unloaded, CargoList::MTA_FINAL_DELIVERY, payment, last_visited);
remaining = v->cargo.MoveTo<StationCargoList>(NULL, amount_unloaded, VehicleCargoList::MTA_FINAL_DELIVERY, payment, last_visited);
result |= 1;
accepted = true;
@ -1174,7 +1174,7 @@ static void LoadUnloadVehicle(Vehicle *v, int *cargo_left)
* station is still accepting the cargo in the vehicle. It doesn't
* accept cargo that was loaded at the same station. */
if ((u->current_order.GetUnloadType() & (OUFB_UNLOAD | OUFB_TRANSFER)) && (!accepted || v->cargo.Count() == cargo_count)) {
remaining = v->cargo.MoveTo(&ge->cargo, amount_unloaded, u->current_order.GetUnloadType() & OUFB_TRANSFER ? CargoList::MTA_TRANSFER : CargoList::MTA_UNLOAD, payment);
remaining = v->cargo.MoveTo(&ge->cargo, amount_unloaded, u->current_order.GetUnloadType() & OUFB_TRANSFER ? VehicleCargoList::MTA_TRANSFER : VehicleCargoList::MTA_UNLOAD, payment);
SetBit(ge->acceptance_pickup, GoodsEntry::PICKUP);
result |= 2;
@ -1259,7 +1259,7 @@ static void LoadUnloadVehicle(Vehicle *v, int *cargo_left)
completely_emptied = false;
anything_loaded = true;
ge->cargo.MoveTo(&v->cargo, cap, CargoList::MTA_CARGO_LOAD, NULL, st->xy);
ge->cargo.MoveTo(&v->cargo, cap, StationCargoList::MTA_CARGO_LOAD, NULL, st->xy);
st->time_since_load = 0;
st->last_vehicle_type = v->type;

@ -1235,8 +1235,8 @@ bool AfterLoadGame()
* to the current tile of the vehicle to prevent excessive profits
*/
FOR_ALL_VEHICLES(v) {
const CargoList::List *packets = v->cargo.Packets();
for (CargoList::List::const_iterator it = packets->begin(); it != packets->end(); it++) {
const VehicleCargoList::List *packets = v->cargo.Packets();
for (VehicleCargoList::List::const_iterator it = packets->begin(); it != packets->end(); it++) {
CargoPacket *cp = *it;
cp->source_xy = Station::IsValidID(cp->source) ? Station::Get(cp->source)->xy : v->tile;
cp->loaded_at_xy = cp->source_xy;
@ -1254,8 +1254,8 @@ bool AfterLoadGame()
for (CargoID c = 0; c < NUM_CARGO; c++) {
GoodsEntry *ge = &st->goods[c];
const CargoList::List *packets = ge->cargo.Packets();
for (CargoList::List::const_iterator it = packets->begin(); it != packets->end(); it++) {
const StationCargoList::List *packets = ge->cargo.Packets();
for (StationCargoList::List::const_iterator it = packets->begin(); it != packets->end(); it++) {
CargoPacket *cp = *it;
cp->source_xy = Station::IsValidID(cp->source) ? Station::Get(cp->source)->xy : st->xy;
cp->loaded_at_xy = cp->source_xy;

@ -824,8 +824,8 @@ struct StationViewWindow : public Window {
this->cargo_rows[i] = (uint16)cargolist.size();
/* Add an entry for each distinct cargo source. */
const CargoList::List *packets = st->goods[i].cargo.Packets();
for (CargoList::List::const_iterator it = packets->begin(); it != packets->end(); it++) {
const StationCargoList::List *packets = st->goods[i].cargo.Packets();
for (StationCargoList::List::const_iterator it = packets->begin(); it != packets->end(); it++) {
const CargoPacket *cp = *it;
if (cp->source != station_id) {
bool added = false;

Loading…
Cancel
Save