Skip to content

Commit

Permalink
Remove more redundant null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
daschuer committed Aug 19, 2021
1 parent b6ddf0f commit f4093e9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 48 deletions.
1 change: 0 additions & 1 deletion src/control/control.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ class ControlDoublePrivate : public QObject {
}
void deleteCreatorCO();


inline const ConfigKey& getKey() {
return m_key;
}
Expand Down
34 changes: 15 additions & 19 deletions src/control/controlobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
#include "util/stat.h"
#include "util/timer.h"

ControlObject::ControlObject() {
ControlObject::ControlObject()
: m_pControl(ControlDoublePrivate::getDefaultControl()) {
}

ControlObject::ControlObject(const ConfigKey& key,
Expand All @@ -37,15 +38,16 @@ ControlObject::ControlObject(const ConfigKey& key,
this,
&ControlObject::privateValueChanged,
Qt::DirectConnection);
} else {
m_pControl = ControlDoublePrivate::getDefaultControl();
}
}

ControlObject::~ControlObject() {
if (m_pControl) {
const bool success = m_pControl->resetCreatorCO(this);
Q_UNUSED(success);
DEBUG_ASSERT(success);
}
DEBUG_ASSERT(m_pControl);
const bool success = m_pControl->resetCreatorCO(this);
Q_UNUSED(success);
DEBUG_ASSERT(success);
}

// slot
Expand All @@ -67,13 +69,11 @@ ControlObject* ControlObject::getControl(const ConfigKey& key, ControlFlags flag
}

void ControlObject::setValueFromMidi(MidiOpCode o, double v) {
if (m_pControl) {
m_pControl->setValueFromMidi(o, v);
}
m_pControl->setValueFromMidi(o, v);
}

double ControlObject::getMidiParameter() const {
return m_pControl ? m_pControl->getMidiParameter() : 0.0;
return m_pControl->getMidiParameter();
}

// static
Expand All @@ -83,27 +83,23 @@ double ControlObject::get(const ConfigKey& key) {
}

double ControlObject::getParameter() const {
return m_pControl ? m_pControl->getParameter() : 0.0;
return m_pControl->getParameter();
}

double ControlObject::getParameterForValue(double value) const {
return m_pControl ? m_pControl->getParameterForValue(value) : 0.0;
return m_pControl->getParameterForValue(value);
}

double ControlObject::getParameterForMidi(double midiParameter) const {
return m_pControl ? m_pControl->getParameterForMidi(midiParameter) : 0.0;
return m_pControl->getParameterForMidi(midiParameter);
}

void ControlObject::setParameter(double v) {
if (m_pControl) {
m_pControl->setParameter(v, this);
}
m_pControl->setParameter(v, this);
}

void ControlObject::setParameterFrom(double v, QObject* pSender) {
if (m_pControl) {
m_pControl->setParameter(v, pSender);
}
m_pControl->setParameter(v, pSender);
}

// static
Expand Down
14 changes: 6 additions & 8 deletions src/control/controlproxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@
#include "control/control.h"
#include "moc_controlproxy.cpp"

namespace {
const ConfigKey kNullKey; // because we return it as a reference
} // namespace


ControlProxy::ControlProxy(const QString& g, const QString& i, QObject* pParent, ControlFlags flags)
: ControlProxy(ConfigKey(g, i), pParent, flags) {
}

ControlProxy::ControlProxy(const ConfigKey& key, QObject* pParent, ControlFlags flags)
: QObject(pParent),
m_pControl(nullptr) {
: QObject(pParent) {
initialize(key, flags);
}

Expand All @@ -32,12 +26,16 @@ void ControlProxy::initialize(const ConfigKey& key, ControlFlags flags) {
m_pControl = ControlDoublePrivate::getControl(key, flags);
DEBUG_ASSERT(m_pControl || flags.testFlag(ControlFlag::NoAssertIfMissing));
DEBUG_ASSERT(valid() || flags.testFlag(ControlFlag::NoAssertIfMissing));

if (!m_pControl) {
m_pControl = ControlDoublePrivate::getDefaultControl();
}
}

ControlProxy::~ControlProxy() {
//qDebug() << "ControlProxy::~ControlProxy()";
}

const ConfigKey& ControlProxy::getKey() const {
return m_pControl ? m_pControl->getKey() : kNullKey;
return m_pControl->getKey();
}
34 changes: 14 additions & 20 deletions src/control/controlproxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ControlProxy : public QObject {
bool connectValueChanged(Receiver receiver,
Slot func,
Qt::ConnectionType requestedConnectionType = Qt::AutoConnection) {
if (!m_pControl) {
if (!valid()) {
return false;
}

Expand Down Expand Up @@ -108,12 +108,12 @@ class ControlProxy : public QObject {
}

inline bool valid() const {
return m_pControl != nullptr;
return m_pControl->getKey().isValid();
}

/// Returns the value of the object. Thread safe, non-blocking.
inline double get() const {
return m_pControl ? m_pControl->get() : 0.0;
return m_pControl->get();
}

/// Returns the bool interpretation of the value. Thread safe, non-blocking.
Expand All @@ -123,42 +123,36 @@ class ControlProxy : public QObject {

/// Returns the parameterized value of the object. Thread safe, non-blocking.
inline double getParameter() const {
return m_pControl ? m_pControl->getParameter() : 0.0;
return m_pControl->getParameter();
}

/// Returns the parameterized value of the object. Thread safe, non-blocking.
inline double getParameterForValue(double value) const {
return m_pControl ? m_pControl->getParameterForValue(value) : 0.0;
return m_pControl->getParameterForValue(value);
}

/// Returns the normalized parameter of the object. Thread safe, non-blocking.
inline double getDefault() const {
return m_pControl ? m_pControl->defaultValue() : 0.0;
return m_pControl->defaultValue();
}

public slots:
/// Sets the control value to v. Thread safe, non-blocking.
void set(double v) {
if (m_pControl) {
m_pControl->set(v, this);
}
m_pControl->set(v, this);
}
/// Sets the control parameterized value to v. Thread safe, non-blocking.
void setParameter(double v) {
if (m_pControl) {
m_pControl->setParameter(v, this);
}
m_pControl->setParameter(v, this);
}
/// Resets the control to its default value. Thread safe, non-blocking.
void reset() {
if (m_pControl) {
// NOTE(rryan): This is important. The originator of this action does
// not know the resulting value so it makes sense that we should emit a
// general valueChanged() signal even though the change originated from
// us. For this reason, we provide nullptr here so that the change is
// not filtered in valueChanged()
m_pControl->reset();
}
// NOTE(rryan): This is important. The originator of this action does
// not know the resulting value so it makes sense that we should emit a
// general valueChanged() signal even though the change originated from
// us. For this reason, we provide NULL here so that the change is
// not filtered in valueChanged()
m_pControl->reset();
}

signals:
Expand Down

0 comments on commit f4093e9

Please sign in to comment.