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