PluginProvider.h 1.5 KB
Newer Older
1
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
#ifndef PLUGINPROVER_H
#define PLUGINPROVER_H

#include "dlfcn.h"
#include <string>

using namespace std;

namespace AMDiS {
template< typename DesiredCreator >
struct PluginProvider { 
	typedef DesiredCreator* Creator();
	typedef void Remover(DesiredCreator* );

	string createName;
	string removeName;

	Remover* remover;

	PluginProvider(string c, string r):
		createName(c),
		removeName(r),
		remover(NULL)
	{}

	enum Error { NOLIBRARY, NOCREATOR, NOREMOVER };
	struct Exception {
		Error e;
		string message;
		Exception(Error e, string m):
			e(e),message(m)
		{}
	};

	DesiredCreator* create(string libname) {
		void* library=dlopen(libname.c_str(), RTLD_LAZY);
		if(library==NULL) {
			throw Exception(NOLIBRARY, dlerror());
		}

		dlerror();

		Creator* creator=(Creator*) (dlsym(library, createName.c_str()));
		if(creator == NULL) {
			throw Exception(NOCREATOR, dlerror());
		}

		remover=(Remover*) (dlsym(library, removeName.c_str()));
		if(remover == NULL) {
			throw Exception(NOREMOVER, dlerror());
		}

		DesiredCreator* creatingCreator=creator();
		return creatingCreator;
	}

	void remove(DesiredCreator* dc) {
		if(remover == NULL) {
			throw Exception(NOREMOVER, "the remover was not set\n");
		}
		remover(dc);
	}
};

//typedef OEMSolverCreator* createSolverCreator();
//typedef void removeSolverCreator(OEMSolverCreator*);
struct SolverPluginProvider : PluginProvider< OEMSolverCreator > {
	SolverPluginProvider():
		PluginProvider< OEMSolverCreator >("createSolverCreator","removeSolverCreator")
	{}
};
}
#endif