Changeset - r24513:ba5551156e4e
[Not reviewed]
master
0 11 0
glx22 - 3 years ago 2020-12-26 23:13:56
glx@openttd.org
Codechange: Replace FOR_VEHICLE_ORDERS with range-based for loops
11 files changed with 69 insertions and 48 deletions:
0 comments (0 inline, 0 general)
src/autoreplace_cmd.cpp
Show inline comments
 
@@ -176,15 +176,14 @@ static void TransferCargo(Vehicle *old_v
 
 */
 
static bool VerifyAutoreplaceRefitForOrders(const Vehicle *v, EngineID engine_type)
 
{
 
	CargoTypes union_refit_mask_a = GetUnionOfArticulatedRefitMasks(v->engine_type, false);
 
	CargoTypes union_refit_mask_b = GetUnionOfArticulatedRefitMasks(engine_type, false);
 

	
 
	const Order *o;
 
	const Vehicle *u = (v->type == VEH_TRAIN) ? v->First() : v;
 
	FOR_VEHICLE_ORDERS(u, o) {
 
	for (const Order *o : u->Orders()) {
 
		if (!o->IsRefit() || o->IsAutoRefit()) continue;
 
		CargoID cargo_type = o->GetRefitCargo();
 

	
 
		if (!HasBit(union_refit_mask_a, cargo_type)) continue;
 
		if (!HasBit(union_refit_mask_b, cargo_type)) return false;
 
	}
src/industry_cmd.cpp
Show inline comments
 
@@ -2606,14 +2606,13 @@ static int WhoCanServiceIndustry(Industr
 
		if (!c_accepts && !c_produces) continue; // Wrong cargo
 

	
 
		/* Check orders of the vehicle.
 
		 * We cannot check the first of shared orders only, since the first vehicle in such a chain
 
		 * may have a different cargo type.
 
		 */
 
		const Order *o;
 
		FOR_VEHICLE_ORDERS(v, o) {
 
		for (const Order *o : v->Orders()) {
 
			if (o->IsType(OT_GOTO_STATION) && !(o->GetUnloadType() & OUFB_TRANSFER)) {
 
				/* Vehicle visits a station to load or unload */
 
				Station *st = Station::Get(o->GetDestination());
 
				assert(st != nullptr);
 

	
 
				/* Same cargo produced by industry is dropped here => not serviced by vehicle v */
src/order_backup.cpp
Show inline comments
 
@@ -53,14 +53,13 @@ OrderBackup::OrderBackup(const Vehicle *
 
		this->clone = (v->FirstShared() == v) ? v->NextShared() : v->FirstShared();
 
	} else {
 
		/* Else copy the orders */
 
		Order **tail = &this->orders;
 

	
 
		/* Count the number of orders */
 
		const Order *order;
 
		FOR_VEHICLE_ORDERS(v, order) {
 
		for (const Order *order : v->Orders()) {
 
			Order *copy = new Order();
 
			copy->AssignOrder(*order);
 
			*tail = copy;
 
			tail = &copy->next;
 
		}
 
	}
src/order_base.h
Show inline comments
 
@@ -389,9 +389,7 @@ public:
 

	
 
	void FreeChain(bool keep_orderlist = false);
 

	
 
	void DebugCheckSanity() const;
 
};
 

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

	
 
#endif /* ORDER_BASE_H */
src/order_cmd.cpp
Show inline comments
 
@@ -966,14 +966,13 @@ void InsertOrder(Vehicle *v, Order *new_
 
		/* Update any possible open window of the vehicle */
 
		InvalidateVehicleOrder(u, INVALID_VEH_ORDER_ID | (sel_ord << 8));
 
	}
 

	
 
	/* As we insert an order, the order to skip to will be 'wrong'. */
 
	VehicleOrderID cur_order_id = 0;
 
	Order *order;
 
	FOR_VEHICLE_ORDERS(v, order) {
 
	for (Order *order : v->Orders()) {
 
		if (order->IsType(OT_CONDITIONAL)) {
 
			VehicleOrderID order_id = order->GetConditionSkipToOrder();
 
			if (order_id >= sel_ord) {
 
				order->SetConditionSkipToOrder(order_id + 1);
 
			}
 
			if (order_id == cur_order_id) {
 
@@ -1087,14 +1086,13 @@ void DeleteOrder(Vehicle *v, VehicleOrde
 
		/* Update any possible open window of the vehicle */
 
		InvalidateVehicleOrder(u, sel_ord | (INVALID_VEH_ORDER_ID << 8));
 
	}
 

	
 
	/* As we delete an order, the order to skip to will be 'wrong'. */
 
	VehicleOrderID cur_order_id = 0;
 
	Order *order = nullptr;
 
	FOR_VEHICLE_ORDERS(v, order) {
 
	for (Order *order : v->Orders()) {
 
		if (order->IsType(OT_CONDITIONAL)) {
 
			VehicleOrderID order_id = order->GetConditionSkipToOrder();
 
			if (order_id >= sel_ord) {
 
				order_id = max(order_id - 1, 0);
 
			}
 
			if (order_id == cur_order_id) {
 
@@ -1222,14 +1220,13 @@ CommandCost CmdMoveOrder(TileIndex tile,
 
			assert(v->orders.list == u->orders.list);
 
			/* Update any possible open window of the vehicle */
 
			InvalidateVehicleOrder(u, moving_order | (target_order << 8));
 
		}
 

	
 
		/* As we move an order, the order to skip to will be 'wrong'. */
 
		Order *order;
 
		FOR_VEHICLE_ORDERS(v, order) {
 
		for (Order *order : v->Orders()) {
 
			if (order->IsType(OT_CONDITIONAL)) {
 
				VehicleOrderID order_id = order->GetConditionSkipToOrder();
 
				if (order_id == moving_order) {
 
					order_id = target_order;
 
				} else if (order_id > moving_order && order_id <= target_order) {
 
					order_id--;
 
@@ -1557,15 +1554,13 @@ CommandCost CmdCloneOrder(TileIndex tile
 
				return CMD_ERROR;
 
			}
 

	
 
			/* Is the vehicle already in the shared list? */
 
			if (src->FirstShared() == dst->FirstShared()) return CMD_ERROR;
 

	
 
			const Order *order;
 

	
 
			FOR_VEHICLE_ORDERS(src, order) {
 
			for (const Order *order : src->Orders()) {
 
				if (!OrderGoesToStation(dst, order)) continue;
 

	
 
				/* Allow copying unreachable destinations if they were already unreachable for the source.
 
				 * This is basically to allow cloning / autorenewing / autoreplacing vehicles, while the stations
 
				 * are temporarily invalid due to reconstruction. */
 
				const Station *st = Station::Get(order->GetDestination());
 
@@ -1610,14 +1605,13 @@ CommandCost CmdCloneOrder(TileIndex tile
 

	
 
			CommandCost ret = CheckOwnership(src->owner);
 
			if (ret.Failed()) return ret;
 

	
 
			/* Trucks can't copy all the orders from busses (and visa versa),
 
			 * and neither can helicopters and aircraft. */
 
			const Order *order;
 
			FOR_VEHICLE_ORDERS(src, order) {
 
			for (const Order *order : src->Orders()) {
 
				if (OrderGoesToStation(dst, order) &&
 
						!CanVehicleUseStation(dst, Station::Get(order->GetDestination()))) {
 
					return_cmd_error(STR_ERROR_CAN_T_COPY_SHARE_ORDER);
 
				}
 
			}
 

	
 
@@ -1629,23 +1623,22 @@ CommandCost CmdCloneOrder(TileIndex tile
 
			/* make sure there are orders available */
 
			if (!Order::CanAllocateItem(src->GetNumOrders()) || !OrderList::CanAllocateItem()) {
 
				return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
 
			}
 

	
 
			if (flags & DC_EXEC) {
 
				const Order *order;
 
				Order *first = nullptr;
 
				Order **order_dst;
 

	
 
				/* If the destination vehicle had an order list, destroy the chain but keep the OrderList.
 
				 * We only reset the order indices, if the new orders are obviously different.
 
				 * (We mainly do this to keep the order indices valid and in range.) */
 
				DeleteVehicleOrders(dst, true, dst->GetNumOrders() != src->GetNumOrders());
 

	
 
				order_dst = &first;
 
				FOR_VEHICLE_ORDERS(src, order) {
 
				for (const Order *order : src->Orders()) {
 
					*order_dst = new Order();
 
					(*order_dst)->AssignOrder(*order);
 
					order_dst = &(*order_dst)->next;
 
				}
 
				if (dst->orders.list == nullptr) {
 
					dst->orders.list = new OrderList(first, dst);
 
@@ -1746,19 +1739,18 @@ void CheckOrders(const Vehicle *v)
 

	
 
	/* do nothing we we're not the first vehicle in a share-chain */
 
	if (v->FirstShared() != v) return;
 

	
 
	/* Only check every 20 days, so that we don't flood the message log */
 
	if (v->owner == _local_company && v->day_counter % 20 == 0) {
 
		const Order *order;
 
		StringID message = INVALID_STRING_ID;
 

	
 
		/* Check the order list */
 
		int n_st = 0;
 

	
 
		FOR_VEHICLE_ORDERS(v, order) {
 
		for (const Order *order : v->Orders()) {
 
			/* Dummy order? */
 
			if (order->IsType(OT_DUMMY)) {
 
				message = STR_NEWS_VEHICLE_HAS_VOID_ORDER;
 
				break;
 
			}
 
			/* Does station have a load-bay for this vehicle? */
 
@@ -1826,13 +1818,13 @@ void RemoveOrderFromAllVehicles(OrderTyp
 
			order->MakeDummy();
 
			SetWindowDirty(WC_VEHICLE_VIEW, v->index);
 
		}
 

	
 
		/* Clear the order from the order-list */
 
		int id = -1;
 
		FOR_VEHICLE_ORDERS(v, order) {
 
		for (Order *order : v->Orders()) {
 
			id++;
 
restart:
 

	
 
			OrderType ot = order->GetType();
 
			if (ot == OT_GOTO_DEPOT && (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) continue;
 
			if (ot == OT_GOTO_DEPOT && hangar && v->type != VEH_AIRCRAFT) continue; // Not an aircraft? Can't have a hangar order.
 
@@ -1876,15 +1868,13 @@ restart:
 
/**
 
 * Checks if a vehicle has a depot in its order list.
 
 * @return True iff at least one order is a depot order.
 
 */
 
bool Vehicle::HasDepotOrder() const
 
{
 
	const Order *order;
 

	
 
	FOR_VEHICLE_ORDERS(this, order) {
 
	for (const Order *order : this->Orders()) {
 
		if (order->IsType(OT_GOTO_DEPOT)) return true;
 
	}
 

	
 
	return false;
 
}
 

	
 
@@ -1937,15 +1927,13 @@ uint16 GetServiceIntervalClamped(uint in
 
 * @return false if there are no valid orders
 
 * @note Conditional orders are not considered valid destination orders
 
 *
 
 */
 
static bool CheckForValidOrders(const Vehicle *v)
 
{
 
	const Order *order;
 

	
 
	FOR_VEHICLE_ORDERS(v, order) {
 
	for (const Order *order : v->Orders()) {
 
		switch (order->GetType()) {
 
			case OT_GOTO_STATION:
 
			case OT_GOTO_DEPOT:
 
			case OT_GOTO_WAYPOINT:
 
				return true;
 

	
src/order_gui.cpp
Show inline comments
 
@@ -790,14 +790,13 @@ public:
 

	
 
		this->UpdateAutoRefitState();
 

	
 
		if (_settings_client.gui.quick_goto && v->owner == _local_company) {
 
			/* If there are less than 2 station, make Go To active. */
 
			int station_orders = 0;
 
			const Order *order;
 
			FOR_VEHICLE_ORDERS(v, order) {
 
			for(const Order *order : v->Orders()) {
 
				if (order->IsType(OT_GOTO_STATION)) station_orders++;
 
			}
 

	
 
			if (station_orders < 2) this->OrderClick_Goto(OPOS_GOTO);
 
		}
 
		this->OnInvalidateData(VIWD_MODIFY_ORDERS);
src/saveload/afterload.cpp
Show inline comments
 
@@ -1730,14 +1730,13 @@ bool AfterLoadGame()
 
				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);
 
				for (Order *order : v->Orders()) order->SetNonStopType(ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS);
 
			}
 
		}
 
	} else if (IsSavegameVersionBefore(SLV_94)) {
 
		/* Unload and transfer are now mutual exclusive. */
 
		for (Order *order : Order::Iterate()) {
 
			if ((order->GetUnloadType() & (OUFB_UNLOAD | OUFB_TRANSFER)) == (OUFB_UNLOAD | OUFB_TRANSFER)) {
src/script/api/script_vehiclelist.cpp
Show inline comments
 
@@ -28,15 +28,13 @@ ScriptVehicleList::ScriptVehicleList()
 
ScriptVehicleList_Station::ScriptVehicleList_Station(StationID station_id)
 
{
 
	if (!ScriptBaseStation::IsValidBaseStation(station_id)) return;
 

	
 
	for (const Vehicle *v : Vehicle::Iterate()) {
 
		if ((v->owner == ScriptObject::GetCompany() || ScriptObject::GetCompany() == OWNER_DEITY) && v->IsPrimaryVehicle()) {
 
			const Order *order;
 

	
 
			FOR_VEHICLE_ORDERS(v, order) {
 
			for (const Order *order : v->Orders()) {
 
				if ((order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT)) && order->GetDestination() == station_id) {
 
					this->AddItem(v->index);
 
					break;
 
				}
 
			}
 
		}
 
@@ -78,15 +76,13 @@ ScriptVehicleList_Depot::ScriptVehicleLi
 
		default: // No depot
 
			return;
 
	}
 

	
 
	for (const Vehicle *v : Vehicle::Iterate()) {
 
		if ((v->owner == ScriptObject::GetCompany() || ScriptObject::GetCompany() == OWNER_DEITY) && v->IsPrimaryVehicle() && v->type == type) {
 
			const Order *order;
 

	
 
			FOR_VEHICLE_ORDERS(v, order) {
 
			for (const Order *order : v->Orders()) {
 
				if (order->IsType(OT_GOTO_DEPOT) && order->GetDestination() == dest) {
 
					this->AddItem(v->index);
 
					break;
 
				}
 
			}
 
		}
src/station_cmd.cpp
Show inline comments
 
@@ -2483,14 +2483,13 @@ CommandCost CmdOpenCloseAirport(TileInde
 
 * @param company company ID
 
 */
 
bool HasStationInUse(StationID station, bool include_company, CompanyID company)
 
{
 
	for (const Vehicle *v : Vehicle::Iterate()) {
 
		if ((v->owner == company) == include_company) {
 
			const Order *order;
 
			FOR_VEHICLE_ORDERS(v, order) {
 
			for (const Order *order : v->Orders()) {
 
				if ((order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT)) && order->GetDestination() == station) {
 
					return true;
 
				}
 
			}
 
		}
 
	}
src/vehicle_base.h
Show inline comments
 
@@ -966,12 +966,61 @@ public:
 
	{
 
		Vehicle *v = this->Previous();
 
		while (v != nullptr && v->IsArticulatedPart()) v = v->Previous();
 

	
 
		return v;
 
	}
 

	
 
	/**
 
	 * Iterator to iterate orders
 
	 * Supports deletion of current order
 
	 */
 
	struct OrderIterator {
 
		typedef Order value_type;
 
		typedef Order* pointer;
 
		typedef Order& reference;
 
		typedef size_t difference_type;
 
		typedef std::forward_iterator_tag iterator_category;
 

	
 
		explicit OrderIterator(OrderList *list) : list(list), prev(nullptr)
 
		{
 
			this->order = (this->list == nullptr) ? nullptr : this->list->GetFirstOrder();
 
		}
 

	
 
		bool operator==(const OrderIterator &other) const { return this->order == other.order; }
 
		bool operator!=(const OrderIterator &other) const { return !(*this == other); }
 
		Order * operator*() const { return this->order; }
 
		OrderIterator & operator++()
 
		{
 
			this->prev = (this->prev == nullptr) ? this->list->GetFirstOrder() : this->prev->next;
 
			this->order = (this->prev == nullptr) ? nullptr : this->prev->next;
 
			return *this;
 
		}
 

	
 
	private:
 
		OrderList *list;
 
		Order *order;
 
		Order *prev;
 
	};
 

	
 
	/**
 
	 * Iterable ensemble of orders
 
	 */
 
	struct IterateWrapper {
 
		OrderList *list;
 
		IterateWrapper(OrderList *list = nullptr) : list(list) {}
 
		OrderIterator begin() { return OrderIterator(this->list); }
 
		OrderIterator end() { return OrderIterator(nullptr); }
 
		bool empty() { return this->begin() == this->end(); }
 
	};
 

	
 
	/**
 
	 * Returns an iterable ensemble of orders of a vehicle
 
	 * @return an iterable ensemble of orders of a vehicle
 
	 */
 
	IterateWrapper Orders() const { return IterateWrapper(this->orders.list); }
 
};
 

	
 
/**
 
 * Class defining several overloaded accessors so we don't
 
 * have to cast vehicle types that often
 
 */
src/vehiclelist.cpp
Show inline comments
 
@@ -115,15 +115,13 @@ bool GenerateVehicleSortList(VehicleList
 
	list->clear();
 

	
 
	switch (vli.type) {
 
		case VL_STATION_LIST:
 
			for (const Vehicle *v : Vehicle::Iterate()) {
 
				if (v->type == vli.vtype && v->IsPrimaryVehicle()) {
 
					const Order *order;
 

	
 
					FOR_VEHICLE_ORDERS(v, order) {
 
					for (const Order *order : v->Orders()) {
 
						if ((order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT) || order->IsType(OT_IMPLICIT))
 
								&& order->GetDestination() == vli.index) {
 
							list->push_back(v);
 
							break;
 
						}
 
					}
 
@@ -162,15 +160,13 @@ bool GenerateVehicleSortList(VehicleList
 
			}
 
			break;
 

	
 
		case VL_DEPOT_LIST:
 
			for (const Vehicle *v : Vehicle::Iterate()) {
 
				if (v->type == vli.vtype && v->IsPrimaryVehicle()) {
 
					const Order *order;
 

	
 
					FOR_VEHICLE_ORDERS(v, order) {
 
					for (const Order *order : v->Orders()) {
 
						if (order->IsType(OT_GOTO_DEPOT) && !(order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) && order->GetDestination() == vli.index) {
 
							list->push_back(v);
 
							break;
 
						}
 
					}
 
				}
0 comments (0 inline, 0 general)