OpenTTD Source: src/vehiclelist_func.h Source File

OpenTTD Source 14.0-beta1

vehiclelist_func.h
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10 #ifndef VEHICLELIST_FUNC_H
11 #define VEHICLELIST_FUNC_H
12
13 #include "order_base.h"
14 #include "vehicle_base.h"
15
23 template <class VehiclePredicate, class OrderPredicate, class VehicleFunc>
24 void FindVehiclesWithOrder(VehiclePredicate veh_pred, OrderPredicate ord_pred, VehicleFunc veh_func)
25 {
26 for (const OrderList *orderlist : OrderList::Iterate()) {
27
28 /* We assume all vehicles sharing an order list match the condition. */
29 const Vehicle *v = orderlist->GetFirstSharedVehicle();
30 if (!veh_pred(v)) continue;
31
32 /* Vehicle is a candidate, search for a matching order. */
33 for (const Order *order = orderlist->GetFirstOrder(); order != nullptr; order = order->next) {
34
35 if (!ord_pred(order)) continue;
36
37 /* An order matches, we can add all shared vehicles to the list. */
38 for (; v != nullptr; v = v->NextShared()) {
39 veh_func(v);
40 }
41 break;
42 }
43 }
44 }
45
46 #endif /* VEHICLELIST_FUNC_H */
order_base.h
vehicle_base.h
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:240
FindVehiclesWithOrder
void FindVehiclesWithOrder(VehiclePredicate veh_pred, OrderPredicate ord_pred, VehicleFunc veh_func)
Find vehicles matching an order.
Definition: vehiclelist_func.h:24
Pool::PoolItem<&_orderlist_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:384
OrderList
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition: order_base.h:260
Vehicle::NextShared
Vehicle * NextShared() const
Get the next vehicle of the shared vehicle chain.
Definition: vehicle_base.h:709
Order::next
Order * next
Pointer to next order. If nullptr, end of list.
Definition: order_base.h:59
Order
Definition: order_base.h:36