From e44bc913ca3d212e5e8eea8c70c47d58c633f335 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 27 Sep 2014 20:58:23 +0200 Subject: options: move to base class for clarity --- facetracknoir/options.h | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'facetracknoir') diff --git a/facetracknoir/options.h b/facetracknoir/options.h index ce705033..291ac071 100644 --- a/facetracknoir/options.h +++ b/facetracknoir/options.h @@ -29,6 +29,8 @@ namespace options { template + // don't elide usages of the function, qvariant default implicit + // conversion results in nonsensical runtime behavior -sh inline T qcruft_to_t (const QVariant& t); template<> @@ -174,16 +176,28 @@ namespace options { } }; - typedef std::shared_ptr pbundle; + using pbundle = std::shared_ptr; class base_value : public QObject { Q_OBJECT public: base_value(pbundle b, const QString& name) : b(b), self_name(name), cookie_snap(0) {} - protected: virtual QVariant operator=(const QVariant& datum) = 0; + template + QVariant operator=(const T& datum) + { + return this->operator =(qVariantFromValue(datum)); + } + protected: pbundle b; QString self_name; + template + QVariant store(const T& datum) + { + b->store(self_name, qVariantFromValue(datum)); + emit valueChanged(datum); + return datum; + } void maybe_lazy_change() { long cookie = b->cookie(); @@ -211,33 +225,22 @@ namespace options { template class value : public base_value { - protected: + public: QVariant operator=(const QVariant& datum) { - auto foo = qcruft_to_t(datum); - b->store(self_name, qVariantFromValue(foo)); - emit valueChanged(foo); - return datum; + return store(qcruft_to_t(datum)); } - public: static constexpr const Qt::ConnectionType QT_CONNTYPE = Qt::UniqueConnection; static constexpr const Qt::ConnectionType OPT_CONNTYPE = Qt::UniqueConnection; - value(pbundle b, const QString& name, T def) : - base_value(b, name) + value(pbundle b, const QString& name, T def) : base_value(b, name) { if (!b->contains(name) || b->get(name).type() == QVariant::Invalid) - { this->operator=(qVariantFromValue(def)); - } } operator T() { maybe_lazy_change(); return b->get(self_name); } - QVariant operator=(const T& datum) - { - return this->operator =(qVariantFromValue(datum)); - } }; template -- cgit v1.2.3