Split rarely used CommandCost fields into aux struct via unique_ptr

Move static text ref stack into aux struct
pull/444/head
Jonathan G Rennison 2 years ago
parent 38c2fa3b66
commit 135dc547e2

@ -1190,6 +1190,23 @@ CommandCost DoCommandPInternal(TileIndex tile, uint32 p1, uint32 p2, uint64 p3,
} }
#undef return_dcpi #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. * 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. * Activate usage of the NewGRF #TextRefStack for the error message.
* @param grffile NewGRF that provides the #TextRefStack * @param grffile NewGRF that provides the #TextRefStack
@ -1221,11 +1231,15 @@ void CommandCost::UseTextRefStack(const GRFFile *grffile, uint num_registers)
{ {
extern TemporaryStorageArray<int32, 0x110> _temp_store; extern TemporaryStorageArray<int32, 0x110> _temp_store;
assert(num_registers < lengthof(textref_stack)); if (!this->aux_data) {
this->textref_stack_grffile = grffile; this->aux_data.reset(new CommandCostAuxliaryData());
this->textref_stack_size = num_registers; }
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++) { 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()) { if (this->Succeeded()) {
return seprintf(buf, last, "Success: cost: " OTTD_PRINTF64, (int64) this->GetCost()); return seprintf(buf, last, "Success: cost: " OTTD_PRINTF64, (int64) this->GetCost());
} else { } 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; char *b = buf;
b += seprintf(b, last, "Failed: cost: " OTTD_PRINTF64, (int64) this->GetCost()); 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); b = GetString(b, this->message, last);
} }
if (this->textref_stack_size > 0) StopTextRefStackUsage(); if (textref_stack_size > 0) StopTextRefStackUsage();
return b - buf; return b - buf;
} }

@ -22,26 +22,34 @@ struct GRFFile;
* a possible error message/state together. * a possible error message/state together.
*/ */
class CommandCost { class CommandCost {
ExpensesType expense_type; ///< the type of expence as shown on the finances view
Money cost; ///< The cost of this action 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 bool success; ///< Whether the comment went fine up to this moment
const GRFFile *textref_stack_grffile; ///< NewGRF providing the #TextRefStack content. StringID message; ///< Warning message for when success is unset
uint textref_stack_size; ///< Number of uint32 values to put on the #TextRefStack for the error message.
StringID extra_message = INVALID_STRING_ID; ///< Additional 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: public:
/** /**
* Creates a command cost return with no cost and no error * 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 * 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 * 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 * Creates a command cost with given expense type and start cost of 0
* @param ex_t the expense type * @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 * Creates a command return value with the given start cost and expense type
* @param ex_t the expense type * @param ex_t the expense type
* @param cst the initial cost of this command * @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 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 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 const uint32 *GetTextRefStack() const
{ {
return textref_stack; return this->aux_data != nullptr ? this->aux_data->textref_stack : nullptr;
} }
/** /**

Loading…
Cancel
Save