1
0
Fork 0

Codechange: explicitly initialise LeaugeTable and LeagueTableElement member variables

pull/13608/head
Rubidium 2025-02-18 17:15:18 +01:00 committed by rubidium42
parent 7e471bf04d
commit 35e7255a5a
3 changed files with 19 additions and 24 deletions

View File

@ -29,17 +29,19 @@ extern LeagueTablePool _league_table_pool;
* Each LeagueTable is composed of one or more elements. Elements are sorted by their rating (higher=better). * Each LeagueTable is composed of one or more elements. Elements are sorted by their rating (higher=better).
**/ **/
struct LeagueTableElement : LeagueTableElementPool::PoolItem<&_league_table_element_pool> { struct LeagueTableElement : LeagueTableElementPool::PoolItem<&_league_table_element_pool> {
LeagueTableID table; ///< Id of the table which this element belongs to LeagueTableID table = LeagueTableID::Invalid(); ///< Id of the table which this element belongs to
int64_t rating; ///< Value that determines ordering of elements in the table (higher=better) int64_t rating = 0; ///< Value that determines ordering of elements in the table (higher=better)
CompanyID company; ///< Company Id to show the color blob for or CompanyID::Invalid() CompanyID company = CompanyID::Invalid(); ///< Company Id to show the color blob for or CompanyID::Invalid()
std::string text; ///< Text of the element std::string text{}; ///< Text of the element
std::string score; ///< String representation of the score associated with the element std::string score{}; ///< String representation of the score associated with the element
Link link; ///< What opens when element is clicked Link link{}; ///< What opens when element is clicked
/** /**
* We need an (empty) constructor so struct isn't zeroed (as C++ standard states) * We need an (empty) constructor so struct isn't zeroed (as C++ standard states)
*/ */
LeagueTableElement() { } LeagueTableElement() { }
LeagueTableElement(LeagueTableID table, int64_t rating, CompanyID company, const std::string &text, const std::string &score, const Link &link) :
table(table), rating(rating), company(company), text(text), score(score), link(link) {}
/** /**
* (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
@ -50,14 +52,15 @@ struct LeagueTableElement : LeagueTableElementPool::PoolItem<&_league_table_elem
/** Struct about custom league tables */ /** Struct about custom league tables */
struct LeagueTable : LeagueTablePool::PoolItem<&_league_table_pool> { struct LeagueTable : LeagueTablePool::PoolItem<&_league_table_pool> {
std::string title; ///< Title of the table std::string title{}; ///< Title of the table
std::string header; ///< Text to show above the table std::string header{}; ///< Text to show above the table
std::string footer; ///< Text to show below the table std::string footer{}; ///< Text to show below the table
/** /**
* We need an (empty) constructor so struct isn't zeroed (as C++ standard states) * We need an (empty) constructor so struct isn't zeroed (as C++ standard states)
*/ */
LeagueTable() { } LeagueTable() { }
LeagueTable(const std::string &title, const std::string &header, const std::string &footer) : title(title), header(header), footer(footer) { }
/** /**
* (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

View File

@ -60,10 +60,7 @@ std::tuple<CommandCost, LeagueTableID> CmdCreateLeagueTable(DoCommandFlags flags
if (title.empty()) return { CMD_ERROR, LeagueTableID::Invalid() }; if (title.empty()) return { CMD_ERROR, LeagueTableID::Invalid() };
if (flags.Test(DoCommandFlag::Execute)) { if (flags.Test(DoCommandFlag::Execute)) {
LeagueTable *lt = new LeagueTable(); LeagueTable *lt = new LeagueTable(title, header, footer);
lt->title = title;
lt->header = header;
lt->footer = footer;
return { CommandCost(), lt->index }; return { CommandCost(), lt->index };
} }
@ -92,13 +89,7 @@ std::tuple<CommandCost, LeagueTableElementID> CmdCreateLeagueTableElement(DoComm
if (company != CompanyID::Invalid() && !Company::IsValidID(company)) return { CMD_ERROR, LeagueTableElementID::Invalid() }; if (company != CompanyID::Invalid() && !Company::IsValidID(company)) return { CMD_ERROR, LeagueTableElementID::Invalid() };
if (flags.Test(DoCommandFlag::Execute)) { if (flags.Test(DoCommandFlag::Execute)) {
LeagueTableElement *lte = new LeagueTableElement(); LeagueTableElement *lte = new LeagueTableElement(table, rating, company, text, score, link);
lte->table = table;
lte->rating = rating;
lte->company = company;
lte->text = text;
lte->score = score;
lte->link = link;
InvalidateWindowData(WC_COMPANY_LEAGUE, table); InvalidateWindowData(WC_COMPANY_LEAGUE, table);
return { CommandCost(), lte->index }; return { CommandCost(), lte->index };
} }

View File

@ -25,10 +25,11 @@ enum LinkType : uint8_t {
typedef uint32_t LinkTargetID; ///< Contains either tile, industry ID, town ID, story page ID or company ID typedef uint32_t LinkTargetID; ///< Contains either tile, industry ID, town ID, story page ID or company ID
struct Link { struct Link {
LinkType type; LinkType type = LT_NONE;
LinkTargetID target; LinkTargetID target = 0;
Link(LinkType type, LinkTargetID target): type{type}, target{target} {}
Link(): Link(LT_NONE, 0) {} Link() {}
Link(LinkType type, LinkTargetID target) : type{type}, target{target} {}
}; };
using LeagueTableID = PoolID<uint8_t, struct LeagueTableIDTag, 255, 0xFF>; ///< ID of a league table using LeagueTableID = PoolID<uint8_t, struct LeagueTableIDTag, 255, 0xFF>; ///< ID of a league table