Commit 357793df authored by Laurent Belcour's avatar Laurent Belcour

First version of the plugin manager

parent e18951ae
TARGET = lib
CONFIG += static
TEMPLATE = lib
CONFIG *= static \
qt
DESTDIR = ../build
HEADERS = args.h \
common.h \
data.h \
fitter.h \
function.h \
plugin_manager.h
SOURCES = plugin_manager.cpp
#include "plugin_manager.h"
#include <QCoreApplication>
#include <QPluginLoader>
#include <QtPlugin>
#include <QDir>
// Create the object, parse the argument and load
// all the plugins
//
plugins_manager::plugins_manager(const arguments& args)
{
QDir pluginsDir;
if(args.is_defined("plugins"))
{
pluginsDir = QDir(args["plugins"].c_str()) ;
}
else if(QCoreApplication::instance() != NULL)
{
pluginsDir = QDir(QCoreApplication::instance()->applicationDirPath());
}
foreach (QString fileName, pluginsDir.entryList(QDir::Files))
{
QPluginLoader loader ;
loader.setLoadHints(QLibrary::ExportExternalSymbolsHint) ;
loader.setFileName(pluginsDir.absoluteFilePath(fileName));
// Convert filename for outputing
std::string filename(fileName.toStdString()) ;
if(!loader.load())
{
#ifdef DEBUG
std::cout << "<<DEBUG>> " << loader.errorString().toStdString() << std::endl ;
#endif
continue ;
}
QObject *plugin = loader.instance();
if (plugin != NULL)
{
#ifdef DEBUG
std::cout << "<<DEBUG>> loading plugin " << filename << std::endl ;
#endif
if(dynamic_cast<function*>(plugin) != NULL)
{
#ifdef DEBUG
std::cout << "<<DEBUG>> -> it is a function" << std::endl ;
#endif
_functions.insert(std::pair<std::string, function*>(filename, dynamic_cast<function*>(plugin))) ;
}
if(dynamic_cast<data*>(plugin) != NULL)
{
#ifdef DEBUG
std::cout << "<<DEBUG>> -> it is a data loader" << std::endl ;
#endif
_datas.insert(std::pair<std::string, data*>(filename, dynamic_cast<data*>(plugin))) ;
}
if(dynamic_cast<fitter*>(plugin) != NULL)
{
#ifdef DEBUG
std::cout << "<<DEBUG>> -> it is a fitter" << std::endl ;
#endif
_fitters.insert(std::pair<std::string, fitter*>(filename, dynamic_cast<fitter*>(plugin))) ;
}
}
else
{
#ifdef DEBUG
std::cout << "<<DEBUG>> " << loader.errorString().toStdString() << std::endl ;
#endif
}
}
}
// Get instances of the function, the data and the
// fitter
//
function* plugins_manager::get_function() const
{
if(_functions.empty())
{
return NULL ;
}
else
{
return _functions.begin()->second ;
}
}
data* plugins_manager::get_data() const
{
if(_datas.empty())
{
return NULL ;
}
else
{
return _datas.begin()->second ;
}
}
fitter* plugins_manager::get_fitter() const
{
if(_fitters.empty())
{
return NULL ;
}
else
{
return _fitters.begin()->second ;
}
}
// Get instances of the function, the data and the
// fitter, select one based on the name. Return null
// if no one exist.
//
function* plugins_manager::get_function(const std::string& n) const
{
if(_functions.empty())
{
return NULL ;
}
else
{
return _functions.at(n)->second ;
}
}
data* plugins_manager::get_data(const std::string& n) const
{
if(_datas.empty())
{
return NULL ;
}
else
{
return _datas.at(n)->second ;
}
}
fitter* plugins_manager::get_fitter(const std::string& n) const
{
if(_fitters.empty())
{
return NULL ;
}
else
{
return _fitters.at(n)->second ;
}
}
#pragma once
#include <function.h>
#include <data.h>
#include <fitter.h>
#include <args.h>
#include <map>
#include <string>
#include "function.h"
#include "data.h"
#include "fitter.h"
#include "args.h"
class plugins_manager
{
......@@ -15,15 +18,23 @@ class plugins_manager
plugins_manager(const arguments& args) ;
// Get instances of the function, the data and the
// fitter
// fitter. Select the first in the map,
//
function* get_function() const ;
data* get_data() const ;
fitter* get_fitter() const ;
// Get instances of the function, the data and the
// fitter, select one based on the name. Return null
// if no one exist.
//
function* get_function(const std::string& n) const ;
data* get_data(const std::string& n) const ;
fitter* get_fitter(const std::string& n) const ;
private: //data
std::vector<function*> _functions ;
std::vector<data*> _datas ;
std::vector<fitter*> _fitters ;
std::map<std::string, function*> _functions ;
std::map<std::string, data*> _datas ;
std::map<std::string, fitter*> _fitters ;
} ;
......@@ -10,7 +10,8 @@ INCLUDEPATH += ../rational_function \
../rational_data \
../..
HEADERS = rational_fitter.h
HEADERS = rational_fitter.h \
eiquadprog.hpp
SOURCES = rational_fitter.cpp
LIBS += -L../../build \
......
TEMPLATE = subdirs
SUBDIRS = \#core \
SUBDIRS = core \
plugins \
tests
#plugins.depends = core
plugins.depends = core
tests.depends = plugins
......@@ -2,6 +2,7 @@
#include <core/data.h>
#include <core/function.h>
#include <core/fitter.h>
#include <core/plugins_manager.h>
#include <QPluginLoader>
#include <QtPlugin>
......@@ -23,6 +24,7 @@ int main(int argc, char** argv)
QApplication app(argc, argv, false);
arguments args(argc, argv) ;
#ifdef OLD
std::vector<function*> functions ;
std::vector<data*> datas ;
std::vector<fitter*> fitters ;
......@@ -99,7 +101,12 @@ int main(int argc, char** argv)
plugins.push_back(fileName) ;
}
}
fitter* fit = fitters[0] ;
#else
plugins_manager manager(args) ;
fitter* fit = manager.get_fitter() ;
#endif
if(! args.is_defined("input")) {
std::cerr << "<<ERROR>> the input filename is not defined" << std::endl ;
......@@ -110,18 +117,18 @@ int main(int argc, char** argv)
return 1 ;
}
if(fitters.size() > 0 && datas.size() > 0 && functions.size() > 0)
// if(fitters.size() > 0 && datas.size() > 0 && functions.size() > 0)
{
fitters[0]->set_parameters(args) ;
fit->set_parameters(args) ;
/*
function* f = functions[0] ;
data* d = datas[0] ;
*/
function* f = fitters[0]->provide_function() ;
data* d = fitters[0]->provide_data() ;
function* f = fit->provide_function() ;
data* d = fit->provide_data() ;
d->load(args["input"], args);
bool is_fitted = fitters[0]->fit_data(d, f) ;
bool is_fitted = fit->fit_data(d, f) ;
// Display the result
if(is_fitted)
......@@ -194,11 +201,11 @@ int main(int argc, char** argv)
}
}
else
/* else
{
std::cout << "<<ERROR>> not enough plugin defined" << std::endl ;
}
*/
return 0 ;
}
......@@ -4,6 +4,7 @@ DESTDIR = ../../build
INCLUDEPATH += ../../ ../../libs/rational_1d \
SOURCES += main.cpp
LIBS += -L../../build -lcore
#QMAKE_CXXFLAGS += -frounding-math -fPIC
#QMAKE_LFLAGS += -Wl,-rpath='\$\$ORIGIN:.:./build:./plugins'
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment