All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ActionSet.h
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 #ifndef __PLUMED_core_ActionSet_h
23 #define __PLUMED_core_ActionSet_h
24 
25 #include "Action.h"
26 
27 namespace PLMD {
28 
29 class PlumedMain;
30 
31 /// std::vector containing the sequence of Action to be done.
32 /// It is a vector of Action*, and as such it has the entire
33 /// std::vector interface. Moreover, it implements methods to extract
34 /// Acion* of a given type (select<T>()), NOT of a given type (selectNot<T>())
35 /// or to find an Action with a given label (selectWithLabel())
36 /// Finally, since it holds pointers, there is a clearDelete() function
37 /// which deletes the pointers before deleting the vector
38 class ActionSet:
39  public std::vector<Action*>
40 {
42 public:
44  ~ActionSet();
45 /// Clear and deletes all the included pointers.
46  void clearDelete();
47 
48 /// Extract pointers to all Action's of type T
49 /// To extract all Colvar , use select<Colvar*>();
50  template <class T>
51  std::vector<T> select()const;
52 /// Extract pointers to all Action's which are not of type T
53 /// E.g., to extract all noncolvars, use
54 /// selectNot<Colvar*>();
55  template <class T>
56  std::vector<Action*> selectNot()const;
57 /// Extract pointer to an action labeled s, only if it is of
58 /// type T. E.g., to extract an action labeled "pippo", use selectWithLabel<Action*>("pippo")
59 /// If you want it to be a Colvar, use selectWithLabel<Colvar*>(pippo). If it is
60 /// not found, it returns NULL
61  template <class T>
62  T selectWithLabel(const std::string&s)const;
63 /// get the labels in the list of actions in form of a string (useful to debug)
64  std::string getLabelList() const;
65 /// get the labels in the form of a vector of strings
66  std::vector<std::string> getLabelVector() const;
67 };
68 
69 /////
70 // INLINE IMPLEMENTATIONS:
71 
72 template <class T>
73 std::vector<T> ActionSet::select()const{
74  std::vector<T> ret;
75  for(const_iterator p=begin();p!=end();++p){
76  T t=dynamic_cast<T>(*p);
77  if(t) ret.push_back(t);
78  };
79  return ret;
80 }
81 
82 template <class T>
83 T ActionSet::selectWithLabel(const std::string&s)const{
84  for(const_iterator p=begin();p!=end();++p){
85  T t=dynamic_cast<T>(*p);
86  if(t && dynamic_cast<Action*>(t)->getLabel()==s) return t;
87  };
88  return NULL;
89 }
90 
91 template <class T>
92 std::vector<Action*> ActionSet::selectNot()const{
93  std::vector<Action*> ret;
94  for(const_iterator p=begin();p!=end();++p){
95  T t=dynamic_cast<T>(*p);
96  if(!t) ret.push_back(*p);
97  };
98  return ret;
99 }
100 
101 }
102 
103 #endif
104 
PlumedMain & plumed
Definition: ActionSet.h:41
std::vector< std::string > getLabelVector() const
get the labels in the form of a vector of strings
Definition: ActionSet.cpp:51
ActionSet(PlumedMain &p)
Definition: ActionSet.cpp:27
std::vector containing the sequence of Action to be done.
Definition: ActionSet.h:38
std::string getLabelList() const
get the labels in the list of actions in form of a string (useful to debug)
Definition: ActionSet.cpp:43
std::vector< T > select() const
Extract pointers to all Action's of type T To extract all Colvar , use select();.
Definition: ActionSet.h:73
std::vector< Action * > selectNot() const
Extract pointers to all Action's which are not of type T E.g., to extract all noncolvars, use selectNot();.
Definition: ActionSet.h:92
Main plumed object.
Definition: PlumedMain.h:71
T selectWithLabel(const std::string &s) const
Extract pointer to an action labeled s, only if it is of type T.
Definition: ActionSet.h:83
void clearDelete()
Clear and deletes all the included pointers.
Definition: ActionSet.cpp:37