plugin.hpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00030 #pragma once
00031 #include "types/config.hpp"
00032
00033 namespace swarm {
00034
00044 struct plugin {
00046 virtual void* create(const config& cfg) = 0;
00048 virtual std::string description() { return ""; }
00052 virtual std::string id() = 0;
00053
00055 static void add(plugin* p);
00057 static plugin* find(const std::string& id);
00059 static void* instance(const std::string& name, const config& cfg);
00061 static std::vector<plugin*> all();
00062
00063 struct help_t{};
00070 static help_t help;
00071
00072 };
00073
00086 template<class T>
00087 struct basic_plugin : public plugin {
00088
00089 std::string _id, _description;
00091 basic_plugin(const std::string& i, const std::string& d = std::string() )
00092 : _id(i),_description(d) {}
00093
00095 virtual void* create(const config& cfg) { return new T(cfg); }
00097 virtual std::string id() { return _id; }
00099 virtual std::string description() { return _description; }
00100 };
00101
00113 template<class T>
00114 struct plugin_initializer {
00115 T t;
00116 plugin_initializer() {
00117 plugin::add(&t);
00118 }
00119 };
00120
00121
00134 template<class T>
00135 struct basic_plugin_initializer {
00136 basic_plugin<T> t;
00137
00139 basic_plugin_initializer(const std::string& id
00140 , const std::string& description = std::string() )
00141 : t(id,description) {
00142 plugin::add(&t);
00143 }
00144 };
00145
00146
00157 template<class T>
00158 struct integrator_plugin_initializer : public basic_plugin_initializer<T> {
00161 integrator_plugin_initializer(const std::string& id
00162 , const std::string& description = std::string() )
00163 : basic_plugin_initializer<T>("integrator_" + id,description) {}
00164 };
00165
00177 template<class T>
00178 struct writer_plugin_initializer : public basic_plugin_initializer<T> {
00179 writer_plugin_initializer(const std::string& id
00180 , const std::string& description = std::string() )
00181 : basic_plugin_initializer<T>("writer_" + id,description) {}
00182 };
00183
00187 struct plugin_not_found : std::exception {
00189 std::string _name;
00190 plugin_not_found(std::string name) : _name(name) {}
00191 virtual ~plugin_not_found() throw(){}
00192
00193 virtual const char * what() const throw() {
00194 return ("Plugin \"" + _name + "\" was not found ").c_str();
00195 }
00196 };
00197
00206 std::ostream& operator << (std::ostream&, const plugin::help_t&);
00207
00208 }