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: 2025-03-25 09:33:27 Functions: 27 31 87.1 %

          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 "ActionShortcut.h"
      27             : #include <memory>
      28             : 
      29             : namespace PLMD {
      30             : 
      31             : class PlumedMain;
      32             : 
      33             : /// std::vector containing the sequence of Action to be done.
      34             : /// It is a vector of Action*, and as such it has the entire
      35             : /// std::vector interface. Moreover, it implements methods to extract
      36             : /// Acion* of a given type (select<T>()), NOT of a given type (selectNot<T>())
      37             : /// or to find an Action with a given label (selectWithLabel())
      38             : /// Finally, since it holds pointers, there is a clearDelete() function
      39             : /// which deletes the pointers before deleting the vector
      40             : class ActionSet:
      41             :   public std::vector<std::unique_ptr<Action>> {
      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             : /// Extract pointer to last action of type T located before
      73             : /// action. If action is not in ActionSet or if there is no action of type T
      74             : /// returns NULL.
      75             : /// Typically to be used as selectLatest<Type>(this);
      76             :   template <class T>
      77             :   T selectLatest(const Action*action)const;
      78             : /// Get any shortcuts with this shortcut label
      79             :   ActionShortcut* getShortcutActionWithLabel( const std::string& s ) const ;
      80             : };
      81             : 
      82             : /////
      83             : // INLINE IMPLEMENTATIONS:
      84             : 
      85             : template <class T>
      86      147488 : std::vector<T> ActionSet::select()const {
      87             :   std::vector<T> ret;
      88    87171903 :   for(const auto & p : (*this)) {
      89    87024415 :     T t=dynamic_cast<T>(p.get());
      90    87024415 :     if(t) {
      91    14642253 :       ret.push_back(t);
      92             :     }
      93             :   };
      94      147488 :   return ret;
      95             : }
      96             : 
      97             : template <class T>
      98     1534120 : T ActionSet::selectWithLabel(const std::string&s)const {
      99   162162092 :   for(const auto & p : (*this)) {
     100   133029969 :     T t=dynamic_cast<T>(p.get());
     101   162044160 :     if(t && t->getLabel()==s) {
     102             :       return t;
     103             :     }
     104             :   };
     105             :   return NULL;
     106             : }
     107             : 
     108             : template <class T>
     109             : std::vector<Action*> ActionSet::selectNot()const {
     110             :   std::vector<Action*> ret;
     111             :   for(const auto & p : (*this)) {
     112             :     T t=dynamic_cast<T>(p);
     113             :     if(!t) {
     114             :       ret.push_back(p.get());
     115             :     }
     116             :   };
     117             :   return ret;
     118             : }
     119             : 
     120             : template <class T>
     121           1 : std::string ActionSet::getLabelList() const {
     122             :   std::string outlist;
     123          16 :   for(const auto & p : (*this)) {
     124          15 :     if(dynamic_cast<T>(p.get())) {
     125          22 :       outlist+=p->getLabel()+" ";
     126             :     }
     127             :   };
     128           1 :   return  outlist;
     129             : }
     130             : 
     131             : 
     132             : template <class T>
     133             : std::vector<std::string> ActionSet::getLabelVector() const {
     134             :   std::vector<std::string> outlist;
     135             :   for(const auto & p : (*this)) {
     136             :     if(dynamic_cast<T>(p.get())) {
     137             :       outlist.push_back(p->getLabel());
     138             :     }
     139             :   };
     140             :   return  outlist;
     141             : }
     142             : 
     143             : template <class T>
     144       19441 : T ActionSet::selectLatest(const Action*action) const {
     145             :   T t=nullptr;
     146    14222769 :   for(const auto & p : (*this)) {
     147    14203356 :     if(p.get()==action) {
     148             :       return t;
     149             :     }
     150    14203328 :     T r=dynamic_cast<T>(p.get());
     151    14203328 :     if(r) {
     152             :       t=r;
     153             :     }
     154             :   }
     155             :   return t;
     156             : }
     157             : 
     158             : 
     159             : 
     160             : }
     161             : 
     162             : #endif
     163             : 

Generated by: LCOV version 1.16