a2502e487e7004604810304f0f92880ef69ff3a9
[oota-llvm.git] / include / llvm / Support / ProgramOption.h
1 // $Id$ -*-c++-*-
2 //***************************************************************************
3 //
4 // File:
5 //    ProgramOption.h
6 //
7 // Purpose:
8 //    General representations for a program option.
9 //
10 // History:
11 //    08/08/95 - adve  - created in the dHPF compiler
12 //    11/26/96 - adve  - EvalOpt now returns #args consumed, or -1 for error
13 //    07/15/01 - vadve - Copied to LLVM system and modified
14 //
15 //**************************************************************************/
16
17 #ifndef LLVM_SUPPORT_PROGRAMOPTION_H
18 #define LLVM_SUPPORT_PROGRAMOPTION_H
19
20 //************************** System Include Files **************************/
21
22 #include <string>
23
24 //*************************** User Include Files ***************************/
25
26 #include "llvm/Support/Unique.h"
27 #include "llvm/Support/StringUtils.h"
28
29 //********************** Local Variable Definitions ************************/
30
31 class ProgramOption: public Unique {
32 public:
33   /*ctor*/      ProgramOption   (const char* _argString,
34                                  const char* _helpMesg,
35                                  int _minExpectedArgs = 1)
36                         : optionSpecified(false),
37                           argString(_argString),
38                           helpMesg(_helpMesg),
39                           minExpectedArgs(_minExpectedArgs) {}
40     
41   /*dtor*/ virtual ~ProgramOption() {}
42   
43   // Pure virtual function for an option with 0 or more arguments.
44   // `optarg' points to the start of the next word in argv[].
45   // It will be NULL if there are no more words.
46   // The return value indicates the number of words of argv[] that
47   // were consumed by EvalOpt and should be discarded.
48   // A return value of -1 indicates an error.
49   // 
50   virtual int           EvalOpt         (const char* optarg) = 0;
51
52   // Returns the value associated with the option as a human-readable
53   // string.  The memory returned is allocated via `malloc'.
54   virtual char*         GetTextValue    () const = 0;
55   
56   // Inline accessor functions for common option information
57   // 
58   bool                  OptionSpecified () const { return optionSpecified; }
59   const char*           ArgString       () const { return argString.c_str(); }
60   const char*           HelpMesg        () const { return helpMesg.c_str(); }
61   int                   MinExpectedArgs () const { return minExpectedArgs; }
62   
63 protected:
64   bool   optionSpecified;
65   string argString;
66   string helpMesg;
67   int    minExpectedArgs;
68 };
69
70 //**************************************************************************/
71
72 class StringOption : public ProgramOption {
73 public:
74   /*ctor*/              StringOption    (const char* argString,
75                                          const char* helpMesg,
76                                          const char* initValue = "", 
77                                          bool append = false);
78         // append = false:  EvalOpt will overwrite preexisting value 
79         // append = true :  EvalOpt will append <optArg> to value 
80   
81   /*dtor*/ virtual      ~StringOption   () {}
82   
83   virtual int           EvalOpt         (const char* optarg);
84   
85   const char*           Value           () const { return value.c_str(); }
86   virtual char*         GetTextValue    () const { return strdup(Value()); }
87   
88 protected:
89   string value;
90   bool  append; 
91 };
92
93 //**************************************************************************/
94
95 // -<flag_opt>   sets the flag to TRUE
96 // -<flag_opt> 0 sets the flag to FALSE
97 // 
98 // To provide an actual argument (not option) of "0", mark the
99 // end of the options with "--" (see getopt(1)).
100
101 class FlagOption : public ProgramOption {
102 public:
103   /*ctor*/              FlagOption      (const char* argString,
104                                          const char* helpMesg,
105                                          bool initValue = false);
106     
107   /*dtor*/ virtual      ~FlagOption     () {}
108     
109   virtual int           EvalOpt         (const char* optarg);
110     
111   bool                  Value           () const { return value; }
112   virtual char*         GetTextValue    () const { return strdup(
113                                                     value ? "true" : "false");}
114 private:
115   bool  value;
116 };
117
118 //**************************************************************************/
119
120 class RealValuedOption : public ProgramOption {
121 public:
122   /*ctor*/              RealValuedOption(const char* argString,
123                                          const char* helpMesg,
124                                          double initValue = 0.0);
125   /*dtor*/ virtual      ~RealValuedOption() {}
126     
127   virtual int           EvalOpt         (const char* optarg);
128   
129   double                Value           () const { return value; }
130   virtual char*         GetTextValue    () const;
131     
132 private:
133   double        value;
134 };
135
136 //**************************************************************************/
137
138 class IntegerValuedOption : public RealValuedOption {
139 public:
140   /*ctor*/              IntegerValuedOption(const char* argString,
141                                             const char* helpMesg,
142                                             int initValue = 0);
143   /*ctor*/ virtual      ~IntegerValuedOption() {}
144   
145   int                   Value           () const;
146   virtual char*         GetTextValue    () const;
147 };
148
149 //**************************************************************************/
150
151 #endif