Swarm-NG  1.1
plugin.cpp
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 
26 #include "common.hpp"
27 #include "plugin.hpp"
28 
29 using namespace std;
30 
31 namespace swarm {
32 
33 typedef map<string,plugin*> plugins_map_t;
34 
35 
36 plugins_map_t& plugins_map() {
37  static plugins_map_t pmap;
38  return pmap;
39 }
40 
41 void plugin::add(plugin* p){
42  plugins_map()[p->id()] = p;
43 }
44 
45 plugin* plugin::find(const std::string& name){
46  plugin* p = plugins_map()[name];
47  if(p==0)
48  throw plugin_not_found(name);
49  return p;
50 }
51 void* plugin::instance(const std::string& name,const config& cfg){
52  return plugin::find(name)->create(cfg);
53 }
54 
55 vector<plugin*> plugin::all() {
56  vector<plugin*> v;
57  for(plugins_map_t::iterator i = plugins_map().begin(); i != plugins_map().end(); i++) {
58  v.push_back(i->second);
59  }
60  return v;
61 }
62 
63 
64 plugin::help_t plugin::help;
65 
66 ostream& operator << (ostream& out, const plugin::help_t&){
67  vector<plugin*> ps = plugin::all();
68  out << "Found " << ps.size() << " plugins " << endl;
69  for(int i = 0; i < ps.size(); i++){
70  out << ps[i]->id() << "\t" << ps[i]->description() << endl;
71  }
72  return out;
73 }
74 
75 }