From e726779c9b6801ca5875870987538457ef61aa6b Mon Sep 17 00:00:00 2001 From: Xaver Hugl Date: Tue, 5 Jul 2022 14:00:41 +0200 Subject: [PATCH] manage plugins with std::unique_ptr --- src/plugin.cpp | 10 ++---- src/plugin.h | 7 ++-- src/pluginmanager.cpp | 33 +++++++++---------- src/pluginmanager.h | 2 +- .../colord-integration/colordintegration.cpp | 3 +- .../colord-integration/colordintegration.h | 2 +- src/plugins/colord-integration/main.cpp | 13 +++----- src/plugins/krunner-integration/main.cpp | 13 +++----- .../windowsrunnerinterface.cpp | 7 ++-- .../windowsrunnerinterface.h | 2 +- src/plugins/nightcolor/main.cpp | 13 +++----- src/plugins/nightcolor/nightcolormanager.cpp | 3 +- src/plugins/nightcolor/nightcolormanager.h | 2 +- src/plugins/screencast/main.cpp | 13 +++----- src/plugins/screencast/screencastmanager.cpp | 5 ++- src/plugins/screencast/screencastmanager.h | 2 +- 16 files changed, 49 insertions(+), 81 deletions(-) diff --git a/src/plugin.cpp b/src/plugin.cpp index cc869fab1d..bfb941f136 100644 --- a/src/plugin.cpp +++ b/src/plugin.cpp @@ -9,14 +9,8 @@ namespace KWin { -Plugin::Plugin(QObject *parent) - : QObject(parent) -{ -} +Plugin::Plugin() = default; -PluginFactory::PluginFactory(QObject *parent) - : QObject(parent) -{ -} +PluginFactory::PluginFactory() = default; } // namespace KWin diff --git a/src/plugin.h b/src/plugin.h index 863bd02a92..e6480534eb 100644 --- a/src/plugin.h +++ b/src/plugin.h @@ -11,6 +11,7 @@ #include "kwinglobals.h" #include +#include namespace KWin { @@ -27,7 +28,7 @@ class KWIN_EXPORT Plugin : public QObject Q_OBJECT public: - explicit Plugin(QObject *parent = nullptr); + explicit Plugin(); }; /** @@ -38,9 +39,9 @@ class KWIN_EXPORT PluginFactory : public QObject Q_OBJECT public: - explicit PluginFactory(QObject *parent = nullptr); + explicit PluginFactory(); - virtual Plugin *create() const = 0; + virtual std::unique_ptr create() const = 0; }; } // namespace KWin diff --git a/src/pluginmanager.cpp b/src/pluginmanager.cpp index ed13a6be2a..fc42e5ffdb 100644 --- a/src/pluginmanager.cpp +++ b/src/pluginmanager.cpp @@ -65,7 +65,7 @@ PluginManager::PluginManager(QObject *parent) const QVector plugins = KPluginMetaData::findPlugins(s_pluginDirectory); for (const KPluginMetaData &metadata : plugins) { - if (m_plugins.contains(metadata.pluginId())) { + if (m_plugins.find(metadata.pluginId()) != m_plugins.end()) { qCWarning(KWIN_CORE) << "Conflicting plugin id" << metadata.pluginId(); continue; } @@ -84,7 +84,12 @@ PluginManager::~PluginManager() QStringList PluginManager::loadedPlugins() const { - return m_plugins.keys(); + QStringList ret; + ret.reserve(m_plugins.size()); + for (const auto &[key, _] : m_plugins) { + ret.push_back(key); + } + return ret; } QStringList PluginManager::availablePlugins() const @@ -101,7 +106,7 @@ QStringList PluginManager::availablePlugins() const bool PluginManager::loadPlugin(const QString &pluginId) { - if (m_plugins.contains(pluginId)) { + if (m_plugins.find(pluginId) != m_plugins.end()) { qCDebug(KWIN_CORE) << "Plugin with id" << pluginId << "is already loaded"; return false; } @@ -160,28 +165,22 @@ bool PluginManager::loadDynamicPlugin(const KPluginMetaData &metadata) bool PluginManager::instantiatePlugin(const QString &pluginId, PluginFactory *factory) { - Plugin *plugin = factory->create(); - if (!plugin) { + if (std::unique_ptr plugin = factory->create()) { + m_plugins[pluginId] = std::move(plugin); + return true; + } else { return false; } - - m_plugins.insert(pluginId, plugin); - plugin->setParent(this); - - connect(plugin, &QObject::destroyed, this, [this, pluginId]() { - m_plugins.remove(pluginId); - }); - - return true; } void PluginManager::unloadPlugin(const QString &pluginId) { - Plugin *plugin = m_plugins.take(pluginId); - if (!plugin) { + auto it = m_plugins.find(pluginId); + if (it != m_plugins.end()) { + m_plugins.erase(it); + } else { qCWarning(KWIN_CORE) << "No plugin with the specified id:" << pluginId; } - delete plugin; } } // namespace KWin diff --git a/src/pluginmanager.h b/src/pluginmanager.h index b90fa368a1..9657a8df39 100644 --- a/src/pluginmanager.h +++ b/src/pluginmanager.h @@ -43,7 +43,7 @@ private: bool loadDynamicPlugin(const QString &pluginId); bool instantiatePlugin(const QString &pluginId, PluginFactory *factory); - QHash m_plugins; + std::map> m_plugins; QHash m_staticPlugins; KWIN_SINGLETON(PluginManager) }; diff --git a/src/plugins/colord-integration/colordintegration.cpp b/src/plugins/colord-integration/colordintegration.cpp index dfc6f8d38e..3759ed714d 100644 --- a/src/plugins/colord-integration/colordintegration.cpp +++ b/src/plugins/colord-integration/colordintegration.cpp @@ -17,8 +17,7 @@ namespace KWin { -ColordIntegration::ColordIntegration(QObject *parent) - : Plugin(parent) +ColordIntegration::ColordIntegration() { qDBusRegisterMetaType(); diff --git a/src/plugins/colord-integration/colordintegration.h b/src/plugins/colord-integration/colordintegration.h index 164db6ce38..ffcbe3d9ee 100644 --- a/src/plugins/colord-integration/colordintegration.h +++ b/src/plugins/colord-integration/colordintegration.h @@ -23,7 +23,7 @@ class KWIN_EXPORT ColordIntegration : public Plugin Q_OBJECT public: - explicit ColordIntegration(QObject *parent = nullptr); + explicit ColordIntegration(); private Q_SLOTS: void handleOutputAdded(Output *output); diff --git a/src/plugins/colord-integration/main.cpp b/src/plugins/colord-integration/main.cpp index edf3b8fa2d..726ea06946 100644 --- a/src/plugins/colord-integration/main.cpp +++ b/src/plugins/colord-integration/main.cpp @@ -18,24 +18,19 @@ class KWIN_EXPORT ColordIntegrationFactory : public PluginFactory Q_INTERFACES(KWin::PluginFactory) public: - explicit ColordIntegrationFactory(QObject *parent = nullptr); + explicit ColordIntegrationFactory() = default; - Plugin *create() const override; + std::unique_ptr create() const override; }; -ColordIntegrationFactory::ColordIntegrationFactory(QObject *parent) - : PluginFactory(parent) -{ -} - -Plugin *ColordIntegrationFactory::create() const +std::unique_ptr ColordIntegrationFactory::create() const { switch (kwinApp()->operationMode()) { case Application::OperationModeX11: return nullptr; case Application::OperationModeXwayland: case Application::OperationModeWaylandOnly: - return new ColordIntegration(); + return std::make_unique(); default: return nullptr; } diff --git a/src/plugins/krunner-integration/main.cpp b/src/plugins/krunner-integration/main.cpp index b3ed27da34..ade52a1c1a 100644 --- a/src/plugins/krunner-integration/main.cpp +++ b/src/plugins/krunner-integration/main.cpp @@ -18,19 +18,14 @@ class KWIN_EXPORT KRunnerIntegrationFactory : public PluginFactory Q_INTERFACES(KWin::PluginFactory) public: - explicit KRunnerIntegrationFactory(QObject *parent = nullptr); + explicit KRunnerIntegrationFactory() = default; - Plugin *create() const override; + std::unique_ptr create() const override; }; -KRunnerIntegrationFactory::KRunnerIntegrationFactory(QObject *parent) - : PluginFactory(parent) +std::unique_ptr KRunnerIntegrationFactory::create() const { -} - -Plugin *KRunnerIntegrationFactory::create() const -{ - return new WindowsRunner(); + return std::make_unique(); } #include "main.moc" diff --git a/src/plugins/krunner-integration/windowsrunnerinterface.cpp b/src/plugins/krunner-integration/windowsrunnerinterface.cpp index 0d8397e616..46b879bc76 100644 --- a/src/plugins/krunner-integration/windowsrunnerinterface.cpp +++ b/src/plugins/krunner-integration/windowsrunnerinterface.cpp @@ -19,8 +19,7 @@ namespace KWin { -WindowsRunner::WindowsRunner(QObject *parent) - : Plugin(parent) +WindowsRunner::WindowsRunner() { if (workspace()) { initialize(); @@ -29,9 +28,7 @@ WindowsRunner::WindowsRunner(QObject *parent) } } -WindowsRunner::~WindowsRunner() -{ -} +WindowsRunner::~WindowsRunner() = default; void WindowsRunner::initialize() { diff --git a/src/plugins/krunner-integration/windowsrunnerinterface.h b/src/plugins/krunner-integration/windowsrunnerinterface.h index d69b1d4d92..5f38239d4f 100644 --- a/src/plugins/krunner-integration/windowsrunnerinterface.h +++ b/src/plugins/krunner-integration/windowsrunnerinterface.h @@ -34,7 +34,7 @@ class WindowsRunner : public Plugin, protected QDBusContext Q_OBJECT Q_CLASSINFO("D-Bus Interface", "org.kde.KWin.WindowsRunner") public: - explicit WindowsRunner(QObject *parent = nullptr); + explicit WindowsRunner(); ~WindowsRunner() override; RemoteActions Actions(); diff --git a/src/plugins/nightcolor/main.cpp b/src/plugins/nightcolor/main.cpp index 4b840973f0..4a2209c279 100644 --- a/src/plugins/nightcolor/main.cpp +++ b/src/plugins/nightcolor/main.cpp @@ -17,19 +17,14 @@ class KWIN_EXPORT NightColorManagerFactory : public PluginFactory Q_INTERFACES(KWin::PluginFactory) public: - explicit NightColorManagerFactory(QObject *parent = nullptr); + explicit NightColorManagerFactory() = default; - Plugin *create() const override; + std::unique_ptr create() const override; }; -NightColorManagerFactory::NightColorManagerFactory(QObject *parent) - : PluginFactory(parent) +std::unique_ptr NightColorManagerFactory::create() const { -} - -Plugin *NightColorManagerFactory::create() const -{ - return new NightColorManager(); + return std::make_unique(); } #include "main.moc" diff --git a/src/plugins/nightcolor/nightcolormanager.cpp b/src/plugins/nightcolor/nightcolormanager.cpp index 5c2c115f49..593fa2605c 100644 --- a/src/plugins/nightcolor/nightcolormanager.cpp +++ b/src/plugins/nightcolor/nightcolormanager.cpp @@ -45,8 +45,7 @@ NightColorManager *NightColorManager::self() return s_instance; } -NightColorManager::NightColorManager(QObject *parent) - : Plugin(parent) +NightColorManager::NightColorManager() { s_instance = this; diff --git a/src/plugins/nightcolor/nightcolormanager.h b/src/plugins/nightcolor/nightcolormanager.h index 451eeb8d14..ed1927a471 100644 --- a/src/plugins/nightcolor/nightcolormanager.h +++ b/src/plugins/nightcolor/nightcolormanager.h @@ -78,7 +78,7 @@ class KWIN_EXPORT NightColorManager : public Plugin Q_OBJECT public: - explicit NightColorManager(QObject *parent = nullptr); + explicit NightColorManager(); ~NightColorManager() override; void init(); diff --git a/src/plugins/screencast/main.cpp b/src/plugins/screencast/main.cpp index 16c405d769..0ce1ffcbeb 100644 --- a/src/plugins/screencast/main.cpp +++ b/src/plugins/screencast/main.cpp @@ -18,24 +18,19 @@ class KWIN_EXPORT ScreencastManagerFactory : public PluginFactory Q_INTERFACES(KWin::PluginFactory) public: - explicit ScreencastManagerFactory(QObject *parent = nullptr); + explicit ScreencastManagerFactory() = default; - Plugin *create() const override; + std::unique_ptr create() const override; }; -ScreencastManagerFactory::ScreencastManagerFactory(QObject *parent) - : PluginFactory(parent) -{ -} - -Plugin *ScreencastManagerFactory::create() const +std::unique_ptr ScreencastManagerFactory::create() const { switch (kwinApp()->operationMode()) { case Application::OperationModeX11: return nullptr; case Application::OperationModeXwayland: case Application::OperationModeWaylandOnly: - return new ScreencastManager(); + return std::make_unique(); default: return nullptr; } diff --git a/src/plugins/screencast/screencastmanager.cpp b/src/plugins/screencast/screencastmanager.cpp index ef003c662f..32ad8a8e81 100644 --- a/src/plugins/screencast/screencastmanager.cpp +++ b/src/plugins/screencast/screencastmanager.cpp @@ -29,9 +29,8 @@ namespace KWin { -ScreencastManager::ScreencastManager(QObject *parent) - : Plugin(parent) - , m_screencast(new KWaylandServer::ScreencastV1Interface(waylandServer()->display(), this)) +ScreencastManager::ScreencastManager() + : m_screencast(new KWaylandServer::ScreencastV1Interface(waylandServer()->display(), this)) { connect(m_screencast, &KWaylandServer::ScreencastV1Interface::windowScreencastRequested, this, &ScreencastManager::streamWindow); diff --git a/src/plugins/screencast/screencastmanager.h b/src/plugins/screencast/screencastmanager.h index 6e72a6611f..f9d3a19fc0 100644 --- a/src/plugins/screencast/screencastmanager.h +++ b/src/plugins/screencast/screencastmanager.h @@ -22,7 +22,7 @@ class ScreencastManager : public Plugin Q_OBJECT public: - explicit ScreencastManager(QObject *parent = nullptr); + explicit ScreencastManager(); private: void streamWindow(KWaylandServer::ScreencastStreamV1Interface *stream, const QString &winid);