Skip to content

Commit

Permalink
Storage of the proxy pointer in ControlObjectScript
Browse files Browse the repository at this point in the history
Use of std::unique_ptr
  • Loading branch information
JoergAtGithub committed Dec 13, 2021
1 parent f7d00d3 commit cd4d6d4
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
25 changes: 12 additions & 13 deletions src/control/controlobjectscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ ControlObjectScript::ControlObjectScript(
const ConfigKey& key, const RuntimeLoggingCategory& logger, QObject* pParent)
: ControlProxy(key, pParent, ControlFlag::AllowMissingOrInvalid),
m_logger(logger) {
m_proxy = std::make_unique<CompressingProxy>(this);
}

bool ControlObjectScript::addScriptConnection(ScriptConnection* const conn) {
bool ControlObjectScript::addScriptConnection(const ScriptConnection& conn) {
if (m_scriptConnections.isEmpty()) {
// Only connect the slots when they are actually needed
// by script connections.
conn->proxy = new CompressingProxy(this);
connect(m_pControl.data(),
&ControlDoublePrivate::valueChanged,
conn->proxy,
m_proxy.get(),
&CompressingProxy::slotValueChanged,
Qt::QueuedConnection);
connect(conn->proxy,
connect(m_proxy.get(),
&CompressingProxy::signalValueChanged,
this,
&ControlObjectScript::slotValueChanged,
Expand All @@ -31,19 +31,19 @@ bool ControlObjectScript::addScriptConnection(ScriptConnection* const conn) {
}

for (const auto& priorConnection : qAsConst(m_scriptConnections)) {
if (*conn == priorConnection) {
qCWarning(m_logger) << "Connection " + conn->id.toString() +
if (conn == priorConnection) {
qCWarning(m_logger) << "Connection " + conn.id.toString() +
" already connected to (" +
conn->key.group + ", " + conn->key.item +
conn.key.group + ", " + conn.key.item +
"). Ignoring attempt to connect again.";
return false;
}
}

m_scriptConnections.append(*conn);
m_scriptConnections.append(conn);
qCDebug(m_logger) << "Connected (" +
conn->key.group + ", " + conn->key.item +
") to connection " + conn->id.toString();
conn.key.group + ", " + conn.key.item +
") to connection " + conn.id.toString();
return true;
}

Expand All @@ -62,17 +62,16 @@ bool ControlObjectScript::removeScriptConnection(const ScriptConnection& conn) {
// no ScriptConnections left, so disconnect signals
disconnect(m_pControl.data(),
&ControlDoublePrivate::valueChanged,
conn.proxy,
m_proxy.get(),
&CompressingProxy::slotValueChanged);
disconnect(conn.proxy,
disconnect(m_proxy.get(),
&CompressingProxy::signalValueChanged,
this,
&ControlObjectScript::slotValueChanged);
disconnect(this,
&ControlObjectScript::trigger,
this,
&ControlObjectScript::slotValueChanged);
delete (conn.proxy);
}
return success;
}
Expand Down
5 changes: 4 additions & 1 deletion src/control/controlobjectscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

#include <QVector>

#include "control/controlcompressingproxy.h"
#include "control/controlproxy.h"
#include "controllers/scripting/legacy/scriptconnection.h"
#include "util/memory.h"
#include "util/runtimeloggingcategory.h"

// this is used for communicate with controller scripts
Expand All @@ -14,7 +16,7 @@ class ControlObjectScript : public ControlProxy {
const RuntimeLoggingCategory& logger,
QObject* pParent = nullptr);

bool addScriptConnection(ScriptConnection* const conn);
bool addScriptConnection(const ScriptConnection& conn);

bool removeScriptConnection(const ScriptConnection& conn);

Expand All @@ -41,4 +43,5 @@ class ControlObjectScript : public ControlProxy {
private:
QVector<ScriptConnection> m_scriptConnections;
const RuntimeLoggingCategory m_logger;
std::unique_ptr<CompressingProxy> m_proxy;
};
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ QJSValue ControllerScriptInterfaceLegacy::makeConnection(
connection.callback = callback;
connection.id = QUuid::createUuid();

if (coScript->addScriptConnection(&connection)) {
if (coScript->addScriptConnection(connection)) {
return pJsEngine->newQObject(
new ScriptConnectionJSProxy(connection));
}
Expand Down
2 changes: 0 additions & 2 deletions src/controllers/scripting/legacy/scriptconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <QJSValue>
#include <QUuid>

#include "control/controlcompressingproxy.h"
#include "preferences/configobject.h"

class ControllerScriptEngineLegacy;
Expand All @@ -19,7 +18,6 @@ class ScriptConnection {
QJSValue callback;
ControllerScriptInterfaceLegacy* engineJSProxy;
ControllerScriptEngineLegacy* controllerEngine;
CompressingProxy* proxy;

void executeCallback(double value) const;

Expand Down
4 changes: 2 additions & 2 deletions src/test/controlobjectscripttest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class ControlObjectScriptTest : public MixxxTest {
conn2.callback = "mock_callback2";
conn2.id = QUuid::createUuid();

coScript1->addScriptConnection(&conn1);
coScript2->addScriptConnection(&conn2);
coScript1->addScriptConnection(conn1);
coScript2->addScriptConnection(conn2);
}

void TearDown() override {
Expand Down

0 comments on commit cd4d6d4

Please sign in to comment.