diff --git a/src/story.cpp b/src/story.cpp index 0e9dc743eb..d8c46267b3 100644 --- a/src/story.cpp +++ b/src/story.cpp @@ -37,6 +37,15 @@ StoryPagePool _story_page_pool("StoryPage"); INSTANTIATE_POOL_METHODS(StoryPageElement) INSTANTIATE_POOL_METHODS(StoryPage) +StoryPage::~StoryPage() +{ + if (!this->CleaningPool()) { + for (StoryPageElement *spe : StoryPageElement::Iterate()) { + if (spe->page == this->index) delete spe; + } + } +} + /** * This helper for Create/Update PageElement Cmd procedure verifies if the page * element parameters are correct for the given page element type. @@ -219,11 +228,7 @@ std::tuple CmdCreateStoryPage(DoCommandFlags flags, Co _story_page_next_sort_value = 0; } - StoryPage *s = new StoryPage(); - s->sort_value = _story_page_next_sort_value; - s->date = TimerGameCalendar::date; - s->company = company; - s->title = text; + StoryPage *s = new StoryPage(_story_page_next_sort_value, TimerGameCalendar::date, company, text); InvalidateWindowClassesData(WC_STORY_BOOK, -1); if (StoryPage::GetNumItems() == 1) InvalidateWindowData(WC_MAIN_TOOLBAR, 0); @@ -267,10 +272,7 @@ std::tuple CmdCreateStoryPageElement(DoCommandF _story_page_element_next_sort_value = 0; } - StoryPageElement *pe = new StoryPageElement(); - pe->sort_value = _story_page_element_next_sort_value; - pe->type = type; - pe->page = page_id; + StoryPageElement *pe = new StoryPageElement(_story_page_element_next_sort_value, type, page_id); UpdateElement(*pe, tile, reference, text); InvalidateWindowClassesData(WC_STORY_BOOK, page_id); diff --git a/src/story_base.h b/src/story_base.h index 459a2bae2f..fe3fa94c9b 100644 --- a/src/story_base.h +++ b/src/story_base.h @@ -120,7 +120,7 @@ inline bool IsValidStoryPageButtonCursor(StoryPageButtonCursor cursor) /** Helper to construct packed "id" values for button-type StoryPageElement */ struct StoryPageButtonData { - uint32_t referenced_id; + uint32_t referenced_id = 0; void SetColour(Colours button_colour); void SetFlags(StoryPageButtonFlags flags); @@ -152,38 +152,32 @@ struct StoryPageElement : StoryPageElementPool::PoolItem<&_story_page_element_po /** * We need an (empty) constructor so struct isn't zeroed (as C++ standard states) */ - inline StoryPageElement() { } + StoryPageElement() { } + StoryPageElement(uint32_t sort_value, StoryPageElementType type, StoryPageID page) : + sort_value(sort_value), page(page), type(type) { } /** * (Empty) destructor has to be defined else operator delete might be called with nullptr parameter */ - inline ~StoryPageElement() { } + ~StoryPageElement() { } }; /** Struct about stories, current and completed */ struct StoryPage : StoryPagePool::PoolItem<&_story_page_pool> { - uint32_t sort_value; ///< A number that increases for every created story page. Used for sorting. The id of a story page is the pool index. - TimerGameCalendar::Date date; ///< Date when the page was created. - CompanyID company; ///< StoryPage is for a specific company; CompanyID::Invalid() if it is global + uint32_t sort_value = 0; ///< A number that increases for every created story page. Used for sorting. The id of a story page is the pool index. + TimerGameCalendar::Date date{}; ///< Date when the page was created. + CompanyID company = CompanyID::Invalid(); ///< StoryPage is for a specific company; CompanyID::Invalid() if it is global - std::string title; ///< Title of story page + std::string title; ///< Title of story page /** * We need an (empty) constructor so struct isn't zeroed (as C++ standard states) */ - inline StoryPage() { } + StoryPage() { } + StoryPage(uint32_t sort_value, TimerGameCalendar::Date date, CompanyID company, const std::string &title) : + sort_value(sort_value), date(date), company(company), title(title) {} - /** - * (Empty) destructor has to be defined else operator delete might be called with nullptr parameter - */ - inline ~StoryPage() - { - if (!this->CleaningPool()) { - for (StoryPageElement *spe : StoryPageElement::Iterate()) { - if (spe->page == this->index) delete spe; - } - } - } + ~StoryPage(); }; #endif /* STORY_BASE_H */