mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-05 01:05:34 +08:00
@@ -1,105 +0,0 @@
|
||||
/* Copyright (C) 2013 VATSIM Community / authors
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "blackcore/pluginmgr.h"
|
||||
#include "blackmisc/plugins.h"
|
||||
#include "blackmisc/context.h"
|
||||
#include <QDirIterator>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace BlackCore
|
||||
{
|
||||
|
||||
CPluginManager::~CPluginManager()
|
||||
{
|
||||
for (QVector<PluginEntry>::iterator it = m_plugins.begin(); it != m_plugins.end(); ++it)
|
||||
{
|
||||
Q_ASSERT_X(it->loader, "Plugin manager", "Previously loaded plugin's loader ptr is null");
|
||||
|
||||
it->loader->unload();
|
||||
}
|
||||
}
|
||||
|
||||
void CPluginManager::loadAllPlugins(const QString &pluginsPath)
|
||||
{
|
||||
//TODO should we use a custom extension, so we don't attempt to load non-plugin .dll/.so files?
|
||||
#ifdef Q_OS_WIN
|
||||
static const QStringList pluginsFilter("*.dll");
|
||||
#else
|
||||
static const QStringList pluginsFilter("*.so");
|
||||
#endif
|
||||
|
||||
QDirIterator iter (pluginsPath, pluginsFilter, QDir::Files);
|
||||
while (iter.hasNext())
|
||||
{
|
||||
QString filename = iter.next();
|
||||
|
||||
QSharedPointer<QPluginLoader> loader (new QPluginLoader (filename));
|
||||
try
|
||||
{
|
||||
if (! loader->load())
|
||||
{
|
||||
throw std::runtime_error(QString("Failed loading plugin from %1").arg(filename).toStdString());
|
||||
}
|
||||
|
||||
PluginEntry entry;
|
||||
entry.loader = loader;
|
||||
entry.factory = qobject_cast<BlackMisc::IPluginFactory*>(loader->instance());
|
||||
|
||||
if (! entry.factory)
|
||||
{
|
||||
throw std::runtime_error(QString("Plugin loaded from %1 is not compatible").arg(filename).toStdString());
|
||||
}
|
||||
|
||||
m_plugins.push_back(entry);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
//TODO warning?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const CPluginManager::PluginEntry &CPluginManager::getEntry(int index) const
|
||||
{
|
||||
Q_ASSERT_X(index < getPluginCount(), "Plugin manager", "Plugin index out of bounds");
|
||||
|
||||
const PluginEntry &entry = m_plugins[index];
|
||||
|
||||
Q_ASSERT_X(entry.factory, "Plugin manager", "Previously loaded plugin's factory ptr is null");
|
||||
Q_ASSERT_X(entry.loader, "Plugin manager", "Previously loaded plugin's loader ptr is null");
|
||||
|
||||
if (! entry.loader->isLoaded())
|
||||
{
|
||||
//TODO throw plugin loader unexpectedly unloaded
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
const QString CPluginManager::getName(int index) const
|
||||
{
|
||||
return getFactory(index)->getName();
|
||||
}
|
||||
|
||||
const QString CPluginManager::getDescription(int index) const
|
||||
{
|
||||
return getFactory(index)->getDescription();
|
||||
}
|
||||
|
||||
BlackMisc::IPlugin *CPluginManager::constructPlugin(int index)
|
||||
{
|
||||
BlackMisc::IPlugin *plugin = getFactory(index)->create(BlackMisc::IContext::getInstance());
|
||||
if (! plugin->isValid())
|
||||
{
|
||||
delete plugin;
|
||||
plugin = 0;
|
||||
//TODO throw plugin construction failed
|
||||
}
|
||||
return plugin;
|
||||
}
|
||||
|
||||
} // namespace BlackCore
|
||||
@@ -1,136 +0,0 @@
|
||||
/* Copyright (C) 2013 VATSIM Community / authors
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/*!
|
||||
\file
|
||||
*/
|
||||
|
||||
#ifndef BLACKCORE_PLUGINMGR_H
|
||||
#define BLACKCORE_PLUGINMGR_H
|
||||
|
||||
#include <QPluginLoader>
|
||||
#include <QVector>
|
||||
#include <QSharedPointer>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
class IPlugin;
|
||||
class IPluginFactory;
|
||||
}
|
||||
|
||||
namespace BlackCore
|
||||
{
|
||||
|
||||
/*!
|
||||
Plugin manager.
|
||||
Loads plugins and allows them to be queried and instantiated.
|
||||
*/
|
||||
class CPluginManager
|
||||
{
|
||||
public:
|
||||
/*!
|
||||
Destructor.
|
||||
*/
|
||||
virtual ~CPluginManager();
|
||||
|
||||
/*!
|
||||
Attempt to load all plugins found at the given path.
|
||||
\param pluginsPath
|
||||
*/
|
||||
void loadAllPlugins(const QString &pluginsPath);
|
||||
|
||||
/*!
|
||||
Return the total number of plugins loaded so far.
|
||||
\return
|
||||
*/
|
||||
int getPluginCount() const
|
||||
{
|
||||
return m_plugins.size();
|
||||
}
|
||||
|
||||
/*!
|
||||
Return the name of a plugin.
|
||||
\param index the plugin's index in the vector of plugins.
|
||||
\return
|
||||
*/
|
||||
const QString getName(int index) const;
|
||||
|
||||
/*!
|
||||
Return the description of a plugin.
|
||||
\param index the plugin's index in the vector of plugins.
|
||||
\return
|
||||
*/
|
||||
const QString getDescription(int index) const;
|
||||
|
||||
/*!
|
||||
Construct a plugin.
|
||||
\param index the plugin's index in the vector of plugins.
|
||||
\return a pointer to the newly created plugin.
|
||||
\warning You must release this pointer with IPluginFactory::destroy().
|
||||
*/
|
||||
BlackMisc::IPlugin *constructPlugin(int index);
|
||||
|
||||
/*!
|
||||
Direct access to the factory. You don't usually need this.
|
||||
\param index
|
||||
\return
|
||||
*/
|
||||
BlackMisc::IPluginFactory *getFactory(int index)
|
||||
{
|
||||
return const_cast<BlackMisc::IPluginFactory*>(static_cast<const CPluginManager*>(this)->getFactory(index));
|
||||
}
|
||||
|
||||
/*!
|
||||
Direct access to the factory. You don't usually need this.
|
||||
\param index
|
||||
\return
|
||||
*/
|
||||
const BlackMisc::IPluginFactory *getFactory(int index) const
|
||||
{
|
||||
return getEntry(index).factory;
|
||||
}
|
||||
|
||||
/*!
|
||||
Direct access to the QPluginLoader. You don't usually need this.
|
||||
\param index
|
||||
\return
|
||||
*/
|
||||
QPluginLoader *getLoader(int index)
|
||||
{
|
||||
return const_cast<QPluginLoader*>(static_cast<const CPluginManager*>(this)->getLoader(index));
|
||||
}
|
||||
|
||||
/*!
|
||||
Direct access to the QPluginLoader. You don't usually need this.
|
||||
\param index
|
||||
\return
|
||||
*/
|
||||
const QPluginLoader *getLoader(int index) const
|
||||
{
|
||||
return getEntry(index).loader.data();
|
||||
}
|
||||
|
||||
private:
|
||||
class PluginEntry
|
||||
{
|
||||
public:
|
||||
PluginEntry() : factory(0) {}
|
||||
BlackMisc::IPluginFactory *factory;
|
||||
QSharedPointer<QPluginLoader> loader;
|
||||
};
|
||||
|
||||
QVector<PluginEntry> m_plugins;
|
||||
|
||||
PluginEntry &getEntry(int index)
|
||||
{
|
||||
return const_cast<PluginEntry&>(static_cast<const CPluginManager*>(this)->getEntry(index));
|
||||
}
|
||||
|
||||
const PluginEntry &getEntry(int index) const;
|
||||
};
|
||||
|
||||
} // namespace BlackCore
|
||||
|
||||
#endif //BLACKCORE_PLUGINMGR_H
|
||||
Reference in New Issue
Block a user