Split rarely used CommandCost fields into aux struct via unique_ptr

Move static text ref stack into aux struct
This commit is contained in:
Jonathan G Rennison
2022-10-13 20:37:45 +01:00
parent 38c2fa3b66
commit 135dc547e2
2 changed files with 48 additions and 25 deletions

View File

@@ -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<CommandCostAuxliaryData> 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;
}
/**