plugin.cpp

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 
00026 #include "common.hpp"
00027 #include "plugin.hpp"
00028 
00029 using namespace std;
00030 
00031 namespace swarm {
00032 
00033 typedef map<string,plugin*> plugins_map_t;
00034 
00035 
00036 plugins_map_t& plugins_map() {
00037         static plugins_map_t pmap;
00038         return pmap;
00039 }
00040 
00041 void plugin::add(plugin* p){
00042         plugins_map()[p->id()] = p;
00043 }
00044 
00045 plugin* plugin::find(const std::string& name){
00046         plugin* p = plugins_map()[name];
00047         if(p==0)
00048                 throw plugin_not_found(name);
00049         return p;
00050 }
00051 void* plugin::instance(const std::string& name,const config& cfg){
00052         return plugin::find(name)->create(cfg);
00053 }
00054 
00055 vector<plugin*> plugin::all() {
00056         vector<plugin*> v;
00057         for(plugins_map_t::iterator i = plugins_map().begin(); i != plugins_map().end(); i++) {
00058                 v.push_back(i->second);
00059         }
00060         return v;
00061 }
00062 
00063 
00064 plugin::help_t plugin::help;
00065 
00066 ostream& operator << (ostream& out, const plugin::help_t&){
00067         vector<plugin*> ps = plugin::all();
00068         out << "Found " << ps.size() << " plugins " << endl;
00069         for(int i = 0; i < ps.size(); i++){
00070                 out << ps[i]->id() << "\t" << ps[i]->description() << endl;
00071         }
00072         return out;
00073 }
00074 
00075 }

doxygen