(svn r11002) -Codechange: unhackify the cargo packet list saving (a little).

This commit is contained in:
rubidium 2007-08-30 12:10:32 +00:00
parent a407d53d1b
commit ac5fde61fb
3 changed files with 24 additions and 45 deletions

View File

@ -16,9 +16,6 @@ void InitializeCargoPackets()
/* 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 @@ extern const ChunkHandler _cargopacket_chunk_handlers[] = {
*
*/
/* static */ void CargoList::AssertOnWrongPacketOffset()
{
CargoList cl;
if ((void*)&cl != (void*)cl.Packets()) NOT_REACHED();
}
CargoList::~CargoList()
{
while (!packets.empty()) {

View File

@ -67,6 +67,8 @@ struct CargoPacket : PoolItem<CargoPacket, CargoPacketID, &_CargoPacket_pool> {
*/
#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 @@ private:
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(); }

View File

@ -2977,25 +2977,6 @@ static uint32 _cargo_source_xy;
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 const SaveLoad _station_speclist_desc[] = {
};
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;