Changeset - r23965:e71e85950eb1
[Not reviewed]
master
0 8 0
glx - 5 years ago 2019-12-17 00:35:29
glx@openttd.org
Codechange: Replace order related FOR_ALL with range-based for loops
8 files changed with 27 insertions and 73 deletions:
0 comments (0 inline, 0 general)
src/order_backup.cpp
Show inline comments
 
@@ -99,14 +99,13 @@ void OrderBackup::DoRestore(Vehicle *v)
 
 * @note Will automatically remove any previous backups of this user.
 
 */
 
/* static */ void OrderBackup::Backup(const Vehicle *v, uint32 user)
 
{
 
	/* Don't use reset as that broadcasts over the network to reset the variable,
 
	 * which is what we are doing at the moment. */
 
	OrderBackup *ob;
 
	FOR_ALL_ORDER_BACKUPS(ob) {
 
	for (OrderBackup *ob : OrderBackup::Iterate()) {
 
		if (ob->user == user) delete ob;
 
	}
 
	if (OrderBackup::CanAllocateItem()) {
 
		new OrderBackup(v, user);
 
	}
 
}
 
@@ -116,14 +115,13 @@ void OrderBackup::DoRestore(Vehicle *v)
 
 * @param v    The vehicle to restore to.
 
 * @param user The user that built the vehicle, thus wants to restore.
 
 * @note After restoration the backup will automatically be removed.
 
 */
 
/* static */ void OrderBackup::Restore(Vehicle *v, uint32 user)
 
{
 
	OrderBackup *ob;
 
	FOR_ALL_ORDER_BACKUPS(ob) {
 
	for (OrderBackup *ob : OrderBackup::Iterate()) {
 
		if (v->tile != ob->tile || ob->user != user) continue;
 

	
 
		ob->DoRestore(v);
 
		delete ob;
 
	}
 
}
 
@@ -133,14 +131,13 @@ void OrderBackup::DoRestore(Vehicle *v)
 
 * @param tile The tile associated with the OrderBackup.
 
 * @param user The user associated with the OrderBackup.
 
 * @note Must not be used from the GUI!
 
 */
 
/* static */ void OrderBackup::ResetOfUser(TileIndex tile, uint32 user)
 
{
 
	OrderBackup *ob;
 
	FOR_ALL_ORDER_BACKUPS(ob) {
 
	for (OrderBackup *ob : OrderBackup::Iterate()) {
 
		if (ob->user == user && (ob->tile == tile || tile == INVALID_TILE)) delete ob;
 
	}
 
}
 

	
 
/**
 
 * Clear an OrderBackup
 
@@ -166,15 +163,14 @@ CommandCost CmdClearOrderBackup(TileInde
 
 * @note Must not be used from a command.
 
 */
 
/* static */ void OrderBackup::ResetUser(uint32 user)
 
{
 
	assert(_network_server);
 

	
 
	OrderBackup *ob;
 
	FOR_ALL_ORDER_BACKUPS(ob) {
 
		/* If it's not an backup of us, so ignore it. */
 
	for (OrderBackup *ob : OrderBackup::Iterate()) {
 
		/* If it's not a backup of us, ignore it. */
 
		if (ob->user != user) continue;
 

	
 
		DoCommandP(0, 0, user, CMD_CLEAR_ORDER_BACKUP);
 
		return;
 
	}
 
}
 
@@ -190,15 +186,14 @@ CommandCost CmdClearOrderBackup(TileInde
 
	/* The user has CLIENT_ID_SERVER as default when network play is not active,
 
	 * but compiled it. A network client has its own variable for the unique
 
	 * client/user identifier. Finally if networking isn't compiled in the
 
	 * default is just plain and simple: 0. */
 
	uint32 user = _networking && !_network_server ? _network_own_client_id : CLIENT_ID_SERVER;
 

	
 
	OrderBackup *ob;
 
	FOR_ALL_ORDER_BACKUPS(ob) {
 
		/* If it's not an backup of us, so ignore it. */
 
	for (OrderBackup *ob : OrderBackup::Iterate()) {
 
		/* If it's not a backup of us, ignore it. */
 
		if (ob->user != user) continue;
 
		/* If it's not for our chosen tile either, ignore it. */
 
		if (t != INVALID_TILE && t != ob->tile) continue;
 

	
 
		if (from_gui) {
 
			/* We need to circumvent the "prevention" from this command being executed
 
@@ -216,14 +211,13 @@ CommandCost CmdClearOrderBackup(TileInde
 
/**
 
 * Clear the group of all backups having this group ID.
 
 * @param group The group to clear.
 
 */
 
/* static */ void OrderBackup::ClearGroup(GroupID group)
 
{
 
	OrderBackup *ob;
 
	FOR_ALL_ORDER_BACKUPS(ob) {
 
	for (OrderBackup *ob : OrderBackup::Iterate()) {
 
		if (ob->group == group) ob->group = DEFAULT_GROUP;
 
	}
 
}
 

	
 
/**
 
 * Clear/update the (clone) vehicle from an order backup.
 
@@ -232,14 +226,13 @@ CommandCost CmdClearOrderBackup(TileInde
 
 * @note If it is not possible to set another vehicle as clone
 
 *       "example", then this backed up order will be removed.
 
 */
 
/* static */ void OrderBackup::ClearVehicle(const Vehicle *v)
 
{
 
	assert(v != nullptr);
 
	OrderBackup *ob;
 
	FOR_ALL_ORDER_BACKUPS(ob) {
 
	for (OrderBackup *ob : OrderBackup::Iterate()) {
 
		if (ob->clone == v) {
 
			/* Get another item in the shared list. */
 
			ob->clone = (v->FirstShared() == v) ? v->NextShared() : v->FirstShared();
 
			/* But if that isn't there, remove it. */
 
			if (ob->clone == nullptr) delete ob;
 
		}
 
@@ -253,14 +246,13 @@ CommandCost CmdClearOrderBackup(TileInde
 
 * @param hangar Only used for airports in the destination.
 
 *               When false, remove airport and hangar orders.
 
 *               When true, remove either airport or hangar order.
 
 */
 
/* static */ void OrderBackup::RemoveOrder(OrderType type, DestinationID destination, bool hangar)
 
{
 
	OrderBackup *ob;
 
	FOR_ALL_ORDER_BACKUPS(ob) {
 
	for (OrderBackup *ob : OrderBackup::Iterate()) {
 
		for (Order *order = ob->orders; order != nullptr; order = order->next) {
 
			OrderType ot = order->GetType();
 
			if (ot == OT_GOTO_DEPOT && (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) continue;
 
			if (ot == OT_GOTO_DEPOT && hangar && !IsHangarTile(ob->tile)) continue; // Not an aircraft? Can't have a hangar order.
 
			if (ot == OT_IMPLICIT || (IsHangarTile(ob->tile) && ot == OT_GOTO_DEPOT && !hangar)) ot = OT_GOTO_STATION;
 
			if (ot == type && order->GetDestination() == destination) {
src/order_backup.h
Show inline comments
 
@@ -61,20 +61,7 @@ public:
 

	
 
	static void ClearGroup(GroupID group);
 
	static void ClearVehicle(const Vehicle *v);
 
	static void RemoveOrder(OrderType type, DestinationID destination, bool hangar);
 
};
 

	
 
/**
 
 * Iterator over all order backups from a given ID.
 
 * @param var The variable to iterate with.
 
 * @param start The start of the iteration.
 
 */
 
#define FOR_ALL_ORDER_BACKUPS_FROM(var, start) FOR_ALL_ITEMS_FROM(OrderBackup, order_backup_index, var, start)
 

	
 
/**
 
 * Iterator over all order backups.
 
 * @param var The variable to iterate with.
 
 */
 
#define FOR_ALL_ORDER_BACKUPS(var) FOR_ALL_ORDER_BACKUPS_FROM(var, 0)
 

	
 
#endif /* ORDER_BACKUP_H */
src/order_base.h
Show inline comments
 
@@ -387,17 +387,9 @@ public:
 

	
 
	void FreeChain(bool keep_orderlist = false);
 

	
 
	void DebugCheckSanity() const;
 
};
 

	
 
#define FOR_ALL_ORDERS_FROM(var, start) FOR_ALL_ITEMS_FROM(Order, order_index, var, start)
 
#define FOR_ALL_ORDERS(var) FOR_ALL_ORDERS_FROM(var, 0)
 

	
 

	
 
#define FOR_VEHICLE_ORDERS(v, order) for (order = (v->orders.list == nullptr) ? nullptr : v->orders.list->GetFirstOrder(); order != nullptr; order = order->next)
 

	
 

	
 
#define FOR_ALL_ORDER_LISTS_FROM(var, start) FOR_ALL_ITEMS_FROM(OrderList, orderlist_index, var, start)
 
#define FOR_ALL_ORDER_LISTS(var) FOR_ALL_ORDER_LISTS_FROM(var, 0)
 

	
 
#endif /* ORDER_BASE_H */
src/saveload/afterload.cpp
Show inline comments
 
@@ -1448,16 +1448,15 @@ bool AfterLoadGame()
 
			}
 
		}
 
	}
 

	
 
	/* Setting no refit flags to all orders in savegames from before refit in orders were added */
 
	if (IsSavegameVersionBefore(SLV_36)) {
 
		Order *order;
 
		Vehicle *v;
 

	
 
		FOR_ALL_ORDERS(order) {
 
		for (Order *order : Order::Iterate()) {
 
			order->SetRefit(CT_NO_REFIT);
 
		}
 

	
 
		FOR_ALL_VEHICLES(v) {
 
			v->current_order.SetRefit(CT_NO_REFIT);
 
		}
 
@@ -1721,31 +1720,30 @@ bool AfterLoadGame()
 
		}
 
	}
 

	
 

	
 
	if (IsSavegameVersionBefore(SLV_93)) {
 
		/* Rework of orders. */
 
		Order *order;
 
		FOR_ALL_ORDERS(order) order->ConvertFromOldSavegame();
 
		for (Order *order : Order::Iterate()) order->ConvertFromOldSavegame();
 

	
 
		Vehicle *v;
 
		FOR_ALL_VEHICLES(v) {
 
			if (v->orders.list != nullptr && v->orders.list->GetFirstOrder() != nullptr && v->orders.list->GetFirstOrder()->IsType(OT_NOTHING)) {
 
				v->orders.list->FreeChain();
 
				v->orders.list = nullptr;
 
			}
 

	
 
			v->current_order.ConvertFromOldSavegame();
 
			if (v->type == VEH_ROAD && v->IsPrimaryVehicle() && v->FirstShared() == v) {
 
				Order* order;
 
				FOR_VEHICLE_ORDERS(v, order) order->SetNonStopType(ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS);
 
			}
 
		}
 
	} else if (IsSavegameVersionBefore(SLV_94)) {
 
		/* Unload and transfer are now mutual exclusive. */
 
		Order *order;
 
		FOR_ALL_ORDERS(order) {
 
		for (Order *order : Order::Iterate()) {
 
			if ((order->GetUnloadType() & (OUFB_UNLOAD | OUFB_TRANSFER)) == (OUFB_UNLOAD | OUFB_TRANSFER)) {
 
				order->SetUnloadType(OUFB_TRANSFER);
 
				order->SetLoadType(OLFB_NO_LOAD);
 
			}
 
		}
 

	
 
@@ -2157,14 +2155,13 @@ bool AfterLoadGame()
 
			if (!Company::IsValidID(st->owner)) st->owner = OWNER_NONE;
 
		}
 
	}
 

	
 
	/* Trains could now stop in a specific location. */
 
	if (IsSavegameVersionBefore(SLV_117)) {
 
		Order *o;
 
		FOR_ALL_ORDERS(o) {
 
		for (Order *o : Order::Iterate()) {
 
			if (o->IsType(OT_GOTO_STATION)) o->SetStopLocation(OSL_PLATFORM_FAR_END);
 
		}
 
	}
 

	
 
	if (IsSavegameVersionBefore(SLV_120)) {
 
		extern VehicleDefaultSettings _old_vds;
 
@@ -3011,14 +3008,13 @@ bool AfterLoadGame()
 
	 * Furthermore before savegame version SLV_192 the actual content was always corrupt.
 
	 */
 
	if (!_networking || _network_server || IsSavegameVersionBefore(SLV_192)) {
 
#ifndef DEBUG_DUMP_COMMANDS
 
		/* Note: We cannot use CleanPool since that skips part of the destructor
 
		 * and then leaks un-reachable Orders in the order pool. */
 
		OrderBackup *ob;
 
		FOR_ALL_ORDER_BACKUPS(ob) {
 
		for (OrderBackup *ob : OrderBackup::Iterate()) {
 
			delete ob;
 
		}
 
#endif
 
	}
 

	
 
	if (IsSavegameVersionBefore(SLV_198)) {
src/saveload/order_sl.cpp
Show inline comments
 
@@ -120,15 +120,13 @@ const SaveLoad *GetOrderDescription()
 

	
 
	return _order_desc;
 
}
 

	
 
static void Save_ORDR()
 
{
 
	Order *order;
 

	
 
	FOR_ALL_ORDERS(order) {
 
	for (Order *order : Order::Iterate()) {
 
		SlSetArrayIndex(order->index);
 
		SlObject(order, GetOrderDescription());
 
	}
 
}
 

	
 
static void Load_ORDR()
 
@@ -163,14 +161,14 @@ static void Load_ORDR()
 
			}
 

	
 
			free(orders);
 
		}
 

	
 
		/* Update all the next pointer */
 
		Order *o;
 
		FOR_ALL_ORDERS(o) {
 
		for (Order *o : Order::Iterate()) {
 
			size_t order_index = o->index;
 
			/* Delete invalid orders */
 
			if (o->IsType(OT_NOTHING)) {
 
				delete o;
 
				continue;
 
			}
 
			/* The orders were built like this:
 
@@ -194,15 +192,13 @@ static void Load_ORDR()
 

	
 
static void Ptrs_ORDR()
 
{
 
	/* Orders from old savegames have pointers corrected in Load_ORDR */
 
	if (IsSavegameVersionBefore(SLV_5, 2)) return;
 

	
 
	Order *o;
 

	
 
	FOR_ALL_ORDERS(o) {
 
	for (Order *o : Order::Iterate()) {
 
		SlObject(o, GetOrderDescription());
 
	}
 
}
 

	
 
const SaveLoad *GetOrderListDescription()
 
{
 
@@ -213,15 +209,13 @@ const SaveLoad *GetOrderListDescription(
 

	
 
	return _orderlist_desc;
 
}
 

	
 
static void Save_ORDL()
 
{
 
	OrderList *list;
 

	
 
	FOR_ALL_ORDER_LISTS(list) {
 
	for (OrderList *list : OrderList::Iterate()) {
 
		SlSetArrayIndex(list->index);
 
		SlObject(list, GetOrderListDescription());
 
	}
 
}
 

	
 
static void Load_ORDL()
 
@@ -235,15 +229,13 @@ static void Load_ORDL()
 
	}
 

	
 
}
 

	
 
static void Ptrs_ORDL()
 
{
 
	OrderList *list;
 

	
 
	FOR_ALL_ORDER_LISTS(list) {
 
	for (OrderList *list : OrderList::Iterate()) {
 
		SlObject(list, GetOrderListDescription());
 
	}
 
}
 

	
 
const SaveLoad *GetOrderBackupDescription()
 
{
 
@@ -274,14 +266,13 @@ static void Save_BKOR()
 
{
 
	/* We only save this when we're a network server
 
	 * as we want this information on our clients. For
 
	 * normal games this information isn't needed. */
 
	if (!_networking || !_network_server) return;
 

	
 
	OrderBackup *ob;
 
	FOR_ALL_ORDER_BACKUPS(ob) {
 
	for (OrderBackup *ob : OrderBackup::Iterate()) {
 
		SlSetArrayIndex(ob->index);
 
		SlObject(ob, GetOrderBackupDescription());
 
	}
 
}
 

	
 
void Load_BKOR()
 
@@ -294,14 +285,13 @@ void Load_BKOR()
 
		SlObject(ob, GetOrderBackupDescription());
 
	}
 
}
 

	
 
static void Ptrs_BKOR()
 
{
 
	OrderBackup *ob;
 
	FOR_ALL_ORDER_BACKUPS(ob) {
 
	for (OrderBackup *ob : OrderBackup::Iterate()) {
 
		SlObject(ob, GetOrderBackupDescription());
 
	}
 
}
 

	
 
extern const ChunkHandler _order_chunk_handlers[] = {
 
	{ 'BKOR', Save_BKOR, Load_BKOR, Ptrs_BKOR, nullptr, CH_ARRAY},
src/saveload/station_sl.cpp
Show inline comments
 
@@ -37,14 +37,13 @@ static void UpdateWaypointOrder(Order *o
 
 * Perform all steps to upgrade from the old station buoys to the new version
 
 * that uses waypoints. This includes some old saveload mechanics.
 
 */
 
void MoveBuoysToWaypoints()
 
{
 
	/* Buoy orders become waypoint orders */
 
	OrderList *ol;
 
	FOR_ALL_ORDER_LISTS(ol) {
 
	for (OrderList *ol : OrderList::Iterate()) {
 
		VehicleType vt = ol->GetFirstSharedVehicle()->type;
 
		if (vt != VEH_SHIP && vt != VEH_TRAIN) continue;
 

	
 
		for (Order *o = ol->GetFirstOrder(); o != nullptr; o = o->next) UpdateWaypointOrder(o);
 
	}
 

	
src/saveload/waypoint_sl.cpp
Show inline comments
 
@@ -127,14 +127,13 @@ void MoveWaypointsToBaseStations()
 
		}
 

	
 
		wp.new_index = new_wp->index;
 
	}
 

	
 
	/* Update the orders of vehicles */
 
	OrderList *ol;
 
	FOR_ALL_ORDER_LISTS(ol) {
 
	for (OrderList *ol : OrderList::Iterate()) {
 
		if (ol->GetFirstSharedVehicle()->type != VEH_TRAIN) continue;
 

	
 
		for (Order *o = ol->GetFirstOrder(); o != nullptr; o = o->next) UpdateWaypointOrder(o);
 
	}
 

	
 
	Vehicle *v;
src/station_cmd.cpp
Show inline comments
 
@@ -3661,15 +3661,14 @@ void DeleteStaleLinks(Station *from)
 
			if ((uint)(_date - edge.LastUpdate()) > timeout) {
 
				bool updated = false;
 

	
 
				if (auto_distributed) {
 
					/* Have all vehicles refresh their next hops before deciding to
 
					 * remove the node. */
 
					OrderList *l;
 
					std::vector<Vehicle *> vehicles;
 
					FOR_ALL_ORDER_LISTS(l) {
 
					for (OrderList *l : OrderList::Iterate()) {
 
						bool found_from = false;
 
						bool found_to = false;
 
						for (Order *order = l->GetFirstOrder(); order != nullptr; order = order->next) {
 
							if (!order->IsType(OT_GOTO_STATION) && !order->IsType(OT_IMPLICIT)) continue;
 
							if (order->GetDestination() == from->index) {
 
								found_from = true;
0 comments (0 inline, 0 general)