plugins_manager.cpp 4.88 KB
Newer Older
1
#include "plugins_manager.h"
2 3
#include "rational_function.h"
#include "vertical_segment.h"
4 5 6 7

#include <QCoreApplication>
#include <QPluginLoader>
#include <QtPlugin>
8
#include <QLibrary>
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
#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
		}
	}
}

82 83 84 85 86 87 88
#define USING_STATIC
#ifdef USING_STATIC
typedef function* (*FunctionPrototype)();
typedef fitter*   (*FitterPrototype)();
typedef data*     (*DataPrototype)();
#endif

89 90 91
// Get instances of the function, the data and the
// fitter
//
92
function* plugins_manager::get_function()
93
{
94 95 96 97
#ifdef USING_STATIC
    return new rational_function() ;
#else
    if(_functions.empty())
98
	{
99
		return new rational_function() ;
100 101 102 103 104
	}
	else
	{
		return _functions.begin()->second ;
	}
105 106
#endif

107 108 109
}
data* plugins_manager::get_data()     const 
{
Laurent Belcour's avatar
Laurent Belcour committed
110
	//if(_datas.empty())
111
	{
112 113 114
#ifdef DEBUG
        std::cout << "<<DEBUG>>  using vertical segment data loader" << std::endl ;
#endif
115
		return new vertical_segment() ;
Laurent Belcour's avatar
Laurent Belcour committed
116
	}/*
117 118
	else
	{
119
#ifdef DEBUG
120
		std::cout << "<<DEBUG>>  using \"" << _datas.begin()->first << "\" data loader" << std::endl ;
121
#endif
122
		return _datas.begin()->second ;
Laurent Belcour's avatar
Laurent Belcour committed
123
	}*/
124 125 126 127 128 129 130 131 132
}
fitter* plugins_manager::get_fitter()   const 
{
	if(_fitters.empty())
	{
		return NULL ;
	}
	else
	{
133
#ifdef DEBUG
134
		std::cout << "<<DEBUG>>  using \"" <<  _fitters.begin()->first << "\"" << std::endl ;
135
#endif
136 137 138 139
		return _fitters.begin()->second ;
	}
}

140

141 142 143 144
// Get instances of the function, the data and the
// fitter, select one based on the name. Return null
// if no one exist.
//
145
function* plugins_manager::get_function(const std::string& n)
146
{
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
#ifdef USING_STATIC
    FunctionPrototype myFunction = (FunctionPrototype) QLibrary::resolve(QString(n.c_str()), "_Z16provide_functionv");

    if(myFunction != NULL)
    {
#ifdef DEBUG
        std::cout << "<<DEBUG>> using function provider in file \"" << n << "\"" << std::endl;
#endif
        return myFunction();
    }
    else
    {
        std::cerr << "<<ERROR>> no function provider found in file \"" << n << "\"" << std::endl;
        return new rational_function() ;
    }
#else

164
	if(_functions.count(n) == 0)
165
	{
166
		return new rational_function() ;
167 168 169
	}
	else
	{
Laurent Belcour's avatar
Laurent Belcour committed
170
		return _functions.find(n)->second ;
171
	}
172
#endif
173 174 175
}
data* plugins_manager::get_data(const std::string& n)     const 
{
176
	if(_datas.count(n) == 0)
177
	{
178
#ifdef DEBUG
179
		std::cout << "<<DEBUG>>  using vertical segment data loader" << std::endl ;
180
#endif
181
		return new vertical_segment() ;
182 183 184
	}
	else
	{
185
#ifdef DEBUG
186
		std::cout << "<<DEBUG>>  using \"" << n << "\" data loader" << std::endl ;
187
#endif
Laurent Belcour's avatar
Laurent Belcour committed
188
		return _datas.find(n)->second ;
189 190 191 192
	}
}
fitter* plugins_manager::get_fitter(const std::string& n)   const 
{
193
	if(_fitters.count(n) == 0)
194 195 196 197 198
	{
		return NULL ;
	}
	else
	{
199
#ifdef DEBUG
200
		std::cout << "<<DEBUG>>  using \"" <<  n << "\"" << std::endl ;
201
#endif
Laurent Belcour's avatar
Laurent Belcour committed
202
		return _fitters.find(n)->second ;
203 204
	}
}
205 206 207 208
		
// \todo implement the Darwin (MACOS) version.
#ifdef WIN32
#include <windows.h>
209
size_t plugins_manager::get_system_memory()
210 211 212 213 214 215 216 217
{
	MEMORYSTATUSEX status;
	status.dwLength = sizeof(status);
	GlobalMemoryStatusEx(&status);
	return status.ullTotalPhys;
}
#else
#include <unistd.h>
218
size_t plugins_manager::get_system_memory()
219 220 221 222 223 224
{
	long pages = sysconf(_SC_PHYS_PAGES);
	long page_size = sysconf(_SC_PAGE_SIZE);
	return pages * page_size;
}
#endif