Change CommandCost success bool to a flags field

This commit is contained in:
Jonathan G Rennison
2022-12-02 23:01:33 +00:00
parent c777c43be2
commit 523cf75a7a
2 changed files with 25 additions and 19 deletions

View File

@@ -1216,7 +1216,7 @@ CommandCost &CommandCost::operator=(const CommandCost &other)
{ {
this->cost = other.cost; this->cost = other.cost;
this->expense_type = other.expense_type; this->expense_type = other.expense_type;
this->success = other.success; this->flags = other.flags;
this->message = other.message; this->message = other.message;
this->extra_message = other.extra_message; this->extra_message = other.extra_message;
if (other.aux_data) { if (other.aux_data) {
@@ -1233,9 +1233,9 @@ CommandCost &CommandCost::operator=(const CommandCost &other)
void CommandCost::AddCost(const CommandCost &ret) void CommandCost::AddCost(const CommandCost &ret)
{ {
this->AddCost(ret.cost); this->AddCost(ret.cost);
if (this->success && !ret.success) { if (this->Succeeded() && !ret.Succeeded()) {
this->message = ret.message; this->message = ret.message;
this->success = false; this->flags &= ~CCIF_SUCCESS;
} }
} }

View File

@@ -17,6 +17,12 @@
struct GRFFile; struct GRFFile;
enum CommandCostIntlFlags : uint8 {
CCIF_NONE = 0,
CCIF_SUCCESS = 1 << 0,
};
DECLARE_ENUM_AS_BIT_SET(CommandCostIntlFlags)
/** /**
* Common return value for all commands. Wraps the cost and * Common return value for all commands. Wraps the cost and
* a possible error message/state together. * a possible error message/state together.
@@ -24,7 +30,7 @@ struct GRFFile;
class CommandCost { class CommandCost {
Money cost; ///< The cost of this action Money cost; ///< The cost of this action
ExpensesType expense_type; ///< the type of expence as shown on the finances view ExpensesType expense_type; ///< the type of expence as shown on the finances view
bool success; ///< Whether the comment went fine up to this moment CommandCostIntlFlags flags; ///< Flags: see CommandCostIntlFlags
StringID message; ///< Warning message for when success is unset StringID message; ///< Warning message for when success is unset
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
@@ -40,12 +46,12 @@ public:
/** /**
* Creates a command cost return with no cost and no error * Creates a command cost return with no cost and no error
*/ */
CommandCost() : cost(0), expense_type(INVALID_EXPENSES), success(true), message(INVALID_STRING_ID) {} CommandCost() : cost(0), expense_type(INVALID_EXPENSES), flags(CCIF_SUCCESS), 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) : cost(0), expense_type(INVALID_EXPENSES), success(false), message(msg) {} explicit CommandCost(StringID msg) : cost(0), expense_type(INVALID_EXPENSES), flags(CCIF_NONE), message(msg) {}
CommandCost(const CommandCost &other); CommandCost(const CommandCost &other);
CommandCost(CommandCost &&other) = default; CommandCost(CommandCost &&other) = default;
@@ -66,14 +72,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) : cost(0), expense_type(ex_t), success(true), message(INVALID_STRING_ID) {} explicit CommandCost(ExpensesType ex_t) : cost(0), expense_type(ex_t), flags(CCIF_SUCCESS), 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) : cost(cst), expense_type(ex_t), success(true), message(INVALID_STRING_ID) {} CommandCost(ExpensesType ex_t, const Money &cst) : cost(cst), expense_type(ex_t), flags(CCIF_SUCCESS), message(INVALID_STRING_ID) {}
/** /**
@@ -121,7 +127,7 @@ public:
void MakeError(StringID message, StringID extra_message = INVALID_STRING_ID) void MakeError(StringID message, StringID extra_message = INVALID_STRING_ID)
{ {
assert(message != INVALID_STRING_ID); assert(message != INVALID_STRING_ID);
this->success = false; this->flags &= ~CCIF_SUCCESS;
this->message = message; this->message = message;
this->extra_message = extra_message; this->extra_message = extra_message;
} }
@@ -161,7 +167,7 @@ public:
*/ */
StringID GetErrorMessage() const StringID GetErrorMessage() const
{ {
if (this->success) return INVALID_STRING_ID; if (this->Succeeded()) return INVALID_STRING_ID;
return this->message; return this->message;
} }
@@ -171,7 +177,7 @@ public:
*/ */
StringID GetExtraErrorMessage() const StringID GetExtraErrorMessage() const
{ {
if (this->success) return INVALID_STRING_ID; if (this->Succeeded()) return INVALID_STRING_ID;
return this->extra_message; return this->extra_message;
} }
@@ -181,7 +187,7 @@ public:
*/ */
inline bool Succeeded() const inline bool Succeeded() const
{ {
return this->success; return (this->flags & CCIF_SUCCESS);
} }
/** /**
@@ -190,7 +196,7 @@ public:
*/ */
inline bool Failed() const inline bool Failed() const
{ {
return !this->success; return !(this->flags & CCIF_SUCCESS);
} }
/** /**
@@ -216,14 +222,14 @@ public:
void MakeSuccessWithMessage() void MakeSuccessWithMessage()
{ {
assert(this->message != INVALID_STRING_ID); assert(this->message != INVALID_STRING_ID);
this->success = true; this->flags |= CCIF_SUCCESS;
} }
CommandCost UnwrapSuccessWithMessage() const CommandCost UnwrapSuccessWithMessage() const
{ {
assert(this->IsSuccessWithMessage()); assert(this->IsSuccessWithMessage());
CommandCost res = *this; CommandCost res = *this;
res.success = false; res.flags &= ~CCIF_SUCCESS;
return res; return res;
} }