From 65d8f65ed7be48098dabcc76b3b63a89038cb0ba Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Sat, 4 Jun 2016 18:39:46 -0700 Subject: [PATCH] Fix unix builds, make settings ACID --- libraries/shared/src/SettingInterface.h | 4 ++- libraries/shared/src/SettingManager.cpp | 41 +++++++++++++++---------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/libraries/shared/src/SettingInterface.h b/libraries/shared/src/SettingInterface.h index 8a868c5900..2b32e8a3b4 100644 --- a/libraries/shared/src/SettingInterface.h +++ b/libraries/shared/src/SettingInterface.h @@ -18,6 +18,8 @@ #include namespace Setting { + class Manager; + void preInit(); void init(); void cleanupSettings(); @@ -26,7 +28,7 @@ namespace Setting { public: static const QString FIRST_RUN; - QString getKey() const { return _key; } + const QString& getKey() const { return _key; } bool isSet() const { return _isSet; } virtual void setVariant(const QVariant& variant) = 0; diff --git a/libraries/shared/src/SettingManager.cpp b/libraries/shared/src/SettingManager.cpp index abe6d50f2e..bacec2ee0c 100644 --- a/libraries/shared/src/SettingManager.cpp +++ b/libraries/shared/src/SettingManager.cpp @@ -50,12 +50,21 @@ namespace Setting { } void Manager::loadSetting(Interface* handle) { - handle->setVariant(value(handle->getKey())); + const auto& key = handle->getKey(); + withWriteLock([&] { + QVariant loadedValue; + if (_pendingChanges.contains(key)) { + loadedValue = _pendingChanges[key]; + } else { + loadedValue = value(key); + } + handle->setVariant(loadedValue); + }); } void Manager::saveSetting(Interface* handle) { - auto key = handle->getKey(); + const auto& key = handle->getKey(); QVariant handleValue = UNSET_VALUE; if (handle->isSet()) { handleValue = handle->getVariant(); @@ -85,24 +94,22 @@ namespace Setting { } void Manager::saveAll() { - QHash newPendingChanges; withWriteLock([&] { - newPendingChanges.swap(_pendingChanges); + for (auto key : _pendingChanges.keys()) { + auto newValue = _pendingChanges[key]; + auto savedValue = value(key, UNSET_VALUE); + if (newValue == savedValue) { + continue; + } + if (newValue == UNSET_VALUE) { + remove(key); + } else { + setValue(key, newValue); + } + } + _pendingChanges.clear(); }); - for (auto key : newPendingChanges.keys()) { - auto newValue = newPendingChanges[key]; - auto savedValue = value(key, UNSET_VALUE); - if (newValue == savedValue) { - continue; - } - if (newValue == UNSET_VALUE) { - remove(key); - } else { - setValue(key, newValue); - } - } - // Restart timer if (_saveTimer) { _saveTimer->start();