mirror of https://github.com/OpenTTD/OpenTTD.git
Codechange: use std::string instead of stredup/free for goals
This commit is contained in:
parent
cc44aa7438
commit
3342967ad9
15
src/goal.cpp
15
src/goal.cpp
|
@ -84,8 +84,7 @@ std::tuple<CommandCost, GoalID> CmdCreateGoal(DoCommandFlag flags, CompanyID com
|
||||||
g->type = type;
|
g->type = type;
|
||||||
g->dst = dest;
|
g->dst = dest;
|
||||||
g->company = company;
|
g->company = company;
|
||||||
g->text = stredup(text.c_str());
|
g->text = text;
|
||||||
g->progress = nullptr;
|
|
||||||
g->completed = false;
|
g->completed = false;
|
||||||
|
|
||||||
if (g->company == INVALID_COMPANY) {
|
if (g->company == INVALID_COMPANY) {
|
||||||
|
@ -143,8 +142,7 @@ CommandCost CmdSetGoalText(DoCommandFlag flags, GoalID goal, const std::string &
|
||||||
|
|
||||||
if (flags & DC_EXEC) {
|
if (flags & DC_EXEC) {
|
||||||
Goal *g = Goal::Get(goal);
|
Goal *g = Goal::Get(goal);
|
||||||
free(g->text);
|
g->text = text;
|
||||||
g->text = stredup(text.c_str());
|
|
||||||
|
|
||||||
if (g->company == INVALID_COMPANY) {
|
if (g->company == INVALID_COMPANY) {
|
||||||
InvalidateWindowClassesData(WC_GOALS_LIST);
|
InvalidateWindowClassesData(WC_GOALS_LIST);
|
||||||
|
@ -170,12 +168,7 @@ CommandCost CmdSetGoalProgress(DoCommandFlag flags, GoalID goal, const std::stri
|
||||||
|
|
||||||
if (flags & DC_EXEC) {
|
if (flags & DC_EXEC) {
|
||||||
Goal *g = Goal::Get(goal);
|
Goal *g = Goal::Get(goal);
|
||||||
free(g->progress);
|
g->progress = text;
|
||||||
if (text.empty()) {
|
|
||||||
g->progress = nullptr;
|
|
||||||
} else {
|
|
||||||
g->progress = stredup(text.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (g->company == INVALID_COMPANY) {
|
if (g->company == INVALID_COMPANY) {
|
||||||
InvalidateWindowClassesData(WC_GOALS_LIST);
|
InvalidateWindowClassesData(WC_GOALS_LIST);
|
||||||
|
@ -255,7 +248,7 @@ CommandCost CmdGoalQuestion(DoCommandFlag flags, uint16 uniqueid, uint32 target,
|
||||||
if (company == INVALID_COMPANY && !Company::IsValidID(_local_company)) return CommandCost();
|
if (company == INVALID_COMPANY && !Company::IsValidID(_local_company)) return CommandCost();
|
||||||
if (company != INVALID_COMPANY && company != _local_company) return CommandCost();
|
if (company != INVALID_COMPANY && company != _local_company) return CommandCost();
|
||||||
}
|
}
|
||||||
ShowGoalQuestion(uniqueid, type, button_mask, text.c_str());
|
ShowGoalQuestion(uniqueid, type, button_mask, text);
|
||||||
}
|
}
|
||||||
|
|
||||||
return CommandCost();
|
return CommandCost();
|
||||||
|
|
|
@ -22,8 +22,8 @@ struct Goal : GoalPool::PoolItem<&_goal_pool> {
|
||||||
CompanyID company; ///< Goal is for a specific company; INVALID_COMPANY if it is global
|
CompanyID company; ///< Goal is for a specific company; INVALID_COMPANY if it is global
|
||||||
GoalType type; ///< Type of the goal
|
GoalType type; ///< Type of the goal
|
||||||
GoalTypeID dst; ///< Index of type
|
GoalTypeID dst; ///< Index of type
|
||||||
char *text; ///< Text of the goal.
|
std::string text; ///< Text of the goal.
|
||||||
char *progress; ///< Progress text of the goal.
|
std::string progress; ///< Progress text of the goal.
|
||||||
bool completed; ///< Is the goal completed or not?
|
bool completed; ///< Is the goal completed or not?
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -34,7 +34,7 @@ struct Goal : GoalPool::PoolItem<&_goal_pool> {
|
||||||
/**
|
/**
|
||||||
* (Empty) destructor has to be defined else operator delete might be called with nullptr parameter
|
* (Empty) destructor has to be defined else operator delete might be called with nullptr parameter
|
||||||
*/
|
*/
|
||||||
inline ~Goal() { free(this->text); free(this->progress); }
|
inline ~Goal() { }
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* GOAL_BASE_H */
|
#endif /* GOAL_BASE_H */
|
||||||
|
|
|
@ -212,7 +212,7 @@ struct GoalListWindow : public Window {
|
||||||
}
|
}
|
||||||
|
|
||||||
case GC_PROGRESS:
|
case GC_PROGRESS:
|
||||||
if (s->progress != nullptr) {
|
if (!s->progress.empty()) {
|
||||||
SetDParamStr(0, s->progress);
|
SetDParamStr(0, s->progress);
|
||||||
StringID str = s->completed ? STR_GOALS_PROGRESS_COMPLETE : STR_GOALS_PROGRESS;
|
StringID str = s->completed ? STR_GOALS_PROGRESS_COMPLETE : STR_GOALS_PROGRESS;
|
||||||
DrawString(r.WithWidth(progress_col_width, !rtl), str, TC_FROMSTRING, SA_RIGHT | SA_FORCE);
|
DrawString(r.WithWidth(progress_col_width, !rtl), str, TC_FROMSTRING, SA_RIGHT | SA_FORCE);
|
||||||
|
@ -242,7 +242,7 @@ struct GoalListWindow : public Window {
|
||||||
/* Calculate progress column width. */
|
/* Calculate progress column width. */
|
||||||
uint max_width = 0;
|
uint max_width = 0;
|
||||||
for (const Goal *s : Goal::Iterate()) {
|
for (const Goal *s : Goal::Iterate()) {
|
||||||
if (s->progress != nullptr) {
|
if (!s->progress.empty()) {
|
||||||
SetDParamStr(0, s->progress);
|
SetDParamStr(0, s->progress);
|
||||||
StringID str = s->completed ? STR_GOALS_PROGRESS_COMPLETE : STR_GOALS_PROGRESS;
|
StringID str = s->completed ? STR_GOALS_PROGRESS_COMPLETE : STR_GOALS_PROGRESS;
|
||||||
uint str_width = GetStringBoundingBox(str).width;
|
uint str_width = GetStringBoundingBox(str).width;
|
||||||
|
@ -322,14 +322,14 @@ void ShowGoalsList(CompanyID company)
|
||||||
|
|
||||||
/** Ask a question about a goal. */
|
/** Ask a question about a goal. */
|
||||||
struct GoalQuestionWindow : public Window {
|
struct GoalQuestionWindow : public Window {
|
||||||
char *question; ///< Question to ask (private copy).
|
std::string question; ///< Question to ask (private copy).
|
||||||
int buttons; ///< Number of valid buttons in #button.
|
int buttons; ///< Number of valid buttons in #button.
|
||||||
int button[3]; ///< Buttons to display.
|
int button[3]; ///< Buttons to display.
|
||||||
TextColour colour; ///< Colour of the question text.
|
TextColour colour; ///< Colour of the question text.
|
||||||
|
|
||||||
GoalQuestionWindow(WindowDesc *desc, WindowNumber window_number, TextColour colour, uint32 button_mask, const char *question) : Window(desc), colour(colour)
|
GoalQuestionWindow(WindowDesc *desc, WindowNumber window_number, TextColour colour, uint32 button_mask, const std::string &question) : Window(desc), colour(colour)
|
||||||
{
|
{
|
||||||
this->question = stredup(question);
|
this->question = question;
|
||||||
|
|
||||||
/* Figure out which buttons we have to enable. */
|
/* Figure out which buttons we have to enable. */
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
@ -352,10 +352,6 @@ struct GoalQuestionWindow : public Window {
|
||||||
this->FinishInitNested(window_number);
|
this->FinishInitNested(window_number);
|
||||||
}
|
}
|
||||||
|
|
||||||
~GoalQuestionWindow()
|
|
||||||
{
|
|
||||||
free(this->question);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SetStringParameters(int widget) const override
|
void SetStringParameters(int widget) const override
|
||||||
{
|
{
|
||||||
|
@ -554,7 +550,7 @@ static WindowDesc _goal_question_list_desc[] = {
|
||||||
* @param button_mask Buttons to display.
|
* @param button_mask Buttons to display.
|
||||||
* @param question Question to ask.
|
* @param question Question to ask.
|
||||||
*/
|
*/
|
||||||
void ShowGoalQuestion(uint16 id, byte type, uint32 button_mask, const char *question)
|
void ShowGoalQuestion(uint16 id, byte type, uint32 button_mask, const std::string &question)
|
||||||
{
|
{
|
||||||
assert(type < GQT_END);
|
assert(type < GQT_END);
|
||||||
new GoalQuestionWindow(&_goal_question_list_desc[type], id, type == 3 ? TC_WHITE : TC_BLACK, button_mask, question);
|
new GoalQuestionWindow(&_goal_question_list_desc[type], id, type == 3 ? TC_WHITE : TC_BLACK, button_mask, question);
|
||||||
|
|
|
@ -49,7 +49,7 @@ void ShowIndustryDirectory();
|
||||||
void ShowIndustryCargoesWindow();
|
void ShowIndustryCargoesWindow();
|
||||||
void ShowSubsidiesList();
|
void ShowSubsidiesList();
|
||||||
void ShowGoalsList(CompanyID company);
|
void ShowGoalsList(CompanyID company);
|
||||||
void ShowGoalQuestion(uint16 id, byte type, uint32 button_mask, const char *question);
|
void ShowGoalQuestion(uint16 id, byte type, uint32 button_mask, const std::string &question);
|
||||||
void ShowStoryBook(CompanyID company, uint16 page_id = INVALID_STORY_PAGE);
|
void ShowStoryBook(CompanyID company, uint16 page_id = INVALID_STORY_PAGE);
|
||||||
|
|
||||||
void ShowEstimatedCostOrIncome(Money cost, int x, int y);
|
void ShowEstimatedCostOrIncome(Money cost, int x, int y);
|
||||||
|
|
|
@ -20,8 +20,8 @@ static const SaveLoad _goals_desc[] = {
|
||||||
SLE_VAR(Goal, company, SLE_FILE_U16 | SLE_VAR_U8),
|
SLE_VAR(Goal, company, SLE_FILE_U16 | SLE_VAR_U8),
|
||||||
SLE_VAR(Goal, type, SLE_FILE_U16 | SLE_VAR_U8),
|
SLE_VAR(Goal, type, SLE_FILE_U16 | SLE_VAR_U8),
|
||||||
SLE_VAR(Goal, dst, SLE_UINT32),
|
SLE_VAR(Goal, dst, SLE_UINT32),
|
||||||
SLE_STR(Goal, text, SLE_STR | SLF_ALLOW_CONTROL, 0),
|
SLE_SSTR(Goal, text, SLE_STR | SLF_ALLOW_CONTROL),
|
||||||
SLE_CONDSTR(Goal, progress, SLE_STR | SLF_ALLOW_CONTROL, 0, SLV_182, SL_MAX_VERSION),
|
SLE_CONDSSTR(Goal, progress, SLE_STR | SLF_ALLOW_CONTROL, SLV_182, SL_MAX_VERSION),
|
||||||
SLE_CONDVAR(Goal, completed, SLE_BOOL, SLV_182, SL_MAX_VERSION),
|
SLE_CONDVAR(Goal, completed, SLE_BOOL, SLV_182, SL_MAX_VERSION),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue