]> vaikene.ee Git - evaf/blobdiff - src/libs/Common/registry.cpp
Warning fixes and copyright update.
[evaf] / src / libs / Common / registry.cpp
index c0f9cae9e1f4e4330213f20d3d6a758fc044e447..c3bc2bef251e08c2d0a96a891d845d50fa0ec1a1 100644 (file)
@@ -3,7 +3,7 @@
  * @brief Common registry implementation
  * @author Enar Vaikene
  *
- * Copyright (c) 2011 Enar Vaikene
+ * Copyright (c) 2011-2019 Enar Vaikene
  *
  * This file is part of the eVaf C++ cross-platform application development framework.
  *
@@ -41,7 +41,7 @@ using namespace eVaf::Common::Internal;
 Registry::Registry()
     : iRegistry()
 {
-    setObjectName(QString("%1-iRegistry").arg(VER_MODULENAME_STR));
+    setObjectName(QString("%1-iRegistry").arg(VER_MODULE_NAME_STR));
 
     // Register our own interface
     registerInterface("iRegistry", this);
@@ -54,11 +54,32 @@ Registry::~Registry()
 
 bool Registry::registerInterface(QString const & name, QObject * obj)
 {
-    mInterfaces.insert(name, QPointer<QObject>(obj));
+    // Add the interface to the list of registered interfaces and connect to
+    // the destroyed() signal.
+    mInterfaces.insert(name, obj);
+    connect(obj, SIGNAL(destroyed(QObject *)), this, SLOT(interfaceDestroyed(QObject *)));
+
+    return true;
+}
+
+QObject * Registry::queryInterface(QString const & name) const
+{
+    Interfaces::const_iterator it = mInterfaces.constFind(name);
+    return it != mInterfaces.constEnd() ? *it : nullptr;
 }
 
-QObject * Registry::queryInterface(QString const & name)
+void Registry::interfaceDestroyed(QObject * obj)
 {
-    QHash<QString, QPointer<QObject> >::const_iterator it = mInterfaces.constFind(name);
-    return it != mInterfaces.constEnd() ? *it : 0;
+    // Interface object destroyed. Remove it from the list of registered
+    // interfaces.
+    Interfaces::iterator it = mInterfaces.begin();
+    Interfaces::const_iterator e = mInterfaces.end();
+    while (it != e) {
+        if (*it == obj) {
+            it = mInterfaces.erase(it);
+        }
+        else {
+            ++it;
+        }
+    }
 }