changeset 13291:07fc7c78b30b draft

(svn r17800) -Codechange: first steps into making CargoList a template
author rubidium <rubidium@openttd.org>
date Sun, 18 Oct 2009 14:28:26 +0000
parents a156d45d1208
children 83cbdcfbcf11
files src/autoreplace_cmd.cpp src/cargopacket.cpp src/cargopacket.h src/economy.cpp src/saveload/afterload.cpp src/station_gui.cpp
diffstat 6 files changed, 47 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/src/autoreplace_cmd.cpp
+++ b/src/autoreplace_cmd.cpp
@@ -119,7 +119,7 @@
 			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);
 		}
 	}
 
--- a/src/cargopacket.cpp
+++ b/src/cargopacket.cpp
@@ -68,7 +68,8 @@
  *
  */
 
-CargoList::~CargoList()
+template <class Tinst>
+CargoList<Tinst>::~CargoList()
 {
 	while (!this->packets.empty()) {
 		delete this->packets.front();
@@ -76,14 +77,16 @@
 	}
 }
 
-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 CargoList::Append(CargoPacket *cp)
+template <class Tinst>
+void CargoList<Tinst>::Append(CargoPacket *cp)
 {
 	assert(cp != NULL);
 
@@ -123,7 +127,8 @@
 }
 
 
-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 @@
 	}
 }
 
-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 @@
 	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 @@
 		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);
--- a/src/cargopacket.h
+++ b/src/cargopacket.h
@@ -29,7 +29,7 @@
 /** 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 @@
 	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 @@
 
 /**
  * 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,17 +194,12 @@
 	 * 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 @@
 	 * @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 @@
 /**
  * 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 @@
 /**
  * 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();
--- a/src/economy.cpp
+++ b/src/economy.cpp
@@ -1162,7 +1162,7 @@
 
 			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 @@
 			 * 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 @@
 			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;
--- a/src/saveload/afterload.cpp
+++ b/src/saveload/afterload.cpp
@@ -1235,8 +1235,8 @@
 		 * 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 @@
 			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;
--- a/src/station_gui.cpp
+++ b/src/station_gui.cpp
@@ -824,8 +824,8 @@
 				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;