Swarm-NG  1.1
plugin.hpp
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) 2011 by Saleh Dindar and the Swarm-NG Development Team *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 3 of the License. *
7  * *
8  * This program is distributed in the hope that it will be useful, *
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11  * GNU General Public License for more details. *
12  * *
13  * You should have received a copy of the GNU General Public License *
14  * along with this program; if not, write to the *
15  * Free Software Foundation, Inc., *
16  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
17  ************************************************************************/
18 
30 #pragma once
31 #include "types/config.hpp"
32 
33 namespace swarm {
34 
44 struct plugin {
46  virtual void* create(const config& cfg) = 0;
48  virtual std::string description() { return ""; }
52  virtual std::string id() = 0;
53 
55  static void add(plugin* p);
57  static plugin* find(const std::string& id);
59  static void* instance(const std::string& name, const config& cfg);
61  static std::vector<plugin*> all();
62 
63  struct help_t{};
70  static help_t help;
71 
72 };
73 
86 template<class T>
87 struct basic_plugin : public plugin {
88 
89  std::string _id, _description;
91  basic_plugin(const std::string& i, const std::string& d = std::string() )
92  : _id(i),_description(d) {}
93 
95  virtual void* create(const config& cfg) { return new T(cfg); }
97  virtual std::string id() { return _id; }
99  virtual std::string description() { return _description; }
100 };
101 
113 template<class T>
115  T t;
117  plugin::add(&t);
118  }
119 };
120 
121 
134 template<class T>
136  basic_plugin<T> t;
137 
139  basic_plugin_initializer(const std::string& id
140  , const std::string& description = std::string() )
141  : t(id,description) {
142  plugin::add(&t);
143  }
144 };
145 
146 
157 template<class T>
161  integrator_plugin_initializer(const std::string& id
162  , const std::string& description = std::string() )
163  : basic_plugin_initializer<T>("integrator_" + id,description) {}
164 };
165 
177 template<class T>
179  writer_plugin_initializer(const std::string& id
180  , const std::string& description = std::string() )
181  : basic_plugin_initializer<T>("writer_" + id,description) {}
182 };
183 
187 struct plugin_not_found : std::exception {
189  std::string _name;
190  plugin_not_found(std::string name) : _name(name) {}
191  virtual ~plugin_not_found() throw(){}
192 
193  virtual const char * what() const throw() {
194  return ("Plugin \"" + _name + "\" was not found ").c_str();
195  }
196 };
197 
206 std::ostream& operator << (std::ostream&, const plugin::help_t&);
207 
208 }