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/>.
8 /** @file vehiclelist_func.h Functions and type for generating vehicle lists. */
10 #ifndef VEHICLELIST_FUNC_H
11 #define VEHICLELIST_FUNC_H
13 #include "order_base.h"
14 #include "vehicle_base.h"
17 * Find vehicles matching an order.
18 * This can be used, e.g. to find all vehicles that stop at a particular station.
19 * @param veh_pred Vehicle selection predicate. This is called only for the first vehicle using the order list.
20 * @param ord_pred Order selection predicate.
21 * @param veh_func Called for each vehicle that matches both vehicle and order predicates.
23 template <class VehiclePredicate
, class OrderPredicate
, class VehicleFunc
>
24 void FindVehiclesWithOrder(VehiclePredicate veh_pred
, OrderPredicate ord_pred
, VehicleFunc veh_func
)
26 for (const OrderList
*orderlist
: OrderList::Iterate()) {
28 /* We assume all vehicles sharing an order list match the condition. */
29 Vehicle
*v
= orderlist
->GetFirstSharedVehicle();
30 if (!veh_pred(v
)) continue;
32 /* Vehicle is a candidate, search for a matching order. */
33 for (const Order
*order
= orderlist
->GetFirstOrder(); order
!= nullptr; order
= order
->next
) {
35 if (!ord_pred(order
)) continue;
37 /* An order matches, we can add all shared vehicles to the list. */
38 for (; v
!= nullptr; v
= v
->NextShared()) {
46 #endif /* VEHICLELIST_FUNC_H */