modernize: Use auto where the type is clear from context
[quassel.git] / src / common / signalproxy.cpp
index f344603..8935538 100644 (file)
@@ -18,6 +18,8 @@
  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
  ***************************************************************************/
 
+#include <utility>
+
 #include <QCoreApplication>
 #include <QHostAddress>
 #include <QMetaMethod>
@@ -59,18 +61,18 @@ public:
     SignalRelay(SignalProxy *parent) : QObject(parent), _proxy(parent) {}
     inline SignalProxy *proxy() const { return _proxy; }
 
-    int qt_metacall(QMetaObject::Call _c, int _id, void **_a);
+    int qt_metacall(QMetaObject::Call _c, int _id, void **_a) override;
 
     void attachSignal(QObject *sender, int signalId, const QByteArray &funcName);
     void detachSignal(QObject *sender, int signalId = -1);
 
 private:
     struct Signal {
-        QObject *sender;
-        int signalId;
+        QObject *sender{nullptr};
+        int signalId{-1};
         QByteArray signature;
-        Signal(QObject *sender, int sigId, const QByteArray &signature) : sender(sender), signalId(sigId), signature(signature) {}
-        Signal() : sender(0), signalId(-1) {}
+        Signal(QObject *sender, int sigId, QByteArray signature) : sender(sender), signalId(sigId), signature(std::move(signature)) {}
+        Signal() = default;
     };
 
     SignalProxy *_proxy;
@@ -96,11 +98,7 @@ void SignalProxy::SignalRelay::attachSignal(QObject *sender, int signalId, const
     }
     else {
         fn = SIGNAL(fakeMethodSignature());
-#if QT_VERSION >= 0x050000
         fn = fn.replace("fakeMethodSignature()", sender->metaObject()->method(signalId).methodSignature());
-#else
-        fn = fn.replace("fakeMethodSignature()", sender->metaObject()->method(signalId).signature());
-#endif
     }
 
     _slots[slotId] = Signal(sender, signalId, fn);
