Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Lord-Grey committed Dec 31, 2022
1 parent 36e9630 commit 3157f3d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 20 deletions.
19 changes: 1 addition & 18 deletions include/mdns/MdnsBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

// Utility includes
#include <utils/Logger.h>
#include <utils/WeakConnect.h>

namespace {
constexpr std::chrono::milliseconds DEFAULT_DISCOVER_TIMEOUT{ 500 };
Expand Down Expand Up @@ -103,24 +104,6 @@ private slots:
void onServiceRemoved(const QMdnsEngine::Service& service);

private:

template <typename Func1, typename Func2, typename std::enable_if_t<!std::is_member_pointer<Func2>::value, int> = 0>
static inline QMetaObject::Connection weakConnect(typename QtPrivate::FunctionPointer<Func1>::Object* sender,
Func1 signal,
Func2 slot)
{
QMetaObject::Connection conn_normal = QObject::connect(sender, signal, slot);

QMetaObject::Connection* conn_delete = new QMetaObject::Connection();

*conn_delete = QObject::connect(sender, signal, [conn_normal, conn_delete]() {
QObject::disconnect(conn_normal);
QObject::disconnect(*conn_delete);
delete conn_delete;
});
return conn_normal;
}

/// The logger instance for mDNS-Service
Logger* _log;

Expand Down
63 changes: 63 additions & 0 deletions include/utils/WeakConnect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#ifndef WEAKCONNECT_H
#define WEAKCONNECT_H

#include <type_traits>

// Qt includes
#include <QObject>

template <typename Func1, typename Func2, typename std::enable_if_t<std::is_member_pointer<Func2>::value, int> = 0>
static inline QMetaObject::Connection weakConnect(typename QtPrivate::FunctionPointer<Func1>::Object* sender,
Func1 signal,
typename QtPrivate::FunctionPointer<Func2>::Object* receiver,
Func2 slot)
{
QMetaObject::Connection conn_normal = QObject::connect(sender, signal, receiver, slot);

QMetaObject::Connection* conn_delete = new QMetaObject::Connection();

*conn_delete = QObject::connect(sender, signal, [conn_normal, conn_delete]() {
QObject::disconnect(conn_normal);
QObject::disconnect(*conn_delete);
delete conn_delete;
});
return conn_normal;
}

template <typename Func1, typename Func2, typename std::enable_if_t<!std::is_member_pointer<Func2>::value, int> = 0>
static inline QMetaObject::Connection weakConnect(typename QtPrivate::FunctionPointer<Func1>::Object* sender,
Func1 signal,
Func2 slot)
{
QMetaObject::Connection conn_normal = QObject::connect(sender, signal, slot);

QMetaObject::Connection* conn_delete = new QMetaObject::Connection();

*conn_delete = QObject::connect(sender, signal, [conn_normal, conn_delete]() {
QObject::disconnect(conn_normal);
QObject::disconnect(*conn_delete);
delete conn_delete;
});
return conn_normal;
}

template <typename Func1, typename Func2, typename std::enable_if_t<!std::is_member_pointer<Func2>::value, int> = 0>
static inline QMetaObject::Connection weakConnect(typename QtPrivate::FunctionPointer<Func1>::Object* sender,
Func1 signal,
typename QtPrivate::FunctionPointer<Func2>::Object* receiver,
Func2 slot)
{
Q_UNUSED(receiver);
QMetaObject::Connection conn_normal = QObject::connect(sender, signal, slot);

QMetaObject::Connection* conn_delete = new QMetaObject::Connection();

*conn_delete = QObject::connect(sender, signal, [conn_normal, conn_delete]() {
QObject::disconnect(conn_normal);
QObject::disconnect(*conn_delete);
delete conn_delete;
});
return conn_normal;
}

#endif // WEAKCONNECT_H
11 changes: 9 additions & 2 deletions libsrc/api/JsonAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <hyperion/GrabberWrapper.h>
#include <grabber/QtGrabber.h>

#include <utils/WeakConnect.h>

#if defined(ENABLE_MF)
#include <grabber/MFGrabber.h>
#elif defined(ENABLE_V4L2)
Expand Down Expand Up @@ -1453,7 +1455,7 @@ void JsonAPI::handleAuthorizeCommand(const QJsonObject &message, const QString &
void JsonAPI::handleInstanceCommand(const QJsonObject &message, const QString &command, int tan)
{
const QString &subc = message["subcommand"].toString();
const quint8 &inst = message["instance"].toInt();
const quint8 &inst = static_cast<quint8>(message["instance"].toInt());
const QString &name = message["name"].toString();

if (subc == "switchTo")
Expand All @@ -1471,7 +1473,12 @@ void JsonAPI::handleInstanceCommand(const QJsonObject &message, const QString &c

if (subc == "startInstance")
{
connect(this, &API::onStartInstanceResponse, [=] (const int &tan) { sendSuccessReply(command + "-" + subc, tan); });
//Only send update once
weakConnect(this, &API::onStartInstanceResponse, [this, command, subc] (int tan)
{
sendSuccessReply(command + "-" + subc, tan);
});

if (!API::startInstance(inst, tan))
sendErrorReply("Can't start Hyperion instance index " + QString::number(inst), command + "-" + subc, tan);

Expand Down

0 comments on commit 3157f3d

Please sign in to comment.