Main Page | Namespace List | Class Hierarchy | Compound List | File List | Namespace Members | Compound Members | File Members

paramproc.h

Go to the documentation of this file.
00001 /**************************************************************************
00002 ***    
00003 *** Copyright (c) 1995-2000 Regents of the University of California,
00004 ***               Andrew E. Caldwell, Andrew B. Kahng and Igor L. Markov
00005 *** Copyright (c) 2000-2004 Regents of the University of Michigan,
00006 ***               Saurabh N. Adya, Jarrod A. Roy and Igor L. Markov
00007 ***
00008 ***  Contact author(s): abk@cs.ucsd.edu, imarkov@umich.edu
00009 ***  Original Affiliation:   UCLA, Computer Science Department,
00010 ***                          Los Angeles, CA 90095-1596 USA
00011 ***
00012 ***  Permission is hereby granted, free of charge, to any person obtaining 
00013 ***  a copy of this software and associated documentation files (the
00014 ***  "Software"), to deal in the Software without restriction, including
00015 ***  without limitation 
00016 ***  the rights to use, copy, modify, merge, publish, distribute, sublicense, 
00017 ***  and/or sell copies of the Software, and to permit persons to whom the 
00018 ***  Software is furnished to do so, subject to the following conditions:
00019 ***
00020 ***  The above copyright notice and this permission notice shall be included
00021 ***  in all copies or substantial portions of the Software.
00022 ***
00023 *** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
00024 *** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00025 *** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
00026 *** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
00027 *** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
00028 *** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
00029 *** THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00030 ***
00031 ***
00032 ***************************************************************************/
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00043 
00044 /*
00045  970622 ilm    Added conversions into double and char * for Param
00046  970622 ilm    Added type NOPARAM for no parameters.
00047  970723 ilm    Changed #include <bool.h> to #include <stl_config.h>
00048                for STL 2.01 compliance
00049  970820 ilm    moved enum ParamType inside class Param
00050                added new ctor for use with NOPARAM
00051                allowed for +option as well as -option
00052                added member bool on() to tell if it was +option
00053  970824 ilm    took off conversions in class Param
00054                defined classes NoParams, BoolParam, UnsignedParam etc
00055                   with unambiguous conversions and clever found()
00056  980313 ilm    fixed const-correctness
00057 */ 
00058 
00059 #ifndef _PARAMPROC_H_
00060 #define _PARAMPROC_H__
00061 
00062 #include <stdlib.h>
00063 #include <iostream>
00064 #include <string.h>
00065 #include <limits.h>
00066 
00067 //#ifndef _MSC_VER
00069 //#include <stl_config.h>
00070 //#endif 
00071 
00072 #include "abkstring.h"
00073 #include "abkassert.h"
00074 
00075 //:(base class) Catches a given parameter from the command line 
00076 class Param            
00077 {               
00078 public:
00079     enum Type { NOPARAM, BOOL, INT, UNSIGNED, DOUBLE, STRING };
00080     // NOPARAM means "empty command line"
00081     // BOOL means "no value: either found in command line or not"
00082 private:
00083     bool          _b;  // found
00084     bool          _on; 
00085     int           _i;
00086     unsigned      _u;
00087     double        _d;
00088     const char *  _s;
00089     Type          _pt;
00090     const char *  _key;
00091 public:
00092     Param(const char * keyy, Type part, int argc, const char * const argv[]);
00093     Param(Type part, int argc, const char * const argv[]); // for NOPARAM only
00094    ~Param() {};
00095     bool      found()       const; 
00096     // for any Param::Type, always need to check before anything else
00097     bool      on()          const;  
00098     // for any Param::Type; true if the option was invoked with +
00099 
00100     int       getInt()      const;
00101     unsigned  getUnsigned() const;
00102     double    getDouble()   const;
00103     const char* getString() const;
00104 
00105 /*  operator  double()      const;  // deprecated : use below classes */
00106 /*  operator  char* ()      const;  //              instead of Param */
00107 };
00108 
00109 //:Constructed from argc/argv, returns to true 
00110 // if the command line had no parameters 
00111 class NoParams  : private Param
00112 {
00113 public:
00114      NoParams(int argc, const char * const argv[]):Param(Param::NOPARAM,argc,argv) {}
00115      bool found()    const { return Param::found(); }
00116      operator bool() const { return Param::found(); }
00117      Param::on;      // base class member access adjustment
00118 };
00119 
00120 //: Catches a given boolean parameter
00121 class BoolParam : private Param
00122 {
00123 public:
00124     BoolParam(const char * key, int argc, const char * const argv[]) 
00125     : Param(key,Param::BOOL,argc,argv) {}
00126     bool found() const    { return Param::found(); }
00127     operator bool() const { return Param::found(); }
00128     Param::on;      // base class member access adjustment
00129 };
00130 
00131 //: Catches a given Unsigned parameter
00132 class UnsignedParam : private Param
00133 {
00134 public:
00135     UnsignedParam(const char * key, int argc, const char *const argv[])
00136     : Param(key,Param::UNSIGNED,argc,argv) {}
00137     bool found() const { return Param::found() && getUnsigned()!=unsigned(-1); }
00138     operator unsigned() const { return getUnsigned();  }
00139     Param::on;     // base class member access adjustment
00140 };
00141 
00142 //: Catches a given integer parameter
00143 class IntParam : private Param
00144 {
00145 public:
00146     IntParam(const char * key, int argc, const char * const argv[])
00147     : Param(key,Param::INT,argc,argv) {}
00148     bool found()   const { return Param::found();   }
00149     operator int() const { return getInt();  }
00150     Param::on;      // base class member access adjustment
00151 };
00152 
00153 //: Catches a given double parameter
00154 class DoubleParam : private Param
00155 {
00156 public:
00157     DoubleParam(const char * key, int argc, const char * const argv[])
00158     : Param(key,Param::DOUBLE,argc,argv) {}
00159     bool found() const { return Param::found() && getDouble()!=-1.29384756657; }
00160     operator double() const { return getDouble();  }
00161     Param::on;      // base class member access adjustment
00162 };
00163 
00164 //: Catches a given string parameter
00165 class StringParam : private Param
00166 {
00167 public:
00168     StringParam(const char * key, int argc, const char * const argv[])
00169     : Param(key,Param::STRING,argc,argv) {}
00170     bool found()     const       
00171           { return Param::found() && strcmp(getString(),"Uninitialized"); }
00172     operator const char*() const  { return getString();  }
00173     Param::on;      // base class member access adjustment
00174 };
00175 
00176 #endif

Generated on Mon Apr 25 01:09:24 2005 for Parquete by doxygen 1.3.2