From 142406f337760ea69ed39ff16fc051a95fbb7cfd Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 19 Dec 2020 22:32:55 -0500 Subject: [PATCH] Core: Add initial System class Introduces the system class that will eventually contain all relevant system state, as opposed to everything being distributed all over the place as global variables. Throughout the codebase we have code that from its interface-view, does not actually require its dependencies to be described in the interface, and we routinely run into issues with initialization where we sometimes make use of a facility before it's been initialized, which leads to annoying to debug cases, because the reader needs to run through the codebase and see what order things get initialized in, and how they're being used. This is particularly a frequent issue in the video code. Further, we also have a lot of code that makes use of file-scope variables (many of which are non-trivial), which must all be default initialized before the application can actually enter main(). While this may not be a huge issue in itself, some of these are allocating, which means that the application may need to use memory that it otherwise wouldn't need to (e.g. when a game isn't running, this excess memory is being used). Being able to wrap all these subsystems into objects would be nicer, since they can be constructed when they're actually needed. Them being objects also means we can better express dependencies on subsystems as types directly in the interface, making them explicit to the reader instead of a change randomly blowing up, said reader inspecting it, and finding out that something needed to be initialized beforehand. With the global turned into a function parameter, the dependency is explicit and they know just by reading it, that the given subsystem needs to be in a valid state before calling the function. For a prior example of an emulator that has moved to this model, see yuzu, which has been migrated off of global variables all over the place and replaced with a system instance (which has now reached the stage, where the singleton can be removed). --- Source/Core/Core/CMakeLists.txt | 2 ++ Source/Core/Core/Core.vcxproj | 2 ++ Source/Core/Core/Core.vcxproj.filters | 2 ++ Source/Core/Core/System.cpp | 18 ++++++++++++++ Source/Core/Core/System.h | 36 +++++++++++++++++++++++++++ 5 files changed, 60 insertions(+) create mode 100644 Source/Core/Core/System.cpp create mode 100644 Source/Core/Core/System.h diff --git a/Source/Core/Core/CMakeLists.txt b/Source/Core/Core/CMakeLists.txt index de8fd8ac93..03c87e6db3 100644 --- a/Source/Core/Core/CMakeLists.txt +++ b/Source/Core/Core/CMakeLists.txt @@ -41,6 +41,8 @@ add_library(core SyncIdentifier.h SysConf.cpp SysConf.h + System.cpp + System.h TitleDatabase.cpp TitleDatabase.h WiiRoot.cpp diff --git a/Source/Core/Core/Core.vcxproj b/Source/Core/Core/Core.vcxproj index a7ada5b18e..f672f088d1 100644 --- a/Source/Core/Core/Core.vcxproj +++ b/Source/Core/Core/Core.vcxproj @@ -371,6 +371,7 @@ + @@ -678,6 +679,7 @@ + diff --git a/Source/Core/Core/Core.vcxproj.filters b/Source/Core/Core/Core.vcxproj.filters index 749b81b9c2..ae2205c989 100644 --- a/Source/Core/Core/Core.vcxproj.filters +++ b/Source/Core/Core/Core.vcxproj.filters @@ -187,6 +187,7 @@ + @@ -1022,6 +1023,7 @@ + diff --git a/Source/Core/Core/System.cpp b/Source/Core/Core/System.cpp new file mode 100644 index 0000000000..7bc1b50c62 --- /dev/null +++ b/Source/Core/Core/System.cpp @@ -0,0 +1,18 @@ +// Copyright 2020 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include "Core/System.h" + +namespace Core +{ +struct System::Impl +{ +}; + +System::System() : m_impl{std::make_unique()} +{ +} + +System::~System() = default; +} // namespace Core diff --git a/Source/Core/Core/System.h b/Source/Core/Core/System.h new file mode 100644 index 0000000000..82b2c8af54 --- /dev/null +++ b/Source/Core/Core/System.h @@ -0,0 +1,36 @@ +// Copyright 2020 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#include + +namespace Core +{ +// Central class that encapsulates the running system. +class System +{ +public: + ~System(); + + System(const System&) = delete; + System& operator=(const System&) = delete; + + System(System&&) = delete; + System& operator=(System&&) = delete; + + // Intermediate instance accessor until global state is eliminated. + static System& GetInstance() + { + static System instance; + return instance; + } + +private: + System(); + + struct Impl; + std::unique_ptr m_impl; +}; +} // namespace Core