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

          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       66033 : 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             : /// If key is present and newkey is not, the value of key is moved to newkey; returns true if this happened
      84             :   bool renameKey(std::string_view key, std::string_view newkey);
      85             : };
      86             : 
      87             : 
      88             : 
      89             : template<typename T>
      90      262841 : TokenizedLine::presentAndFound TokenizedLine::readAndRemove(std::string_view key,
      91             :     T& value,
      92             :     const int replica_index ) {
      93             :   auto keytext = tokens.find(key);
      94      262841 :   bool present = keytext != tokens.end();
      95             :   bool found=false;
      96      262841 :   if(present) {
      97      172723 :     found = Tools::parse(TokenizedLine::replica(keytext->second,replica_index),
      98             :                          value);
      99      172723 :     tokens.erase(keytext);
     100             :   }
     101      262841 :   return {present, found};
     102             : }
     103             : 
     104             : 
     105             : template<typename T>
     106      546255 : TokenizedLine::presentAndFound TokenizedLine::readAndRemoveVector(std::string_view key,
     107             :     std::vector<T>& value,
     108             :     const int replica_index ) {
     109             : 
     110             :   auto keytext = tokens.find(key);
     111      546255 :   bool present = keytext != tokens.end();
     112             :   bool found=false;
     113      546255 :   if(present) {
     114      520025 :     found = Tools::parseVector(TokenizedLine::replica(keytext->second,replica_index),
     115             :                                value,
     116             :                                replica_index);
     117      520025 :     tokens.erase(keytext);
     118             :   }
     119      546255 :   return {present, found};
     120             : }
     121             : 
     122             : 
     123             : } //namespace PLMD
     124             : #endif //__PLUMED_tools_TokenizedLine_h
     125             : 

Generated by: LCOV version 1.16