1
0
Fork 0

Codechange: strongly type OrderID and OrderListID

pull/13511/head
Rubidium 2025-01-31 21:50:51 +01:00 committed by rubidium42
parent d61d643906
commit 9ab36b594d
5 changed files with 12 additions and 11 deletions

View File

@ -20,8 +20,8 @@
#include "timer/timer_game_tick.h" #include "timer/timer_game_tick.h"
#include "saveload/saveload.h" #include "saveload/saveload.h"
typedef Pool<Order, OrderID, 256, 0xFF0000> OrderPool; using OrderPool = Pool<Order, OrderID, 256, OrderID::End().base()>;
typedef Pool<OrderList, OrderListID, 128, 64000> OrderListPool; using OrderListPool = Pool<OrderList, OrderListID, 128, OrderListID::End().base()>;
extern OrderPool _order_pool; extern OrderPool _order_pool;
extern OrderListPool _orderlist_pool; extern OrderListPool _orderlist_pool;

View File

@ -380,7 +380,7 @@ static Order GetOrderCmdFromTile(const Vehicle *v, TileIndex tile)
{ {
/* Override the index as it is not coming from a pool, so would not be initialised correctly. */ /* Override the index as it is not coming from a pool, so would not be initialised correctly. */
Order order; Order order;
order.index = 0; order.index = OrderID::Begin();
/* check depot first */ /* check depot first */
if (IsDepotTypeTile(tile, (TransportType)(uint)v->type) && IsTileOwner(tile, _local_company)) { if (IsDepotTypeTile(tile, (TransportType)(uint)v->type) && IsTileOwner(tile, _local_company)) {
@ -665,7 +665,7 @@ private:
{ {
Order order; Order order;
order.next = nullptr; order.next = nullptr;
order.index = 0; order.index = OrderID::Begin();
order.MakeGoToDepot(INVALID_DEPOT, ODTFB_PART_OF_ORDERS, order.MakeGoToDepot(INVALID_DEPOT, ODTFB_PART_OF_ORDERS,
_settings_client.gui.new_nonstop && this->vehicle->IsGroundVehicle() ? ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS : ONSF_STOP_EVERYWHERE); _settings_client.gui.new_nonstop && this->vehicle->IsGroundVehicle() ? ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS : ONSF_STOP_EVERYWHERE);
order.SetDepotActionType(ODATFB_NEAREST_DEPOT); order.SetDepotActionType(ODATFB_NEAREST_DEPOT);
@ -1221,7 +1221,7 @@ public:
if (order_id != INVALID_VEH_ORDER_ID) { if (order_id != INVALID_VEH_ORDER_ID) {
Order order; Order order;
order.next = nullptr; order.next = nullptr;
order.index = 0; order.index = OrderID::Begin();
order.MakeConditional(order_id); order.MakeConditional(order_id);
Command<CMD_INSERT_ORDER>::Post(STR_ERROR_CAN_T_INSERT_NEW_ORDER, this->vehicle->tile, this->vehicle->index, this->OrderGetSel(), order); Command<CMD_INSERT_ORDER>::Post(STR_ERROR_CAN_T_INSERT_NEW_ORDER, this->vehicle->tile, this->vehicle->index, this->OrderGetSel(), order);

View File

@ -12,11 +12,12 @@
#include "core/enum_type.hpp" #include "core/enum_type.hpp"
#include "depot_type.h" #include "depot_type.h"
#include "core/pool_type.hpp"
#include "station_type.h" #include "station_type.h"
typedef uint8_t VehicleOrderID; ///< The index of an order within its current vehicle (not pool related) typedef uint8_t VehicleOrderID; ///< The index of an order within its current vehicle (not pool related)
typedef uint32_t OrderID; using OrderID = PoolID<uint32_t, struct OrderIDTag, 0xFF0000, 0xFFFFFF>;
typedef uint16_t OrderListID; using OrderListID = PoolID<uint16_t, struct OrderListIDTag, 64000, 0xFFFF>;
struct DestinationID { struct DestinationID {
using BaseType = uint16_t; using BaseType = uint16_t;
@ -39,7 +40,7 @@ static const VehicleOrderID INVALID_VEH_ORDER_ID = 0xFF;
static const VehicleOrderID MAX_VEH_ORDER_ID = INVALID_VEH_ORDER_ID - 1; static const VehicleOrderID MAX_VEH_ORDER_ID = INVALID_VEH_ORDER_ID - 1;
/** Invalid order (sentinel) */ /** Invalid order (sentinel) */
static const OrderID INVALID_ORDER = 0xFFFFFF; static constexpr OrderID INVALID_ORDER = OrderID::Invalid();
/** /**
* Maximum number of orders in implicit-only lists before we start searching * Maximum number of orders in implicit-only lists before we start searching

View File

@ -164,7 +164,7 @@ struct ORDRChunkHandler : ChunkHandler {
/* Update all the next pointer */ /* Update all the next pointer */
for (Order *o : Order::Iterate()) { for (Order *o : Order::Iterate()) {
size_t order_index = o->index; size_t order_index = o->index.base();
/* Delete invalid orders */ /* Delete invalid orders */
if (o->IsType(OT_NOTHING)) { if (o->IsType(OT_NOTHING)) {
delete o; delete o;

View File

@ -278,7 +278,7 @@ struct TimetableWindow : Window {
int GetOrderFromTimetableWndPt(int y, [[maybe_unused]] const Vehicle *v) int GetOrderFromTimetableWndPt(int y, [[maybe_unused]] const Vehicle *v)
{ {
int32_t sel = this->vscroll->GetScrolledRowFromWidget(y, this, WID_VT_TIMETABLE_PANEL, WidgetDimensions::scaled.framerect.top); int32_t sel = this->vscroll->GetScrolledRowFromWidget(y, this, WID_VT_TIMETABLE_PANEL, WidgetDimensions::scaled.framerect.top);
if (sel == INT32_MAX) return INVALID_ORDER; if (sel == INT32_MAX) return INVALID_ORDER.base();
assert(IsInsideBS(sel, 0, v->GetNumOrders() * 2)); assert(IsInsideBS(sel, 0, v->GetNumOrders() * 2));
return sel; return sel;
} }
@ -645,7 +645,7 @@ struct TimetableWindow : Window {
int selected = GetOrderFromTimetableWndPt(pt.y, v); int selected = GetOrderFromTimetableWndPt(pt.y, v);
this->CloseChildWindows(); this->CloseChildWindows();
this->sel_index = (selected == INVALID_ORDER || selected == this->sel_index) ? -1 : selected; this->sel_index = (selected == INVALID_ORDER.base() || selected == this->sel_index) ? -1 : selected;
break; break;
} }