diff --git a/src/order_base.h b/src/order_base.h index d739044012..7be22873a3 100644 --- a/src/order_base.h +++ b/src/order_base.h @@ -21,6 +21,7 @@ #include "vehicle_type.h" #include "date_type.h" +#include #include typedef Pool OrderPool; @@ -29,9 +30,7 @@ extern OrderPool _order_pool; extern OrderListPool _orderlist_pool; struct OrderExtraInfo { - uint8 cargo_type_flags[NUM_CARGO]; ///< Load/unload types for each cargo type. - - OrderExtraInfo(); + uint8 cargo_type_flags[NUM_CARGO] = {}; ///< Load/unload types for each cargo type. }; /* If you change this, keep in mind that it is saved on 3 places: @@ -53,7 +52,7 @@ private: CargoID refit_cargo; ///< Refit CargoID - OrderExtraInfo *extra;///< Extra order info + std::unique_ptr extra; ///< Extra order info uint16 wait_time; ///< How long in ticks to wait at the destination. uint16 travel_time; ///< How long in ticks the journey to this destination should take. @@ -70,16 +69,18 @@ private: public: Order *next; ///< Pointer to next order. If NULL, end of list - Order() : refit_cargo(CT_NO_REFIT), extra(NULL), max_speed(UINT16_MAX) {} + Order() : refit_cargo(CT_NO_REFIT), max_speed(UINT16_MAX) {} ~Order(); Order(uint32 packed); - Order(const Order& other) : extra(NULL) + Order(const Order& other) { *this = other; } + Order(Order&& other) = default; + inline Order& operator=(Order const& other) { AssignOrder(other); diff --git a/src/order_cmd.cpp b/src/order_cmd.cpp index 66b467a4ec..81fc313889 100644 --- a/src/order_cmd.cpp +++ b/src/order_cmd.cpp @@ -48,7 +48,6 @@ INSTANTIATE_POOL_METHODS(OrderList) /** Clean everything up. */ Order::~Order() { - DeAllocExtraInfo(); if (CleaningPool()) return; /* We can visit oil rigs and buoys that are not our own. They will be shown in @@ -297,22 +296,14 @@ void Order::AssignOrder(const Order &other) void Order::AllocExtraInfo() { - if (this->extra == NULL) { - this->extra = new OrderExtraInfo(); + if (!this->extra) { + this->extra.reset(new OrderExtraInfo()); } } void Order::DeAllocExtraInfo() { - if (this->extra != NULL) { - delete this->extra; - this->extra = NULL; - } -} - -OrderExtraInfo::OrderExtraInfo() -{ - memset(cargo_type_flags, 0, sizeof(cargo_type_flags)); + this->extra.reset(); } void CargoStationIDStackSet::FillNextStoppingStation(const Vehicle *v, const OrderList *o, const Order *first, uint hops) diff --git a/src/saveload/order_sl.cpp b/src/saveload/order_sl.cpp index 4e3d5ff391..03335506ff 100644 --- a/src/saveload/order_sl.cpp +++ b/src/saveload/order_sl.cpp @@ -209,9 +209,9 @@ void Save_ORDX() Order *order; FOR_ALL_ORDERS(order) { - if (order->extra != NULL) { + if (order->extra) { SlSetArrayIndex(order->index); - SlObject(order->extra, GetOrderExtraInfoDescription()); + SlObject(order->extra.get(), GetOrderExtraInfoDescription()); } } } @@ -223,7 +223,7 @@ void Load_ORDX() Order *order = Order::GetIfValid(index); assert(order != NULL); order->AllocExtraInfo(); - SlObject(order->extra, GetOrderExtraInfoDescription()); + SlObject(order->extra.get(), GetOrderExtraInfoDescription()); } }