944cd19aa410fa0006493bc3bf20e472af62ed0c
[oota-llvm.git] / include / llvm / Support / ProgramOptions.h
1 // $Id$ -*-c++-*-
2 //***************************************************************************
3 //
4 // File:
5 //      ProgramOptions.h
6 //
7 // Purpose:
8 //      A representation of options for any program.
9 //
10 // History:
11 //      08/08/95 - adve  - Created in the dHPF compiler
12 //      10/10/96 - mpal, dbaker - converted to const member functions.
13 //      10/19/96 - meven - slightly changed interface to accomodate 
14 //                         arguments other than -X type options
15 //      07/15/01 - vadve - Copied to LLVM system and modified
16 //
17 //**************************************************************************/
18
19 #ifndef LLVM_SUPPORT_PROGRAMOPTIONS_H
20 #define LLVM_SUPPORT_PROGRAMOPTIONS_H
21
22 #include "llvm/Support/Unique.h"
23 #include <vector>
24 #include <hash_map>
25
26 template <> struct hash<string> {
27   size_t operator()(string const &str) const {
28     return hash<char const *>()(str.c_str());
29   }
30 };
31
32 class ProgramOption;
33
34 //---------------------------------------------------------------------------
35 //
36 //  Class: ProgramOptions
37 //
38 //  Base Classes: none
39 //
40 //  Class Data Members:
41 //      ProgramOptionsRepr*     Internal representation of program options,
42 //                              accessible to derived classes.
43 //  Purpose:
44 //     Base class for representing the set of options for a program.
45 //
46 //---------------------------------------------------------------------------
47
48 class ProgramOptions: public Unique {
49 public:
50   /*ctor*/      ProgramOptions  (int _argc,
51                                  const char* _argv[],
52                                  const char* _envp[]);
53   /*dtor*/      ~ProgramOptions ()      {}      
54   
55   //--------------------------------------------------------------------
56   // Retrieving different kinds of arguments.
57   // The required argument is specified by the optionString.
58   //--------------------------------------------------------------------
59     
60   const char*   StringOptionValue(const char* optionString) const;
61   bool          FlagOptionValue  (const char* optionString) const;
62   double        RealOptionValue  (const char* optionString) const;
63   int           IntOptionValue   (const char* optionString) const;
64   
65   bool          OptionSpecified  (const char* optionString) const;
66     
67   //--------------------------------------------------------------------
68   // The name used to invoke this program.
69   //--------------------------------------------------------------------
70   const char* ProgramName        () const;
71     
72   //--------------------------------------------------------------------
73   // Access to unparsed arguments
74   //--------------------------------------------------------------------
75   int         NumberOfOtherOptions() const;
76   const char* OtherOption(int i) const;
77
78   //--------------------------------------------------------------------
79   // Access to the original arguments
80   //--------------------------------------------------------------------
81   const char**          GetOriginalArgs() const;
82   void PrintArgs(ostream &out) const;
83
84   //--------------------------------------------------------------------
85   // Derived classes may use PrintOptions in their own PrintUsage() fct 
86   // to print information about optional, required, or additional
87   // arguments 
88   //--------------------------------------------------------------------
89   virtual void PrintOptions    (ostream& stream) const;
90   virtual void Usage           () const;
91
92   //--------------------------------------------------------------------
93   // Generate a human-friendly description of the options actually set.
94   // The vector returned contains a multiple of 3 of entries, entry 3n is
95   // the name of the option, entry 3n + 1 contains the description of
96   // the option and entry 3n + 2 contains the ascii value of the option.
97   // All entries are allocated using malloc and can be freed with 'free'.
98   //--------------------------------------------------------------------
99   virtual vector<string> GetDescription () const;
100   
101 protected:
102   //--------------------------------------------------------------------
103   // Called by the subclass to register each possible option
104   // used by the program.  Assumes ownership of the ProgramOption.
105   //--------------------------------------------------------------------
106   void                          Register        (ProgramOption* option);
107
108   //--------------------------------------------------------------------
109   // Parses the options.
110   //--------------------------------------------------------------------
111   void  ParseArgs       (int argc,
112                          const char* argv[],
113                          const char* envp[]);
114   
115   inline ProgramOption* OptionHandler(const string &optString) {
116     hash_map<string, ProgramOption*>::iterator hp = 
117       optionRegistry.find(optString);
118     return (hp != optionRegistry.end()) ? hp->second : 0;
119   }
120   inline const ProgramOption* OptionHandler(const string &optString) const {
121     hash_map<string, ProgramOption*>::const_iterator hp = 
122       optionRegistry.find(optString);
123     return (hp != optionRegistry.end()) ? hp->second : 0;
124   }
125 protected:
126   //--------------------------------------------------------------------
127   // Functions that must be overridden by the subclass.
128   //--------------------------------------------------------------------
129   
130   virtual void  ParseExtraArgs  () = 0; // called after successful ParseArgs
131   
132   virtual void  PrintUsage      (ostream& stream) const = 0;
133   
134 protected:
135   hash_map<string, ProgramOption*> optionRegistry;
136   int                   argc;
137   const char**          argv;
138   const char**          envp;
139   int                   argsConsumed;
140 };
141
142 //**************************************************************************/
143
144 #endif