diff --git a/src/tests/test_window_desc.cpp b/src/tests/test_window_desc.cpp index 85d2f6ebd8..39fa391068 100644 --- a/src/tests/test_window_desc.cpp +++ b/src/tests/test_window_desc.cpp @@ -19,7 +19,7 @@ * List of WindowDescs. Defined in window.cpp but not exposed as this unit-test is the only other place that needs it. * WindowDesc is a self-registering class so all WindowDescs will be included in the list. */ -extern std::vector *_window_descs; +extern std::vector &GetWindowDescs(); class WindowDescTestsFixture { @@ -32,7 +32,7 @@ TEST_CASE("WindowDesc - ini_key uniqueness") { std::set seen; - for (const WindowDesc *window_desc : *_window_descs) { + for (const WindowDesc *window_desc : GetWindowDescs()) { if (window_desc->ini_key == nullptr) continue; @@ -45,7 +45,7 @@ TEST_CASE("WindowDesc - ini_key uniqueness") TEST_CASE("WindowDesc - ini_key validity") { - const WindowDesc *window_desc = GENERATE(from_range(std::begin(*_window_descs), std::end(*_window_descs))); + const WindowDesc *window_desc = GENERATE(from_range(std::begin(GetWindowDescs()), std::end(GetWindowDescs()))); bool has_inikey = window_desc->ini_key != nullptr; bool has_widget = std::any_of(window_desc->nwid_begin, window_desc->nwid_end, [](const NWidgetPart &part) { return part.type == WWT_DEFSIZEBOX || part.type == WWT_STICKYBOX; }); @@ -76,7 +76,7 @@ static bool IsNWidgetTreeClosed(const NWidgetPart *nwid_begin, const NWidgetPart TEST_CASE("WindowDesc - NWidgetParts properly closed") { - const WindowDesc *window_desc = GENERATE(from_range(std::begin(*_window_descs), std::end(*_window_descs))); + const WindowDesc *window_desc = GENERATE(from_range(std::begin(GetWindowDescs()), std::end(GetWindowDescs()))); INFO(fmt::format("{}:{}", window_desc->source_location.file_name(), window_desc->source_location.line())); @@ -85,7 +85,7 @@ TEST_CASE("WindowDesc - NWidgetParts properly closed") TEST_CASE_METHOD(WindowDescTestsFixture, "WindowDesc - NWidgetPart validity") { - const WindowDesc *window_desc = GENERATE(from_range(std::begin(*_window_descs), std::end(*_window_descs))); + const WindowDesc *window_desc = GENERATE(from_range(std::begin(GetWindowDescs()), std::end(GetWindowDescs()))); INFO(fmt::format("{}:{}", window_desc->source_location.file_name(), window_desc->source_location.line())); diff --git a/src/window.cpp b/src/window.cpp index 012bab7537..03e73afe27 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -93,10 +93,14 @@ bool _mouse_hovering; ///< The mouse is hovering over the same point. SpecialMouseMode _special_mouse_mode; ///< Mode of the mouse. /** - * List of all WindowDescs. - * This is a pointer to ensure initialisation order with the various static WindowDesc instances. + * Get list of all WindowDescs. + * @return List of all WindowDescs. */ -std::vector *_window_descs = nullptr; +std::vector &GetWindowDescs() +{ + static std::vector *s_window_descs = new std::vector(); + return *s_window_descs; +} /** Config file to store WindowDesc */ std::string _windows_file; @@ -121,13 +125,14 @@ WindowDesc::WindowDesc(WindowPosition def_pos, const char *ini_key, int16_t def_ default_width_trad(def_width_trad), default_height_trad(def_height_trad) { - if (_window_descs == nullptr) _window_descs = new std::vector(); - _window_descs->push_back(this); + auto &window_descs = GetWindowDescs(); + window_descs.push_back(this); } WindowDesc::~WindowDesc() { - _window_descs->erase(std::find(_window_descs->begin(), _window_descs->end(), this)); + auto &window_descs = GetWindowDescs(); + window_descs.erase(std::find(window_descs.begin(), window_descs.end(), this)); } /** @@ -157,7 +162,7 @@ void WindowDesc::LoadFromConfig() { IniFile ini; ini.LoadFromDisk(_windows_file, NO_DIRECTORY); - for (WindowDesc *wd : *_window_descs) { + for (WindowDesc *wd : GetWindowDescs()) { if (wd->ini_key == nullptr) continue; IniLoadWindowSettings(ini, wd->ini_key, wd); } @@ -177,12 +182,14 @@ static bool DescSorter(WindowDesc * const &a, WindowDesc * const &b) */ void WindowDesc::SaveToConfig() { + auto &window_descs = GetWindowDescs(); + /* Sort the stuff to get a nice ini file on first write */ - std::sort(_window_descs->begin(), _window_descs->end(), DescSorter); + std::sort(window_descs.begin(), window_descs.end(), DescSorter); IniFile ini; ini.LoadFromDisk(_windows_file, NO_DIRECTORY); - for (WindowDesc *wd : *_window_descs) { + for (WindowDesc *wd : window_descs) { if (wd->ini_key == nullptr) continue; IniSaveWindowSettings(ini, wd->ini_key, wd); } diff --git a/src/window_gui.h b/src/window_gui.h index 30e1d6fd90..cdc97f3662 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -194,7 +194,7 @@ private: /** * Delete copy constructor to prevent compilers from - * copying the structure, which fails due to _window_descs. + * copying the structure, which fails due to WindowDesc registration. */ WindowDesc(const WindowDesc &) = delete; WindowDesc& operator=(const WindowDesc &) = delete;