Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2011-2018 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 : #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 : ///
45 841 : class ActionRegister {
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/vim ready for input into the manual
78 : bool printManual(const std::string& action, const bool& vimout);
79 : /// Print out a template command for an action
80 : bool printTemplate(const std::string& action, bool include_optional);
81 : void remove(creator_pointer);
82 : ~ActionRegister();
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
90 : ActionRegister& actionRegister();
91 :
92 : std::ostream & operator<<(std::ostream &log,const ActionRegister&ar);
93 :
94 : }
95 :
96 : #define PLUMED_CONCATENATE_DIRECT(s1, s2) s1##s2
97 : #define PLUMED_CONCATENATE(s1, s2) PLUMED_CONCATENATE_DIRECT(s1, s2)
98 : #define PLUMED_UNIQUENAME(str) PLUMED_CONCATENATE(str, __LINE__)
99 :
100 : /// Shortcut for Action registration
101 : /// \relates PLMD::ActionRegister
102 : /// For easier registration, this file also provides a macro PLUMED_REGISTER_ACTION.
103 : /// \param classname the name of the class to be registered
104 : /// \param directive a string containing the corresponding directive
105 : /// This macro should be used in the .cpp file of the corresponding class
106 : #define PLUMED_REGISTER_ACTION(classname,directive) \
107 : static class PLUMED_UNIQUENAME(classname##RegisterMe){ \
108 : static PLMD::Action* create(const PLMD::ActionOptions&ao){return new classname(ao);} \
109 : public: \
110 : PLUMED_UNIQUENAME(classname##RegisterMe)(){PLMD::actionRegister().add(directive,create,classname::registerKeywords);} \
111 : ~PLUMED_UNIQUENAME(classname##RegisterMe)(){PLMD::actionRegister().remove(create);} \
112 : } PLUMED_UNIQUENAME(classname##RegisterMe);
113 :
114 :
115 : #endif
116 :
|