From bd9948bbffd7dbc2f944823b76b51d499f9b32ed Mon Sep 17 00:00:00 2001 From: Mat Sutcliffe Date: Tue, 25 Dec 2018 14:27:20 +0000 Subject: [PATCH] Ref T486 Using QMetaObject::invokeMethod with pointer-to-member-function (or functor). --- src/blackcore/context/contextsimulatorimpl.cpp | 6 +++--- src/blackcore/db/databasereader.cpp | 1 + src/blackcore/vatsim/vatsimstatusfilereader.cpp | 2 +- src/blackmisc/loghandler.cpp | 5 +++-- src/blackmisc/worker.cpp | 2 +- src/blackmisc/worker.h | 4 ++-- tests/blackmisc/testvaluecache/testvaluecache.cpp | 8 ++++---- 7 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/blackcore/context/contextsimulatorimpl.cpp b/src/blackcore/context/contextsimulatorimpl.cpp index 942a3eb29..c130ba052 100644 --- a/src/blackcore/context/contextsimulatorimpl.cpp +++ b/src/blackcore/context/contextsimulatorimpl.cpp @@ -129,7 +129,7 @@ namespace BlackCore ISimulatorListener *listener = m_plugins->getListener(simulatorInfo.getIdentifier()); Q_ASSERT(listener); - QMetaObject::invokeMethod(listener, "stop", Qt::QueuedConnection); + QMetaObject::invokeMethod(listener, &ISimulatorListener::stop, Qt::QueuedConnection); } int CContextSimulator::checkListeners() @@ -480,7 +480,7 @@ namespace BlackCore // start if not already running if (!listener->isRunning()) { - const bool s = QMetaObject::invokeMethod(listener, "start", Qt::QueuedConnection); + const bool s = QMetaObject::invokeMethod(listener, &ISimulatorListener::start, Qt::QueuedConnection); Q_ASSERT_X(s, Q_FUNC_INFO, "cannot invoke method"); Q_UNUSED(s); } @@ -993,7 +993,7 @@ namespace BlackCore ISimulatorListener *listener = m_plugins->getListener(info.getIdentifier()); if (listener) { - const bool s = QMetaObject::invokeMethod(listener, "stop"); + const bool s = QMetaObject::invokeMethod(listener, &ISimulatorListener::stop); Q_ASSERT_X(s, Q_FUNC_INFO, "Cannot invoke stop"); Q_UNUSED(s); } diff --git a/src/blackcore/db/databasereader.cpp b/src/blackcore/db/databasereader.cpp index fe04144e9..b3878e8b4 100644 --- a/src/blackcore/db/databasereader.cpp +++ b/src/blackcore/db/databasereader.cpp @@ -220,6 +220,7 @@ namespace BlackCore if (entities == CEntityFlags::NoEntity) { return; } if (!this->isInternetAccessible(QStringLiteral("No network/internet access, will not read %1").arg(CEntityFlags::flagToString(entities)))) { return; } + //! \todo MS 2018-12 Error: CDatabaseReader has no ps_read method const bool s = QMetaObject::invokeMethod(this, "ps_read", Q_ARG(BlackMisc::Network::CEntityFlags::Entity, entities), Q_ARG(BlackMisc::Db::CDbFlags::DataRetrievalModeFlag, mode), diff --git a/src/blackcore/vatsim/vatsimstatusfilereader.cpp b/src/blackcore/vatsim/vatsimstatusfilereader.cpp index 96d7a6f3f..ea7eb3e72 100644 --- a/src/blackcore/vatsim/vatsimstatusfilereader.cpp +++ b/src/blackcore/vatsim/vatsimstatusfilereader.cpp @@ -47,7 +47,7 @@ namespace BlackCore void CVatsimStatusFileReader::readInBackgroundThread() { - const bool s = QMetaObject::invokeMethod(this, "ps_read"); + const bool s = QMetaObject::invokeMethod(this, &CVatsimStatusFileReader::ps_read); Q_ASSERT_X(s, Q_FUNC_INFO, "Invoke failed"); Q_UNUSED(s); } diff --git a/src/blackmisc/loghandler.cpp b/src/blackmisc/loghandler.cpp index 0a11fa840..4c5cb6fe1 100644 --- a/src/blackmisc/loghandler.cpp +++ b/src/blackmisc/loghandler.cpp @@ -44,6 +44,7 @@ namespace BlackMisc void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &message) { const CStatusMessage statusMessage(type, context, message); + const auto invokee = [statusMessage] { CLogHandler::instance()->logLocalMessage(statusMessage); }; #if defined(Q_CC_MSVC) && defined(QT_NO_DEBUG) if (type == QtFatalMsg) { MessageBoxW(nullptr, message.toStdWString().c_str(), nullptr, MB_OK); } #endif @@ -51,10 +52,10 @@ namespace BlackMisc { // Fatal message means this thread is about to crash the application. A queued connection would be useless. // Blocking queued connection means we pause this thread just long enough to let the main thread handle the message. - QMetaObject::invokeMethod(CLogHandler::instance(), "logLocalMessage", Qt::BlockingQueuedConnection, Q_ARG(BlackMisc::CStatusMessage, statusMessage)); + QMetaObject::invokeMethod(CLogHandler::instance(), invokee, Qt::BlockingQueuedConnection); return; } - QMetaObject::invokeMethod(CLogHandler::instance(), "logLocalMessage", Q_ARG(BlackMisc::CStatusMessage, statusMessage)); + QMetaObject::invokeMethod(CLogHandler::instance(), invokee); } void CLogHandler::install(bool skipIfAlreadyInstalled) diff --git a/src/blackmisc/worker.cpp b/src/blackmisc/worker.cpp index 98afb2c53..1624b09af 100644 --- a/src/blackmisc/worker.cpp +++ b/src/blackmisc/worker.cpp @@ -73,7 +73,7 @@ namespace BlackMisc worker->setObjectName(name); worker->moveToThread(thread); - const bool s = QMetaObject::invokeMethod(worker, "ps_runTask"); + const bool s = QMetaObject::invokeMethod(worker, &CWorker::ps_runTask); Q_ASSERT_X(s, Q_FUNC_INFO, "cannot invoke"); Q_UNUSED(s); thread->start(); diff --git a/src/blackmisc/worker.h b/src/blackmisc/worker.h index b500ab2dd..ac2ac212d 100644 --- a/src/blackmisc/worker.h +++ b/src/blackmisc/worker.h @@ -54,7 +54,7 @@ namespace BlackMisc template void singleShot(int msec, QObject *target, F &&task) { - QSharedPointer timer(new QTimer, [](QObject * o) { QMetaObject::invokeMethod(o, "deleteLater"); }); + QSharedPointer timer(new QTimer, [](QObject * o) { QMetaObject::invokeMethod(o, &QObject::deleteLater); }); timer->setSingleShot(true); timer->moveToThread(target->thread()); QObject::connect(timer.data(), &QTimer::timeout, target, [trace = getStackTrace(), task = std::forward(task), timer]() mutable @@ -63,7 +63,7 @@ namespace BlackMisc timer.clear(); task(); }); - QMetaObject::invokeMethod(timer.data(), "start", Q_ARG(int, msec)); + QMetaObject::invokeMethod(timer.data(), [t = timer.data(), msec] { t->start(msec); }); } //! @} diff --git a/tests/blackmisc/testvaluecache/testvaluecache.cpp b/tests/blackmisc/testvaluecache/testvaluecache.cpp index 3e417bd5a..b6720471d 100644 --- a/tests/blackmisc/testvaluecache/testvaluecache.cpp +++ b/tests/blackmisc/testvaluecache/testvaluecache.cpp @@ -212,13 +212,13 @@ namespace BlackMiscTest CValueCache otherCache(1); connect(&thisCache, &CValueCache::valuesChangedByLocal, &thisCache, [ & ](const CValueCachePacket &values) { - QMetaObject::invokeMethod(&thisCache, "changeValuesFromRemote", Q_ARG(BlackMisc::CValueCachePacket, values), Q_ARG(BlackMisc::CIdentifier, thisProcess)); - QMetaObject::invokeMethod(&otherCache, "changeValuesFromRemote", Q_ARG(BlackMisc::CValueCachePacket, values), Q_ARG(BlackMisc::CIdentifier, otherProcess)); + QMetaObject::invokeMethod(&thisCache, [=, &thisCache] { thisCache.changeValuesFromRemote(values, thisProcess); }); + QMetaObject::invokeMethod(&otherCache, [=, &otherCache] { otherCache.changeValuesFromRemote(values, otherProcess); }); }); connect(&otherCache, &CValueCache::valuesChangedByLocal, &thisCache, [ & ](const CValueCachePacket &values) { - QMetaObject::invokeMethod(&thisCache, "changeValuesFromRemote", Q_ARG(BlackMisc::CValueCachePacket, values), Q_ARG(BlackMisc::CIdentifier, otherProcess)); - QMetaObject::invokeMethod(&otherCache, "changeValuesFromRemote", Q_ARG(BlackMisc::CValueCachePacket, values), Q_ARG(BlackMisc::CIdentifier, thisProcess)); + QMetaObject::invokeMethod(&thisCache, [=, &thisCache] { thisCache.changeValuesFromRemote(values, otherProcess); }); + QMetaObject::invokeMethod(&otherCache, [=, &otherCache] { otherCache.changeValuesFromRemote(values, thisProcess); }); }); for (int i = 0; i < 4; ++i) { QTest::ignoreMessage(QtDebugMsg, QRegularExpression("Empty cache value")); }