All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CLToolRegister.cpp
Go to the documentation of this file.
1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2  Copyright (c) 2013 The plumed team
3  (see the PEOPLE file at the root of the distribution for a list of names)
4 
5  See http://www.plumed-code.org for more information.
6 
7  This file is part of plumed, version 2.0.
8 
9  plumed is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  plumed is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 #include "CLToolRegister.h"
23 #include "tools/Tools.h"
24 #include "CLTool.h"
25 #include <algorithm>
26 #include <iostream>
27 
28 
29 using namespace std;
30 namespace PLMD{
31 
32 CLToolRegister::~CLToolRegister(){
33  if(m.size()>0){
34  string names="";
35  for(mIterator p=m.begin();p!=m.end();++p)names+=p->first+" ";
36  std::cerr<<"WARNING: CLTools "+ names +" has not been properly unregistered. This might lead to memory leak!!\n";
37  }
38 }
39 
41  static CLToolRegister ans;
42  return ans;
43 }
44 
45 void CLToolRegister::remove(creator_pointer f){
46  for(mIterator p=m.begin();p!=m.end();++p){
47  if((*p).second==f){
48  m.erase(p); break;
49  }
50  }
51 }
52 
53 void CLToolRegister::add(string key,creator_pointer f,keywords_pointer kf){
54  if(m.count(key)){
55  m.erase(key);
56  disabled.insert(key);
57  }else{
58  m.insert(pair<string,creator_pointer>(key,f));
59  Keywords keys; kf(keys);
60  mk.insert(pair<string,Keywords>(key,keys));
61  };
62 }
63 
64 bool CLToolRegister::check(string key){
65  if(m.count(key)>0) return true;
66  return false;
67 }
68 
69 CLTool* CLToolRegister::create(const CLToolOptions&ao){
70  if(ao.line.size()<1)return NULL;
71  CLTool* cltool;
72  if(check(ao.line[0])){
73  CLToolOptions nao( ao,mk[ao.line[0]] );
74  cltool=m[ao.line[0]](nao);
75  } else cltool=NULL;
76  return cltool;
77 }
78 
79 
80 std::ostream & operator<<(std::ostream &log,const CLToolRegister&ar){
81  vector<string> s(ar.list());
82  for(unsigned i=0;i<s.size();i++) log<<" "<<s[i]<<"\n";
83  if(ar.disabled.size()>0){
84  s.assign(ar.disabled.size(),"");
85  copy(ar.disabled.begin(),ar.disabled.end(),s.begin());
86  sort(s.begin(),s.end());
87  log<<"+++++++ WARNING +++++++\n";
88  log<<"The following keywords have been registered more than once and will be disabled:\n";
89  for(unsigned i=0;i<s.size();i++) log<<" - "<<s[i]<<"\n";
90  log<<"+++++++ END WARNING +++++++\n";
91  };
92  return log;
93 }
94 
95 bool CLToolRegister::printManual( const std::string& cltool ){
96  if ( check(cltool) ){
97  mk[cltool].print_html(false);
98  return true;
99  } else {
100  return false;
101  }
102 }
103 
104 vector<string> CLToolRegister::list()const{
105  vector<string> s;
106  for(const_mIterator it=m.begin();it!=m.end();++it)
107  s.push_back((*it).first);
108  sort(s.begin(),s.end());
109  return s;
110 }
111 
112 
113 
114 }
std::vector< std::string > list() const
Returns a list of the allowed CLTools.
void add(const Value &val1, Value *val2)
Definition: Value.cpp:175
void copy(const Value &val1, Value &val2)
Definition: Value.cpp:159
std::vector< std::string > line
Definition: CLTool.h:40
STL namespace.
std::ostream & operator<<(std::ostream &log, const CLToolRegister &ar)
This is the abstract base class to use for implementing new command line tool, within it there is inf...
Definition: CLTool.h:55
This class holds the keywords and their documentation.
Definition: Keywords.h:36
std::map< std::string, creator_pointer >::iterator mIterator
Iterator over the map.
std::set< std::string > disabled
Set of disabled cltools (which were registered more than once)
CLToolRegister & cltoolRegister()
void const char const char int double int double double int int double int * m
Definition: Matrix.h:42
Same as ActionRegister, but for CLTools.
std::map< std::string, creator_pointer >::const_iterator const_mIterator
Iterator over the map.