libStatGen Software  1
Parameters.h
1 /*
2  * Copyright (C) 2010-2012 Regents of the University of Michigan
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __PARAMETERS_H__
19 #define __PARAMETERS_H__
20 
21 #include "StringMap.h"
22 #include "PhoneHome.h"
23 
24 #include <ctype.h>
25 #include <stddef.h>
26 
27 class ParameterList;
28 
29 class Parameter
30 {
31 protected:
32 
33  static const char PARAM_STR_SEP = ',';
34  char ch;
35  char * description;
36  void * var;
37 
38  static int nameCol;
39  static int statusCol;
40 
41  virtual void Translate(const char * value) = 0;
42  virtual bool TranslateExtras(const char * value, const char * extras);
43 
44  static bool CheckInteger(const char * value);
45  static bool CheckDouble(const char * value);
46 
47  String * warnings;
48  bool myNoPhoneHome;
49  String myVersion;
50 
51 public:
52 
53  Parameter(char c, const char * desc, void * v);
54 
55  virtual ~Parameter()
56  {
57  delete [] description;
58  }
59 
60  virtual bool Read(int argc, char ** argv, int argn);
61  virtual void Status() = 0;
62  virtual void addParamsToString(String& params)
63  {
64  if(var != NULL)
65  {
66  if(!params.IsEmpty())
67  {
68  params += PARAM_STR_SEP;
69  }
70  params += description;
71  }
72  }
73 
74 
75  static void SetNameLen(int len)
76  {
77  nameCol = len;
78  }
79  static void SetStatusLen(int len)
80  {
81  statusCol = len;
82  }
83 
84  void SetWarningBuffer(String & buffer)
85  {
86  warnings = &buffer;
87  }
88  void warning(const char * format, ...);
89 
90  friend class ParameterList;
91 };
92 
93 class IntParameter : public Parameter
94 {
95 public:
96  IntParameter(char c, const char * desc, int & v)
97  : Parameter(c, desc, &v)
98  {}
99 
100  virtual void Status();
101 
102 protected:
103  virtual void Translate(const char * value);
104  virtual bool TranslateExtras(const char * value, const char * extras);
105 };
106 
108 {
109 public:
110  HiddenInteger(char c, const char * desc, int & v)
111  : IntParameter(c, desc, v)
112  {}
113 
114  virtual void Status() { }
115 };
116 
117 
119 {
120 public:
121  SwitchParameter(char c, const char * desc, bool & v)
122  : Parameter(c, desc, &v)
123  {}
124 
125  virtual void Status();
126 
127 protected:
128  virtual void Translate(const char * value);
129 };
130 
132 {
133 public:
134  HiddenSwitch(char c, const char * desc, bool & v)
135  : SwitchParameter(c, desc, v)
136  {}
137 
138  virtual void Status() { }
139 };
140 
142 {
143 public:
144  DoubleParameter(char c, const char * desc, double & v);
145 
146  virtual void Status();
147 
148  DoubleParameter & SetPrecision(int precision)
149  {
150  this->precision = precision;
151 
152  return *this;
153  }
154 
155 protected:
156  virtual void Translate(const char * value);
157  virtual bool TranslateExtras(const char * value, const char * extras);
158 
159  int precision;
160 };
161 
163 {
164 public:
165  HiddenDouble(char c, const char * desc, double &v)
166  : DoubleParameter(c, desc, v)
167  {}
168 
169  virtual void Status() { }
170 };
171 
173 {
174 public:
175  StringParameter(char c, const char * desc, String & v, bool allowBlank = true)
176  : Parameter(c, desc, &v)
177  {
178  required = !allowBlank;
179  }
180 
181  virtual void Status();
182 
183 protected:
184  bool required;
185 
186  virtual void Translate(const char * value);
187  virtual bool TranslateExtras(const char * value, const char * extras);
188 };
189 
191 {
192 public:
193  HiddenString(char c, const char * desc, String & v)
194  : StringParameter(c, desc, v)
195  {}
196 
197  virtual void Status() { }
198 };
199 
201 {
202  char ch;
203  char * description;
204  int code;
205 };
206 
207 #define BEGIN_OPTION_LIST(name) ; OptionList name[] = {
208 #define END_OPTION_LIST(none) , {0, none, 0} };
209 
210 class ListParameter : public Parameter
211 {
212 public:
213  ListParameter(char c, const char * desc, int & v, OptionList * opt);
214 
215  virtual void Status();
216 
217 protected:
218  String key;
219  OptionList * options;
220  virtual void Translate(const char * value);
221 };
222 
223 class SetParameter : public Parameter
224 {
225 public:
226  SetParameter(char c, const char * desc, int & v, OptionList * opt);
227 
228  virtual void Status();
229 
230 protected:
231  String key;
232  OptionList * options;
233  virtual void Translate(const char * value);
234 };
235 
237 {
238  const char * description;
239  void * value;
240  bool exclusive;
241  int type;
242  bool touched;
243 };
244 
245 #define LP_BOOL_PARAMETER 1
246 #define LP_INT_PARAMETER 2
247 #define LP_DOUBLE_PARAMETER 3
248 #define LP_STRING_PARAMETER 4
249 #define LP_LEGACY_PARAMETERS 99
250 #define LP_PHONEHOME_VERSION 98
251 
252 #define BEGIN_LONG_PARAMETERS(array) LongParameterList array[] = {\
253  { NULL, NULL, false, 0, 0},
254 #define LONG_PARAMETER_GROUP(label) { label, NULL, false, 0, 0},
255 #define LONG_PARAMETER(label,boolptr) { label, boolptr, false, 1, 0},
256 #define EXCLUSIVE_PARAMETER(label,boolptr) { label, boolptr, true, 1, 0},
257 #define LONG_INTPARAMETER(label,intptr) { label, intptr, false, 2, 0},
258 #define LONG_SMARTINTPARAMETER(label,intptr) { label, intptr, true, 2, 0},
259 #define LONG_DOUBLEPARAMETER(label,doubleptr) { label, doubleptr, false, 3, 0},
260 #define LONG_STRINGPARAMETER(label,stringptr) { label, stringptr, false, 4, 0},
261 #define LONG_PHONEHOME(version) { "PhoneHome", NULL, false, 0, 0}, { version, NULL, false, LP_PHONEHOME_VERSION, 0}, {"phoneHomeThinning", &PhoneHome::allThinning, false, LP_INT_PARAMETER, 0},
262 #define BEGIN_LEGACY_PARAMETERS() { "$$$", NULL, false, 99, 0},
263 #define END_LONG_PARAMETERS() { NULL, NULL, false, 0, 0}};
264 
265 class LongParameters : public Parameter
266 {
267 public:
268  LongParameters(const char * desc, LongParameterList * list);
269 
270  virtual void Status();
271  virtual void addParamsToString(String& params);
272 
273  LongParameters * SetPrecision(int precision)
274  {
275  this->precision = precision;
276 
277  return this;
278  }
279 
280 protected:
281  StringMap index;
282  StringMap legacyIndex;
283 
284  LongParameterList * list;
285  int group_len;
286  int precision;
287 
288  virtual void Translate(const char * value);
289  virtual bool TranslateExtras(const char * value, const char * extras);
290 
291  void ExplainAmbiguity(const char * value);
292 
293  void Status(LongParameterList * ptr, int & line_len, bool & need_a_comma);
294 };
295 
297 {
298 protected:
299  Parameter ** pl;
300  int count;
301  int size;
302 
303  void MakeString(int argc, char ** argv, int start = 1);
304  void HandlePhoneHome(int argc, char ** argv, int start);
305 
306 public:
307  char * string;
308 
309  ParameterList(int s = 36)
310  {
311  size = s;
312  count = 0;
313  pl = new Parameter * [size];
314  string = NULL;
315  }
316 
317  virtual ~ParameterList();
318 
319  void Add(Parameter * p);
320 
321  // Tries to process all command line arguments
322  virtual void Read(int argc, char ** argv, int start = 1);
323 
324  // Allows for trailing, unprocessed, filenames in the command line
325  // The number of translated argv[] items is returned
326  virtual int ReadWithTrailer(int argc, char ** argv, int start = 1);
327 
328  // Outputs summary of parameter switches and settings
329  virtual void Status();
330 
331  // Keeps track of warnings generated during parameter processing
332  String warnings;
333  String messages;
334 
335  // Functions that gracefully enforce parameter settings
336  void Enforce(bool & var, bool value, const char * reason, ...);
337  void Enforce(int & var, int value, const char * reason, ...);
338  void Enforce(double & var, double value, const char * reason, ...);
339  void Enforce(String & var, const char * value, const char * reason, ...);
340 };
341 
342 
343 // Container for holding the long parameter list.
344 // Allows parameters to be added.
345 // Allows users to not have to use BEGIN_LONG_PARAMETERS or to understand
346 // the details of a LongParameterList.
348 {
349 public:
352 
353  // Get a pointer to the LongParameterList.
354  inline LongParameterList* getLongParameterList()
355  { return(myArray); }
356 
357  void add(const char * label, void * val, bool excl,
358  int paramType, bool touch = 0);
359 
360  inline void addGroup(const char * label)
361  { add(label, NULL, false, 0, 0); }
362 
363  inline void addBool(const char * label, void * boolptr)
364  { add(label, boolptr, false, LP_BOOL_PARAMETER, 0); }
365 
366  inline void addExclusiveBool(const char * label, void * boolptr)
367  { add(label, boolptr, true, LP_BOOL_PARAMETER, 0); }
368 
369  inline void addInt(const char * label, void * intptr)
370  { add(label, intptr, false, LP_INT_PARAMETER, 0); }
371 
372  inline void addSmartInt(const char * label, void * intptr)
373  { add(label, intptr, true, LP_INT_PARAMETER, 0); }
374 
375  inline void addDouble(const char * label, void * doubleptr)
376  { add(label, doubleptr, false, LP_DOUBLE_PARAMETER, 0); }
377 
378  inline void addString(const char * label, void * stringptr)
379  { add(label, stringptr, false, LP_STRING_PARAMETER, 0); }
380 
381  inline void addPhoneHome(const char* version)
382  {
383  add("PhoneHome", NULL, false, 0, 0);
384  add(version, NULL, false, LP_PHONEHOME_VERSION, 0);
385  add("phoneHomeThinning", &PhoneHome::allThinning, false, LP_INT_PARAMETER, 0);
386  }
387 
388  inline void startLegacyParams()
389  { add("$$$", NULL, false, 99, 0); }
390 
391 private:
392  // At most 100 parameters are allowed.
393  static const int MAX_PARAM_ARRAY_SIZE = 100;
394  LongParameterList myArray[MAX_PARAM_ARRAY_SIZE];
395 
396  int myEndIndex;
397 };
398 
399 
400 #endif
ListParameter
Definition: Parameters.h:211
OptionList
Definition: Parameters.h:201
HiddenString
Definition: Parameters.h:191
String
Definition: StringBasics.h:39
HiddenInteger
Definition: Parameters.h:108
LongParameters
Definition: Parameters.h:266
Parameter
Definition: Parameters.h:30
StringParameter
Definition: Parameters.h:173
StringMap
Definition: StringMap.h:24
HiddenSwitch
Definition: Parameters.h:132
LongParameterList
Definition: Parameters.h:237
ParameterList
Definition: Parameters.h:297
LongParamContainer
Definition: Parameters.h:348
SwitchParameter
Definition: Parameters.h:119
IntParameter
Definition: Parameters.h:94
HiddenDouble
Definition: Parameters.h:163
SetParameter
Definition: Parameters.h:224
DoubleParameter
Definition: Parameters.h:142