Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2011-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 "ActionRegister.h" 23 : #include "ModuleMap.h" 24 : #include "Action.h" 25 : #include <algorithm> 26 : 27 : namespace PLMD { 28 : 29 7000756 : ActionRegister& actionRegister() { 30 7008077 : static ActionRegister ans; 31 7000756 : return ans; 32 : } 33 : 34 0 : std::unique_ptr<Action> ActionRegister::create(const ActionOptions&ao) { 35 0 : std::vector<void*> images; // empty vector 36 0 : return create(images,ao); 37 0 : } 38 : 39 66035 : std::unique_ptr<Action> ActionRegister::create(const std::vector<void*> & images,const ActionOptions&ao) try { 40 66035 : if(ao.line.size()<1) { 41 : return nullptr; 42 : } 43 : 44 66035 : auto content=get(images,ao.line[0]); 45 66033 : Keywords keys; 46 : //the name of the function is not clear but does `keys.thisactname = ao.line[0];` 47 66033 : keys.setDisplayName(ao.line[0]); 48 66033 : content.keys(keys); 49 66033 : ActionOptions nao( ao,keys ); 50 66033 : auto fullPath=getFullPath(images,ao.line[0]); 51 : nao.setFullPath(fullPath); 52 66033 : return content.create(nao); 53 446 : } catch (PLMD::ExceptionRegisterError &e ) { 54 : auto& actionName = e.getMissingKey(); 55 : e <<"Action \"" << actionName << "\" is not known."; 56 2 : if (getModuleMap().count(actionName)>0) { 57 : e << "\nAn Action named \"" 58 : <<actionName 59 : <<"\" is available in module \"" 60 0 : << getModuleMap().at(actionName) 61 : << "\".\nPlease consider installing PLUMED with that module enabled."; 62 : } 63 2 : throw e; 64 2 : } 65 : 66 476 : bool ActionRegister::printManual(const std::string& action, const bool& vimout, const bool& spellout) { 67 476 : if ( check(action) ) { 68 474 : Keywords keys; 69 474 : getKeywords( action, keys ); 70 474 : if( vimout ) { 71 : printf("%s",action.c_str()); 72 472 : keys.print_vim(); 73 : printf("\n"); 74 2 : } else if( spellout ) { 75 1 : keys.print_spelling(); 76 : } else { 77 1 : keys.print_html(); 78 : } 79 : return true; 80 474 : } else { 81 : return false; 82 : } 83 : } 84 : 85 2 : bool ActionRegister::printTemplate(const std::string& action, bool include_optional) { 86 : //no need to insert the try/catch block: check will ensure that action is known 87 2 : if( check(action) ) { 88 2 : Keywords keys; 89 : //the name of the function is not clear but does `keys.thisactname = action;` 90 2 : keys.setDisplayName(action); 91 2 : get(action).keys(keys); 92 2 : keys.print_template(action, include_optional); 93 : return true; 94 2 : } else { 95 : return false; 96 : } 97 : } 98 : 99 4 : std::vector<std::string> ActionRegister::getActionNames() const { 100 4 : return getKeys(); 101 : } 102 : 103 3448216 : ActionRegister::ID ActionRegister::add(std::string key,creator_pointer cp,keywords_pointer kp) { 104 : // this force each action to be registered as an uppercase string 105 3448216 : if ( std::any_of( std::begin( key ), std::end( key ), []( char c ) { 106 14766542 : return ( std::islower( c ) ) 107 : ; 108 0 : } ) ) plumed_error() << "Action: " + key + " cannot be registered, use only UPPERCASE characters"; 109 6896432 : return RegisterBase::add(key,Pointers{cp,kp}); 110 : } 111 : 112 26040 : bool ActionRegister::getKeywords(const std::string& action, Keywords& keys) { 113 : //no need to insert the try/catch block: check will ensure that action is known 114 26040 : if(check(action)) { 115 : //the name of the function is not clear but does `keys.thisactname = action;` 116 26040 : keys.setDisplayName(action); 117 26040 : get(action).keys(keys); 118 26040 : return true; 119 : } 120 : return false; 121 : } 122 : 123 96 : void ActionRegister::getKeywords(const std::vector<void*> & images, const std::string& action, Keywords& keys) { 124 96 : auto content=get(images,action); 125 : //the name of the function is not clear but does `keys.thisactname = action;` 126 96 : keys.setDisplayName(action); 127 96 : content.keys(keys); 128 96 : } 129 : 130 : }