Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PollingControlProxy #1717

Merged
merged 2 commits into from
Jul 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/control/pollingcontrolproxy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#pragma once

#include <QSharedPointer>
#include <QString>

#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(ControlFlags flags = ControlFlag::None)
: PollingControlProxy(ConfigKey(), flags) {
}

PollingControlProxy(const QString& g, const QString& i, ControlFlags flags = ControlFlag::None)
: PollingControlProxy(ConfigKey(g, i), flags) {
}

PollingControlProxy(const ConfigKey& key, ControlFlags flags = ControlFlag::None) {
m_pControl = ControlDoublePrivate::getControl(key, flags);
if (!m_pControl) {
DEBUG_ASSERT(flags & ControlFlag::AllowMissingOrInvalid);
m_pControl = ControlDoublePrivate::getDefaultControl();
}
DEBUG_ASSERT(m_pControl);
}

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<ControlDoublePrivate> m_pControl;
};