summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2015-12-06 19:53:06 +0100
committerStanislaw Halik <sthalik@misaki.pl>2015-12-06 19:53:06 +0100
commit69a188510e14130e2aded0c9398a67d4e3ff4dcd (patch)
treedba07404c0bf633b055d257cd2162a8a9b34c4be
parentda8ec5c60841a926874c9ef2c4fed0078d47ebc8 (diff)
compat/options: move from header
-rw-r--r--opentrack-compat/options.cpp190
-rw-r--r--opentrack-compat/options.hpp206
2 files changed, 218 insertions, 178 deletions
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<QSettings> group::ini_file()
+{
+ const auto pathname = ini_pathname();
+ if (pathname != "")
+ return std::make_shared<QSettings>(ini_pathname(), QSettings::IniFormat);
+ return std::make_shared<QSettings>();
+}
+
+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<QVariant>(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<v>(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<string, QVariant> 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<QSettings> ini_file();
template<typename t>
t get(const string& k)
{
return qcruft_to_t<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<QSettings> ini_file()
- {
- const auto pathname = ini_pathname();
- if (pathname != "")
- return std::make_shared<QSettings>(ini_pathname(), QSettings::IniFormat);
- return std::make_shared<QSettings>();
- }
};
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<QVariant>(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<typename t>
t get(const string& name)
{
QMutexLocker l(&mtx);
return transient.get<t>(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<k, tt> 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<v>(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<typename t, typename q>