Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2012-2019 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.org for more information. 6 : 7 : This file is part of plumed, version 2. 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 1958 : CLToolRegister::~CLToolRegister() { 33 1958 : if(m.size()>0) { 34 0 : string names=""; 35 0 : for(const auto & p : m) names+=p.first+" "; 36 0 : std::cerr<<"WARNING: CLTools "+ names +" has not been properly unregistered. This might lead to memory leak!!\n"; 37 : } 38 1958 : } 39 : 40 58492 : CLToolRegister& cltoolRegister() { 41 60450 : static CLToolRegister ans; 42 58492 : return ans; 43 : } 44 : 45 27412 : void CLToolRegister::remove(creator_pointer f) { 46 209506 : for(auto p=m.begin(); p!=m.end(); ++p) { 47 182094 : if((*p).second==f) { 48 : m.erase(p); break; 49 : } 50 : } 51 27412 : } 52 : 53 27412 : void CLToolRegister::add(string key,creator_pointer f,keywords_pointer kf) { 54 27412 : if(m.count(key)) { 55 : m.erase(key); 56 : disabled.insert(key); 57 : } else { 58 27412 : m.insert(pair<string,creator_pointer>(key,f)); 59 27412 : Keywords keys; kf(keys); 60 54824 : mk.insert(pair<string,Keywords>(key,keys)); 61 : }; 62 27412 : } 63 : 64 1487 : bool CLToolRegister::check(string key)const { 65 1487 : if(m.count(key)>0) return true; 66 0 : return false; 67 : } 68 : 69 1487 : std::unique_ptr<CLTool> CLToolRegister::create(const CLToolOptions&ao) { 70 1487 : if(ao.line.size()<1)return NULL; 71 : std::unique_ptr<CLTool> cltool; 72 2974 : if(check(ao.line[0])) { 73 1487 : CLToolOptions nao( ao,mk[ao.line[0]] ); 74 2974 : cltool=m[ao.line[0]](nao); 75 : } 76 : return cltool; 77 : } 78 : 79 : 80 270 : std::ostream & operator<<(std::ostream &log,const CLToolRegister&ar) { 81 270 : vector<string> s(ar.list()); 82 11610 : for(unsigned i=0; i<s.size(); i++) log<<" "<<s[i]<<"\n"; 83 270 : if(!ar.disabled.empty()) { 84 0 : s.assign(ar.disabled.size(),""); 85 : copy(ar.disabled.begin(),ar.disabled.end(),s.begin()); 86 0 : sort(s.begin(),s.end()); 87 0 : log<<"+++++++ WARNING +++++++\n"; 88 0 : log<<"The following keywords have been registered more than once and will be disabled:\n"; 89 0 : for(unsigned i=0; i<s.size(); i++) log<<" - "<<s[i]<<"\n"; 90 0 : log<<"+++++++ END WARNING +++++++\n"; 91 : }; 92 270 : return log; 93 : } 94 : 95 0 : bool CLToolRegister::printManual( const std::string& cltool, const bool& spelling ) { 96 0 : if( spelling && check(cltool) ) { 97 0 : mk[cltool].print_spelling(); 98 0 : return true; 99 0 : } else if ( check(cltool) ) { 100 0 : mk[cltool].print_html(); 101 0 : return true; 102 : } else { 103 : return false; 104 : } 105 : } 106 : 107 0 : std::vector<std::string> CLToolRegister::getKeys(const std::string& cltool)const { 108 0 : if ( check(cltool) ) { 109 : return mk.find(cltool)->second.getKeys(); 110 : } else { 111 : std::vector<std::string> empty; 112 0 : return empty; 113 : } 114 : } 115 : 116 : 117 2181 : vector<string> CLToolRegister::list()const { 118 : vector<string> s; 119 34896 : for(const auto & it : m) s.push_back(it.first); 120 2181 : sort(s.begin(),s.end()); 121 2181 : return s; 122 : } 123 : 124 : 125 : 126 5874 : }