From be36337e46218c6f843a478036497a3f494502ed Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 1 Nov 2015 11:29:24 +0100 Subject: options: don't define options singleton in every module Instead, define in opentrack-api.so only. Also, move to opentrack-compat to break a circular dependency --- opentrack-compat/options.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 opentrack-compat/options.cpp (limited to 'opentrack-compat/options.cpp') diff --git a/opentrack-compat/options.cpp b/opentrack-compat/options.cpp new file mode 100644 index 00000000..91cd3664 --- /dev/null +++ b/opentrack-compat/options.cpp @@ -0,0 +1,16 @@ +#include "options.hpp" + +namespace options +{ + +namespace detail +{ +OPENTRACK_COMPAT_EXPORT opt_singleton& singleton() +{ + static auto ret = std::make_shared(); + return *ret; +} + +} + +} -- cgit v1.2.3 From e44b2853196565ebd22eb1e27109e5e54e77ed15 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 22 Nov 2015 15:13:47 +0100 Subject: compat/options: no need to heap-alloc here --- opentrack-compat/options.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'opentrack-compat/options.cpp') diff --git a/opentrack-compat/options.cpp b/opentrack-compat/options.cpp index 91cd3664..48cc514b 100644 --- a/opentrack-compat/options.cpp +++ b/opentrack-compat/options.cpp @@ -7,8 +7,8 @@ namespace detail { OPENTRACK_COMPAT_EXPORT opt_singleton& singleton() { - static auto ret = std::make_shared(); - return *ret; + static opt_singleton ret; + return ret; } } -- cgit v1.2.3 From 69a188510e14130e2aded0c9398a67d4e3ff4dcd Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 6 Dec 2015 19:53:06 +0100 Subject: compat/options: move from header --- opentrack-compat/options.cpp | 190 +++++++++++++++++++++++++++++++++++++++ opentrack-compat/options.hpp | 206 ++++++------------------------------------- 2 files changed, 218 insertions(+), 178 deletions(-) (limited to 'opentrack-compat/options.cpp') diff --git a/opentrack-compat/options.cpp b/opentrack-compat/options.cpp index 48cc514b..7938b075 100644 --- a/opentrack-compat/options.cpp +++ b/opentrack-compat/options.cpp @@ -13,4 +13,194 @@ OPENTRACK_COMPAT_EXPORT opt_singleton& singleton() } +group::group(const string& name) : name(name) +{ + auto conf = ini_file(); + auto q_name = QString::fromStdString(name); + conf->beginGroup(q_name); + for (auto& k_ : conf->childKeys()) + { + auto tmp = k_.toUtf8(); + string k(tmp); + kvs[k] = conf->value(k_); + } + conf->endGroup(); +} + +void group::save() +{ + auto s = ini_file(); + auto q_name = QString::fromStdString(name); + s->beginGroup(q_name); + for (auto& i : kvs) + { + auto k = QString::fromStdString(i.first); + s->setValue(k, i.second); + } + s->endGroup(); + s->sync(); +} + +void group::put(const std::string &s, const QVariant &d) +{ + kvs[s] = d; +} + +bool group::contains(const std::string &s) +{ + return kvs.count(s) != 0; +} + +QString group::ini_directory() +{ + const auto dirs = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation); + if (dirs.size() == 0) + return ""; + if (QDir(dirs[0]).mkpath(OPENTRACK_ORG)) + return dirs[0] + "/" OPENTRACK_ORG; + return ""; +} + +QString group::ini_filename() +{ + QSettings settings(OPENTRACK_ORG); + return settings.value(OPENTRACK_CONFIG_FILENAME_KEY, OPENTRACK_DEFAULT_CONFIG).toString(); +} + +QString group::ini_pathname() +{ + const auto dir = ini_directory(); + if (dir == "") + return ""; + QSettings settings(OPENTRACK_ORG); + return dir + "/" + settings.value(OPENTRACK_CONFIG_FILENAME_KEY, OPENTRACK_DEFAULT_CONFIG).toString(); +} + +const QStringList group::ini_list() +{ + const auto dirname = ini_directory(); + if (dirname == "") + return QStringList(); + QDir settings_dir(dirname); + return settings_dir.entryList( QStringList { "*.ini" } , QDir::Files, QDir::Name ); +} + +const mem group::ini_file() +{ + const auto pathname = ini_pathname(); + if (pathname != "") + return std::make_shared(ini_pathname(), QSettings::IniFormat); + return std::make_shared(); +} + +impl_bundle::impl_bundle(const std::string &group_name) + : + mtx(QMutex::Recursive), + group_name(group_name), + saved(group_name), + transient(saved), + modified(false) +{} + +void impl_bundle::reload() +{ + { + QMutexLocker l(&mtx); + saved = group(group_name); + transient = saved; + modified = false; + } + emit reloading(); +} + +void impl_bundle::store_kv(const std::string &name, const QVariant &datum) +{ + QMutexLocker l(&mtx); + + auto old = transient.get(name); + if (!transient.contains(name) || datum != old) + { + modified = true; + transient.put(name, datum); + } +} + +bool impl_bundle::contains(const std::string &name) +{ + QMutexLocker l(&mtx); + return transient.contains(name); +} + +void impl_bundle::save() +{ + { + QMutexLocker l(&mtx); + modified = false; + saved = transient; + transient.save(); + } + emit saving(); +} + +bool impl_bundle::modifiedp() +{ + QMutexLocker l(&mtx); + return modified; +} + +namespace detail +{ + +pbundle opt_singleton::bundle(const opt_singleton::k &key) +{ + QMutexLocker l(&implsgl_mtx); + + if (implsgl_data.count(key) != 0) + { + auto shared = std::get<1>(implsgl_data[key]).lock(); + if (shared != nullptr) + return shared; + } + + qDebug() << "bundle +" << QString::fromStdString(key); + + auto shr = std::make_shared(key); + implsgl_data[key] = tt(cnt(1), shr); + return shr; +} + +void opt_singleton::bundle_decf(const opt_singleton::k &key) +{ + QMutexLocker l(&implsgl_mtx); + + if (--std::get<0>(implsgl_data[key]) == 0) + implsgl_data.erase(key); +} + +opt_singleton::opt_singleton() : implsgl_mtx(QMutex::Recursive) {} + +} + +opt_bundle::opt_bundle(const std::string &group_name) + : impl_bundle(group_name) +{ +} + +opt_bundle::~opt_bundle() +{ + qDebug() << "bundle -" << QString::fromStdString(group_name); + detail::singleton().bundle_decf(group_name); +} + +base_value::base_value(pbundle b, const std::string &name) : b(b), self_name(name) {} + +opts::~opts() +{ + b->reload(); +} + +opts::opts(const std::string &name) : b(bundle(name)) {} + + + } diff --git a/opentrack-compat/options.hpp b/opentrack-compat/options.hpp index 3e4fb20e..81a53067 100644 --- a/opentrack-compat/options.hpp +++ b/opentrack-compat/options.hpp @@ -90,96 +90,26 @@ namespace options { } // snapshot of qsettings group at given time - class group { + class OPENTRACK_COMPAT_EXPORT group { private: map kvs; string name; public: - group(const string& name) : name(name) - { - auto conf = ini_file(); - auto q_name = QString::fromStdString(name); - conf->beginGroup(q_name); - for (auto& k_ : conf->childKeys()) - { - auto tmp = k_.toUtf8(); - string k(tmp); - kvs[k] = conf->value(k_); - } - conf->endGroup(); - } - - void save() - { - auto s = ini_file(); - auto q_name = QString::fromStdString(name); - s->beginGroup(q_name); - for (auto& i : kvs) - { - auto k = QString::fromStdString(i.first); - s->setValue(k, i.second); - } - s->endGroup(); - s->sync(); - } + group(const string& name); + void save(); + void put(const string& s, const QVariant& d); + bool contains(const string& s); + static QString ini_directory(); + static QString ini_filename(); + static QString ini_pathname(); + static const QStringList ini_list(); + static const mem ini_file(); template t get(const string& k) { return qcruft_to_t(kvs[k]); } - - void put(const string& s, const QVariant& d) - { - kvs[s] = d; - } - - bool contains(const string& s) - { - return kvs.count(s) != 0; - } - - static QString ini_directory() - { - const auto dirs = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation); - if (dirs.size() == 0) - return ""; - if (QDir(dirs[0]).mkpath(OPENTRACK_ORG)) - return dirs[0] + "/" OPENTRACK_ORG; - return ""; - } - - static QString ini_filename() - { - QSettings settings(OPENTRACK_ORG); - return settings.value(OPENTRACK_CONFIG_FILENAME_KEY, OPENTRACK_DEFAULT_CONFIG).toString(); - } - - static QString ini_pathname() - { - const auto dir = ini_directory(); - if (dir == "") - return ""; - QSettings settings(OPENTRACK_ORG); - return dir + "/" + settings.value(OPENTRACK_CONFIG_FILENAME_KEY, OPENTRACK_DEFAULT_CONFIG).toString(); - } - - static const QStringList ini_list() - { - const auto dirname = ini_directory(); - if (dirname == "") - return QStringList(); - QDir settings_dir(dirname); - return settings_dir.entryList( QStringList { "*.ini" } , QDir::Files, QDir::Name ); - } - - static const mem ini_file() - { - const auto pathname = ini_pathname(); - if (pathname != "") - return std::make_shared(ini_pathname(), QSettings::IniFormat); - return std::make_shared(); - } }; class OPENTRACK_COMPAT_EXPORT impl_bundle : public QObject { @@ -196,64 +126,20 @@ namespace options { void reloading(); void saving(); public: - impl_bundle(const string& group_name) : - mtx(QMutex::Recursive), - group_name(group_name), - saved(group_name), - transient(saved), - modified(false) - { - } - + impl_bundle(const string& group_name); string name() { return group_name; } - - void reload() { - { - QMutexLocker l(&mtx); - saved = group(group_name); - transient = saved; - modified = false; - } - emit reloading(); - } - - void store_kv(const string& name, const QVariant& datum) - { - QMutexLocker l(&mtx); - - auto old = transient.get(name); - if (!transient.contains(name) || datum != old) - { - modified = true; - transient.put(name, datum); - } - } - bool contains(const string& name) - { - QMutexLocker l(&mtx); - return transient.contains(name); - } + void reload(); + void store_kv(const string& name, const QVariant& datum); + bool contains(const string& name); + void save(); + bool modifiedp(); + template t get(const string& name) { QMutexLocker l(&mtx); return transient.get(name); } - void save() - { - { - QMutexLocker l(&mtx); - modified = false; - saved = transient; - transient.save(); - } - emit saving(); - } - - bool modifiedp() { - QMutexLocker l(&mtx); - return modified; - } }; class opt_bundle; @@ -272,33 +158,9 @@ namespace options { QMutex implsgl_mtx; map implsgl_data; public: - opt_singleton() : implsgl_mtx(QMutex::Recursive) {} - - pbundle bundle(const k& key) - { - QMutexLocker l(&implsgl_mtx); - - if (implsgl_data.count(key) != 0) - { - auto shared = std::get<1>(implsgl_data[key]).lock(); - if (shared != nullptr) - return shared; - } - - qDebug() << "bundle +" << QString::fromStdString(key); - - auto shr = std::make_shared(key); - implsgl_data[key] = tt(cnt(1), shr); - return shr; - } - - void bundle_decf(const k& key) - { - QMutexLocker l(&implsgl_mtx); - - if (--std::get<0>(implsgl_data[key]) == 0) - implsgl_data.erase(key); - } + opt_singleton(); + pbundle bundle(const k& key); + void bundle_decf(const k& key); }; OPENTRACK_COMPAT_EXPORT opt_singleton& singleton(); @@ -308,19 +170,12 @@ namespace options { static inline pbundle bundle(const string name) { return detail::singleton().bundle(name); } - class opt_bundle : public impl_bundle + class OPENTRACK_COMPAT_EXPORT opt_bundle : public impl_bundle { public: opt_bundle() : impl_bundle("i-have-no-name") {} - opt_bundle(const string& group_name) : impl_bundle(group_name) - { - } - - ~opt_bundle() - { - qDebug() << "bundle -" << QString::fromStdString(group_name); - detail::singleton().bundle_decf(group_name); - } + opt_bundle(const string& group_name); + ~opt_bundle(); }; class OPENTRACK_COMPAT_EXPORT base_value : public QObject @@ -330,7 +185,7 @@ namespace options { #define DEFINE_SIGNAL(t) void valueChanged(t) public: string name() { return self_name; } - base_value(pbundle b, const string& name) : b(b), self_name(name) {} + base_value(pbundle b, const string& name); signals: DEFINE_SIGNAL(double); DEFINE_SIGNAL(int); @@ -355,7 +210,7 @@ namespace options { virtual void reload() = 0; }; - static inline string string_from_qstring(const QString& datum) + static inline std::string string_from_qstring(const QString &datum) { auto tmp = datum.toUtf8(); return string(tmp.constData()); @@ -393,16 +248,11 @@ namespace options { t def; }; - struct opts + struct OPENTRACK_COMPAT_EXPORT opts { pbundle b; - - opts(const std::string& name) : b(bundle(name)) {} - - ~opts() - { - b->reload(); - } + opts(const std::string& name); + ~opts(); }; template -- cgit v1.2.3 From dcd4df5e49e6207273c961069d14ce42ad83d147 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 6 Dec 2015 20:02:18 +0100 Subject: compat/options: get rid of std::string usage It's pointless to use it here. --- opentrack-compat/options.cpp | 37 +++++++++++++------------------- opentrack-compat/options.hpp | 51 ++++++++++++++++++-------------------------- 2 files changed, 36 insertions(+), 52 deletions(-) (limited to 'opentrack-compat/options.cpp') diff --git a/opentrack-compat/options.cpp b/opentrack-compat/options.cpp index 7938b075..550fec24 100644 --- a/opentrack-compat/options.cpp +++ b/opentrack-compat/options.cpp @@ -13,15 +13,14 @@ OPENTRACK_COMPAT_EXPORT opt_singleton& singleton() } -group::group(const string& name) : name(name) +group::group(const QString& name) : name(name) { auto conf = ini_file(); - auto q_name = QString::fromStdString(name); - conf->beginGroup(q_name); + conf->beginGroup(name); for (auto& k_ : conf->childKeys()) { auto tmp = k_.toUtf8(); - string k(tmp); + QString k(tmp); kvs[k] = conf->value(k_); } conf->endGroup(); @@ -30,23 +29,19 @@ group::group(const string& name) : name(name) void group::save() { auto s = ini_file(); - auto q_name = QString::fromStdString(name); - s->beginGroup(q_name); + s->beginGroup(name); for (auto& i : kvs) - { - auto k = QString::fromStdString(i.first); - s->setValue(k, i.second); - } + s->setValue(i.first, i.second); s->endGroup(); s->sync(); } -void group::put(const std::string &s, const QVariant &d) +void group::put(const QString &s, const QVariant &d) { kvs[s] = d; } -bool group::contains(const std::string &s) +bool group::contains(const QString &s) { return kvs.count(s) != 0; } @@ -93,7 +88,7 @@ const mem group::ini_file() return std::make_shared(); } -impl_bundle::impl_bundle(const std::string &group_name) +impl_bundle::impl_bundle(const QString &group_name) : mtx(QMutex::Recursive), group_name(group_name), @@ -113,7 +108,7 @@ void impl_bundle::reload() emit reloading(); } -void impl_bundle::store_kv(const std::string &name, const QVariant &datum) +void impl_bundle::store_kv(const QString &name, const QVariant &datum) { QMutexLocker l(&mtx); @@ -125,7 +120,7 @@ void impl_bundle::store_kv(const std::string &name, const QVariant &datum) } } -bool impl_bundle::contains(const std::string &name) +bool impl_bundle::contains(const QString &name) { QMutexLocker l(&mtx); return transient.contains(name); @@ -162,7 +157,7 @@ pbundle opt_singleton::bundle(const opt_singleton::k &key) return shared; } - qDebug() << "bundle +" << QString::fromStdString(key); + qDebug() << "bundle +" << key; auto shr = std::make_shared(key); implsgl_data[key] = tt(cnt(1), shr); @@ -181,26 +176,24 @@ opt_singleton::opt_singleton() : implsgl_mtx(QMutex::Recursive) {} } -opt_bundle::opt_bundle(const std::string &group_name) +opt_bundle::opt_bundle(const QString &group_name) : impl_bundle(group_name) { } opt_bundle::~opt_bundle() { - qDebug() << "bundle -" << QString::fromStdString(group_name); + qDebug() << "bundle -" << group_name; detail::singleton().bundle_decf(group_name); } -base_value::base_value(pbundle b, const std::string &name) : b(b), self_name(name) {} +base_value::base_value(pbundle b, const QString &name) : b(b), self_name(name) {} opts::~opts() { b->reload(); } -opts::opts(const std::string &name) : b(bundle(name)) {} - - +opts::opts(const QString &name) : b(bundle(name)) {} } diff --git a/opentrack-compat/options.hpp b/opentrack-compat/options.hpp index 81a53067..d41c5cd1 100644 --- a/opentrack-compat/options.hpp +++ b/opentrack-compat/options.hpp @@ -10,7 +10,6 @@ #include #include #include -#include #include #include @@ -52,7 +51,6 @@ template using mem = std::shared_ptr; namespace options { template using map = std::map; - using std::string; template // don't elide usages of the function, qvariant default implicit @@ -92,13 +90,13 @@ namespace options { // snapshot of qsettings group at given time class OPENTRACK_COMPAT_EXPORT group { private: - map kvs; - string name; + map kvs; + QString name; public: - group(const string& name); + group(const QString& name); void save(); - void put(const string& s, const QVariant& d); - bool contains(const string& s); + void put(const QString& s, const QVariant& d); + bool contains(const QString& s); static QString ini_directory(); static QString ini_filename(); static QString ini_pathname(); @@ -106,7 +104,7 @@ namespace options { static const mem ini_file(); template - t get(const string& k) + t get(const QString& k) { return qcruft_to_t(kvs[k]); } @@ -116,7 +114,7 @@ namespace options { Q_OBJECT protected: QMutex mtx; - const string group_name; + const QString group_name; group saved; group transient; bool modified; @@ -126,16 +124,16 @@ namespace options { void reloading(); void saving(); public: - impl_bundle(const string& group_name); - string name() { return group_name; } + impl_bundle(const QString& group_name); + QString name() { return group_name; } void reload(); - void store_kv(const string& name, const QVariant& datum); - bool contains(const string& name); + void store_kv(const QString& name, const QVariant& datum); + bool contains(const QString& name); void save(); bool modifiedp(); template - t get(const string& name) + t get(const QString& name) { QMutexLocker l(&mtx); return transient.get(name); @@ -149,7 +147,7 @@ namespace options { struct OPENTRACK_COMPAT_EXPORT opt_singleton { public: - using k = std::string; + using k = QString; using v = opt_bundle; using cnt = int; using pbundle = std::shared_ptr; @@ -168,13 +166,13 @@ namespace options { using pbundle = std::shared_ptr; - static inline pbundle bundle(const string name) { return detail::singleton().bundle(name); } + static inline pbundle bundle(const QString name) { return detail::singleton().bundle(name); } class OPENTRACK_COMPAT_EXPORT opt_bundle : public impl_bundle { public: opt_bundle() : impl_bundle("i-have-no-name") {} - opt_bundle(const string& group_name); + opt_bundle(const QString& group_name); ~opt_bundle(); }; @@ -184,8 +182,8 @@ namespace options { #define DEFINE_SLOT(t) void setValue(t datum) { store(datum); } #define DEFINE_SIGNAL(t) void valueChanged(t) public: - string name() { return self_name; } - base_value(pbundle b, const string& name); + QString name() { return self_name; } + base_value(pbundle b, const QString& name); signals: DEFINE_SIGNAL(double); DEFINE_SIGNAL(int); @@ -193,7 +191,7 @@ namespace options { DEFINE_SIGNAL(QString); protected: pbundle b; - string self_name; + QString self_name; template void store(const t& datum) @@ -210,12 +208,6 @@ namespace options { virtual void reload() = 0; }; - static inline std::string string_from_qstring(const QString &datum) - { - auto tmp = datum.toUtf8(); - return string(tmp.constData()); - } - template class value : public base_value { public: @@ -226,7 +218,7 @@ namespace options { } static constexpr const Qt::ConnectionType DIRECT_CONNTYPE = Qt::DirectConnection; static constexpr const Qt::ConnectionType SAFE_CONNTYPE = Qt::UniqueConnection; - value(pbundle b, const string& name, t def) : base_value(b, name), def(def) + value(pbundle b, const QString& name, t def) : base_value(b, name), def(def) { QObject::connect(b.get(), SIGNAL(reloading()), this, SLOT(reload()), @@ -234,8 +226,7 @@ namespace options { if (!b->contains(name) || b->get(name).type() == QVariant::Invalid) *this = def; } - value(pbundle b, const QString& name, t def) : value(b, string_from_qstring(name), def) {} - value(pbundle b, const char* name, t def) : value(b, string(name), def) {} + value(pbundle b, const char* name, t def) : value(b, QString(name), def) {} operator t() const { @@ -251,7 +242,7 @@ namespace options { struct OPENTRACK_COMPAT_EXPORT opts { pbundle b; - opts(const std::string& name); + opts(const QString& name); ~opts(); }; -- cgit v1.2.3