@@ -145,11 +143,7 @@ int SignalProxy::SignalRelay::qt_metacall(QMetaObject::Call _c, int _id, void **
             const QList<int> &argTypes = eMeta->argTypes(signal.signalId);
             for (int i = 0; i < argTypes.size(); i++) {
                 if (argTypes[i] == 0) {
-#if QT_VERSION >= 0x050000
                     qWarning() << "SignalRelay::qt_metacall(): received invalid data for argument number" << i << "of signal" << QString("%1::%2").arg(caller->metaObject()->className()).arg(caller->metaObject()->method(signal.signalId).methodSignature().constData());
-#else
-                    qWarning() << "SignalRelay::qt_metacall(): received invalid data for argument number" << i << "of signal" << QString("%1::%2").arg(caller->metaObject()->className()).arg(caller->metaObject()->method(signal.signalId).signature());
-#endif
                     qWarning() << "                            - make sure all your data types are known by the Qt MetaSystem";
                     return _id;
                 }
@@ -174,7 +168,9 @@ int SignalProxy::SignalRelay::qt_metacall(QMetaObject::Call _c, int _id, void **
 //  SignalProxy
 // ==================================================
 
-thread_local SignalProxy *SignalProxy::_current{nullptr};
+namespace {
+thread_local SignalProxy *_current{nullptr};
+}
 
 SignalProxy::SignalProxy(QObject *parent)
     : QObject(parent)
@@ -212,6 +208,12 @@ SignalProxy::~SignalProxy()
 }
 
 
+SignalProxy *SignalProxy::current()
+{
+    return _current;
+}
+
+
 void SignalProxy::setProxyMode(ProxyMode mode)
 {
     if (!_peerMap.empty()) {
@@ -340,8 +342,8 @@ void SignalProxy::removePeer(Peer *peer)
         return;
     }
 
-    disconnect(peer, 0, this, 0);
-    peer->setSignalProxy(0);
+    disconnect(peer, nullptr, this, nullptr);
+    peer->setSignalProxy(nullptr);
 
     _peerMap.remove(peer->id());
     emit peerRemoved(peer);
@@ -386,7 +388,7 @@ void SignalProxy::objectRenamed(const QByteArray &classname, const QString &newn
 
 const QMetaObject *SignalProxy::metaObject(const QObject *obj)
 {
-    if (const SyncableObject *syncObject = qobject_cast<const SyncableObject *>(obj))
+    if (const auto *syncObject = qobject_cast<const SyncableObject *>(obj))
         return syncObject->syncMetaObject();
     else
         return obj->metaObject();
@@ -398,7 +400,7 @@ SignalProxy::ExtendedMetaObject *SignalProxy::extendedMetaObject(const QMetaObje
     if (_extendedMetaObjects.contains(meta))
         return _extendedMetaObjects[meta];
     else
-        return 0;
+        return nullptr;
 }
 
 
@@ -475,8 +477,11 @@ void SignalProxy::synchronize(SyncableObject *obj)
 
 void SignalProxy::detachObject(QObject *obj)
 {
-    detachSignals(obj);
-    detachSlots(obj);
+    // Don't try to connect SignalProxy from itself on shutdown
+    if (obj != this) {
+        detachSignals(obj);
+        detachSlots(obj);
+    }
 }
 
 
@@ -659,18 +664,14 @@ bool SignalProxy::invokeSlot(QObject *receiver, int methodId, const QVariantList
         return false;
     }
 
-    void *_a[] = { 0,           // return type...
-                   0, 0, 0, 0, 0, // and 10 args - that's the max size qt can handle with signals and slots
-                   0, 0, 0, 0, 0 };
+    void *_a[] = { nullptr,           // return type...
+                   nullptr, nullptr, nullptr, nullptr, nullptr, // and 10 args - that's the max size qt can handle with signals and slots
+                   nullptr, nullptr, nullptr, nullptr, nullptr };
 
     // check for argument compatibility and build params array
     for (int i = 0; i < numArgs; i++) {
         if (!params[i].isValid()) {
-#if QT_VERSION >= 0x050000
             qWarning() << "SignalProxy::invokeSlot(): received invalid data for argument number" << i << "of method" << QString("%1::%2()").arg(receiver->metaObject()->className()).arg(receiver->metaObject()->method(methodId).methodSignature().constData());
-#else
-            qWarning() << "SignalProxy::invokeSlot(): received invalid data for argument number" << i << "of method" << QString("%1::%2()").arg(receiver->metaObject()->className()).arg(receiver->metaObject()->method(methodId).signature());
-#endif
             qWarning() << "                            - make sure all your data types are known by the Qt MetaSystem";
             return false;
         }
@@ -739,7 +740,7 @@ void SignalProxy::customEvent(QEvent *event)
 {
     switch ((int)event->type()) {
     case RemovePeerEvent: {
-        ::RemovePeerEvent *e = static_cast< ::RemovePeerEvent *>(event);
+        auto *e = static_cast< ::RemovePeerEvent *>(event);
         removePeer(e->peer);
         event->accept();
         break;
@@ -787,7 +788,7 @@ void SignalProxy::disconnectDevice(QIODevice *dev, const QString &reason)
 {
     if (!reason.isEmpty())
         qWarning() << qPrintable(reason);
-    QAbstractSocket *sock  = qobject_cast<QAbstractSocket *>(dev);
+    auto *sock  = qobject_cast<QAbstractSocket *>(dev);
     if (sock)
         qWarning() << qPrintable(tr("Disconnecting")) << qPrintable(sock->peerAddress().toString());
     dev->close();
@@ -847,7 +848,10 @@ QVariantList SignalProxy::peerData() {
 }
 
 Peer *SignalProxy::peerById(int peerId) {
-    return _peerMap[peerId];
+    // We use ::value() here instead of the [] operator because the latter has the side-effect
+    // of automatically inserting a null value with the passed key into the map.  See
+    // https://doc.qt.io/qt-5/qhash.html#operator-5b-5d and https://doc.qt.io/qt-5/qhash.html#value.
+    return _peerMap.value(peerId);
 }
 
 void SignalProxy::restrictTargetPeers(QSet<Peer*> peers, std::function<void()> closure)
@@ -890,11 +894,7 @@ SignalProxy::ExtendedMetaObject::ExtendedMetaObject(const QMetaObject *meta, boo
         if (_meta->method(i).methodType() != QMetaMethod::Slot)
             continue;
 
-#if QT_VERSION >= 0x050000
         if (_meta->method(i).methodSignature().contains('*'))
-#else
-        if (QByteArray(_meta->method(i).signature()).contains('*'))
-#endif
             continue;  // skip methods with ptr params
 
         QByteArray method = methodName(_meta->method(i));
@@ -923,11 +923,7 @@ SignalProxy::ExtendedMetaObject::ExtendedMetaObject(const QMetaObject *meta, boo
             }
             if (checkConflicts) {
                 qWarning() << "class" << meta->className() << "contains overloaded methods which is currently not supported!";
-#if QT_VERSION >= 0x050000
                 qWarning() << " - " << _meta->method(i).methodSignature() << "conflicts with" << _meta->method(_methodIds[method]).methodSignature();
-#else
-                qWarning() << " - " << _meta->method(i).signature() << "conflicts with" << _meta->method(_methodIds[method]).signature();
-#endif
             }
             continue;
         }
@@ -967,11 +963,7 @@ const QHash<int, int> &SignalProxy::ExtendedMetaObject::receiveMap()
             if (QMetaType::Void == (QMetaType::Type)returnType(i))
                 continue;
 
-#if QT_VERSION >= 0x050000
             signature = requestSlot.methodSignature();
-#else
-            signature = QByteArray(requestSlot.signature());
-#endif
             if (!signature.startsWith("request"))
                 continue;
 
@@ -1005,22 +997,14 @@ const QHash<int, int> &SignalProxy::ExtendedMetaObject::receiveMap()
 
 QByteArray SignalProxy::ExtendedMetaObject::methodName(const QMetaMethod &method)
 {
-#if QT_VERSION >= 0x050000
     QByteArray sig(method.methodSignature());
-#else
-    QByteArray sig(method.signature());
-#endif
     return sig.left(sig.indexOf("("));
 }
 
 
 QString SignalProxy::ExtendedMetaObject::methodBaseName(const QMetaMethod &method)
 {
-#if QT_VERSION >= 0x050000
     QString methodname = QString(method.methodSignature()).section("(", 0, 0);
-#else
-    QString methodname = QString(method.signature()).section("(", 0, 0);
-#endif
 
     // determine where we have to chop:
     int upperCharPos;
@@ -1058,11 +1042,7 @@ SignalProxy::ExtendedMetaObject::MethodDescriptor::MethodDescriptor(const QMetaM
     _argTypes = argTypes;
 
     // determine minArgCount
-#if QT_VERSION >= 0x050000
     QString signature(method.methodSignature());
-#else
-    QString signature(method.signature());
-#endif
     _minArgCount = method.parameterTypes().count() - signature.count("=");
 
     _receiverMode = (_methodName.startsWith("request"))