plugin.hpp

Go to the documentation of this file.
00001 /*************************************************************************
00002  * Copyright (C) 2011 by Saleh Dindar and the Swarm-NG Development Team  *
00003  *                                                                       *
00004  * This program is free software; you can redistribute it and/or modify  *
00005  * it under the terms of the GNU General Public License as published by  *
00006  * the Free Software Foundation; either version 3 of the License.        *
00007  *                                                                       *
00008  * This program is distributed in the hope that it will be useful,       *
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00011  * GNU General Public License for more details.                          *
00012  *                                                                       *
00013  * You should have received a copy of the GNU General Public License     *
00014  * along with this program; if not, write to the                         *
00015  * Free Software Foundation, Inc.,                                       *
00016  * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
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 }

doxygen