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