Parameters.h

00001 /*
00002  *  Copyright (C) 2010  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #ifndef __PARAMETERS_H__
00019 #define __PARAMETERS_H__
00020 
00021 #include "StringMap.h"
00022 
00023 #include <ctype.h>
00024 #include <stddef.h>
00025 
00026 class ParameterList;
00027 
00028 class Parameter
00029 {
00030 protected:
00031     char ch;
00032     char * description;
00033     void * var;
00034 
00035     static int nameCol;
00036     static int statusCol;
00037 
00038     virtual void Translate(const char * value) = 0;
00039     virtual bool TranslateExtras(const char * value, const char * extras);
00040 
00041     static bool CheckInteger(const char * value);
00042     static bool CheckDouble(const char * value);
00043 
00044     String * warnings;
00045 
00046 public:
00047 
00048     Parameter(char c, const char * desc, void * v);
00049 
00050     virtual ~Parameter()
00051     {
00052         delete [] description;
00053     }
00054 
00055     virtual bool Read(int argc, char ** argv, int argn);
00056     virtual void Status() = 0;
00057 
00058     static void SetNameLen(int len)
00059     {
00060         nameCol = len;
00061     }
00062     static void SetStatusLen(int len)
00063     {
00064         statusCol = len;
00065     }
00066 
00067     void SetWarningBuffer(String & buffer)
00068     {
00069         warnings = &buffer;
00070     }
00071     void warning(const char * format, ...);
00072 
00073     friend class ParameterList;
00074 };
00075 
00076 class IntParameter : public Parameter
00077 {
00078 public:
00079     IntParameter(char c, const char * desc, int & v)
00080         : Parameter(c, desc, &v)
00081     {}
00082 
00083     virtual void Status();
00084 
00085 protected:
00086     virtual void Translate(const char * value);
00087     virtual bool TranslateExtras(const char * value, const char * extras);
00088 };
00089 
00090 class HiddenInteger : public IntParameter
00091 {
00092 public:
00093     HiddenInteger(char c, const char * desc, int & v)
00094         : IntParameter(c, desc, v)
00095     {}
00096 
00097     virtual void Status() { }
00098 };
00099 
00100 
00101 class SwitchParameter : public Parameter
00102 {
00103 public:
00104     SwitchParameter(char c, const char * desc, bool & v)
00105         : Parameter(c, desc, &v)
00106     {}
00107 
00108     virtual void Status();
00109 
00110 protected:
00111     virtual void Translate(const char * value);
00112 };
00113 
00114 class HiddenSwitch : public SwitchParameter
00115 {
00116 public:
00117     HiddenSwitch(char c, const char * desc, bool & v)
00118         : SwitchParameter(c, desc, v)
00119     {}
00120 
00121     virtual void Status() { }
00122 };
00123 
00124 class DoubleParameter : public Parameter
00125 {
00126 public:
00127     DoubleParameter(char c, const char * desc, double & v);
00128 
00129     virtual void Status();
00130 
00131     DoubleParameter & SetPrecision(int precision)
00132     {
00133         this->precision = precision;
00134 
00135         return *this;
00136     }
00137 
00138 protected:
00139     virtual void Translate(const char * value);
00140     virtual bool TranslateExtras(const char * value, const char * extras);
00141 
00142     int precision;
00143 };
00144 
00145 class HiddenDouble : public DoubleParameter
00146 {
00147 public:
00148     HiddenDouble(char c, const char * desc, double &v)
00149         : DoubleParameter(c, desc, v)
00150     {}
00151 
00152     virtual void Status() { }
00153 };
00154 
00155 class StringParameter : public Parameter
00156 {
00157 public:
00158     StringParameter(char c, const char * desc, String & v, bool allowBlank = true)
00159             : Parameter(c, desc, &v)
00160     {
00161         required = !allowBlank;
00162     }
00163 
00164     virtual void Status();
00165 
00166 protected:
00167     bool required;
00168 
00169     virtual void Translate(const char * value);
00170     virtual bool TranslateExtras(const char * value, const char * extras);
00171 };
00172 
00173 class HiddenString : public StringParameter
00174 {
00175 public:
00176     HiddenString(char c, const char * desc, String & v)
00177         : StringParameter(c, desc, v)
00178     {}
00179 
00180     virtual void Status() { }
00181 };
00182 
00183 struct OptionList
00184 {
00185     char     ch;
00186     char *   description;
00187     int      code;
00188 };
00189 
00190 #define BEGIN_OPTION_LIST(name)     ; OptionList name[] = {
00191 #define END_OPTION_LIST(none)       , {0, none, 0} };
00192 
00193 class ListParameter : public Parameter
00194 {
00195 public:
00196     ListParameter(char c, const char * desc, int & v, OptionList * opt);
00197 
00198     virtual void Status();
00199 
00200 protected:
00201     String       key;
00202     OptionList * options;
00203     virtual void Translate(const char * value);
00204 };
00205 
00206 class SetParameter : public Parameter
00207 {
00208 public:
00209     SetParameter(char c, const char * desc, int & v, OptionList * opt);
00210 
00211     virtual void Status();
00212 
00213 protected:
00214     String       key;
00215     OptionList * options;
00216     virtual void Translate(const char * value);
00217 };
00218 
00219 struct LongParameterList
00220 {
00221     const char * description;
00222     void * value;
00223     bool exclusive;
00224     int  type;
00225     bool touched;
00226 };
00227 
00228 #define LP_BOOL_PARAMETER     1
00229 #define LP_INT_PARAMETER      2
00230 #define LP_DOUBLE_PARAMETER   3
00231 #define LP_STRING_PARAMETER   4
00232 #define LP_LEGACY_PARAMETERS  99
00233 
00234 #define BEGIN_LONG_PARAMETERS(array)   LongParameterList array[] = {\
00235                                               { NULL,  NULL,      false,  0, 0},
00236 #define LONG_PARAMETER_GROUP(label)           { label, NULL,      false,  0, 0},
00237 #define LONG_PARAMETER(label,boolptr)         { label, boolptr,   false,  1, 0},
00238 #define EXCLUSIVE_PARAMETER(label,boolptr)    { label, boolptr,   true,   1, 0},
00239 #define LONG_INTPARAMETER(label,intptr)       { label, intptr,    false,  2, 0},
00240 #define LONG_SMARTINTPARAMETER(label,intptr)  { label, intptr,    true,   2, 0},
00241 #define LONG_DOUBLEPARAMETER(label,doubleptr) { label, doubleptr, false,  3, 0},
00242 #define LONG_STRINGPARAMETER(label,stringptr) { label, stringptr, false,  4, 0},
00243 #define BEGIN_LEGACY_PARAMETERS()             { "$$$", NULL,      false, 99, 0},
00244 #define END_LONG_PARAMETERS()                 { NULL,  NULL,      false,  0, 0}};
00245 
00246 class LongParameters : public Parameter
00247 {
00248 public:
00249     LongParameters(const char * desc, LongParameterList * list);
00250 
00251     virtual void Status();
00252 
00253     LongParameters * SetPrecision(int precision)
00254     {
00255         this->precision = precision;
00256 
00257         return this;
00258     }
00259 
00260 protected:
00261     StringMap index;
00262     StringMap legacyIndex;
00263 
00264     LongParameterList * list;
00265     int group_len;
00266     int precision;
00267 
00268     virtual void Translate(const char * value);
00269     virtual bool TranslateExtras(const char * value, const char * extras);
00270 
00271     void ExplainAmbiguity(const char * value);
00272 
00273     void Status(LongParameterList * ptr, int & line_len, bool & need_a_comma);
00274 };
00275 
00276 class ParameterList
00277 {
00278 protected:
00279     Parameter ** pl;
00280     int count;
00281     int size;
00282 
00283     void MakeString(int argc, char ** argv, int start = 1);
00284 
00285 public:
00286     char * string;
00287 
00288     ParameterList(int s = 36)
00289     {
00290         size = s;
00291         count = 0;
00292         pl = new Parameter * [size];
00293         string = NULL;
00294     }
00295 
00296     virtual ~ParameterList();
00297 
00298     void Add(Parameter * p);
00299 
00300     // Tries to process all command line arguments
00301     virtual void Read(int argc, char ** argv, int start = 1);
00302 
00303     // Allows for trailing, unprocessed, filenames in the command line
00304     // The number of translated argv[] items is returned
00305     virtual int ReadWithTrailer(int argc, char ** argv, int start = 1);
00306 
00307     // Outputs summary of parameter switches and settings
00308     virtual void Status();
00309 
00310     // Keeps track of warnings generated during parameter processing
00311     String  warnings;
00312     String  messages;
00313 
00314     // Functions that gracefully enforce parameter settings
00315     void Enforce(bool & var, bool value, const char * reason, ...);
00316     void Enforce(int & var, int value, const char * reason, ...);
00317     void Enforce(double & var, double value, const char * reason, ...);
00318     void Enforce(String & var, const char * value, const char * reason, ...);
00319 };
00320 
00321 #endif
Generated on Tue Sep 6 17:52:00 2011 for libStatGen Software by  doxygen 1.6.3