manage plugins with std::unique_ptr

This commit is contained in:
Xaver Hugl 2022-07-05 14:00:41 +02:00
parent dc4436a754
commit e726779c9b
16 changed files with 49 additions and 81 deletions

View file

@ -9,14 +9,8 @@
namespace KWin namespace KWin
{ {
Plugin::Plugin(QObject *parent) Plugin::Plugin() = default;
: QObject(parent)
{
}
PluginFactory::PluginFactory(QObject *parent) PluginFactory::PluginFactory() = default;
: QObject(parent)
{
}
} // namespace KWin } // namespace KWin

View file

@ -11,6 +11,7 @@
#include "kwinglobals.h" #include "kwinglobals.h"
#include <QObject> #include <QObject>
#include <memory>
namespace KWin namespace KWin
{ {
@ -27,7 +28,7 @@ class KWIN_EXPORT Plugin : public QObject
Q_OBJECT Q_OBJECT
public: public:
explicit Plugin(QObject *parent = nullptr); explicit Plugin();
}; };
/** /**
@ -38,9 +39,9 @@ class KWIN_EXPORT PluginFactory : public QObject
Q_OBJECT Q_OBJECT
public: public:
explicit PluginFactory(QObject *parent = nullptr); explicit PluginFactory();
virtual Plugin *create() const = 0; virtual std::unique_ptr<Plugin> create() const = 0;
}; };
} // namespace KWin } // namespace KWin

View file

@ -65,7 +65,7 @@ PluginManager::PluginManager(QObject *parent)
const QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(s_pluginDirectory); const QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(s_pluginDirectory);
for (const KPluginMetaData &metadata : plugins) { 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(); qCWarning(KWIN_CORE) << "Conflicting plugin id" << metadata.pluginId();
continue; continue;
} }
@ -84,7 +84,12 @@ PluginManager::~PluginManager()
QStringList PluginManager::loadedPlugins() const 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 QStringList PluginManager::availablePlugins() const
@ -101,7 +106,7 @@ QStringList PluginManager::availablePlugins() const
bool PluginManager::loadPlugin(const QString &pluginId) 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"; qCDebug(KWIN_CORE) << "Plugin with id" << pluginId << "is already loaded";
return false; return false;
} }
@ -160,28 +165,22 @@ bool PluginManager::loadDynamicPlugin(const KPluginMetaData &metadata)
bool PluginManager::instantiatePlugin(const QString &pluginId, PluginFactory *factory) bool PluginManager::instantiatePlugin(const QString &pluginId, PluginFactory *factory)
{ {
Plugin *plugin = factory->create(); if (std::unique_ptr<Plugin> plugin = factory->create()) {
if (!plugin) { m_plugins[pluginId] = std::move(plugin);
return true;
} else {
return false; 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) void PluginManager::unloadPlugin(const QString &pluginId)
{ {
Plugin *plugin = m_plugins.take(pluginId); auto it = m_plugins.find(pluginId);
if (!plugin) { if (it != m_plugins.end()) {
m_plugins.erase(it);
} else {
qCWarning(KWIN_CORE) << "No plugin with the specified id:" << pluginId; qCWarning(KWIN_CORE) << "No plugin with the specified id:" << pluginId;
} }
delete plugin;
} }
} // namespace KWin } // namespace KWin

View file

@ -43,7 +43,7 @@ private:
bool loadDynamicPlugin(const QString &pluginId); bool loadDynamicPlugin(const QString &pluginId);
bool instantiatePlugin(const QString &pluginId, PluginFactory *factory); bool instantiatePlugin(const QString &pluginId, PluginFactory *factory);
QHash<QString, Plugin *> m_plugins; std::map<QString, std::unique_ptr<Plugin>> m_plugins;
QHash<QString, QStaticPlugin> m_staticPlugins; QHash<QString, QStaticPlugin> m_staticPlugins;
KWIN_SINGLETON(PluginManager) KWIN_SINGLETON(PluginManager)
}; };

View file

@ -17,8 +17,7 @@
namespace KWin namespace KWin
{ {
ColordIntegration::ColordIntegration(QObject *parent) ColordIntegration::ColordIntegration()
: Plugin(parent)
{ {
qDBusRegisterMetaType<CdStringMap>(); qDBusRegisterMetaType<CdStringMap>();

View file

@ -23,7 +23,7 @@ class KWIN_EXPORT ColordIntegration : public Plugin
Q_OBJECT Q_OBJECT
public: public:
explicit ColordIntegration(QObject *parent = nullptr); explicit ColordIntegration();
private Q_SLOTS: private Q_SLOTS:
void handleOutputAdded(Output *output); void handleOutputAdded(Output *output);

View file

@ -18,24 +18,19 @@ class KWIN_EXPORT ColordIntegrationFactory : public PluginFactory
Q_INTERFACES(KWin::PluginFactory) Q_INTERFACES(KWin::PluginFactory)
public: public:
explicit ColordIntegrationFactory(QObject *parent = nullptr); explicit ColordIntegrationFactory() = default;
Plugin *create() const override; std::unique_ptr<Plugin> create() const override;
}; };
ColordIntegrationFactory::ColordIntegrationFactory(QObject *parent) std::unique_ptr<Plugin> ColordIntegrationFactory::create() const
: PluginFactory(parent)
{
}
Plugin *ColordIntegrationFactory::create() const
{ {
switch (kwinApp()->operationMode()) { switch (kwinApp()->operationMode()) {
case Application::OperationModeX11: case Application::OperationModeX11:
return nullptr; return nullptr;
case Application::OperationModeXwayland: case Application::OperationModeXwayland:
case Application::OperationModeWaylandOnly: case Application::OperationModeWaylandOnly:
return new ColordIntegration(); return std::make_unique<ColordIntegration>();
default: default:
return nullptr; return nullptr;
} }

View file

@ -18,19 +18,14 @@ class KWIN_EXPORT KRunnerIntegrationFactory : public PluginFactory
Q_INTERFACES(KWin::PluginFactory) Q_INTERFACES(KWin::PluginFactory)
public: public:
explicit KRunnerIntegrationFactory(QObject *parent = nullptr); explicit KRunnerIntegrationFactory() = default;
Plugin *create() const override; std::unique_ptr<Plugin> create() const override;
}; };
KRunnerIntegrationFactory::KRunnerIntegrationFactory(QObject *parent) std::unique_ptr<Plugin> KRunnerIntegrationFactory::create() const
: PluginFactory(parent)
{ {
} return std::make_unique<WindowsRunner>();
Plugin *KRunnerIntegrationFactory::create() const
{
return new WindowsRunner();
} }
#include "main.moc" #include "main.moc"

View file

@ -19,8 +19,7 @@
namespace KWin namespace KWin
{ {
WindowsRunner::WindowsRunner(QObject *parent) WindowsRunner::WindowsRunner()
: Plugin(parent)
{ {
if (workspace()) { if (workspace()) {
initialize(); initialize();
@ -29,9 +28,7 @@ WindowsRunner::WindowsRunner(QObject *parent)
} }
} }
WindowsRunner::~WindowsRunner() WindowsRunner::~WindowsRunner() = default;
{
}
void WindowsRunner::initialize() void WindowsRunner::initialize()
{ {

View file

@ -34,7 +34,7 @@ class WindowsRunner : public Plugin, protected QDBusContext
Q_OBJECT Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.kde.KWin.WindowsRunner") Q_CLASSINFO("D-Bus Interface", "org.kde.KWin.WindowsRunner")
public: public:
explicit WindowsRunner(QObject *parent = nullptr); explicit WindowsRunner();
~WindowsRunner() override; ~WindowsRunner() override;
RemoteActions Actions(); RemoteActions Actions();

View file

@ -17,19 +17,14 @@ class KWIN_EXPORT NightColorManagerFactory : public PluginFactory
Q_INTERFACES(KWin::PluginFactory) Q_INTERFACES(KWin::PluginFactory)
public: public:
explicit NightColorManagerFactory(QObject *parent = nullptr); explicit NightColorManagerFactory() = default;
Plugin *create() const override; std::unique_ptr<Plugin> create() const override;
}; };
NightColorManagerFactory::NightColorManagerFactory(QObject *parent) std::unique_ptr<Plugin> NightColorManagerFactory::create() const
: PluginFactory(parent)
{ {
} return std::make_unique<NightColorManager>();
Plugin *NightColorManagerFactory::create() const
{
return new NightColorManager();
} }
#include "main.moc" #include "main.moc"

View file

@ -45,8 +45,7 @@ NightColorManager *NightColorManager::self()
return s_instance; return s_instance;
} }
NightColorManager::NightColorManager(QObject *parent) NightColorManager::NightColorManager()
: Plugin(parent)
{ {
s_instance = this; s_instance = this;

View file

@ -78,7 +78,7 @@ class KWIN_EXPORT NightColorManager : public Plugin
Q_OBJECT Q_OBJECT
public: public:
explicit NightColorManager(QObject *parent = nullptr); explicit NightColorManager();
~NightColorManager() override; ~NightColorManager() override;
void init(); void init();

View file

@ -18,24 +18,19 @@ class KWIN_EXPORT ScreencastManagerFactory : public PluginFactory
Q_INTERFACES(KWin::PluginFactory) Q_INTERFACES(KWin::PluginFactory)
public: public:
explicit ScreencastManagerFactory(QObject *parent = nullptr); explicit ScreencastManagerFactory() = default;
Plugin *create() const override; std::unique_ptr<Plugin> create() const override;
}; };
ScreencastManagerFactory::ScreencastManagerFactory(QObject *parent) std::unique_ptr<Plugin> ScreencastManagerFactory::create() const
: PluginFactory(parent)
{
}
Plugin *ScreencastManagerFactory::create() const
{ {
switch (kwinApp()->operationMode()) { switch (kwinApp()->operationMode()) {
case Application::OperationModeX11: case Application::OperationModeX11:
return nullptr; return nullptr;
case Application::OperationModeXwayland: case Application::OperationModeXwayland:
case Application::OperationModeWaylandOnly: case Application::OperationModeWaylandOnly:
return new ScreencastManager(); return std::make_unique<ScreencastManager>();
default: default:
return nullptr; return nullptr;
} }

View file

@ -29,9 +29,8 @@
namespace KWin namespace KWin
{ {
ScreencastManager::ScreencastManager(QObject *parent) ScreencastManager::ScreencastManager()
: Plugin(parent) : m_screencast(new KWaylandServer::ScreencastV1Interface(waylandServer()->display(), this))
, m_screencast(new KWaylandServer::ScreencastV1Interface(waylandServer()->display(), this))
{ {
connect(m_screencast, &KWaylandServer::ScreencastV1Interface::windowScreencastRequested, connect(m_screencast, &KWaylandServer::ScreencastV1Interface::windowScreencastRequested,
this, &ScreencastManager::streamWindow); this, &ScreencastManager::streamWindow);

View file

@ -22,7 +22,7 @@ class ScreencastManager : public Plugin
Q_OBJECT Q_OBJECT
public: public:
explicit ScreencastManager(QObject *parent = nullptr); explicit ScreencastManager();
private: private:
void streamWindow(KWaylandServer::ScreencastStreamV1Interface *stream, const QString &winid); void streamWindow(KWaylandServer::ScreencastStreamV1Interface *stream, const QString &winid);