mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Move static instance of WindowDesc list to function.
This ensures the list cannot be accessed incorrectly and is not treated as a pointer.pull/12772/head
parent
68f7d585b4
commit
20ab943caa
|
@ -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<WindowDesc*> *_window_descs;
|
||||
extern std::vector<WindowDesc *> &GetWindowDescs();
|
||||
|
||||
|
||||
class WindowDescTestsFixture {
|
||||
|
@ -32,7 +32,7 @@ TEST_CASE("WindowDesc - ini_key uniqueness")
|
|||
{
|
||||
std::set<std::string> 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()));
|
||||
|
||||
|
|
|
@ -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<WindowDesc*> *_window_descs = nullptr;
|
||||
std::vector<WindowDesc *> &GetWindowDescs()
|
||||
{
|
||||
static std::vector<WindowDesc *> *s_window_descs = new std::vector<WindowDesc *>();
|
||||
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<WindowDesc*>();
|
||||
_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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue