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