diff options
Diffstat (limited to 'options/bundle.cpp')
-rw-r--r-- | options/bundle.cpp | 191 |
1 files changed, 98 insertions, 93 deletions
diff --git a/options/bundle.cpp b/options/bundle.cpp index 4a133402..517005f0 100644 --- a/options/bundle.cpp +++ b/options/bundle.cpp @@ -8,133 +8,134 @@ #include "bundle.hpp" #include "value.hpp" +#include "globals.hpp" -#include <QThread> -#include <QApplication> +#include <cstdlib> -using options::base_value; +#include <QThread> +#include <QCoreApplication> -namespace options -{ +using namespace options; +using namespace options::globals; -namespace detail { +namespace options::detail { -bundle::bundle(const QString& group_name) - : mtx(QMutex::Recursive), +bundle::bundle(const QString& group_name) : group_name(group_name), saved(group_name), transient(saved) { } -bundle::~bundle() +bundle::~bundle() = default; + +void bundle::reload_no_notify() { + if (group_name.isEmpty()) + return; + + QMutexLocker l{&mtx}; + + saved = group(group_name); + transient = saved; } -void bundle::reload() +void bundle::notify() { - if (group_name.size()) { QMutexLocker l(&mtx); - saved = group(group_name); - const bool has_changes = is_modified(); - transient = saved; + connector::notify_all_values(); + } - if (has_changes) - { - connector::notify_all_values(); - emit reloading(); - emit changed(); - } + emit reloading(); + emit changed(); +} + +void bundle::reload() +{ + { + QMutexLocker l{&mtx}; + reload_no_notify(); + connector::notify_all_values(); } + emit reloading(); + emit changed(); } void bundle::set_all_to_default() { - QMutexLocker l(&mtx); - - forall([](const QString&, base_value* val) { set_base_value_to_default(val); }); - - if (is_modified()) - group::mark_ini_modified(); + connector::set_all_to_default_(); } -void bundle::store_kv(const QString& name, const QVariant& datum) +void bundle::store_kv(const QString& name, QVariant&& value) { - QMutexLocker l(&mtx); - - transient.put(name, datum); + if (group_name.isEmpty()) + return; - if (group_name.size()) + { + options::globals::detail::mark_ini_modified(); + QMutexLocker l{&mtx}; + transient.put(name, value); connector::notify_values(name); + } emit changed(); } +void bundle::store_kv(const QString& name, const QVariant& value) +{ + if (group_name.isEmpty()) + return; + + store_kv(name, QVariant{value}); +} + +QVariant bundle::get_variant(const QString& name) const +{ + QMutexLocker l{&mtx}; + return transient.get_variant(name); +} + bool bundle::contains(const QString &name) const { - QMutexLocker l(mtx); + QMutexLocker l{&mtx}; return transient.contains(name); } void bundle::save() { - if (QThread::currentThread() != qApp->thread()) + if (QThread::currentThread() != qApp->thread()) // NOLINT qDebug() << "group::save - current thread not ui thread"; - if (group_name.size() == 0) + if (group_name.isEmpty()) return; - bool modified_ = false; - { - QMutexLocker l(&mtx); - - if (is_modified()) - { - //qDebug() << "bundle" << group_name << "changed, saving"; - modified_ = true; - saved = transient; - saved.save(); - } + QMutexLocker l{&mtx}; + saved = transient; + saved.save(); } - if (modified_) - { - qDebug() << "saving" << name(); - emit saving(); - } + emit saving(); } -bool bundle::is_modified() const +void bundler::reload_no_notify_() { - QMutexLocker l(mtx); - - for (const auto& kv : transient.kvs) - { - const QVariant other = saved.get<QVariant>(kv.first); - if (!saved.contains(kv.first) || !is_equal(kv.first, kv.second, other)) - { - //if (logspam) - // qDebug() << "bundle" << group_name << "modified" << "key" << kv.first << "-" << other << "<>" << kv.second; - return true; - } - } + QMutexLocker l(&implsgl_mtx); - for (const auto& kv : saved.kvs) + for (auto& kv : implsgl_data) { - if (!transient.contains(kv.first)) + weak bundle = kv.second; + shared bundle_ = bundle.lock(); + if (bundle_) { - //if (logspam) - // qDebug() << "bundle" << group_name << "modified" << "key" << kv.first << "-" << other << "<>" << kv.second; - return true; + //qDebug() << "bundle: reverting" << kv.first << "due to profile change"; + bundle_->reload_no_notify(); } } - - return false; } -void bundler::after_profile_changed_() +void bundler::notify_() { QMutexLocker l(&implsgl_mtx); @@ -145,30 +146,31 @@ void bundler::after_profile_changed_() if (bundle_) { //qDebug() << "bundle: reverting" << kv.first << "due to profile change"; - bundle_->reload(); + bundle_->notify(); } } } -void bundler::refresh_all_bundles() +void bundler::reload_() { - singleton().after_profile_changed_(); + QMutexLocker l(&implsgl_mtx); + notify_(); + reload_no_notify_(); } -bundler::bundler() : implsgl_mtx(QMutex::Recursive) -{ -} +void bundler::notify() { singleton().notify_(); } +void bundler::reload_no_notify() { singleton().reload_no_notify_(); } +void bundler::reload() { singleton().reload_(); } -bundler::~bundler() -{ - //qDebug() << "exit: bundle singleton"; -} +bundler::bundler() = default; +bundler::~bundler() = default; -std::shared_ptr<bundler::v> bundler::make_bundle(const bundler::k& key) +std::shared_ptr<bundler::v> bundler::make_bundle_(const k& key) { QMutexLocker l(&implsgl_mtx); - auto it = implsgl_data.find(key); + using iter = decltype(implsgl_data.cbegin()); + const iter it = implsgl_data.find(key); if (it != implsgl_data.end()) { @@ -182,33 +184,36 @@ std::shared_ptr<bundler::v> bundler::make_bundle(const bundler::k& key) auto shr = shared(new v(key), [this, key](v* ptr) { QMutexLocker l(&implsgl_mtx); - auto it = implsgl_data.find(key); + const iter it = implsgl_data.find(key); if (it != implsgl_data.end()) - implsgl_data.erase(it); + (void)implsgl_data.erase(it); else - qDebug() << "ERROR: can't find self-bundle!"; + { + qCritical() << "ERROR: can't find self-bundle!"; + std::abort(); + } delete ptr; }); implsgl_data[key] = weak(shr); return shr; } -OTR_OPTIONS_EXPORT bundler& singleton() +bundler& bundler::singleton() { static bundler ret; return ret; } -QMutex* bundle::get_mtx() const { return mtx; } +} // ns options::detail -} // end options::detail +namespace options { -OTR_OPTIONS_EXPORT std::shared_ptr<bundle_> make_bundle(const QString& name) +std::shared_ptr<bundle_> make_bundle(const QString& name) { - if (name.size()) - return detail::singleton().make_bundle(name); + if (!name.isEmpty()) + return detail::bundler::singleton().make_bundle_(name); else return std::make_shared<bundle_>(QString()); } -} // end options +} // ns options |