LCOV - code coverage report
Current view: top level - tools - TokenizedLine.h (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 11 11 100.0 %
Date: 2025-12-04 11:19:34 Functions: 13 14 92.9 %

          Line data    Source code
       1             : 
       2             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       3             :    Copyright (c) 2025 The plumed team
       4             :    (see the PEOPLE file at the root of the distribution for a list of names)
       5             : 
       6             :    See http://www.plumed.org for more information.
       7             : 
       8             :    This file is part of plumed, version 2.
       9             : 
      10             :    plumed is free software: you can redistribute it and/or modify
      11             :    it under the terms of the GNU Lesser General Public License as published by
      12             :    the Free Software Foundation, either version 3 of the License, or
      13             :    (at your option) any later version.
      14             : 
      15             :    plumed is distributed in the hope that it will be useful,
      16             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      17             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      18             :    GNU Lesser General Public License for more details.
      19             : 
      20             :    You should have received a copy of the GNU Lesser General Public License
      21             :    along with plumed.  If not, see <http://www.gnu.org/licenses/>.
      22             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      23             : #ifndef __PLUMED_tools_TokenizedLine_h
      24             : #define __PLUMED_tools_TokenizedLine_h
      25             : #include "Tools.h"
      26             : #include <map>
      27             : #include <vector>
      28             : namespace PLMD {
      29             : ///This class abstracts the input line in tokens.
      30             : ///
      31             : ///The underlying container accelerates the lookup for the keywords
      32             : ///ISSUE: In case of a vector keyword and the use of the '@replicas:' idiom:
      33             : /// If the user wants to chante only the FIRS element of the vector with @replica
      34             : /// the parse will interpret the whole string as a replica string
      35             : /// There are no problem with @replicas: applied on other components:
      36             : ///  - {@replicas:{1,3,4},1,3} won't work (the result ideally is equivalent to "@replicas:{{1,1,3},{3,1,3},{4,1,3}}"
      37             : ///  - {1,@replicas:{1,3,4},3} will work (the result is equivalent to "@replicas:{{1,1,3},{1,3,3},{1,4,3}}"
      38       49176 : class TokenizedLine {
      39             : public:
      40             :   // the first element is the whole line, if contains @replica: will get also divided
      41             :   using line=std::vector<std::string>;
      42             :   using mapType=std::map<std::string,line,std::less<void>>;
      43             : private:
      44             :   using vectorIt = typename std::vector<std::string>::iterator;
      45             :   using const_vectorIt = typename std::vector<std::string>::const_iterator;
      46             :   mapType tokens;
      47             : public:
      48             :   static std::string_view replica(const line&,int rep=-1);
      49             :   struct presentAndFound {
      50             :     bool present;
      51             :     bool found;
      52             :   };
      53             : /// Initializer from vector iterators
      54             :   TokenizedLine(vectorIt begin, vectorIt end);
      55             : /// Initializer from vector iterators
      56             :   TokenizedLine(const_vectorIt begin, const_vectorIt end);
      57             : /// Initializer from a vector of strings (ideally the output of getWords)
      58             :   TokenizedLine(const std::vector<std::string>&);
      59             : ///return a plain string with the all the current KEY=value combinations, it is possible to clear the tokens after that
      60             :   std::string convertToString(bool alsoClear);
      61             : ///returns the list of the keys:
      62             :   std::string keyList(std::string_view sep = ", ");
      63             : ///return a keyword and its argument
      64             :   std::string getKeyword(std::string_view key) const;
      65             : ///return the value of the asked key, "" otherwise;
      66             :   std::string getValue(std::string_view key, int rep=-1) const;
      67             : /// Return the size of the underlying container;
      68             :   std::size_t size() const;
      69             : /// Returns true if the underlying container is empty
      70             :   bool empty() const;
      71             : /// Read a value from the tokens and remove it from the list
      72             :   template<typename T>
      73             :   presentAndFound readAndRemove(std::string_view key,
      74             :                                 T& value,
      75             :                                 int rep=-1);
      76             : /// Read a list of values from the tokens and remove it from the list
      77             :   template<typename T>
      78             :   presentAndFound readAndRemoveVector(std::string_view key,
      79             :                                       std::vector<T>& value,
      80             :                                       int rep=-1);
      81             : ///return true if the flag is present and removes it from the tokens
      82             :   bool readAndRemoveFlag(std::string_view key);
      83             : };
      84             : 
      85             : 
      86             : 
      87             : template<typename T>
      88      180039 : TokenizedLine::presentAndFound TokenizedLine::readAndRemove(std::string_view key,
      89             :     T& value,
      90             :     const int replica_index ) {
      91             :   auto keytext = tokens.find(key);
      92             :   bool present = keytext != tokens.end();
      93             :   bool found=false;
      94      180039 :   if(present) {
      95      113124 :     found = Tools::parse(TokenizedLine::replica(keytext->second,replica_index),
      96             :                          value);
      97      113124 :     tokens.erase(keytext);
      98             :   }
      99      180039 :   return {present, found};
     100             : }
     101             : 
     102             : 
     103             : template<typename T>
     104      436090 : TokenizedLine::presentAndFound TokenizedLine::readAndRemoveVector(std::string_view key,
     105             :     std::vector<T>& value,
     106             :     const int replica_index ) {
     107             : 
     108             :   auto keytext = tokens.find(key);
     109             :   bool present = keytext != tokens.end();
     110             :   bool found=false;
     111      436090 :   if(present) {
     112      418395 :     found = Tools::parseVector(TokenizedLine::replica(keytext->second,replica_index),
     113             :                                value,
     114             :                                replica_index);
     115      418395 :     tokens.erase(keytext);
     116             :   }
     117      436090 :   return {present, found};
     118             : }
     119             : 
     120             : 
     121             : } //namespace PLMD
     122             : #endif //__PLUMED_tools_TokenizedLine_h
     123             : 

Generated by: LCOV version 1.16