(svn r11002) -Codechange: unhackify the cargo packet list saving (a little).
authorrubidium
Thu, 30 Aug 2007 12:10:32 +0000
changeset 7987 b3627df3493a
parent 7986 881998b115c2
child 7988 6075538f6111
(svn r11002) -Codechange: unhackify the cargo packet list saving (a little).
src/cargopacket.cpp
src/cargopacket.h
src/station_cmd.cpp
--- a/src/cargopacket.cpp	Wed Aug 29 21:49:08 2007 +0000
+++ b/src/cargopacket.cpp	Thu Aug 30 12:10:32 2007 +0000
@@ -16,9 +16,6 @@
 	/* Clean the cargo packet pool and create 1 block in it */
 	_CargoPacket_pool.CleanPool();
 	_CargoPacket_pool.AddBlockToPool();
-
-	/* Check whether our &cargolist == &cargolist.packets "hack" works */
-	CargoList::AssertOnWrongPacketOffset();
 }
 
 CargoPacket::CargoPacket(StationID source, uint16 count)
@@ -87,13 +84,6 @@
  *
  */
 
-/* static */ void CargoList::AssertOnWrongPacketOffset()
-{
-	CargoList cl;
-	if ((void*)&cl != (void*)cl.Packets()) NOT_REACHED();
-}
-
-
 CargoList::~CargoList()
 {
 	while (!packets.empty()) {
--- a/src/cargopacket.h	Wed Aug 29 21:49:08 2007 +0000
+++ b/src/cargopacket.h	Thu Aug 30 12:10:32 2007 +0000
@@ -67,6 +67,8 @@
  */
 #define FOR_ALL_CARGOPACKETS(cp) FOR_ALL_CARGOPACKETS_FROM(cp, 0)
 
+extern void SaveLoad_STNS(Station *st);
+
 /**
  * Simple collection class for a list of cargo packets
  */
@@ -93,21 +95,7 @@
 	uint days_in_transit; ///< Cache for the number of days in transit
 
 public:
-	/**
-	 * Needed for an ugly hack:
-	 *  - vehicles and stations need to store cargo lists, so they use CargoList as container
-	 *  - this internals of the container should be protected, e.g. private (or protected) by C++
-	 *  - for saving/loading we need to pass pointer to objects
-	 *  -> so *if* the pointer to the cargo list is the same as the pointer to the packet list
-	 *     encapsulated in the CargoList, we can just pass the CargoList as "offset".
-	 *     Normally we would then just add the offset of the packets variable within the cargo list
-	 *     but that is not possible because the variable is private. Furthermore we are not sure
-	 *     that this works on all platforms, we need to check whether the offset is actually 0.
-	 *     This cannot be done compile time, because the variable is private. So we need to write
-	 *     a function that does actually check the offset runtime and call it somewhere where it
-	 *     is always called but it should not be called often.
-	 */
-	static void AssertOnWrongPacketOffset();
+	friend void SaveLoad_STNS(Station *st);
 
 	/** Create the cargo list */
 	CargoList() { this->InvalidateCache(); }
--- a/src/station_cmd.cpp	Wed Aug 29 21:49:08 2007 +0000
+++ b/src/station_cmd.cpp	Thu Aug 30 12:10:32 2007 +0000
@@ -2977,25 +2977,6 @@
 static uint16 _cargo_days;
 static Money  _cargo_feeder_share;
 
-static const SaveLoad _goods_desc[] = {
-	SLEG_CONDVAR(            _waiting_acceptance, SLE_UINT16,                  0, 67),
-	 SLE_CONDVAR(GoodsEntry, acceptance_pickup,   SLE_UINT8,                  68, SL_MAX_VERSION),
-	SLE_CONDNULL(2,                                                           51, 67),
-	     SLE_VAR(GoodsEntry, days_since_pickup,   SLE_UINT8),
-	     SLE_VAR(GoodsEntry, rating,              SLE_UINT8),
-	SLEG_CONDVAR(            _cargo_source,       SLE_FILE_U8 | SLE_VAR_U16,   0, 6),
-	SLEG_CONDVAR(            _cargo_source,       SLE_UINT16,                  7, 67),
-	SLEG_CONDVAR(            _cargo_source_xy,    SLE_UINT32,                 44, 67),
-	SLEG_CONDVAR(            _cargo_days,         SLE_UINT8,                   0, 67),
-	     SLE_VAR(GoodsEntry, last_speed,          SLE_UINT8),
-	     SLE_VAR(GoodsEntry, last_age,            SLE_UINT8),
-	SLEG_CONDVAR(            _cargo_feeder_share, SLE_FILE_U32 | SLE_VAR_I64, 14, 64),
-	SLEG_CONDVAR(            _cargo_feeder_share, SLE_INT64,                  65, 67),
-	 SLE_CONDLST(GoodsEntry, cargo,               REF_CARGO_PACKET,           68, SL_MAX_VERSION),
-
-	SLE_END()
-};
-
 static const SaveLoad _station_speclist_desc[] = {
 	SLE_CONDVAR(StationSpecList, grfid,    SLE_UINT32, 27, SL_MAX_VERSION),
 	SLE_CONDVAR(StationSpecList, localidx, SLE_UINT8,  27, SL_MAX_VERSION),
@@ -3004,8 +2985,28 @@
 };
 
 
-static void SaveLoad_STNS(Station *st)
+void SaveLoad_STNS(Station *st)
 {
+	static const SaveLoad _goods_desc[] = {
+		SLEG_CONDVAR(            _waiting_acceptance, SLE_UINT16,                  0, 67),
+		 SLE_CONDVAR(GoodsEntry, acceptance_pickup,   SLE_UINT8,                  68, SL_MAX_VERSION),
+		SLE_CONDNULL(2,                                                           51, 67),
+		     SLE_VAR(GoodsEntry, days_since_pickup,   SLE_UINT8),
+		     SLE_VAR(GoodsEntry, rating,              SLE_UINT8),
+		SLEG_CONDVAR(            _cargo_source,       SLE_FILE_U8 | SLE_VAR_U16,   0, 6),
+		SLEG_CONDVAR(            _cargo_source,       SLE_UINT16,                  7, 67),
+		SLEG_CONDVAR(            _cargo_source_xy,    SLE_UINT32,                 44, 67),
+		SLEG_CONDVAR(            _cargo_days,         SLE_UINT8,                   0, 67),
+		     SLE_VAR(GoodsEntry, last_speed,          SLE_UINT8),
+		     SLE_VAR(GoodsEntry, last_age,            SLE_UINT8),
+		SLEG_CONDVAR(            _cargo_feeder_share, SLE_FILE_U32 | SLE_VAR_I64, 14, 64),
+		SLEG_CONDVAR(            _cargo_feeder_share, SLE_INT64,                  65, 67),
+		 SLE_CONDLST(GoodsEntry, cargo.packets,       REF_CARGO_PACKET,           68, SL_MAX_VERSION),
+
+		SLE_END()
+};
+
+
 	SlObject(st, _station_desc);
 
 	_waiting_acceptance = 0;