LCOV - code coverage report
Current view: top level - core - ActionRegister.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 38 55 69.1 %
Date: 2019-08-13 10:15:31 Functions: 10 11 90.9 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2011-2019 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             : #include "ActionRegister.h"
      23             : #include "tools/Tools.h"
      24             : #include "Action.h"
      25             : #include <algorithm>
      26             : #include <iostream>
      27             : 
      28             : 
      29             : using namespace std;
      30             : namespace PLMD {
      31             : 
      32        1958 : ActionRegister::~ActionRegister() {
      33        1958 :   if(m.size()>0) {
      34           0 :     string names="";
      35           0 :     for(const auto & p : m) names+=p.first+" ";
      36           0 :     std::cerr<<"WARNING: Directive "+ names +" has not been properly unregistered. This might lead to memory leak!!\n";
      37             :   }
      38        1958 : }
      39             : 
      40     1064091 : ActionRegister& actionRegister() {
      41     1066049 :   static ActionRegister ans;
      42     1064091 :   return ans;
      43             : }
      44             : 
      45      528661 : void ActionRegister::remove(creator_pointer f) {
      46    42191044 :   for(auto p=m.begin(); p!=m.end(); ++p) {
      47    41662383 :     if((*p).second==f) {
      48             :       m.erase(p); break;
      49             :     }
      50             :   }
      51      528661 : }
      52             : 
      53      528661 : void ActionRegister::add(string key,creator_pointer f,keywords_pointer k) {
      54             :   // this force each action to be registered as an uppercase string
      55     6643504 :   if ( std::any_of( std::begin( key ), std::end( key ), []( char c ) { return ( islower( c ) ); } ) ) plumed_error() << "Action: " + key + " cannot be registered, use only UPPERCASE characters";
      56      528661 :   if(m.count(key)) {
      57             :     m.erase(key);
      58             :     disabled.insert(key);
      59             :   } else {
      60      528661 :     m.insert(pair<string,creator_pointer>(key,f));
      61             :     // Store a pointer to the function that creates keywords
      62             :     // A pointer is stored and not the keywords because all
      63             :     // Vessels must be dynamically loaded before the actions.
      64      528661 :     mk.insert(pair<string,keywords_pointer>(key,k));
      65             :   };
      66      528661 : }
      67             : 
      68        6498 : bool ActionRegister::check(string key) {
      69       12994 :   if(m.count(key)>0 && mk.count(key)>0) return true;
      70           2 :   return false;
      71             : }
      72             : 
      73        6228 : std::unique_ptr<Action> ActionRegister::create(const ActionOptions&ao) {
      74        6228 :   if(ao.line.size()<1)return NULL;
      75             :   // Create a copy of the manual locally. The manual is
      76             :   // then added to the ActionOptions. This allows us to
      77             :   // ensure during construction that all the keywords for
      78             :   // the action have been documented. In addition, we can
      79             :   // generate the documentation when the user makes an error
      80             :   // in the input.
      81             :   std::unique_ptr<Action> action;
      82       12456 :   if( check(ao.line[0]) ) {
      83       12452 :     Keywords keys; mk[ao.line[0]](keys);
      84        6226 :     ActionOptions nao( ao,keys );
      85       18640 :     action=m[ao.line[0]](nao);
      86             :   }
      87             :   return action;
      88             : }
      89             : 
      90         270 : bool ActionRegister::printManual( const std::string& action, const bool& vimout, const bool& spellout ) {
      91         540 :   if ( check(action) ) {
      92         270 :     Keywords keys; mk[action](keys);
      93         270 :     if( vimout ) {
      94         270 :       printf("%s",action.c_str()); keys.print_vim(); printf("\n");
      95           0 :     } else if( spellout ) {
      96           0 :       keys.print_spelling();
      97             :     } else {
      98           0 :       keys.print_html();
      99             :     }
     100         270 :     return true;
     101             :   } else {
     102             :     return false;
     103             :   }
     104             : }
     105             : 
     106           0 : bool ActionRegister::printTemplate( const std::string& action, bool include_optional ) {
     107           0 :   if( check(action) ) {
     108           0 :     Keywords keys; mk[action](keys);
     109           0 :     keys.print_template(action, include_optional);
     110           0 :     return true;
     111             :   } else {
     112             :     return false;
     113             :   }
     114             : }
     115             : 
     116         271 : std::ostream & operator<<(std::ostream &log,const ActionRegister&ar) {
     117             :   vector<string> s;
     118       73713 :   for(const auto & it : ar.m) s.push_back(it.first);
     119         271 :   sort(s.begin(),s.end());
     120      219784 :   for(unsigned i=0; i<s.size(); i++) log<<"  "<<s[i]<<"\n";
     121         271 :   if(!ar.disabled.empty()) {
     122           0 :     s.assign(ar.disabled.size(),"");
     123             :     copy(ar.disabled.begin(),ar.disabled.end(),s.begin());
     124           0 :     sort(s.begin(),s.end());
     125           0 :     log<<"+++++++ WARNING +++++++\n";
     126           0 :     log<<"The following keywords have been registered more than once and will be disabled:\n";
     127           0 :     for(unsigned i=0; i<s.size(); i++) log<<"  - "<<s[i]<<"\n";
     128           0 :     log<<"+++++++ END WARNING +++++++\n";
     129             :   };
     130         271 :   return log;
     131             : }
     132             : 
     133             : 
     134        5874 : }

Generated by: LCOV version 1.14