summaryrefslogtreecommitdiff
path: root/pluginmanager.hpp
diff options
context:
space:
mode:
authorMax Christian Pohle2016-08-23 02:46:27 +0200
committerMax Christian Pohle2016-08-23 02:46:27 +0200
commit0f595e076f03c02b366901e8d3c5cc0c50681108 (patch)
tree4c3e241e1b66d027f95d03d846c37f01c030fa8b /pluginmanager.hpp
downloadpluginmanager-master.tar.bz2
pluginmanager-master.zip
generic plugin manager example, fully workingHEADmaster
Diffstat (limited to 'pluginmanager.hpp')
-rw-r--r--pluginmanager.hpp99
1 files changed, 99 insertions, 0 deletions
diff --git a/pluginmanager.hpp b/pluginmanager.hpp
new file mode 100644
index 0000000..2415acf
--- /dev/null
+++ b/pluginmanager.hpp
@@ -0,0 +1,99 @@
1/*
2 * pluginmanager.hpp
3 *
4 * Copyright 2009 Max Christian Pohle <coderz@gmx.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22#ifndef pluginmanager
23#define pluginmanager
24 #define PLUGIN_ENGINE_VERSION 1
25
26 #include <string>
27 #include <map>
28 #include <sstream>
29 #include <dlfcn.h>
30 #include <iostream>
31 #include <list>
32
33 #include "plugins/plugin.hpp"
34
35 using namespace std;
36
37 template <class T>
38 class PluginManager
39 {
40 private:
41 int engine_version;
42 typedef T create();
43 map<string,create*> plugins;
44 typename map<string, create*>::iterator iter;
45
46 public:
47 PluginManager<T>() : engine_version(PLUGIN_ENGINE_VERSION) {}
48
49 T get_instance(string t)
50 {
51 iter = plugins.find(t);
52 if (iter != plugins.end())
53 { return iter->second(); }
54 }
55
56 list<string> list_plugins()
57 {
58 list<string> retval;
59 for(iter=plugins.begin(); iter!=plugins.end(); ++iter)
60 { retval.push_front(iter->first); }
61 return retval;
62 }
63
64 void load_plugin(string filename)
65 {
66 std::stringstream serr;
67 char* dlsym_error;
68 std::cerr << "\n> OPENING " << filename << "...";
69 void* plugin2open = dlopen(filename.c_str(), RTLD_LAZY);
70 if (dlsym_error = dlerror())
71 {
72 std::stringstream a;
73 a <<
74 std::string(filename) <<
75 std::string(" is not a valid plugin\n\t") <<
76 std::string(dlsym_error);
77 throw (char*) a.str().c_str();
78 }
79 else
80 {
81 std::cerr << "done.\n";
82 }
83
84 create* f_creator = (create*) dlsym(plugin2open, "create");
85 if (dlsym_error = dlerror())
86 {
87 throw (char*)"plugin has no creation-interface"
88 ", so is obviously no subclass of 'Plugin'";
89 }
90 else
91 {
92 std::cout << "> LOADING " << filename << "...";
93 plugins.insert(make_pair(filename, f_creator));
94 std::cout << "done.\n";
95 }
96 }
97 };
98
99#endif
..