diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2021-10-19 10:00:42 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2021-10-19 14:02:54 +0200 |
commit | 7e693ad7a6b2ee7c3f5c62a4f9819718b3b8b9f1 (patch) | |
tree | c0ab9353561d4dbf95637f9934a93a62eb3a23e4 | |
parent | 515a3f8e3a3e6091cc58798c36fe51859178143b (diff) |
options: don't notify on idempotent values
-rw-r--r-- | options/base-value.cpp | 5 | ||||
-rw-r--r-- | options/base-value.hpp | 2 | ||||
-rw-r--r-- | options/value.hpp | 26 |
3 files changed, 24 insertions, 9 deletions
diff --git a/options/base-value.cpp b/options/base-value.cpp index c7691a8f..9ccbbdf8 100644 --- a/options/base-value.cpp +++ b/options/base-value.cpp @@ -26,9 +26,8 @@ value_::~value_() b->on_value_destructed(this); } -void value_::maybe_trace(bool x) const +void value_::maybe_trace(const char* str) const { if (TRACE_NOTIFY) - qDebug().noquote() << "notify" << (x ? '+' : '-') - << QThread::currentThreadId() << b->name() << self_name; + qDebug().noquote() << str << QThread::currentThreadId() << b->name() << self_name; } diff --git a/options/base-value.hpp b/options/base-value.hpp index 9b2ebe53..6365f82a 100644 --- a/options/base-value.hpp +++ b/options/base-value.hpp @@ -67,7 +67,7 @@ protected: virtual void store_variant(QVariant&&) noexcept = 0; virtual void store_variant(const QVariant&) noexcept = 0; - void maybe_trace(bool x) const; + void maybe_trace(const char* str) const; template<typename t> void store_(const t& datum) diff --git a/options/value.hpp b/options/value.hpp index 10903b19..85b76cb6 100644 --- a/options/value.hpp +++ b/options/value.hpp @@ -39,7 +39,9 @@ template<typename t> class value final : public value_ { static_assert(std::is_same_v<t, remove_cvref_t<t>>); + mutable QMutex mtx; const t def; + mutable t cached_value; using traits = detail::value_traits<t>; never_inline @@ -95,11 +97,25 @@ public: never_inline void notify() const override { - if (!is_null()) + if (is_null()) + return; + + auto x = get(); { - maybe_trace(true); - emit valueChanged(traits::storage_from_value(get())); - maybe_trace(false); + QMutexLocker l(&mtx); + if (traits::is_equal(x, cached_value)) + { + //maybe_trace("notify ~"); + return; + } + else + { + cached_value = x; + l.unlock(); + maybe_trace("notify +"); + emit valueChanged(traits::storage_from_value(x)); + maybe_trace("notify -"); + } } } @@ -131,7 +147,7 @@ public: static constexpr Qt::ConnectionType DIRECT_CONNTYPE = Qt::DirectConnection; static constexpr Qt::ConnectionType SAFE_CONNTYPE = Qt::QueuedConnection; - value(bundle b, const QString& name, t def) noexcept : value_(b, name), def(std::move(def)) + value(bundle b, const QString& name, t def) noexcept : value_(b, name), def(std::move(def)), cached_value{get()} { } |