Improved thread safety for Settings.

Also moved some data from the global namespace to an anonymous namespace.
This commit is contained in:
Leon Styhre 2021-11-15 22:53:21 +01:00
parent 6bc4a09c9b
commit 81d6f0fd30
2 changed files with 43 additions and 54 deletions

View file

@ -19,46 +19,47 @@
#include <pugixml.hpp> #include <pugixml.hpp>
#include <vector> #include <vector>
Settings* Settings::sInstance = nullptr; namespace
// These values are NOT saved to es_settings.xml since they're not set via
// the in-program settings menu. Most can be set using command-line arguments,
// but some are debug flags that are either hardcoded or set by internal debug
// functions.
std::vector<std::string> settingsSkipSaving
{ {
// clang-format off // These values are NOT saved to es_settings.xml since they're not set via
// These options can be set using command-line arguments: // the in-program settings menu. Most can be set using command-line arguments,
"WindowWidth", // Set via --resolution [width] [height] // but some are debug flags that are either hardcoded or set by internal debug
"WindowHeight", // set via --resolution [width] [height] // functions.
"ParseGamelistOnly" // --gamelist-only std::vector<std::string> settingsSkipSaving
"IgnoreGamelist", // --ignore-gamelist {
"SplashScreen", // --no-splash // clang-format off
"Debug", // --debug // These options can be set using command-line arguments:
#if !defined(_WIN64) "WindowWidth", // Set via --resolution [width] [height]
"Windowed", // --windowed "WindowHeight", // set via --resolution [width] [height]
#endif "ParseGamelistOnly" // --gamelist-only
"VSync", // --vsync [1/on or 0/off] "IgnoreGamelist", // --ignore-gamelist
"ForceFull", // --force-full "SplashScreen", // --no-splash
"ForceKiosk", // --force-kiosk "Debug", // --debug
"ForceKid", // --force-kid #if !defined(_WIN64)
"Windowed", // --windowed
#endif
"VSync", // --vsync [1/on or 0/off]
"ForceFull", // --force-full
"ForceKiosk", // --force-kiosk
"ForceKid", // --force-kid
// These options are not shown in the --help text and are intended // These options are not shown in the --help text and are intended
// for debugging and testing purposes: // for debugging and testing purposes:
"ScreenWidth", // Set via --screensize [width] [height] "ScreenWidth", // Set via --screensize [width] [height]
"ScreenHeight", // set via --screensize [width] [height] "ScreenHeight", // set via --screensize [width] [height]
"ScreenOffsetX", // Set via --screenoffset [X] [Y] "ScreenOffsetX", // Set via --screenoffset [X] [Y]
"ScreenOffsetY", // Set via --screenoffset [X] [Y] "ScreenOffsetY", // Set via --screenoffset [X] [Y]
"ScreenRotate", // --screenrotate [0-3] "ScreenRotate", // --screenrotate [0-3]
// These options are not configurable from the command-line: // These options are not configurable from the command-line:
"DebugGrid", "DebugGrid",
"DebugText", "DebugText",
"DebugImage", "DebugImage",
"SplashScreenProgress", "SplashScreenProgress",
"ScraperFilter" "ScraperFilter"
// clang-format on // clang-format on
}; };
} // namespace
Settings::Settings() Settings::Settings()
{ {
@ -67,20 +68,10 @@ Settings::Settings()
loadFile(); loadFile();
} }
Settings* Settings::getInstance() std::shared_ptr<Settings> Settings::getInstance()
{ {
if (sInstance == nullptr) static std::shared_ptr<Settings> instance{std::shared_ptr<Settings>(new Settings)};
sInstance = new Settings(); return instance;
return sInstance;
}
void Settings::deinit()
{
if (sInstance) {
delete sInstance;
sInstance = nullptr;
}
} }
void Settings::setDefaults() void Settings::setDefaults()

View file

@ -11,14 +11,14 @@
#define ES_CORE_SETTINGS_H #define ES_CORE_SETTINGS_H
#include <map> #include <map>
#include <memory>
#include <string> #include <string>
// This is a singleton for storing settings. // This is a singleton for storing settings.
class Settings class Settings
{ {
public: public:
static Settings* getInstance(); static std::shared_ptr<Settings> getInstance();
static void deinit();
void loadFile(); void loadFile();
void saveFile(); void saveFile();
@ -39,8 +39,6 @@ public:
bool setString(const std::string& name, const std::string& value); bool setString(const std::string& name, const std::string& value);
private: private:
static Settings* sInstance;
Settings(); Settings();
// Clear everything and load default values. // Clear everything and load default values.