Codechange: create a type for the "free_data" of NewsItems and (de)allocate it with new and delete

This commit is contained in:
rubidium42
2021-06-16 17:50:18 +02:00
committed by rubidium42
parent df601b8559
commit aa9818db90
7 changed files with 39 additions and 34 deletions

View File

@@ -115,6 +115,12 @@ struct NewsTypeData {
NewsDisplay GetDisplay() const;
};
/** Container for any custom data that must be deleted after the news item has reached end-of-life. */
struct NewsAllocatedData {
virtual ~NewsAllocatedData() {}
};
/** Information about a single item of news. */
struct NewsItem {
NewsItem *prev; ///< Previous news item
@@ -129,23 +135,29 @@ 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.
void *free_data; ///< Data to be freed when the news item has reached its end.
const NewsAllocatedData *data; ///< Custom data for the news item that have to be deallocated (deleted) when the news item has reached its end.
~NewsItem()
{
free(this->free_data);
delete this->data;
}
uint64 params[10]; ///< Parameters for string resolving.
};
/** Container for a single string to be passed as NewsAllocatedData. */
struct NewsStringData : NewsAllocatedData {
std::string string; ///< The string to retain.
NewsStringData(const std::string &str) : string(str) {}
};
/**
* Data that needs to be stored for company news messages.
* The problem with company news messages are the custom name
* of the companies and the fact that the company data is reset,
* resulting in wrong names and such.
*/
struct CompanyNewsInformation {
struct CompanyNewsInformation : NewsAllocatedData {
char company_name[64]; ///< The name of the company
char president_name[64]; ///< The name of the president
char other_company_name[64]; ///< The name of the company taking over this one