All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ActionRegister.h
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 #ifndef __PLUMED_core_ActionRegister_h
23 #define __PLUMED_core_ActionRegister_h
24 
25 #include <string>
26 #include <map>
27 #include <set>
28 #include <iosfwd>
29 #include "tools/Keywords.h"
30 
31 namespace PLMD{
32 
33 class Action;
34 class ActionOptions;
35 
36 /// Register holding all the allowed keywords.
37 /// This is a register which holds a map between strings (directives) and function pointers.
38 /// The function pointers are pointing to functions which create an object of
39 /// the corresponding class given the corresponding options (ActionOptions).
40 /// There should be only one of there objects allocated.
41 /// Actions should be registered here at the beginning of execution
42 /// If the same directive is used for different classes, it is automatically disabled
43 /// to avoid random results.
44 ///
46 /// Write on a stream the list of registered directives
47  friend std::ostream &operator<<(std::ostream &,const ActionRegister&);
48 /// Pointer to a function which, given the options, create an Action
49  typedef Action*(*creator_pointer)(const ActionOptions&);
50 /// Pointer to a function which, returns the keywords allowed
51  typedef void(*keywords_pointer)(Keywords&);
52 /// Map action to a function which creates the related object
53  std::map<std::string,creator_pointer> m;
54 /// Iterator over the map
55  typedef std::map<std::string,creator_pointer>::iterator mIterator;
56 /// Iterator over the map
57  typedef std::map<std::string,creator_pointer>::const_iterator const_mIterator;
58 /// Map action to a function which documents the related object
59  std::map<std::string,keywords_pointer> mk;
60 /// Iterator over the map
61  typedef std::map<std::string,Keywords>::iterator mIteratork;
62 /// Iterator over the map
63  typedef std::map<std::string,Keywords>::const_iterator const_mIteratork;
64 /// Set of disabled actions (which were registered more than once)
65  std::set<std::string> disabled;
66 public:
67 /// Register a new class.
68 /// \param key The name of the directive to be used in the input file
69 /// \param cp A pointer to a function which creates an object of that class
70 /// \param kp A pointer to a function which returns the allowed keywords
71  void add(std::string key,creator_pointer cp,keywords_pointer kp);
72 /// Verify if a directive is present in the register
73  bool check(std::string action);
74 /// Create an Action of the type indicated in the options
75 /// \param ao object containing information for initialization, such as the full input line, a pointer to PlumedMain, etc
76  Action* create(const ActionOptions&ao);
77 /// Print out the keywords for an action in html ready for input into the manual
78  bool printManual(const std::string& action);
79 /// Print out a template command for an action
80  bool printTemplate(const std::string& action, bool include_optional);
81  void remove(creator_pointer);
83 };
84 
85 /// Function returning a reference to the ActionRegister.
86 /// \relates ActionRegister
87 /// To avoid problems with order of initialization, this function contains
88 /// a static ActionRegister which is built the first time the function is called.
89 /// In this manner, it is always initialized before it's used
91 
92 std::ostream & operator<<(std::ostream &log,const ActionRegister&ar);
93 
94 }
95 
96 
97 /// Shortcut for Action registration
98 /// \relates PLMD::ActionRegister
99 /// For easier registration, this file also provides a macro PLUMED_REGISTER_ACTION.
100 /// \param classname the name of the class to be registered
101 /// \param directive a string containing the corresponding directive
102 /// This macro should be used in the .cpp file of the corresponding class
103 #define PLUMED_REGISTER_ACTION(classname,directive) \
104  static class classname##RegisterMe{ \
105  static PLMD::Action* create(const PLMD::ActionOptions&ao){return new classname(ao);} \
106  public: \
107  classname##RegisterMe(){PLMD::actionRegister().add(directive,create,classname::registerKeywords);} \
108  ~classname##RegisterMe(){PLMD::actionRegister().remove(create);} \
109  } classname##RegisterMeObject;
110 
111 
112 #endif
113 
bool printTemplate(const std::string &action, bool include_optional)
Print out a template command for an action.
ActionRegister & actionRegister()
std::map< std::string, keywords_pointer > mk
Map action to a function which documents the related object.
bool check(std::string action)
Verify if a directive is present in the register.
Action *(* creator_pointer)(const ActionOptions &)
Pointer to a function which, given the options, create an Action.
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
void(* keywords_pointer)(Keywords &)
Pointer to a function which, returns the keywords allowed.
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.
bool printManual(const std::string &action)
Print out the keywords for an action in html ready for input into the manual.
std::map< std::string, Keywords >::const_iterator const_mIteratork
Iterator over the map.
std::map< std::string, creator_pointer >::iterator mIterator
Iterator over the map.
friend std::ostream & operator<<(std::ostream &, const ActionRegister &)
Write on a stream the list of registered directives.
std::map< std::string, Keywords >::iterator mIteratork
Iterator over the map.
std::map< std::string, creator_pointer >::const_iterator const_mIterator
Iterator over the map.
Action * create(const ActionOptions &ao)
Create an Action of the type indicated in the options.
Register holding all the allowed keywords.
void add(std::string key, creator_pointer cp, keywords_pointer kp)
Register a new class.
std::ostream & operator<<(std::ostream &log, const ActionRegister &ar)
std::set< std::string > disabled
Set of disabled actions (which were registered more than once)