Skip to content

Commit

Permalink
use std algos
Browse files Browse the repository at this point in the history
Signed-off-by: Viktor Kopp <[email protected]>
  • Loading branch information
vifactor authored and alexmucde committed Jan 9, 2025
1 parent 7b47413 commit 269733c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 39 deletions.
2 changes: 1 addition & 1 deletion qdlt/qdltplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ bool QDltPlugin::isCommand()
}

// generic plugin interfaces
QString QDltPlugin::name()
QString QDltPlugin::name() const
{
if(plugininterface)
return plugininterface->name();
Expand Down
2 changes: 1 addition & 1 deletion qdlt/qdltplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class QDLT_EXPORT QDltPlugin
bool isCommand();

// generic plugin interfaces
QString name();
QString name() const;
QString pluginVersion();
QString pluginInterfaceVersion();
QString error();
Expand Down
56 changes: 30 additions & 26 deletions qdlt/qdltpluginmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ QStringList QDltPluginManager::loadPluginsPath(QDir &dir)
}

void QDltPluginManager::loadConfig(QString pluginName, QString filename) {
forEachPlugin([&](auto* plugin) {
QMutexLocker mutexLocker(&pluginListMutex);
std::for_each(plugins.begin(), plugins.end(), [&](auto* plugin) {
if (plugin->name() == pluginName)
plugin->setFilename(filename);
});
Expand All @@ -136,26 +137,20 @@ void QDltPluginManager::decodeMsg(QDltMsg &msg, int triggeredByUser)
}

QDltPlugin* QDltPluginManager::findPlugin(const QString& name) const {
QDltPlugin* result = nullptr;

QMutexLocker mutexLocker(&pluginListMutex);
for (auto* plugin : plugins) {
if (plugin->name() == name) {
result = plugin;
break;
}
}
auto it = std::find_if(plugins.begin(), plugins.end(), [&](auto* plugin) {
return plugin->name() == name;
});

return result;
return it != plugins.end() ? *it : nullptr;
}

void QDltPluginManager::initPluginPriority(const QStringList& desiredPrio)
{
QStringList finalPrio;

if(plugins.size() > 1) {
int prio = 0;
for (int i = 0; i < desiredPrio.count(); ++i) {
QString pluginName(desiredPrio[i]);
for (const auto& pluginName: desiredPrio) {
if (setPluginPriority(pluginName, prio)) {
++prio;
}
Expand Down Expand Up @@ -194,8 +189,7 @@ bool QDltPluginManager::raisePluginPriority(const QString &name)
QMutexLocker mutexLocker(&pluginListMutex);
for(int num=1; num < plugins.size(); ++num)
{
if( plugins[num]->name() == name)
{
if (plugins[num]->name() == name) {
qDebug() << "raise prio of" << name << "from" << num << "to" << num-1;
plugins.move(num, num-1);
result = true;
Expand Down Expand Up @@ -236,29 +230,35 @@ bool QDltPluginManager::setPluginPriority(const QString& name, int prio)
QStringList QDltPluginManager::getPluginPriorities() const
{
QStringList finalPrio;
finalPrio.reserve(plugins.size());

QMutexLocker mutexLocker(&pluginListMutex);
for (auto* plugin: plugins) {
finalPrio << plugin->name();
}
std::transform(plugins.begin(), plugins.end(), std::back_inserter(finalPrio), [](const auto* plugin) {
return plugin->name();
});

return finalPrio;
}

QList<QDltPlugin*> QDltPluginManager::getDecoderPlugins()
QList<QDltPlugin*> QDltPluginManager::getDecoderPlugins() const
{
QList<QDltPlugin*> list;
forEachPlugin([&](auto* plugin) {

QMutexLocker mutexLocker(&pluginListMutex);
std::for_each(plugins.begin(), plugins.end(), [&](auto* plugin) {
if (plugin->isDecoder() && plugin->getMode() >= QDltPlugin::ModeEnable)
list.append(plugin);
});

return list;
}

QList<QDltPlugin*> QDltPluginManager::getViewerPlugins()
QList<QDltPlugin*> QDltPluginManager::getViewerPlugins() const
{
QList<QDltPlugin*> list;
forEachPlugin([&](auto* plugin) {

QMutexLocker mutexLocker(&pluginListMutex);
std::for_each(plugins.begin(), plugins.end(), [&](auto* plugin) {
if (plugin->isViewer() && plugin->getMode() >= QDltPlugin::ModeEnable)
list.append(plugin);
});
Expand All @@ -268,7 +268,8 @@ QList<QDltPlugin*> QDltPluginManager::getViewerPlugins()

bool QDltPluginManager::stateChanged(int index, QDltConnection::QDltConnectionState connectionState,QString hostname)
{
forEachPlugin([&](auto* plugin) {
QMutexLocker mutexLocker(&pluginListMutex);
std::for_each(plugins.begin(), plugins.end(), [&](auto* plugin) {
if (plugin->isControl())
plugin->stateChanged(index, connectionState, hostname);
});
Expand All @@ -278,7 +279,8 @@ bool QDltPluginManager::stateChanged(int index, QDltConnection::QDltConnectionSt

bool QDltPluginManager::autoscrollStateChanged(bool enabled)
{
forEachPlugin([&](auto* plugin){
QMutexLocker mutexLocker(&pluginListMutex);
std::for_each(plugins.begin(), plugins.end(), [&](auto* plugin){
if(plugin->isControl() )
plugin->autoscrollStateChanged(enabled);
});
Expand All @@ -288,7 +290,8 @@ bool QDltPluginManager::autoscrollStateChanged(bool enabled)

bool QDltPluginManager::initControl(QDltControl *control)
{
forEachPlugin([&](auto* plugin){
QMutexLocker mutexLocker(&pluginListMutex);
std::for_each(plugins.begin(), plugins.end(), [&](auto* plugin){
if(plugin->isControl() )
plugin->initControl(control);
});
Expand All @@ -298,7 +301,8 @@ bool QDltPluginManager::initControl(QDltControl *control)

bool QDltPluginManager::initConnections(QStringList list)
{
forEachPlugin([&](auto* plugin){
QMutexLocker mutexLocker(&pluginListMutex);
std::for_each(plugins.begin(), plugins.end(), [&](auto* plugin){
if(plugin->isControl() )
plugin->initConnections(list);
});
Expand Down
14 changes: 3 additions & 11 deletions qdlt/qdltpluginmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ class QDLT_EXPORT QDltPluginManager : public QDltMessageDecoder
void decodeMsg(QDltMsg &msg,int triggeredByUser) override;

//! Get the list of pointers to all loaded plugins
QList<QDltPlugin*> getPlugins() { return plugins; }
QList<QDltPlugin*> getPlugins() const { return plugins; }

//! Get the list of pointers to all enabled decoder plugins
QList<QDltPlugin*> getDecoderPlugins();
QList<QDltPlugin*> getDecoderPlugins() const;

//! Get the list of pointers to all enabled viewer plugins
QList<QDltPlugin*> getViewerPlugins();
QList<QDltPlugin*> getViewerPlugins() const;

//! Find a plugin with the specific name
/*!
Expand All @@ -81,14 +81,6 @@ class QDLT_EXPORT QDltPluginManager : public QDltMessageDecoder
QStringList getPluginPriorities() const;

private:
template <typename F>
void forEachPlugin(F&& f) {
QMutexLocker mutexLocker(&pluginListMutex);
for (QDltPlugin* plugin : plugins) {
f(plugin);
}
}

mutable QMutex pluginListMutex;

//! The list of pointers to all loaded plugins
Expand Down

0 comments on commit 269733c

Please sign in to comment.