LCOV - code coverage report
Current view: top level - core - ActionSet.h (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 20 20 100.0 %
Date: 2026-03-30 13:16:06 Functions: 22 25 88.0 %

          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             : #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             :   PlumedMain& plumed;
      42             : public:
      43             :   explicit ActionSet(PlumedMain&p);
      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             : /// Only classes that can be dynamic casted to T are reported
      65             :   template <class T>
      66             :   std::string getLabelList() const;
      67             : /// get the labels in the form of a vector of strings
      68             : /// Only classes that can be dynamic casted to T are reported
      69             :   template <class T>
      70             :   std::vector<std::string> getLabelVector() const;
      71             : /// Extract pointer to last action of type T located before
      72             : /// action. If action is not in ActionSet or if there is no action of type T
      73             : /// returns NULL.
      74             : /// Typically to be used as selectLatest<Type>(this);
      75             :   template <class T>
      76             :   T selectLatest(const Action*action)const;
      77             : };
      78             : 
      79             : /////
      80             : // INLINE IMPLEMENTATIONS:
      81             : 
      82             : template <class T>
      83       15876 : std::vector<T> ActionSet::select()const {
      84             :   std::vector<T> ret;
      85     6255585 :   for(const auto & p : (*this)) {
      86     6239709 :     T t=dynamic_cast<T>(p.get());
      87     6239709 :     if(t) {
      88       23869 :       ret.push_back(t);
      89             :     }
      90             :   };
      91       15876 :   return ret;
      92             : }
      93             : 
      94             : template <class T>
      95     1098134 : T ActionSet::selectWithLabel(const std::string&s)const {
      96     9466455 :   for(const auto & p : (*this)) {
      97     3279204 :     T t=dynamic_cast<T>(p.get());
      98     9451703 :     if(t && dynamic_cast<Action*>(t)->getLabel()==s) {
      99             :       return t;
     100             :     }
     101             :   };
     102             :   return NULL;
     103             : }
     104             : 
     105             : template <class T>
     106             : std::vector<Action*> ActionSet::selectNot()const {
     107             :   std::vector<Action*> ret;
     108             :   for(const auto & p : (*this)) {
     109             :     T t=dynamic_cast<T>(p);
     110             :     if(!t) {
     111             :       ret.push_back(p.get());
     112             :     }
     113             :   };
     114             :   return ret;
     115             : }
     116             : 
     117             : template <class T>
     118           1 : std::string ActionSet::getLabelList() const {
     119             :   std::string outlist;
     120           5 :   for(const auto & p : (*this)) {
     121           4 :     if(dynamic_cast<T>(p.get())) {
     122           8 :       outlist+=p->getLabel()+" ";
     123             :     }
     124             :   };
     125           1 :   return  outlist;
     126             : }
     127             : 
     128             : 
     129             : template <class T>
     130             : std::vector<std::string> ActionSet::getLabelVector() const {
     131             :   std::vector<std::string> outlist;
     132             :   for(const auto & p : (*this)) {
     133             :     if(dynamic_cast<T>(p.get())) {
     134             :       outlist.push_back(p->getLabel());
     135             :     }
     136             :   };
     137             :   return  outlist;
     138             : }
     139             : 
     140             : template <class T>
     141        1190 : T ActionSet::selectLatest(const Action*action) const {
     142             :   T t=nullptr;
     143       13739 :   for(const auto & p : (*this)) {
     144       12589 :     if(p.get()==action) {
     145             :       return t;
     146             :     }
     147       12549 :     T r=dynamic_cast<T>(p.get());
     148       12549 :     if(r) {
     149             :       t=r;
     150             :     }
     151             :   }
     152             :   return t;
     153             : }
     154             : 
     155             : 
     156             : 
     157             : }
     158             : 
     159             : #endif
     160             : 

Generated by: LCOV version 1.16