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     virtual void Status();
00083 
00084 protected:
00085     virtual void Translate(const char * value);
00086     virtual bool TranslateExtras(const char * value, const char * extras);
00087 };
00088 
00089 class HiddenInteger : public IntParameter
00090 {
00091 public:
00092     HiddenInteger(char c, const char * desc, int & v)
00093             : IntParameter(c, desc, v) {}
00094 
00095     virtual void Status() { }
00096 };
00097 
00098 
00099 class SwitchParameter : public Parameter
00100 {
00101 public:
00102     SwitchParameter(char c, const char * desc, bool & v)
00103             : Parameter(c, desc, &v) {}
00104 
00105     virtual void Status();
00106 
00107 protected:
00108     virtual void Translate(const char * value);
00109 };
00110 
00111 class HiddenSwitch : public SwitchParameter
00112 {
00113 public:
00114     HiddenSwitch(char c, const char * desc, bool & v)
00115             : SwitchParameter(c, desc, v) {}
00116 
00117     virtual void Status() { }
00118 };
00119 
00120 class DoubleParameter : public Parameter
00121 {
00122 public:
00123     DoubleParameter(char c, const char * desc, double & v);
00124 
00125     virtual void Status();
00126 
00127 protected:
00128     virtual void Translate(const char * value);
00129     virtual bool TranslateExtras(const char * value, const char * extras);
00130 };
00131 
00132 class HiddenDouble : public DoubleParameter
00133 {
00134 public:
00135     HiddenDouble(char c, const char * desc, double &v)
00136             : DoubleParameter(c, desc, v) {}
00137 
00138     virtual void Status() { }
00139 };
00140 
00141 class StringParameter : public Parameter
00142 {
00143 public:
00144     StringParameter(char c, const char * desc, String & v, bool allowBlank = true)
00145             : Parameter(c, desc, &v)
00146     {
00147         required = !allowBlank;
00148     }
00149 
00150     virtual void Status();
00151 
00152 protected:
00153     bool required;
00154 
00155     virtual void Translate(const char * value);
00156     virtual bool TranslateExtras(const char * value, const char * extras);
00157 };
00158 
00159 class HiddenString : public StringParameter
00160 {
00161 public:
00162     HiddenString(char c, const char * desc, String & v)
00163             : StringParameter(c, desc, v) {}
00164 
00165     virtual void Status() { }
00166 };
00167 
00168 struct OptionList
00169 {
00170     char     ch;
00171     char *   description;
00172     int      code;
00173 };
00174 
00175 #define BEGIN_OPTION_LIST(name)     ; OptionList name[] = {
00176 #define END_OPTION_LIST(none)       , {0, none, 0} };
00177 
00178 class ListParameter : public Parameter
00179 {
00180 public:
00181     ListParameter(char c, const char * desc, int & v, OptionList * opt);
00182 
00183     virtual void Status();
00184 
00185 protected:
00186     String       key;
00187     OptionList * options;
00188     virtual void Translate(const char * value);
00189 };
00190 
00191 class SetParameter : public Parameter
00192 {
00193 public:
00194     SetParameter(char c, const char * desc, int & v, OptionList * opt);
00195 
00196     virtual void Status();
00197 
00198 protected:
00199     String       key;
00200     OptionList * options;
00201     virtual void Translate(const char * value);
00202 };
00203 
00204 struct LongParameterList
00205 {
00206     const char * description;
00207     void * value;
00208     bool exclusive;
00209     int  type;
00210     bool touched;
00211 };
00212 
00213 #define LP_BOOL_PARAMETER     1
00214 #define LP_INT_PARAMETER      2
00215 #define LP_DOUBLE_PARAMETER   3
00216 #define LP_STRING_PARAMETER   4
00217 #define LP_LEGACY_PARAMETERS  99
00218 
00219 #define BEGIN_LONG_PARAMETERS(array)   LongParameterList array[] = {\
00220                                               { NULL,  NULL,      false,  0, 0},
00221 #define LONG_PARAMETER_GROUP(label)           { label, NULL,      false,  0, 0},
00222 #define LONG_PARAMETER(label,boolptr)         { label, boolptr,   false,  1, 0},
00223 #define EXCLUSIVE_PARAMETER(label,boolptr)    { label, boolptr,   true,   1, 0},
00224 #define LONG_INTPARAMETER(label,intptr)       { label, intptr,    false,  2, 0},
00225 #define LONG_SMARTINTPARAMETER(label,intptr)  { label, intptr,    true,   2, 0},
00226 #define LONG_DOUBLEPARAMETER(label,doubleptr) { label, doubleptr, false,  3, 0},
00227 #define LONG_STRINGPARAMETER(label,stringptr) { label, stringptr, false,  4, 0},
00228 #define BEGIN_LEGACY_PARAMETERS()             { "$$$", NULL,      false, 99, 0},
00229 #define END_LONG_PARAMETERS()                 { NULL,  NULL,      false,  0, 0}};
00230 
00231 class LongParameters : public Parameter
00232 {
00233 public:
00234     LongParameters(const char * desc, LongParameterList * list);
00235 
00236     virtual void Status();
00237 
00238 protected:
00239     StringMap index;
00240     StringMap legacyIndex;
00241 
00242     LongParameterList * list;
00243     int group_len;
00244 
00245     virtual void Translate(const char * value);
00246     virtual bool TranslateExtras(const char * value, const char * extras);
00247 
00248     void ExplainAmbiguity(const char * value);
00249 
00250     void Status(LongParameterList * ptr, int & line_len, bool & need_a_comma);
00251 };
00252 
00253 class ParameterList
00254 {
00255 protected:
00256     Parameter ** pl;
00257     int count;
00258     int size;
00259 
00260     void MakeString(int argc, char ** argv, int start = 1);
00261 
00262 public:
00263     char * string;
00264 
00265     ParameterList(int s = 36)
00266     {
00267         size = s;
00268         count = 0;
00269         pl = new Parameter * [size];
00270         string = NULL;
00271     }
00272 
00273     virtual ~ParameterList();
00274 
00275     void Add(Parameter * p);
00276 
00277     // Tries to process all command line arguments
00278     virtual void Read(int argc, char ** argv, int start = 1);
00279 
00280     // Allows for trailing, unprocessed, filenames in the command line
00281     // The number of translated argv[] items is returned
00282     virtual int ReadWithTrailer(int argc, char ** argv, int start = 1);
00283 
00284     // Outputs summary of parameter switches and settings
00285     virtual void Status();
00286 
00287     // Keeps track of warnings generated during parameter processing
00288     String  warnings;
00289     String  messages;
00290 
00291     // Functions that gracefully enforce parameter settings
00292     void Enforce(bool & var, bool value, const char * reason, ...);
00293     void Enforce(int & var, int value, const char * reason, ...);
00294     void Enforce(double & var, double value, const char * reason, ...);
00295     void Enforce(String & var, const char * value, const char * reason, ...);
00296 };
00297 
00298 #endif
Generated on Wed Nov 17 15:38:29 2010 for StatGen Software by  doxygen 1.6.3