Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2011-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 : #ifndef __PLUMED_core_ActionSet_h 23 : #define __PLUMED_core_ActionSet_h 24 : 25 : #include "Action.h" 26 : #include <memory> 27 : 28 : namespace PLMD { 29 : 30 : class PlumedMain; 31 : 32 : /// std::vector containing the sequence of Action to be done. 33 : /// It is a vector of Action*, and as such it has the entire 34 : /// std::vector interface. Moreover, it implements methods to extract 35 : /// Acion* of a given type (select<T>()), NOT of a given type (selectNot<T>()) 36 : /// or to find an Action with a given label (selectWithLabel()) 37 : /// Finally, since it holds pointers, there is a clearDelete() function 38 : /// which deletes the pointers before deleting the vector 39 : class ActionSet: 40 : public std::vector<std::unique_ptr<Action>> 41 : { 42 : PlumedMain& plumed; 43 : public: 44 : explicit ActionSet(PlumedMain&p); 45 : ~ActionSet(); 46 : /// Clear and deletes all the included pointers. 47 : void clearDelete(); 48 : 49 : /// Extract pointers to all Action's of type T 50 : /// To extract all Colvar , use select<Colvar*>(); 51 : template <class T> 52 : std::vector<T> select()const; 53 : /// Extract pointers to all Action's which are not of type T 54 : /// E.g., to extract all noncolvars, use 55 : /// selectNot<Colvar*>(); 56 : template <class T> 57 : std::vector<Action*> selectNot()const; 58 : /// Extract pointer to an action labeled s, only if it is of 59 : /// type T. E.g., to extract an action labeled "pippo", use selectWithLabel<Action*>("pippo") 60 : /// If you want it to be a Colvar, use selectWithLabel<Colvar*>(pippo). If it is 61 : /// not found, it returns NULL 62 : template <class T> 63 : T selectWithLabel(const std::string&s)const; 64 : /// get the labels in the list of actions in form of a string (useful to debug) 65 : /// Only classes that can be dynamic casted to T are reported 66 : template <class T> 67 : std::string getLabelList() const; 68 : /// get the labels in the form of a vector of strings 69 : /// Only classes that can be dynamic casted to T are reported 70 : template <class T> 71 : std::vector<std::string> getLabelVector() const; 72 : }; 73 : 74 : ///// 75 : // INLINE IMPLEMENTATIONS: 76 : 77 : template <class T> 78 8034 : std::vector<T> ActionSet::select()const { 79 : std::vector<T> ret; 80 81313 : for(const auto & p : (*this)) { 81 65245 : T t=dynamic_cast<T>(p.get()); 82 65245 : if(t) ret.push_back(t); 83 : }; 84 8034 : return ret; 85 : } 86 : 87 : template <class T> 88 37130 : T ActionSet::selectWithLabel(const std::string&s)const { 89 232178 : for(const auto & p : (*this)) { 90 142559 : T t=dynamic_cast<T>(p.get()); 91 188632 : if(t && dynamic_cast<Action*>(t)->getLabel()==s) return t; 92 : }; 93 : return NULL; 94 : } 95 : 96 : template <class T> 97 : std::vector<Action*> ActionSet::selectNot()const { 98 : std::vector<Action*> ret; 99 : for(const auto & p : (*this)) { 100 : T t=dynamic_cast<T>(p); 101 : if(!t) ret.push_back(p.get()); 102 : }; 103 : return ret; 104 : } 105 : 106 : template <class T> 107 1 : std::string ActionSet::getLabelList() const { 108 : std::string outlist; 109 6 : for(const auto & p : (*this)) { 110 8 : if(dynamic_cast<T>(p.get())) outlist+=p->getLabel()+" "; 111 : }; 112 1 : return outlist; 113 : } 114 : 115 : 116 : template <class T> 117 : std::vector<std::string> ActionSet::getLabelVector() const { 118 : std::vector<std::string> outlist; 119 : for(const auto & p : (*this)) { 120 : if(dynamic_cast<T>(p.get())) outlist.push_back(p->getLabel()); 121 : }; 122 : return outlist; 123 : } 124 : 125 : 126 : 127 : } 128 : 129 : #endif 130 :