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
{
Plugin::Plugin(QObject *parent)
: QObject(parent)
{
}
Plugin::Plugin() = default;
PluginFactory::PluginFactory(QObject *parent)
: QObject(parent)
{
}
PluginFactory::PluginFactory() = default;
} // namespace KWin

View file

@ -11,6 +11,7 @@
#include "kwinglobals.h"
#include <QObject>
#include <memory>
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<Plugin> create() const = 0;
};
} // namespace KWin

View file

@ -65,7 +65,7 @@ PluginManager::PluginManager(QObject *parent)
const QVector<KPluginMetaData> 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> 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

View file

@ -43,7 +43,7 @@ private:
bool loadDynamicPlugin(const QString &pluginId);
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;
KWIN_SINGLETON(PluginManager)
};

View file

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

View file

@ -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);

View file

@ -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<Plugin> create() const override;
};
ColordIntegrationFactory::ColordIntegrationFactory(QObject *parent)
: PluginFactory(parent)
{
}
Plugin *ColordIntegrationFactory::create() const
std::unique_ptr<Plugin> ColordIntegrationFactory::create() const
{
switch (kwinApp()->operationMode()) {
case Application::OperationModeX11:
return nullptr;
case Application::OperationModeXwayland:
case Application::OperationModeWaylandOnly:
return new ColordIntegration();
return std::make_unique<ColordIntegration>();
default:
return nullptr;
}

View file

@ -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<Plugin> create() const override;
};
KRunnerIntegrationFactory::KRunnerIntegrationFactory(QObject *parent)
: PluginFactory(parent)
std::unique_ptr<Plugin> KRunnerIntegrationFactory::create() const
{
}
Plugin *KRunnerIntegrationFactory::create() const
{
return new WindowsRunner();
return std::make_unique<WindowsRunner>();
}
#include "main.moc"

View file

@ -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()
{

View file

@ -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();

View file

@ -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<Plugin> create() const override;
};
NightColorManagerFactory::NightColorManagerFactory(QObject *parent)
: PluginFactory(parent)
std::unique_ptr<Plugin> NightColorManagerFactory::create() const
{
}
Plugin *NightColorManagerFactory::create() const
{
return new NightColorManager();
return std::make_unique<NightColorManager>();
}
#include "main.moc"

View file

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

View file

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

View file

@ -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<Plugin> create() const override;
};
ScreencastManagerFactory::ScreencastManagerFactory(QObject *parent)
: PluginFactory(parent)
{
}
Plugin *ScreencastManagerFactory::create() const
std::unique_ptr<Plugin> ScreencastManagerFactory::create() const
{
switch (kwinApp()->operationMode()) {
case Application::OperationModeX11:
return nullptr;
case Application::OperationModeXwayland:
case Application::OperationModeWaylandOnly:
return new ScreencastManager();
return std::make_unique<ScreencastManager>();
default:
return nullptr;
}

View file

@ -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);

View file

@ -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);