plugins_manager.cpp 2.96 KB
Newer Older
1
#include "plugins_manager.h"
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

#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 
{
122
	if(_functions.count(n) == 0)
123 124 125 126 127
	{
		return NULL ;
	}
	else
	{
128
		return _functions.at(n) ;
129 130 131 132
	}
}
data* plugins_manager::get_data(const std::string& n)     const 
{
133
	if(_datas.count(n) == 0)
134 135 136 137 138
	{
		return NULL ;
	}
	else
	{
139
		return _datas.at(n) ;
140 141 142 143
	}
}
fitter* plugins_manager::get_fitter(const std::string& n)   const 
{
144
	if(_fitters.count(n) == 0)
145 146 147 148 149
	{
		return NULL ;
	}
	else
	{
150
		return _fitters.at(n) ;
151 152
	}
}