JDFTx  1.2.0
command.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------
2 Copyright 2011 Ravishankar Sundararaman
3 
4 This file is part of JDFTx.
5 
6 JDFTx is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 JDFTx is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with JDFTx. If not, see <http://www.gnu.org/licenses/>.
18 -------------------------------------------------------------------*/
19 
20 #ifndef JDFTX_COMMAND_COMMAND_H
21 #define JDFTX_COMMAND_COMMAND_H
22 
23 #include <electronic/common.h>
24 #include <commands/ParamList.h>
25 #include <core/string.h>
26 #include <memory>
27 #include <set>
28 
60 class Command
62 {
63 public:
64  string name;
65  string format;
66  string comments;
67  string section;
68  string category;
69  string subcategory;
70 
71  std::set<string> requires;
72  std::set<string> forbids;
73 
75  bool hasDefault;
76  string emptyParamError;
77 
82  virtual void process(ParamList& pl, Everything& e)=0;
83 
87  virtual void printStatus(Everything& e, int iRep)=0;
88 
89 protected:
97  Command(string name, string path);
98 
99  void require(string);
100  void forbid(string);
101 };
102 
103 std::map<string,Command*>& getCommandMap();
104 
105 static EnumStringMap<bool> boolMap(false, "no", true, "yes");
106 
107 
114 template<typename GetDescription>
115 string addDescriptions(string optionList, const GetDescription& getDescription, string spacer="\n+ ")
116 {
117  //Determine max width of name, so as to align:
118  istringstream iss(optionList);
119  size_t nameWidth=0;
120  while(!iss.eof())
121  { string name;
122  getline(iss, name, '|');
123  trim(name);
124  nameWidth = std::max(name.length(),nameWidth);
125  }
126  //Process decsription list:
127  iss.seekg(0, std::ios::beg); //rewind;
128  string ret;
129  while(!iss.eof())
130  { //Get a name from the option list:
131  string name;
132  getline(iss, name, '|');
133  trim(name);
134  if(!name.length()) break;
135  //Get the description:
136  string desc = getDescription(name);
137  //Pad the name to required width:
138  if(name.length()<nameWidth)
139  name.resize(nameWidth, ' ');
140  ret += spacer + name;
141  if(desc.length()) ret += ": " + desc;
142  }
143  return ret;
144 }
145 
148 inline string nullDescription(const string&)
149 { return string();
150 }
151 
154 template<typename Enum> struct LinkDescription
155 { const EnumStringMap<Enum>& nameMap;
156  const EnumStringMap<Enum>& descMap;
157 
161  : nameMap(nameMap), descMap(descMap)
162  {
163  }
164 
168  string operator()(const string& name) const
169  { Enum type = Enum();
170  bool nameFound = nameMap.getEnum(name.c_str(), type);
171  assert(nameFound);
172  return descMap.getString(type);
173  }
174 };
175 
180 template<typename Enum>
182 { return LinkDescription<Enum>(nameMap, descMap);
183 }
184 
189 std::shared_ptr<SpeciesInfo> findSpecies(string id, Everything& e);
190 
197 bool isReadable(string fname);
198 
199 #endif // JDFTX_COMMAND_COMMAND_H
LinkDescription(const EnumStringMap< Enum > &nameMap, const EnumStringMap< Enum > &descMap)
Definition: command.h:160
STL strings and streams with case insensitive comparison.
string nullDescription(const string &)
Null description function that can be used with addDescriptions(). This can be useful for simply refo...
Definition: command.h:148
virtual void printStatus(Everything &e, int iRep)=0
Print a command line that would result in the current status.
A template to ease option parsing (maps enums <–> strings)
Definition: Util.h:179
string name
Identifier for the command in the input file. Must be unique!
Definition: command.h:64
string subcategory
Subcategory of command under which to list the documentation.
Definition: command.h:69
void require(string)
utility to add a command to the requires list
bool isReadable(string fname)
Check if file is readable in an MPI friendly way. This function must be called from all processes...
string emptyParamError
Error message if command is manually issued with no parameters. Ddefault: empty i.e. allow empty parameter list.
Definition: command.h:76
Generate a description functor for addDescriptions() from an EnumStringMap.
Definition: command.h:154
Definition: string.h:77
bool allowMultiple
Whether this command can occur multiple times in an input file (default: false).
Definition: command.h:74
Helper class for parsing command lines in input file.
std::map< string, Command * > & getCommandMap()
Retrieve the map from command names to objects created during static initialization.
std::set< string > forbids
Names of other commands that this one is incompatibile with.
Definition: command.h:72
string comments
Detailed help for the command which goes into jdftx -t as well as the Doxygen manual. Please check formatting in both versions.
Definition: command.h:66
string category
Category of command under which to list the documentation.
Definition: command.h:68
Command(string name, string path)
void forbid(string)
utility to add a command to the forbids list
LinkDescription< Enum > linkDescription(const EnumStringMap< Enum > &nameMap, const EnumStringMap< Enum > &descMap)
Helper function to select appropriate LinkDescription<> by overloading.
Definition: command.h:181
string operator()(const string &name) const
Look name up in nameMap, and return the corresponding description in descMap.
Definition: command.h:168
void trim(string &s)
Remove leading and trailing spaces from a string.
Definition: string.h:45
Definition: Everything.h:41
string format
Usage syntax for the command (excluding the command name)
Definition: command.h:65
Wrapper to std::istringstream that eases parsing of input file command lines.
Definition: ParamList.h:30
Abstract base class for all commands.
Definition: command.h:61
std::set< string > requires
Names of other commands that this one requires; those commands will be processed before this one...
Definition: command.h:71
string addDescriptions(string optionList, const GetDescription &getDescription, string spacer="\n+ ")
Process the EnumStringMap::optionList() to add descriptions using an arbitrary functor.
Definition: command.h:115
string section
Which executable the command belongs to, and hence which section it must be documented under...
Definition: command.h:67
bool hasDefault
Whether this command has a default (default: false). If true, process() will be called with an empty ...
Definition: command.h:75
std::basic_string< char, ichar_traits > string
Case-insensitive string.
Definition: string.h:42
std::shared_ptr< SpeciesInfo > findSpecies(string id, Everything &e)
Find species matching an id (and create it from a wildcard if necessary)
#define assert(expr)
A custom assertion with stack trace (NOTE: enabled in release modes as well)
Definition: Util.h:100
virtual void process(ParamList &pl, Everything &e)=0
Process the command from its command line.