plugins_manager.cpp 4.03 KB
Newer Older
1
#include "plugins_manager.h"
2 3
#include "rational_function.h"
#include "vertical_segment.h"
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

#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())
	{
88
		return new rational_function() ;
89 90 91 92 93 94 95 96
	}
	else
	{
		return _functions.begin()->second ;
	}
}
data* plugins_manager::get_data()     const 
{
Laurent Belcour's avatar
Laurent Belcour committed
97
	//if(_datas.empty())
98
	{
99
		std::cout << "<<DEBUG>>  using vertical segment data loader" << std::endl ;
100
		return new vertical_segment() ;
Laurent Belcour's avatar
Laurent Belcour committed
101
	}/*
102 103
	else
	{
104
		std::cout << "<<DEBUG>>  using \"" << _datas.begin()->first << "\" data loader" << std::endl ;
105
		return _datas.begin()->second ;
Laurent Belcour's avatar
Laurent Belcour committed
106
	}*/
107 108 109 110 111 112 113 114 115
}
fitter* plugins_manager::get_fitter()   const 
{
	if(_fitters.empty())
	{
		return NULL ;
	}
	else
	{
116
		std::cout << "<<DEBUG>>  using \"" <<  _fitters.begin()->first << "\"" << std::endl ;
117 118 119 120 121 122 123 124 125 126
		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 
{
127
	if(_functions.count(n) == 0)
128
	{
129
		return new rational_function() ;
130 131 132
	}
	else
	{
Laurent Belcour's avatar
Laurent Belcour committed
133
		return _functions.find(n)->second ;
134 135 136 137
	}
}
data* plugins_manager::get_data(const std::string& n)     const 
{
138
	if(_datas.count(n) == 0)
139
	{
140
		std::cout << "<<DEBUG>>  using vertical segment data loader" << std::endl ;
141
		return new vertical_segment() ;
142 143 144
	}
	else
	{
145
		std::cout << "<<DEBUG>>  using \"" << n << "\" data loader" << std::endl ;
Laurent Belcour's avatar
Laurent Belcour committed
146
		return _datas.find(n)->second ;
147 148 149 150
	}
}
fitter* plugins_manager::get_fitter(const std::string& n)   const 
{
151
	if(_fitters.count(n) == 0)
152 153 154 155 156
	{
		return NULL ;
	}
	else
	{
157
		std::cout << "<<DEBUG>>  using \"" <<  n << "\"" << std::endl ;
Laurent Belcour's avatar
Laurent Belcour committed
158
		return _fitters.find(n)->second ;
159 160
	}
}
161 162 163 164
		
// \todo implement the Darwin (MACOS) version.
#ifdef WIN32
#include <windows.h>
165
size_t plugins_manager::get_system_memory()
166 167 168 169 170 171 172 173
{
	MEMORYSTATUSEX status;
	status.dwLength = sizeof(status);
	GlobalMemoryStatusEx(&status);
	return status.ullTotalPhys;
}
#else
#include <unistd.h>
174
size_t plugins_manager::get_system_memory()
175 176 177 178 179 180
{
	long pages = sysconf(_SC_PHYS_PAGES);
	long page_size = sysconf(_SC_PAGE_SIZE);
	return pages * page_size;
}
#endif