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