1 | /*
|
---|
2 | * MapOfActions.hpp
|
---|
3 | *
|
---|
4 | * Created on: 10.05.2010
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 | #ifndef MAPOFACTIONS_HPP_
|
---|
9 | #define MAPOFACTIONS_HPP_
|
---|
10 |
|
---|
11 | #include <boost/filesystem.hpp>
|
---|
12 | #include <boost/lexical_cast.hpp>
|
---|
13 | #include <boost/program_options.hpp>
|
---|
14 |
|
---|
15 | #include <map>
|
---|
16 | #include <set>
|
---|
17 | #include <vector>
|
---|
18 | #include <typeinfo>
|
---|
19 |
|
---|
20 |
|
---|
21 | #include "Exceptions/IllegalTypeException.hpp"
|
---|
22 | #include "Exceptions/MissingValueException.hpp"
|
---|
23 | #include "Helpers/Assert.hpp"
|
---|
24 | #include "Patterns/Singleton.hpp"
|
---|
25 |
|
---|
26 | class MapOfActionsTest;
|
---|
27 |
|
---|
28 | class Box;
|
---|
29 | class atom;
|
---|
30 | class element;
|
---|
31 | class molecule;
|
---|
32 | class Vector;
|
---|
33 |
|
---|
34 | namespace po = boost::program_options;
|
---|
35 |
|
---|
36 | using boost::lexical_cast;
|
---|
37 |
|
---|
38 | /** Central class for adding functionality to the code.
|
---|
39 | *
|
---|
40 | * In Molecuilder everything that can be done - such as adding atoms,
|
---|
41 | * translating molecules, saving bind information - is an Action.
|
---|
42 | *
|
---|
43 | * In order to reference Action's with what the user sees, this class is the
|
---|
44 | * mediator.
|
---|
45 | *
|
---|
46 | * An Action is described to the user by:
|
---|
47 | * -# a name (this is the most important information)
|
---|
48 | * -# a description
|
---|
49 | * -# a shortform (single letter for use on the command line)
|
---|
50 | * -# a text menu it resides in
|
---|
51 | * -# the type of its argument
|
---|
52 | * -# the command line category
|
---|
53 | *
|
---|
54 | * The Action::NAME is the most important information because every Action
|
---|
55 | * registers itself automatically with the ActionRegistry and can be retrieved
|
---|
56 | * therefrom and from this MapOfActions simply by knowing its name alone.
|
---|
57 | *
|
---|
58 | * In the constructor of MapOfActions all this is set.
|
---|
59 | *
|
---|
60 | * Note that Action will require input from the user. This is done via class
|
---|
61 | * Query.
|
---|
62 | *
|
---|
63 | * And note also that MapOfActions actually contains more than just all
|
---|
64 | * Actions: There are a number of names that actually are just arguments to
|
---|
65 | * actions (e.g. "output-file").
|
---|
66 | *
|
---|
67 | * <h1> Howto add an Action</h1>
|
---|
68 | *
|
---|
69 | * Let us assume your new action (class) is called SuperDuperAction, consisting
|
---|
70 | * of two files SuperDuperAction.cpp and SuperDuperAction.hpp.
|
---|
71 | *
|
---|
72 | * Furthermore, let's say you Action needs two values: a double value as a
|
---|
73 | * upper threshold and a string which is the name of the output file.
|
---|
74 | *
|
---|
75 | * <h2> Command Line preliminaries </h2>
|
---|
76 | *
|
---|
77 | * You have to decide whether (for the command line) it makes sense to have an
|
---|
78 | * extra argument requesting the arguments, or one should be the argument of
|
---|
79 | * your action. I.e. your action name is "super-duper", then the use may
|
---|
80 | * call your action like this:
|
---|
81 | *
|
---|
82 | * ./molecuilder --super-duper 4 --output-file test.dat
|
---|
83 | *
|
---|
84 | * Here, we have picked the threshold as the value for your action and the
|
---|
85 | * name of the output file is given by an additional argument. Of course,
|
---|
86 | * it can be the other way round or by two arguments such as here:
|
---|
87 | *
|
---|
88 | * ./molecuilder --super-duper --threshold 4 --output-file test.dat
|
---|
89 | *
|
---|
90 | * It depends on what possible arguments are already there (don't make up
|
---|
91 | * new ones if present ones actually make sense for your action) and which
|
---|
92 | * argument is more natural or closer to what your action does.
|
---|
93 | *
|
---|
94 | * <h2> Menu preliminaries </h2>
|
---|
95 | *
|
---|
96 | * Whatever you decide, your action will need some Query dialogs to request
|
---|
97 | * the necessary information from the user, either via a command line
|
---|
98 | * argument (--output-file) via a text dialog (referenced by "output-file")
|
---|
99 | * or via a graphical dialog (same reference). And therein, the names
|
---|
100 | * of the arguments have to re-appear.
|
---|
101 | *
|
---|
102 | * Then, the following steps have to be done to incorporate your Action:
|
---|
103 | * -# create a unique name for your action (no capital letters) to reference
|
---|
104 | * it, this name has to appear in the file SuperDuperAction.cpp, e.g.
|
---|
105 | * "super-duper"
|
---|
106 | * -# pick names the other required arguments, best if they are already
|
---|
107 | * present in the MapOfActions. They have to appear in Query's in the
|
---|
108 | * code of your Action.
|
---|
109 | * -# With this name create entries in the following maps for the action
|
---|
110 | * name and for each names of a desired addtional argument if not present:
|
---|
111 | * -# DescriptionMap, a catchy description of what your action does
|
---|
112 | * -# TypeMap, see MapOfActions::OptionTypes for possible types of the single
|
---|
113 | * argument it takes.
|
---|
114 | * -# MenuContainsActionMap, in which menu should your action appear
|
---|
115 | * -# ShortFormMap (optional), single letter for command line call
|
---|
116 | * -# DefaultValueMap (optional), the default value (always a string)
|
---|
117 | * -# add to one of the command line sets by the following categories
|
---|
118 | * -# generic - generic options (i.e. not one of the others)
|
---|
119 | * -# config - action/argument only considers internal bevahior, user
|
---|
120 | * does not have to see it while still having full functionality
|
---|
121 | * -# hidden - this should be hidden from the user
|
---|
122 | * -# visible - this should be visible to the user
|
---|
123 | * -# inputfile - this should only be parsed from an input file, not
|
---|
124 | * from command line
|
---|
125 | * -# add to a menu, i.e. make an entry in MenuContainsActionMap.
|
---|
126 | * -# add header file SuperDuperAction.hpp to MapOfActions.cpp and instantiate
|
---|
127 | * your action in populateMenu() (mind the sorting: 1. menu,
|
---|
128 | * 2. alphabetical)
|
---|
129 | *
|
---|
130 | * And that's.
|
---|
131 | *
|
---|
132 | * Now, your action can be called from the command line, within the text
|
---|
133 | * menu and the graphical user interface.
|
---|
134 | *
|
---|
135 | */
|
---|
136 | class MapOfActions : public Singleton<MapOfActions> {
|
---|
137 | friend class Singleton<MapOfActions>;
|
---|
138 | friend class MapOfActionsTest;
|
---|
139 | public:
|
---|
140 | enum OptionTypes { None, Boolean, File, Integer, ListOfIntegers, Double, ListOfDoubles, String, ListOfStrings, Vector, ListOfVectors, Box, Molecule, ListOfMolecules, Atom, ListOfAtoms, Element, ListOfElements };
|
---|
141 |
|
---|
142 | // getter for the action descriptions and short forms
|
---|
143 | const std::string getDescription(std::string actionname);
|
---|
144 | const std::string getKeyAndShortForm(std::string actionname);
|
---|
145 | const std::string getShortForm(std::string actionname);
|
---|
146 | std::map <std::string, std::string> getShortFormToActionMap();
|
---|
147 | const std::string getCurrentValue(std::string actionname);
|
---|
148 |
|
---|
149 | void AddOptionsToParser();
|
---|
150 |
|
---|
151 | // check presence and getter for action type
|
---|
152 | bool hasValue(std::string actionname);
|
---|
153 | bool isShortFormPresent(std::string shortform);
|
---|
154 | const std::string getValueType(std::string actionname);
|
---|
155 | const std::type_info * getType(std::string actionname);
|
---|
156 |
|
---|
157 | std::set<std::string> generic;
|
---|
158 | std::set<std::string> config;
|
---|
159 | std::set<std::string> hidden;
|
---|
160 | std::set<std::string> visible;
|
---|
161 | std::set<std::string> inputfile;
|
---|
162 |
|
---|
163 | std::multimap <std::string, std::string> MenuContainsActionMap;
|
---|
164 | std::map <std::string, std::pair<std::string,std::string> > MenuDescription;
|
---|
165 |
|
---|
166 | bool isCurrentValuePresent(const char *name) const;
|
---|
167 | void queryCurrentValue(const char * name, class atom * &_T);
|
---|
168 | void queryCurrentValue(const char * name, const element * &_T);
|
---|
169 | void queryCurrentValue(const char * name, class molecule * &_T);
|
---|
170 | void queryCurrentValue(const char * name, class Box &_T);
|
---|
171 | void queryCurrentValue(const char * name, class Vector &_T);
|
---|
172 | void queryCurrentValue(const char * name, class BoxVector &_T);
|
---|
173 | void queryCurrentValue(const char * name, std::vector<atom *>&_T);
|
---|
174 | void queryCurrentValue(const char * name, std::vector<const element *>&_T);
|
---|
175 | void queryCurrentValue(const char * name, std::vector<molecule *>&_T);
|
---|
176 | void queryCurrentValue(const char * name, boost::filesystem::path&_T);
|
---|
177 |
|
---|
178 | template<typename T> void queryCurrentValue(const char * name, T &_T)
|
---|
179 | {
|
---|
180 | if (typeid( T ) == *TypeMap[name]) { // constructor of type_info is private, hence can only store by ref or ptr
|
---|
181 | if (CurrentValueMap.find(name) == CurrentValueMap.end())
|
---|
182 | throw MissingValueException(__FILE__, __LINE__);
|
---|
183 | _T = lexical_cast<T>(CurrentValueMap[name].c_str());
|
---|
184 | CurrentValueMap.erase(name);
|
---|
185 | } else
|
---|
186 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
187 | }
|
---|
188 | template<typename T> void queryCurrentValue(const char * name, std::vector<T> &_T)
|
---|
189 | {
|
---|
190 | T temp;
|
---|
191 | if (typeid( std::vector<T> ) == *TypeMap[name]) { // constructor of type_info is private, hence can only store by ref or ptr
|
---|
192 | if (CurrentValueMap.find(name) == CurrentValueMap.end())
|
---|
193 | throw MissingValueException(__FILE__, __LINE__);
|
---|
194 | std::istringstream stream(CurrentValueMap[name]);
|
---|
195 | CurrentValueMap.erase(name);
|
---|
196 | while (!stream.fail()) {
|
---|
197 | stream >> temp >> std::ws;
|
---|
198 | _T.push_back(temp);
|
---|
199 | }
|
---|
200 | } else
|
---|
201 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
202 | }
|
---|
203 |
|
---|
204 | void setCurrentValue(const char * name, class atom * &_T);
|
---|
205 | void setCurrentValue(const char * name, const element * &_T);
|
---|
206 | void setCurrentValue(const char * name, class molecule * &_T);
|
---|
207 | void setCurrentValue(const char * name, class Box &_T);
|
---|
208 | void setCurrentValue(const char * name, class Vector &_T);
|
---|
209 | void setCurrentValue(const char * name, class BoxVector &_T);
|
---|
210 | void setCurrentValue(const char * name, std::vector<atom *>&_T);
|
---|
211 | void setCurrentValue(const char * name, std::vector<const element *>&_T);
|
---|
212 | void setCurrentValue(const char * name, std::vector<molecule *>&_T);
|
---|
213 | void setCurrentValue(const char * name, boost::filesystem::path&_T);
|
---|
214 |
|
---|
215 | template<class T> void setCurrentValue(const char * name, T &_T)
|
---|
216 | {
|
---|
217 | std::ostringstream stream;
|
---|
218 | if (typeid( T ) == *TypeMap[name]) { // constructor of type_info is private, hence can only store by ref or ptr
|
---|
219 | stream << _T;
|
---|
220 | CurrentValueMap[name] = stream.str();
|
---|
221 | } else
|
---|
222 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
223 | }
|
---|
224 | template<class T> void setCurrentValue(const char * name, std::vector<T> &_T)
|
---|
225 | {
|
---|
226 | std::ostringstream stream;
|
---|
227 | if (typeid( std::vector<T> ) == *TypeMap[name]) { // constructor of type_info is private, hence can only store by ref or ptr
|
---|
228 | std::ostringstream stream;
|
---|
229 | for (typename std::vector<T>::iterator iter = _T.begin(); iter != _T.end(); ++iter) {
|
---|
230 | stream << (*iter) << " ";
|
---|
231 | }
|
---|
232 | CurrentValueMap[name] = stream.str();
|
---|
233 | } else
|
---|
234 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | private:
|
---|
239 | // private constructor and destructor
|
---|
240 | MapOfActions();
|
---|
241 | virtual ~MapOfActions();
|
---|
242 |
|
---|
243 | // lookup list from our configs to the ones of CommandLineParser
|
---|
244 | std::map< std::set<std::string> *, po::options_description *> CmdParserLookup;
|
---|
245 |
|
---|
246 | // map of the action names and their description
|
---|
247 | std::map<std::string, std::string> CurrentValueMap;
|
---|
248 | std::map<std::string, std::string> DescriptionMap;
|
---|
249 | std::map<std::string, std::string> ShortFormMap;
|
---|
250 | std::map<std::string, const std::type_info * > TypeMap;
|
---|
251 | std::map<const std::type_info *, enum OptionTypes > TypeEnumMap;
|
---|
252 | };
|
---|
253 |
|
---|
254 |
|
---|
255 | #endif /* MAPOFACTIONS_HPP_ */
|
---|