From 135dc547e2de8d0d785850f7294589d024fc61fd Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Thu, 13 Oct 2022 20:37:45 +0100 Subject: [PATCH] Split rarely used CommandCost fields into aux struct via unique_ptr Move static text ref stack into aux struct --- src/command.cpp | 41 ++++++++++++++++++++++++++++------------- src/command_type.h | 32 ++++++++++++++++++++------------ 2 files changed, 48 insertions(+), 25 deletions(-) diff --git a/src/command.cpp b/src/command.cpp index 41681a961d..aa74231970 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -1190,6 +1190,23 @@ CommandCost DoCommandPInternal(TileIndex tile, uint32 p1, uint32 p2, uint64 p3, } #undef return_dcpi +CommandCost::CommandCost(const CommandCost &other) +{ + *this = other; +} + +CommandCost &CommandCost::operator=(const CommandCost &other) +{ + this->cost = other.cost; + this->expense_type = other.expense_type; + this->success = other.success; + this->message = other.message; + this->extra_message = other.extra_message; + if (other.aux_data) { + this->aux_data.reset(new CommandCostAuxliaryData(*other.aux_data)); + } + return *this; +} /** * Adds the cost of the given command return value to this cost. @@ -1205,13 +1222,6 @@ void CommandCost::AddCost(const CommandCost &ret) } } -/** - * Values to put on the #TextRefStack for the error message. - * There is only one static instance of the array, just like there is only one - * instance of normal DParams. - */ -uint32 CommandCost::textref_stack[16]; - /** * Activate usage of the NewGRF #TextRefStack for the error message. * @param grffile NewGRF that provides the #TextRefStack @@ -1221,11 +1231,15 @@ void CommandCost::UseTextRefStack(const GRFFile *grffile, uint num_registers) { extern TemporaryStorageArray _temp_store; - assert(num_registers < lengthof(textref_stack)); - this->textref_stack_grffile = grffile; - this->textref_stack_size = num_registers; + if (!this->aux_data) { + this->aux_data.reset(new CommandCostAuxliaryData()); + } + + assert(num_registers < lengthof(this->aux_data->textref_stack)); + this->aux_data->textref_stack_grffile = grffile; + this->aux_data->textref_stack_size = num_registers; for (uint i = 0; i < num_registers; i++) { - textref_stack[i] = _temp_store.GetValue(0x100 + i); + this->aux_data->textref_stack[i] = _temp_store.GetValue(0x100 + i); } } @@ -1241,7 +1255,8 @@ int CommandCost::WriteSummaryMessage(char *buf, char *last, StringID cmd_msg) co if (this->Succeeded()) { return seprintf(buf, last, "Success: cost: " OTTD_PRINTF64, (int64) this->GetCost()); } else { - if (this->textref_stack_size > 0) StartTextRefStackUsage(this->textref_stack_grffile, this->textref_stack_size, textref_stack); + const uint textref_stack_size = this->GetTextRefStackSize(); + if (textref_stack_size > 0) StartTextRefStackUsage(this->GetTextRefStackGRF(), textref_stack_size, this->GetTextRefStack()); char *b = buf; b += seprintf(b, last, "Failed: cost: " OTTD_PRINTF64, (int64) this->GetCost()); @@ -1254,7 +1269,7 @@ int CommandCost::WriteSummaryMessage(char *buf, char *last, StringID cmd_msg) co b = GetString(b, this->message, last); } - if (this->textref_stack_size > 0) StopTextRefStackUsage(); + if (textref_stack_size > 0) StopTextRefStackUsage(); return b - buf; } diff --git a/src/command_type.h b/src/command_type.h index 4043825739..79b03e5c3b 100644 --- a/src/command_type.h +++ b/src/command_type.h @@ -22,26 +22,34 @@ struct GRFFile; * a possible error message/state together. */ class CommandCost { - ExpensesType expense_type; ///< the type of expence as shown on the finances view Money cost; ///< The cost of this action - StringID message; ///< Warning message for when success is unset + ExpensesType expense_type; ///< the type of expence as shown on the finances view bool success; ///< Whether the comment went fine up to this moment - const GRFFile *textref_stack_grffile; ///< NewGRF providing the #TextRefStack content. - uint textref_stack_size; ///< Number of uint32 values to put on the #TextRefStack for the error message. + StringID message; ///< Warning message for when success is unset StringID extra_message = INVALID_STRING_ID; ///< Additional warning message for when success is unset - static uint32 textref_stack[16]; + struct CommandCostAuxliaryData { + uint32 textref_stack[16] = {}; + const GRFFile *textref_stack_grffile = nullptr; ///< NewGRF providing the #TextRefStack content. + uint textref_stack_size = 0; ///< Number of uint32 values to put on the #TextRefStack for the error message. + }; + std::unique_ptr aux_data; public: /** * Creates a command cost return with no cost and no error */ - CommandCost() : expense_type(INVALID_EXPENSES), cost(0), message(INVALID_STRING_ID), success(true), textref_stack_grffile(nullptr), textref_stack_size(0) {} + CommandCost() : cost(0), expense_type(INVALID_EXPENSES), success(true), message(INVALID_STRING_ID) {} /** * Creates a command return value the is failed with the given message */ - explicit CommandCost(StringID msg) : expense_type(INVALID_EXPENSES), cost(0), message(msg), success(false), textref_stack_grffile(nullptr), textref_stack_size(0) {} + explicit CommandCost(StringID msg) : cost(0), expense_type(INVALID_EXPENSES), success(false), message(msg) {} + + CommandCost(const CommandCost &other); + CommandCost(CommandCost &&other) = default; + CommandCost &operator=(const CommandCost &other); + CommandCost &operator=(CommandCost &&other) = default; /** * Creates a command return value the is failed with the given message @@ -57,14 +65,14 @@ public: * Creates a command cost with given expense type and start cost of 0 * @param ex_t the expense type */ - explicit CommandCost(ExpensesType ex_t) : expense_type(ex_t), cost(0), message(INVALID_STRING_ID), success(true), textref_stack_grffile(nullptr), textref_stack_size(0) {} + explicit CommandCost(ExpensesType ex_t) : cost(0), expense_type(ex_t), success(true), message(INVALID_STRING_ID) {} /** * Creates a command return value with the given start cost and expense type * @param ex_t the expense type * @param cst the initial cost of this command */ - CommandCost(ExpensesType ex_t, const Money &cst) : expense_type(ex_t), cost(cst), message(INVALID_STRING_ID), success(true), textref_stack_grffile(nullptr), textref_stack_size(0) {} + CommandCost(ExpensesType ex_t, const Money &cst) : cost(cst), expense_type(ex_t), success(true), message(INVALID_STRING_ID) {} /** @@ -125,7 +133,7 @@ public: */ const GRFFile *GetTextRefStackGRF() const { - return this->textref_stack_grffile; + return this->aux_data != nullptr ? this->aux_data->textref_stack_grffile : 0; } /** @@ -134,7 +142,7 @@ public: */ uint GetTextRefStackSize() const { - return this->textref_stack_size; + return this->aux_data != nullptr ? this->aux_data->textref_stack_size : 0; } /** @@ -143,7 +151,7 @@ public: */ const uint32 *GetTextRefStack() const { - return textref_stack; + return this->aux_data != nullptr ? this->aux_data->textref_stack : nullptr; } /**