NAMD
ParseOptions.h
Go to the documentation of this file.
1 
7 /*
8  Simplifies the interaction with the ConfigList. This allows you to
9  define a set of dependencies in the configuration file, along with
10  default values, ranges, and units. Then, given the ConfigList, this
11  checks the list for valid entries and reads those values, warns for
12  unneeded options, and errors for stuff it couldn't understand, or
13  were out of range or in the wrong units, ...
14 */
15 
16 #ifndef PARSEOPTIONS_H
17 
18 #define PARSEOPTIONS_H
19 
20 #include "common.h"
21 #include "Vector.h"
22 
23 class StringList;
24 class ConfigList;
25 
27 // const char *string(Range r); // for printing the range
30 // const char *string(Units u); // for printing the units
31 BigReal convert(Units to, Units from);//return 0 if stupid (like METER to SEC)
32 
33 #define PARSE_FLOAT (BigReal *) NULL
34 #define PARSE_BIGREAL (BigReal *) NULL
35 #define PARSE_VECTOR (Vector *) NULL
36 #define PARSE_INT (int *) NULL
37 #define PARSE_BOOL (int *) NULL
38 #define PARSE_STRING (char *) NULL
39 #define PARSE_ANYTHING (StringList **) NULL
40 #define PARSE_MULTIPLES (StringList **) NULL, TRUE
41 
42 class ParseOptions {
43  public:
44 
45  class DataElement {
46  public:
48  int index;
49  char *name; // name of the variable
52  char *parent; // I need this for "forward" definitions
57  int many_allowed; // only used by StringList; otherwise assumed FALSE
58  union {
59  BigReal fdef; // the default data value
60  int idef;
61  unsigned int uidef;
62  // there is no default element for the StringList or String
63  };
64  Vector vdef; // seperate since it has its own constructor
65  union {
66  BigReal *fptr; // the element to set
67  int *iptr;
68  unsigned int *uiptr;
70  StringList **slptr; // for string lists
71  char *sptr; // for strings
72  };
73  union { // the actual data
75  int idata;
76  int uidata;
77  StringList *sldata; // this points directly to the ConfigList, so
78  }; // it had better not be deallocated.
82 
83  private:
84  void init(const char *newname, const char *newparent, int optional,
85  const char *err);
86  public:
87  ~DataElement(void);
88  // FLOAT
89  DataElement(const char *newname, const char *newparent, int optional,
90  const char *err, BigReal *ptr, BigReal defalt);
91  DataElement(const char *newname, const char *newparent, int optional,
92  const char *err, BigReal *ptr);
93  // VECTOR
94  DataElement(const char *newname, const char *newparent, int optional,
95  const char *err, Vector *ptr, Vector defalt);
96  DataElement(const char *newname, const char *newparent, int optional,
97  const char *err, Vector *ptr);
98 
99  // INT and BOOL
100  DataElement(const char *newname, const char *newparent, int optional,
101  const char *err, int *ptr, int defalt);
102  DataElement(const char *newname, const char *newparent, int optional,
103  const char *err, int *ptr);
104 
105  // UNSIGNED INT
106  DataElement(const char *newname, const char *newparent, int optional,
107  const char *err, unsigned int *ptr, unsigned int defalt);
108  DataElement(const char *newname, const char *newparent, int optional,
109  const char *err, unsigned int *ptr);
110 
111  // STRINGLIST
112  DataElement(const char *newname, const char *newparent, int optional,
113  const char *err, StringList **ptr, int many_allowed = FALSE);
114  // STRING
115  DataElement(const char *newname, const char *newparent, int optional,
116  const char *err, char *ptr);
117  };
118  private:
119  ConfigList const *configList;
120  DataElement **data_array;
121  int array_size;
122  int array_max_size;
123  void add_element(DataElement *el);
124  int make_dependencies(DataElement *el);
125 
126  // indicates if the string is TRUE (1), FALSE (0), or unknown (-1)
127  // if it can't figure it out
128  int atoBool(const char *s);
129  // convert from a string to Units, returns UNITS_UNDEFINED if error
130  Units atoUnits(const char *s);
131 
132  // tells if this node has children (TRUE for yes)
133  Bool is_parent_node(DataElement *el);
134 
135  public:
136  ParseOptions(void);
137  ~ParseOptions(void);
138  // returns 1 if everything okay, or 0 if already defined
139  // the msg is printed to namdErr if it was required and not
140  // defined and no default exists
141  int require(const char *newname, const char *parent, const char *msg,
142  BigReal *ptr, BigReal defalt);
143  int require(const char *newname, const char *parent, const char *msg,
144  BigReal *ptr);
145 
146  int require(const char *newname, const char *parent, const char *msg,
147  Vector *ptr, Vector defalt);
148  int require(const char *newname, const char *parent, const char *msg,
149  Vector *ptr);
150 
151  int require(const char *newname, const char *parent, const char *msg,
152  int *ptr, int defalt);
153  int require(const char *newname, const char *parent, const char *msg,
154  int *ptr);
155 
156  int require(const char *newname, const char *parent, const char *msg,
157  unsigned int *ptr, unsigned int defalt);
158  int require(const char *newname, const char *parent, const char *msg,
159  unsigned int *ptr);
160 
161  // a "boolean" version, which can convert a string to 1 if true
162  // and 0 if false; everywhere else this looks like an int
163  // (now, if we have a real Boolean type, we wouldn't need this 'B')
164  int requireB(const char *newname, const char *parent, const char *msg,
165  int *ptr, int defalt);
166  int requireB(const char *newname, const char *parent, const char *msg,
167  int *ptr);
168  // for the StringList; there is no default version
169  int require(const char *newname, const char *parent, const char *msg,
170  StringList **ptr = NULL, int many_allowed = FALSE);
171  // Strings don't have a default version, either
172  int require(const char *newname, const char *parent, const char *msg,
173  char *ptr);
174 
175  //
176  // XXX prototypes for optional reverse "newname" and "parent"
177  // (the argument names in a prototype don't matter
178  // but it makes the code more difficult to understand)
179  //
180  int optional(const char *newname, const char *parent, const char *msg,
181  BigReal *ptr, BigReal defalt);
182  int optional(const char *newname, const char *parent, const char *msg,
183  BigReal *ptr);
184 
185  int optional(const char *newname, const char *parent, const char *msg,
186  Vector *ptr, Vector defalt);
187  int optional(const char *newname, const char *parent, const char *msg,
188  Vector *ptr);
189 
190  int optional(const char *newname, const char *parent, const char *msg,
191  int *ptr, int defalt);
192  int optional(const char *newname, const char *parent, const char *msg,
193  int *ptr);
194 
195  int optional(const char *newname, const char *parent, const char *msg,
196  unsigned int *ptr, unsigned int defalt);
197  int optional(const char *newname, const char *parent, const char *msg,
198  unsigned int *ptr);
199 
200  int optionalB(const char *newname, const char *parent, const char *msg,
201  int *ptr, int defalt);
202  int optionalB(const char *newname, const char *parent, const char *msg,
203  int *ptr);
204 
205  int optional(const char *newname, const char *parent, const char *msg,
206  StringList **ptr = NULL, int many_allowed = FALSE);
207  int optional(const char *newname, const char *parent, const char *msg,
208  char *ptr);
209 
210 
211  // get the range of the given variable
212  Range range(const char *name);
213  // set the range of the given variable
214  void range(const char *name, Range newrange);
215  private:
216  // find the children of the given element; used by check_consistency
217  int check_children(int idx, int *flg);
218 
219  // read a string into the appropriate data type; do units if need be
220  Bool scan_float(DataElement *el, const char *s);
221  Bool scan_int(DataElement *el, const char *s);
222  Bool scan_uint(DataElement *el, const char *s);
223  Bool scan_bool(DataElement *el, const char *s);
224  Bool scan_vector(DataElement *el, const char *s);
225 
226  // check if the range is correct and, if not NULL, set the variable
227  // (at this point, the new value already exists in the data element)
228  Bool set_float(DataElement *el);
229  void set_vector(DataElement *el);
230  Bool set_int(DataElement *el);
231  Bool set_uint(DataElement *el);
232  void set_bool(DataElement *el);
233  void set_stringlist(DataElement *el); // also checks for 'many_allowed'
234  void set_string(DataElement *el);
235  public:
236  // make sure the options were defined properly; return TRUE okay, FALSE not
237  Bool check_consistency(void);
238  // returns TRUE if everything set okay or FALSE if not
239  Bool set(const ConfigList& configlist);
240 
241  // find the specified element. Returns NULL if not found;
242  private:
243  DataElement *internal_find(const char *name);
244  public:
245  // special accessor for ScriptTcl
246  char* getfromptr(const char* name, char *outbuf);
247  int istruefromptr(const char* name);
248  int issetfromptr(const char* name);
249  // get the specified element. Returns FALSE if not found or undefined;
250  // prints warning if had to do a type conversion
251  Bool get(const char* name, int *val);
252  Bool get(const char* name, BigReal *val);
253  Bool get(const char* name, Vector *val);
254  Bool get(const char* name, StringList **val);
255  Bool get(const char* name, char *val, int n=0);
256 
257  // number of elements for the given name
258  int num(const char* name);
259  // does the given element exist?
260  Bool defined(const char *name);
261  Bool exists(const char *name);
262 
263  // get/ set the units and scale for the given variable
264  Bool units(const char *name, Units units);
265  Bool units(const char *name, Units *units);
266 };
267 
268 #endif
269 
BigReal convert(Units to, Units from)
Definition: ParseOptions.C:89
DataElement(const char *newname, const char *newparent, int optional, const char *err, BigReal *ptr, BigReal defalt)
Bool defined(const char *name)
Definition: ParseOptions.C:913
Bool units(const char *name, Units units)
Definition: Vector.h:64
#define FALSE
Definition: common.h:118
int optionalB(const char *newname, const char *parent, const char *msg, int *ptr, int defalt)
char * getfromptr(const char *name, char *outbuf)
Definition: ParseOptions.C:938
Units
Definition: ParseOptions.h:28
int require(const char *newname, const char *parent, const char *msg, BigReal *ptr, BigReal defalt)
ParseOptions(void)
Definition: ParseOptions.C:199
int num(const char *name)
Bool set(const ConfigList &configlist)
Definition: ParseOptions.C:642
int Bool
Definition: common.h:133
DataElement * parent_ptr
Definition: ParseOptions.h:53
Range
Definition: ParseOptions.h:26
int istruefromptr(const char *name)
Definition: ParseOptions.C:976
~ParseOptions(void)
Definition: ParseOptions.C:212
int optional(const char *newname, const char *parent, const char *msg, BigReal *ptr, BigReal defalt)
Range range(const char *name)
Bool check_consistency(void)
Definition: ParseOptions.C:391
int issetfromptr(const char *name)
Definition: ParseOptions.C:987
int requireB(const char *newname, const char *parent, const char *msg, int *ptr, int defalt)
double BigReal
Definition: common.h:114
Bool exists(const char *name)
Definition: ParseOptions.C:904