aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2018-08-20 07:28:45 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2018-08-20 07:28:45 +0900
commitb3e90ac833424ef21a0f8947f74aa3ed445e16e1 (patch)
tree14026c5e9e147d5a6d3bea8f5e02384234b334c2 /lib
parent4e45218924babe6708fe2128574a3cf6341fe3c6 (diff)
downloadlibquotient-b3e90ac833424ef21a0f8947f74aa3ed445e16e1.tar.gz
libquotient-b3e90ac833424ef21a0f8947f74aa3ed445e16e1.zip
Settings: get<>(); pass arguments more efficiently
Settings::get() and SettingsGroup::get() allow concise and efficient type-specific interface to value(); instead of wrapping your defaultValue into a QVariant, just pass it to get() instead of value().
Diffstat (limited to 'lib')
-rw-r--r--lib/settings.h26
1 files changed, 22 insertions, 4 deletions
diff --git a/lib/settings.h b/lib/settings.h
index 27ec9ba5..0b3ecaff 100644
--- a/lib/settings.h
+++ b/lib/settings.h
@@ -51,6 +51,15 @@ namespace QMatrixClient
const QVariant &value);
Q_INVOKABLE QVariant value(const QString &key,
const QVariant &defaultValue = {}) const;
+
+ template <typename T>
+ T get(const QString& key, const T& defaultValue = {}) const
+ {
+ const auto qv = value(key, QVariant());
+ return qv.isValid() && qv.canConvert<T>() ? qv.value<T>()
+ : defaultValue;
+ }
+
Q_INVOKABLE bool contains(const QString& key) const;
Q_INVOKABLE QStringList childGroups() const;
@@ -67,14 +76,23 @@ namespace QMatrixClient
{
public:
template <typename... ArgTs>
- explicit SettingsGroup(const QString& path, ArgTs... qsettingsArgs)
- : Settings(qsettingsArgs...)
- , groupPath(path)
+ explicit SettingsGroup(QString path, ArgTs&&... qsettingsArgs)
+ : Settings(std::forward<ArgTs>(qsettingsArgs)...)
+ , groupPath(std::move(path))
{ }
Q_INVOKABLE bool contains(const QString& key) const;
Q_INVOKABLE QVariant value(const QString &key,
const QVariant &defaultValue = {}) const;
+
+ template <typename T>
+ T get(const QString& key, const T& defaultValue = {}) const
+ {
+ const auto qv = value(key, QVariant());
+ return qv.isValid() && qv.canConvert<T>() ? qv.value<T>()
+ : defaultValue;
+ }
+
Q_INVOKABLE QString group() const;
Q_INVOKABLE QStringList childGroups() const;
Q_INVOKABLE void setValue(const QString &key,
@@ -96,7 +114,7 @@ namespace QMatrixClient
#define QMC_DEFINE_SETTING(classname, type, propname, qsettingname, defaultValue, setter) \
type classname::propname() const \
{ \
- return value(QStringLiteral(qsettingname), defaultValue).value<type>(); \
+ return get<type>(QStringLiteral(qsettingname), defaultValue); \
} \
\
void classname::setter(type newValue) \