From e9be288f7ce6ed7861a5ed381e2e16ae730524ad Mon Sep 17 00:00:00 2001 From: Mikaƫl Capelle Date: Tue, 29 Sep 2020 21:55:44 +0200 Subject: Add topImplementedInterface(). Minor cleaning. --- src/plugincontainer.cpp | 67 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 16 deletions(-) (limited to 'src/plugincontainer.cpp') diff --git a/src/plugincontainer.cpp b/src/plugincontainer.cpp index 1af8487f..90a7e95f 100644 --- a/src/plugincontainer.cpp +++ b/src/plugincontainer.cpp @@ -24,7 +24,6 @@ namespace bf = boost::fusion; template struct PluginTypeName; -template <> struct PluginTypeName { static QString value() { return {}; } }; template <> struct PluginTypeName { static QString value() { return QT_TR_NOOP("Plugin"); } }; template <> struct PluginTypeName { static QString value() { return QT_TR_NOOP("Diagnose"); } }; template <> struct PluginTypeName { static QString value() { return QT_TR_NOOP("Game"); } }; @@ -35,28 +34,24 @@ template <> struct PluginTypeName { static QString value() template <> struct PluginTypeName { static QString value() { return QT_TR_NOOP("Proxy"); } }; template <> struct PluginTypeName { static QString value() { return QT_TR_NOOP("File Mapper"); } }; -QStringList PluginContainer::implementedInterfaces(const IPlugin* plugin) const +QStringList PluginContainer::implementedInterfaces(IPlugin* plugin) const { - // Find the correspond QObject - Can this be done safely with a cast? - auto& objects = bf::at_key(m_Plugins); - auto it = std::find_if(std::begin(objects), std::end(objects), [plugin](QObject *obj) { - return qobject_cast(obj) == plugin; - }); + // We need a QObject to be able to qobject_cast<> to the plugin types: + QObject* oPlugin = as_qobject(plugin); - if (it == std::end(objects)) { + if (!oPlugin) { return {}; } - // We need a QObject to be able to qobject_cast<> to the plugin types: - const QObject* oPlugin = *it; - // Find all the names: QStringList names; - bf::for_each(m_Plugins, [oPlugin, &names](auto const& p) { - using key_type = typename std::decay_t::first_type; - auto name = PluginTypeName::value(); - if (!name.isEmpty()) { - names.append(name); + boost::mp11::mp_for_each([oPlugin, &names](const auto *p) { + using plugin_type = std::decay_t; + if (qobject_cast(oPlugin)) { + auto name = PluginTypeName::value(); + if (!name.isEmpty()) { + names.append(name); + } } }); @@ -68,6 +63,30 @@ QStringList PluginContainer::implementedInterfaces(const IPlugin* plugin) const return names; } +QString PluginContainer::topImplementedInterface(IPlugin* plugin) const +{ + // We need a QObject to be able to qobject_cast<> to the plugin types: + QObject* oPlugin = as_qobject(plugin); + + if (!oPlugin) { + return {}; + } + + // Find all the names: + QString name; + boost::mp11::mp_for_each([oPlugin, &name](auto *p) { + using plugin_type = std::decay_t; + if (name.isEmpty() && qobject_cast(oPlugin)) { + auto tname = PluginTypeName::value(); + if (!tname.isEmpty()) { + name = tname; + } + } + }); + + return name; +} + PluginContainer::PluginContainer(OrganizerCore *organizer) : m_Organizer(organizer) , m_UserInterface(nullptr) @@ -79,6 +98,7 @@ PluginContainer::~PluginContainer() { unloadPlugins(); } + void PluginContainer::setUserInterface(IUserInterface *userInterface, QWidget *widget) { for (IPluginProxy *proxy : bf::at_key(m_Plugins)) { @@ -115,6 +135,21 @@ QStringList PluginContainer::pluginFileNames() const } +QObject* PluginContainer::as_qobject(MOBase::IPlugin* plugin) const +{ + // Find the correspond QObject - Can this be done safely with a cast? + auto& objects = bf::at_key(m_Plugins); + auto it = std::find_if(std::begin(objects), std::end(objects), [plugin](QObject* obj) { + return qobject_cast(obj) == plugin; + }); + + if (it == std::end(objects)) { + return nullptr; + } + + return *it; +} + bool PluginContainer::verifyPlugin(IPlugin *plugin) { if (plugin == nullptr) { -- cgit v1.3.1