diff options
| -rw-r--r-- | Makefile | 26 | ||||
| -rw-r--r-- | main.cpp | 40 | ||||
| -rw-r--r-- | pluginmanager.hpp | 99 | ||||
| -rw-r--r-- | plugins/plugin.hpp | 39 | ||||
| -rw-r--r-- | plugins/plugin_test1.cpp | 35 | ||||
| -rw-r--r-- | plugins/plugin_test1.hpp | 33 | ||||
| -rw-r--r-- | plugins/plugin_test2.cpp | 24 | ||||
| -rw-r--r-- | plugins/plugin_test2.hpp | 34 | ||||
| -rw-r--r-- | plugins/plugin_test3.cpp | 24 | ||||
| -rw-r--r-- | plugins/plugin_test3.hpp | 34 |
10 files changed, 388 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d6ce077 --- /dev/null +++ b/Makefile | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | FILENAME = pluginmanager | ||
| 2 | |||
| 3 | all: all_plugins executable | ||
| 4 | @echo "> MAKING ALL..." | ||
| 5 | |||
| 6 | test: all | ||
| 7 | @echo "> TESTING PLUGIN1..." | ||
| 8 | ./$(FILENAME) ./plugin1.so | ||
| 9 | @echo "> TESTING PLUGIN2..." | ||
| 10 | ./$(FILENAME) ./plugin2.so | ||
| 11 | @echo "> TESTING PLUGIN3..." | ||
| 12 | ./$(FILENAME) ./plugin3.so | ||
| 13 | |||
| 14 | clean: | ||
| 15 | @echo "> CLEANING..." | ||
| 16 | rm $(FILENAME) | ||
| 17 | rm *.so | ||
| 18 | |||
| 19 | all_plugins: | ||
| 20 | @echo "> MAKING PLUGINS..." | ||
| 21 | $(CXX) $(CXXFLAGS) -shared -fPIC -o plugin1.so plugins/plugin_test1.cpp | ||
| 22 | $(CXX) $(CXXFLAGS) -shared -fPIC -o plugin2.so plugins/plugin_test2.cpp | ||
| 23 | $(CXX) $(CXXFLAGS) -shared -fPIC -o plugin3.so plugins/plugin_test3.cpp | ||
| 24 | |||
| 25 | executable: | ||
| 26 | $(CXX) $(CXXFLAGS) -o $(FILENAME) main.cpp -ldl | ||
diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..9f442cb --- /dev/null +++ b/main.cpp | |||
| @@ -0,0 +1,40 @@ | |||
| 1 | /* | ||
| 2 | * main.cpp <PLUGINMANAGER> | ||
| 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 | #include <iostream> | ||
| 23 | #include "pluginmanager.hpp" | ||
| 24 | |||
| 25 | using namespace std; | ||
| 26 | int main(int argc, char* argv[]) | ||
| 27 | { | ||
| 28 | typedef Plugin* create(); | ||
| 29 | string plugin = string(argv[1]); | ||
| 30 | try | ||
| 31 | { | ||
| 32 | PluginManager<Plugin*> *pm = new PluginManager<Plugin*>(); | ||
| 33 | pm->load_plugin(plugin); | ||
| 34 | Plugin* p = pm->get_instance(plugin); | ||
| 35 | cout << "> VERSION: " << p->get_version() << endl << endl; | ||
| 36 | p->dosomething(); | ||
| 37 | } | ||
| 38 | catch(char* e) | ||
| 39 | { cout << e << endl; } | ||
| 40 | } | ||
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 | ||
diff --git a/plugins/plugin.hpp b/plugins/plugin.hpp new file mode 100644 index 0000000..9a354c2 --- /dev/null +++ b/plugins/plugin.hpp | |||
| @@ -0,0 +1,39 @@ | |||
| 1 | /* | ||
| 2 | * plugin.hpp <PLUGINMANAGER> | ||
| 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 PLUGIN_HPP | ||
| 23 | #define PLUGIN_HPP | ||
| 24 | #include <string> | ||
| 25 | #include <sstream> | ||
| 26 | #include <dlfcn.h> | ||
| 27 | #include <iostream> | ||
| 28 | |||
| 29 | using namespace std; | ||
| 30 | class Plugin | ||
| 31 | { | ||
| 32 | private: | ||
| 33 | int version; | ||
| 34 | public: | ||
| 35 | //Plugin() : version(0) | ||
| 36 | virtual const int get_version() = 0; | ||
| 37 | virtual void dosomething() = 0; | ||
| 38 | }; | ||
| 39 | #endif | ||
diff --git a/plugins/plugin_test1.cpp b/plugins/plugin_test1.cpp new file mode 100644 index 0000000..e02612c --- /dev/null +++ b/plugins/plugin_test1.cpp | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | /* | ||
| 2 | * plugin_test1.hpp <PLUGINMANAGER> | ||
| 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 | #include "plugin_test1.hpp" | ||
| 22 | |||
| 23 | plugin_test::plugin_test() | ||
| 24 | { std::cout << "constructor of plugin I.\n"; } | ||
| 25 | |||
| 26 | void plugin_test::dosomething() | ||
| 27 | { std::cout << "teste dosomething in plugin I.\n"; } | ||
| 28 | |||
| 29 | const int plugin_test::get_version() | ||
| 30 | { return VERSION; } | ||
| 31 | |||
| 32 | // the class factories | ||
| 33 | extern "C" plugin_test* create() | ||
| 34 | { return new plugin_test; } | ||
| 35 | |||
diff --git a/plugins/plugin_test1.hpp b/plugins/plugin_test1.hpp new file mode 100644 index 0000000..ec7597d --- /dev/null +++ b/plugins/plugin_test1.hpp | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | /* | ||
| 2 | * plugin_test1.hpp <PLUGINMANAGER> | ||
| 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 | #ifndef PLUGIN_TEST1_HPP | ||
| 22 | #define PLUGIN_TEST1_HPP | ||
| 23 | |||
| 24 | #include "plugin.hpp" | ||
| 25 | #define VERSION 3 | ||
| 26 | class plugin_test : public Plugin | ||
| 27 | { | ||
| 28 | public: | ||
| 29 | plugin_test(); | ||
| 30 | const int get_version(); | ||
| 31 | virtual void dosomething(); | ||
| 32 | }; | ||
| 33 | #endif | ||
diff --git a/plugins/plugin_test2.cpp b/plugins/plugin_test2.cpp new file mode 100644 index 0000000..1dbbf7b --- /dev/null +++ b/plugins/plugin_test2.cpp | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | /* | ||
| 2 | * plugin_test2.cpp <PLUGINMANAGER> | ||
| 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 | #include "plugin_test2.hpp" | ||
| 22 | |||
| 23 | void plugin_test::dosomething() | ||
| 24 | { std::cout << "teste dosomething in plugin II.\n"; } | ||
diff --git a/plugins/plugin_test2.hpp b/plugins/plugin_test2.hpp new file mode 100644 index 0000000..1291b18 --- /dev/null +++ b/plugins/plugin_test2.hpp | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | /* | ||
| 2 | * plugin_test2.hpp <PLUGINMANAGER> | ||
| 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 | #include "plugin.hpp" | ||
| 22 | #define VERSION 2 | ||
| 23 | class plugin_test : public Plugin | ||
| 24 | { | ||
| 25 | public: | ||
| 26 | plugin_test() | ||
| 27 | { std::cout << "Das ist plugin nummer II.\n"; } | ||
| 28 | const int get_version() | ||
| 29 | { return VERSION; } | ||
| 30 | void dosomething(); | ||
| 31 | }; | ||
| 32 | |||
| 33 | extern "C" Plugin* create() | ||
| 34 | { return new plugin_test(); } | ||
diff --git a/plugins/plugin_test3.cpp b/plugins/plugin_test3.cpp new file mode 100644 index 0000000..39c4988 --- /dev/null +++ b/plugins/plugin_test3.cpp | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | /* | ||
| 2 | * plugin_test3.cpp <PLUGINMANAGER> | ||
| 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 | #include "plugin_test3.hpp" | ||
| 22 | |||
| 23 | void plugin_test::dosomething() | ||
| 24 | { std::cout << "teste dosomething in plugin III.\n"; } | ||
diff --git a/plugins/plugin_test3.hpp b/plugins/plugin_test3.hpp new file mode 100644 index 0000000..a057d8f --- /dev/null +++ b/plugins/plugin_test3.hpp | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | /* | ||
| 2 | * plugin_test3.hpp <PLUGINMANAGER> | ||
| 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 | #include "plugin.hpp" | ||
| 22 | #define VERSION 1 | ||
| 23 | class plugin_test : public Plugin | ||
| 24 | { | ||
| 25 | public: | ||
| 26 | plugin_test() | ||
| 27 | { std::cout << "Das ist plugin nummer III.\n"; } | ||
| 28 | const int get_version() | ||
| 29 | { return VERSION; } | ||
| 30 | void dosomething(); | ||
| 31 | }; | ||
| 32 | |||
| 33 | extern "C" Plugin* create() | ||
| 34 | { return new plugin_test(); } | ||
