refs #545 CSetting<T>::setAndSave to set and save a setting in one step.

This commit is contained in:
Mathew Sutcliffe
2016-01-10 20:43:21 +00:00
parent d9167cf226
commit 956d393bb1
7 changed files with 33 additions and 6 deletions

View File

@@ -22,7 +22,9 @@ namespace BlackCore
{
CContextApplication::CContextApplication(CRuntimeConfig::ContextMode mode, CRuntime *runtime) :
IContextApplication(mode, runtime)
{}
{
connect(CSettingsCache::instance(), &CSettingsCache::valuesSaveRequested, CSettingsCache::instance(), &CSettingsCache::saveToStoreByPacket);
}
CContextApplication *CContextApplication::registerWithDBus(BlackMisc::CDBusServer *server)
{

View File

@@ -148,6 +148,9 @@ namespace BlackMisc
//! Deleted copy assignment operator.
CDataTrait &operator =(const CDataTrait &) = delete;
//! Data cache doesn't support setAndSave (because set() already causes save anyway).
CStatusMessage setAndSave(const T &value) = delete;
};
}

View File

@@ -8,6 +8,7 @@
*/
#include "settingscache.h"
#include "logmessage.h"
#include <QStandardPaths>
namespace BlackMisc
@@ -33,6 +34,15 @@ namespace BlackMisc
return saveToFiles(persistentStore(), keyPrefix);
}
void CSettingsCache::saveToStoreByPacket(const CValueCachePacket &values)
{
CStatusMessage status = saveToFiles(persistentStore(), values.toVariantMap());
if (! status.isEmpty())
{
CLogMessage(this).preformatted(status);
}
}
BlackMisc::CStatusMessage CSettingsCache::loadFromStore()
{
return loadFromFiles(persistentStore());

View File

@@ -34,6 +34,10 @@ namespace BlackMisc
//! Save core settings to disk.
BlackMisc::CStatusMessage saveToStore(const QString &keyPrefix = {});
//! Save individual settings to disk.
//! Core application context connects this slot to the valuesSaveRequested signal.
void saveToStoreByPacket(const CValueCachePacket &values);
//! Load core settings from disk.
BlackMisc::CStatusMessage loadFromStore();

View File

@@ -168,7 +168,7 @@ namespace BlackMisc
{
QMutexLocker lock(&m_mutex);
if (values.empty()) { return; }
CValueCachePacket ratifiedChanges;
CValueCachePacket ratifiedChanges(values.isSaved());
auto out = m_elements.lowerBound(values.cbegin().key());
auto end = m_elements.upperBound((values.cend() - 1).key());
for (auto in = values.cbegin(); in != values.cend(); ++in)
@@ -191,6 +191,7 @@ namespace BlackMisc
}
if (! ratifiedChanges.empty())
{
if (ratifiedChanges.isSaved()) { emit valuesSaveRequested(ratifiedChanges); }
emit valuesChanged(ratifiedChanges, nullptr);
}
}
@@ -395,7 +396,7 @@ namespace BlackMisc
return element.m_value.read();
}
CStatusMessage CValuePage::setValue(Element &element, const CVariant &value)
CStatusMessage CValuePage::setValue(Element &element, const CVariant &value, bool save)
{
Q_ASSERT(QThread::currentThread() == thread());
@@ -412,10 +413,10 @@ namespace BlackMisc
{
Q_ASSERT(isSafeToIncrement(element.m_pendingChanges));
element.m_pendingChanges++;
element.m_saved = false;
element.m_saved = save;
element.m_value.uniqueWrite() = value;
emit valuesWantToCache({ { { element.m_key, value } }, QDateTime::currentMSecsSinceEpoch() });
emit valuesWantToCache({ { { element.m_key, value } }, QDateTime::currentMSecsSinceEpoch(), save });
}
}
else

View File

@@ -180,6 +180,10 @@ namespace BlackMisc
//! of CValueCache instances in all processes including this one. The slot will do its own round-trip detection.
void valuesChangedByLocal(const BlackMisc::CValueCachePacket &values);
//! Emitted when this cache has ratified a change which included a request to save (i.e. via CCaches::setAndSave).
//! If the derived class does not handle such requests, the signal can be ignored.
void valuesSaveRequested(const BlackMisc::CValueCachePacket &values);
private:
struct Element; // remove forward declaration when elementsStartingWith uses C++14 auto deduced return type
@@ -274,6 +278,9 @@ namespace BlackMisc
//! Write a new value. Must be called from the thread in which the owner lives.
CStatusMessage set(const T &value) { return m_page.setValue(m_element, CVariant::from(value)); }
//! Write and save in the same step. Must be called from the thread in which the owner lives.
CStatusMessage setAndSave(const T &value) { return m_page.setValue(m_element, CVariant::from(value), true); }
//! Get the key string of this value.
const QString &getKey() const { return m_page.getKey(m_element); }

View File

@@ -62,7 +62,7 @@ namespace BlackMisc
const CVariant &getValue(const Element &element) const;
//! Write the value corresponding to the element's key and begin synchronizing it to any other pages.
CStatusMessage setValue(Element &element, const CVariant &value);
CStatusMessage setValue(Element &element, const CVariant &value, bool save = false);
//! Get the key string corresponding to the element.
const QString &getKey(const Element &element) const;