From 9c7a7b53a192deef9dadf7f31d68c9a76cfaf35c Mon Sep 17 00:00:00 2001 From: Rubidium Date: Thu, 17 Jun 2021 16:38:34 +0200 Subject: [PATCH] Codechange: use a contructor for NewsItem to set the values And use std::unique_ptr to manage the memory of the allocated data --- src/news_gui.cpp | 46 ++++++++++++++++++++++++++-------------------- src/news_type.h | 9 +++------ 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/news_gui.cpp b/src/news_gui.cpp index fb03207150..4bd4bcecbb 100644 --- a/src/news_gui.cpp +++ b/src/news_gui.cpp @@ -355,7 +355,7 @@ struct NewsWindow : Window { break; case WID_N_MGR_NAME: - SetDParamStr(0, static_cast(this->ni->data)->president_name); + SetDParamStr(0, static_cast(this->ni->data.get())->president_name); str = STR_JUST_RAW_STRING; break; @@ -433,13 +433,13 @@ struct NewsWindow : Window { break; case WID_N_MGR_FACE: { - const CompanyNewsInformation *cni = static_cast(this->ni->data); + const CompanyNewsInformation *cni = static_cast(this->ni->data.get()); DrawCompanyManagerFace(cni->face, cni->colour, r.left, r.top); GfxFillRect(r.left, r.top, r.right, r.bottom, PALETTE_NEWSPAPER, FILLRECT_RECOLOUR); break; } case WID_N_MGR_NAME: { - const CompanyNewsInformation *cni = static_cast(this->ni->data); + const CompanyNewsInformation *cni = static_cast(this->ni->data.get()); SetDParamStr(0, cni->president_name); DrawStringMultiLine(r.left, r.right, r.top, r.bottom, STR_JUST_RAW_STRING, TC_FROMSTRING, SA_CENTER); break; @@ -770,6 +770,27 @@ static void DeleteNewsItem(NewsItem *ni) SetWindowDirty(WC_MESSAGE_HISTORY, 0); } +/** + * Create a new newsitem to be shown. + * @param string_id String to display. + * @param type The type of news. + * @param flags Flags related to how to display the news. + * @param reftype1 Type of ref1. + * @param ref1 Reference 1 to some object: Used for a possible viewport, scrolling after clicking on the news, and for deleting the news when the object is deleted. + * @param reftype2 Type of ref2. + * @param ref2 Reference 2 to some object: Used for scrolling after clicking on the news, and for deleting the news when the object is deleted. + * @param data Pointer to data that must be released once the news message is cleared. + * + * @see NewsSubtype + */ +NewsItem::NewsItem(StringID string_id, NewsType type, NewsFlag flags, NewsReferenceType reftype1, uint32 ref1, NewsReferenceType reftype2, uint32 ref2, const NewsAllocatedData *data) : + string_id(string_id), date(_date), type(type), flags(flags), reftype1(reftype1), reftype2(reftype2), ref1(ref1), ref2(ref2), data(data) +{ + /* show this news message in colour? */ + if (_cur_year >= _settings_client.gui.coloured_news_year) this->flags |= NF_INCOLOUR; + CopyOutDParam(this->params, 0, lengthof(this->params)); +} + /** * Add a new newsitem to be shown. * @param string String to display @@ -779,7 +800,7 @@ static void DeleteNewsItem(NewsItem *ni) * @param ref1 Reference 1 to some object: Used for a possible viewport, scrolling after clicking on the news, and for deleting the news when the object is deleted. * @param reftype2 Type of ref2 * @param ref2 Reference 2 to some object: Used for scrolling after clicking on the news, and for deleting the news when the object is deleted. - * @param free_data Pointer to data that must be freed once the news message is cleared + * @param data Pointer to data that must be released once the news message is cleared. * * @see NewsSubtype */ @@ -788,22 +809,7 @@ void AddNewsItem(StringID string, NewsType type, NewsFlag flags, NewsReferenceTy if (_game_mode == GM_MENU) return; /* Create new news item node */ - NewsItem *ni = new NewsItem; - - ni->string_id = string; - ni->type = type; - ni->flags = flags; - - /* show this news message in colour? */ - if (_cur_year >= _settings_client.gui.coloured_news_year) ni->flags |= NF_INCOLOUR; - - ni->reftype1 = reftype1; - ni->reftype2 = reftype2; - ni->ref1 = ref1; - ni->ref2 = ref2; - ni->data = data; - ni->date = _date; - CopyOutDParam(ni->params, 0, lengthof(ni->params)); + NewsItem *ni = new NewsItem(string, type, flags, reftype1, ref1, reftype2, ref2, data); if (_total_news++ == 0) { assert(_oldest_news == nullptr); diff --git a/src/news_type.h b/src/news_type.h index 8045a58fa9..4dea8c46f5 100644 --- a/src/news_type.h +++ b/src/news_type.h @@ -135,14 +135,11 @@ struct NewsItem { uint32 ref1; ///< Reference 1 to some object: Used for a possible viewport, scrolling after clicking on the news, and for deleting the news when the object is deleted. uint32 ref2; ///< Reference 2 to some object: Used for scrolling after clicking on the news, and for deleting the news when the object is deleted. - const NewsAllocatedData *data; ///< Custom data for the news item that have to be deallocated (deleted) when the news item has reached its end. - - ~NewsItem() - { - delete this->data; - } + std::unique_ptr data; ///< Custom data for the news item that will be deallocated (deleted) when the news item has reached its end. uint64 params[10]; ///< Parameters for string resolving. + + NewsItem(StringID string_id, NewsType type, NewsFlag flags, NewsReferenceType reftype1, uint32 ref1, NewsReferenceType reftype2, uint32 ref2, const NewsAllocatedData *data); }; /** Container for a single string to be passed as NewsAllocatedData. */