From 3b697cdd5f9ae02526b4c55e252f04b7f40d82be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sch=C3=BCrmann?= Date: Wed, 20 Jun 2018 18:17:08 +0200 Subject: [PATCH] Added PollingControlProxy for a light ControlProxy replacement --- src/control/pollingcontrolproxy.h | 77 +++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/control/pollingcontrolproxy.h diff --git a/src/control/pollingcontrolproxy.h b/src/control/pollingcontrolproxy.h new file mode 100644 index 00000000000..132cda9cee9 --- /dev/null +++ b/src/control/pollingcontrolproxy.h @@ -0,0 +1,77 @@ +#pragma once + +#include +#include + +#include "control/control.h" + +/// This is the light version of a control proxy without the QObject overhead. +/// This should be used when no signal connections are used. +/// It is basically a PIMPL version of a ControlDoublePrivate Shared pointer +class PollingControlProxy { + public: + PollingControlProxy() { + initialize(ConfigKey()); + } + + PollingControlProxy(const QString& g, const QString& i) { + initialize(ConfigKey(g, i)); + } + + PollingControlProxy(const ConfigKey& key) { + initialize(key); + } + + void initialize(const ConfigKey& key) { + // Don't bother looking up the control if key is NULL. Prevents log spew. + if (key.isValid()) { + m_pControl = ControlDoublePrivate::getControl(key, ControlFlag::None); + } + + if (!m_pControl) { + m_pControl = ControlDoublePrivate::getDefaultControl(); + } + } + + bool valid() const { + return m_pControl->getKey().isValid(); + } + + /// Returns the value of the object. Thread safe, non-blocking. + double get() const { + return m_pControl->get(); + } + + /// Returns the bool interpretation of the value + bool toBool() const { + return get() > 0.0; + } + + /// Returns the parameterized value of the object. Thread safe, non-blocking. + double getParameter() const { + return m_pControl->getParameter(); + } + + /// Returns the parameterized value of the object. Thread safe, non-blocking. + double getParameterForValue(double value) const { + return m_pControl->getParameterForValue(value); + } + + /// Returns the normalized parameter of the object. Thread safe, non-blocking. + double getDefault() const { + return m_pControl->defaultValue(); + } + + /// Sets the control value to v. Thread safe, non-blocking. + void set(double v) { + m_pControl->set(v, nullptr); + } + /// Sets the control parameterized value to v. Thread safe, non-blocking. + void setParameter(double v) { + m_pControl->setParameter(v, nullptr); + } + + private: + // not null + QSharedPointer m_pControl; +};