All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ActionRegister.cpp
Go to the documentation of this file.
1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2  Copyright (c) 2013 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-code.org for more information.
6 
7  This file is part of plumed, version 2.0.
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 ActionRegister::~ActionRegister(){
33  if(m.size()>0){
34  string names="";
35  for(mIterator p=m.begin();p!=m.end();++p)names+=p->first+" ";
36  std::cerr<<"WARNING: Directive "+ names +" has not been properly unregistered. This might lead to memory leak!!\n";
37  }
38 }
39 
41  static ActionRegister ans;
42  return ans;
43 }
44 
45 void ActionRegister::remove(creator_pointer f){
46  for(mIterator p=m.begin();p!=m.end();++p){
47  if((*p).second==f){
48  m.erase(p); break;
49  }
50  }
51 }
52 
53 void ActionRegister::add(string key,creator_pointer f,keywords_pointer k){
54  if(m.count(key)){
55  m.erase(key);
56  disabled.insert(key);
57  }else{
58  m.insert(pair<string,creator_pointer>(key,f));
59  // Store a pointer to the function that creates keywords
60  // A pointer is stored and not the keywords because all
61  // Vessels must be dynamically loaded before the actions.
62  mk.insert(pair<string,keywords_pointer>(key,k));
63  };
64 }
65 
66 bool ActionRegister::check(string key){
67  if(m.count(key)>0 && mk.count(key)>0) return true;
68  return false;
69 }
70 
71 Action* ActionRegister::create(const ActionOptions&ao){
72  if(ao.line.size()<1)return NULL;
73  // Create a copy of the manual locally. The manual is
74  // then added to the ActionOptions. This allows us to
75  // ensure during construction that all the keywords for
76  // the action have been documented. In addition, we can
77  // generate the documentation when the user makes an error
78  // in the input.
79  Action* action;
80  if( check(ao.line[0]) ){
81  Keywords keys; mk[ao.line[0]](keys);
82  ActionOptions nao( ao,keys );
83  action=m[ao.line[0]](nao);
84  keys.destroyData();
85  } else action=NULL;
86  return action;
87 }
88 
89 bool ActionRegister::printManual( const std::string& action ){
90  if ( check(action) ){
91  Keywords keys; mk[action](keys);
92  keys.print_html(true); keys.destroyData();
93  return true;
94  } else {
95  return false;
96  }
97 }
98 
99 bool ActionRegister::printTemplate( const std::string& action, bool include_optional ){
100  if( check(action) ){
101  Keywords keys; mk[action](keys);
102  keys.print_template(action, include_optional); keys.destroyData();
103  return true;
104  } else {
105  return false;
106  }
107 }
108 
109 std::ostream & operator<<(std::ostream &log,const ActionRegister&ar){
110  vector<string> s;
111  for(ActionRegister::const_mIterator it=ar.m.begin();it!=ar.m.end();++it)
112  s.push_back((*it).first);
113  sort(s.begin(),s.end());
114  for(unsigned i=0;i<s.size();i++) log<<" "<<s[i]<<"\n";
115  if(ar.disabled.size()>0){
116  s.assign(ar.disabled.size(),"");
117  copy(ar.disabled.begin(),ar.disabled.end(),s.begin());
118  sort(s.begin(),s.end());
119  log<<"+++++++ WARNING +++++++\n";
120  log<<"The following keywords have been registered more than once and will be disabled:\n";
121  for(unsigned i=0;i<s.size();i++) log<<" - "<<s[i]<<"\n";
122  log<<"+++++++ END WARNING +++++++\n";
123  };
124  return log;
125 }
126 
127 
128 }
ActionRegister & actionRegister()
void add(const Value &val1, Value *val2)
Definition: Value.cpp:175
void copy(const Value &val1, Value &val2)
Definition: Value.cpp:159
void destroyData()
Clear everything from the keywords object.
Definition: Keywords.cpp:554
STL namespace.
This class holds the keywords and their documentation.
Definition: Keywords.h:36
This class is used to bring the relevant information to the Action constructor.
Definition: Action.h:41
Base class for all the input Actions.
Definition: Action.h:60
std::map< std::string, creator_pointer > m
Map action to a function which creates the related object.
void print_template(const std::string &actionname, bool include_optional) const
Print the template version for the documenation.
Definition: Keywords.cpp:253
std::map< std::string, creator_pointer >::iterator mIterator
Iterator over the map.
std::vector< std::string > line
Input line which sets up the action.
Definition: Action.h:47
void const char const char int double int double double int int double int * m
Definition: Matrix.h:42
std::map< std::string, creator_pointer >::const_iterator const_mIterator
Iterator over the map.
void print_html(const bool isaction) const
Print an html version of the documentation.
Definition: Keywords.cpp:294
Register holding all the allowed keywords.
std::ostream & operator<<(std::ostream &log, const ActionRegister &ar)
std::set< std::string > disabled
Set of disabled actions (which were registered more than once)