General support utilities like a program options class and a StringMap
authorVikram S. Adve <vadve@cs.uiuc.edu>
Sat, 21 Jul 2001 12:40:18 +0000 (12:40 +0000)
committerVikram S. Adve <vadve@cs.uiuc.edu>
Sat, 21 Jul 2001 12:40:18 +0000 (12:40 +0000)
class for mapping strings to arbitrary things.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@228 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/ProgramOption.h [new file with mode: 0644]
include/llvm/Support/ProgramOptions.h [new file with mode: 0644]
include/llvm/Support/StringUtils.h [new file with mode: 0644]
include/llvm/Support/Unique.h [new file with mode: 0644]

diff --git a/include/llvm/Support/ProgramOption.h b/include/llvm/Support/ProgramOption.h
new file mode 100644 (file)
index 0000000..a2502e4
--- /dev/null
@@ -0,0 +1,151 @@
+// $Id$ -*-c++-*-
+//***************************************************************************
+//
+// File:
+//    ProgramOption.h
+//
+// Purpose:
+//    General representations for a program option.
+//
+// History:
+//    08/08/95 - adve  - created in the dHPF compiler
+//    11/26/96 - adve  - EvalOpt now returns #args consumed, or -1 for error
+//    07/15/01 - vadve - Copied to LLVM system and modified
+//
+//**************************************************************************/
+
+#ifndef LLVM_SUPPORT_PROGRAMOPTION_H
+#define LLVM_SUPPORT_PROGRAMOPTION_H
+
+//************************** System Include Files **************************/
+
+#include <string>
+
+//*************************** User Include Files ***************************/
+
+#include "llvm/Support/Unique.h"
+#include "llvm/Support/StringUtils.h"
+
+//********************** Local Variable Definitions ************************/
+
+class ProgramOption: public Unique {
+public:
+  /*ctor*/     ProgramOption   (const char* _argString,
+                                const char* _helpMesg,
+                                int _minExpectedArgs = 1)
+                       : optionSpecified(false),
+                         argString(_argString),
+                         helpMesg(_helpMesg),
+                         minExpectedArgs(_minExpectedArgs) {}
+    
+  /*dtor*/ virtual ~ProgramOption() {}
+  
+  // Pure virtual function for an option with 0 or more arguments.
+  // `optarg' points to the start of the next word in argv[].
+  // It will be NULL if there are no more words.
+  // The return value indicates the number of words of argv[] that
+  // were consumed by EvalOpt and should be discarded.
+  // A return value of -1 indicates an error.
+  // 
+  virtual int          EvalOpt         (const char* optarg) = 0;
+
+  // Returns the value associated with the option as a human-readable
+  // string.  The memory returned is allocated via `malloc'.
+  virtual char*                GetTextValue    () const = 0;
+  
+  // Inline accessor functions for common option information
+  // 
+  bool                 OptionSpecified () const { return optionSpecified; }
+  const char*          ArgString       () const { return argString.c_str(); }
+  const char*          HelpMesg        () const { return helpMesg.c_str(); }
+  int                  MinExpectedArgs () const { return minExpectedArgs; }
+  
+protected:
+  bool  optionSpecified;
+  string argString;
+  string helpMesg;
+  int   minExpectedArgs;
+};
+
+//**************************************************************************/
+
+class StringOption : public ProgramOption {
+public:
+  /*ctor*/             StringOption    (const char* argString,
+                                        const char* helpMesg,
+                                        const char* initValue = "", 
+                                        bool append = false);
+       // append = false:  EvalOpt will overwrite preexisting value 
+       // append = true :  EvalOpt will append <optArg> to value 
+  
+  /*dtor*/ virtual     ~StringOption   () {}
+  
+  virtual int          EvalOpt         (const char* optarg);
+  
+  const char*          Value           () const { return value.c_str(); }
+  virtual char*                GetTextValue    () const { return strdup(Value()); }
+  
+protected:
+  string value;
+  bool append; 
+};
+
+//**************************************************************************/
+
+// -<flag_opt>  sets the flag to TRUE
+// -<flag_opt> 0 sets the flag to FALSE
+// 
+// To provide an actual argument (not option) of "0", mark the
+// end of the options with "--" (see getopt(1)).
+
+class FlagOption : public ProgramOption {
+public:
+  /*ctor*/             FlagOption      (const char* argString,
+                                        const char* helpMesg,
+                                        bool initValue = false);
+    
+  /*dtor*/ virtual     ~FlagOption     () {}
+    
+  virtual int          EvalOpt         (const char* optarg);
+    
+  bool                 Value           () const { return value; }
+  virtual char*                GetTextValue    () const { return strdup(
+                                                   value ? "true" : "false");}
+private:
+  bool value;
+};
+
+//**************************************************************************/
+
+class RealValuedOption : public ProgramOption {
+public:
+  /*ctor*/             RealValuedOption(const char* argString,
+                                        const char* helpMesg,
+                                        double initValue = 0.0);
+  /*dtor*/ virtual     ~RealValuedOption() {}
+    
+  virtual int          EvalOpt         (const char* optarg);
+  
+  double               Value           () const { return value; }
+  virtual char*                GetTextValue    () const;
+    
+private:
+  double       value;
+};
+
+//**************************************************************************/
+
+class IntegerValuedOption : public RealValuedOption {
+public:
+  /*ctor*/             IntegerValuedOption(const char* argString,
+                                           const char* helpMesg,
+                                           int initValue = 0);
+  /*ctor*/ virtual     ~IntegerValuedOption() {}
+  
+  int                  Value           () const;
+  virtual char*                GetTextValue    () const;
+};
+
+//**************************************************************************/
+
+#endif
diff --git a/include/llvm/Support/ProgramOptions.h b/include/llvm/Support/ProgramOptions.h
new file mode 100644 (file)
index 0000000..34badb5
--- /dev/null
@@ -0,0 +1,147 @@
+// $Id$ -*-c++-*-
+//***************************************************************************
+//
+// File:
+//     ProgramOptions.h
+//
+// Purpose:
+//     A representation of options for any program.
+//
+// History:
+//     08/08/95 - adve  - Created in the dHPF compiler
+//      10/10/96 - mpal, dbaker - converted to const member functions.
+//      10/19/96 - meven - slightly changed interface to accomodate 
+//                         arguments other than -X type options
+//     07/15/01 - vadve - Copied to LLVM system and modified
+//
+//**************************************************************************/
+
+#ifndef LLVM_SUPPORT_PROGRAMOPTIONS_H
+#define LLVM_SUPPORT_PROGRAMOPTIONS_H
+
+//************************** System Include Files **************************/
+
+#include <iostream.h>
+
+//*************************** User Include Files ***************************/
+
+#include "llvm/Support/Unique.h"
+#include "llvm/Support/StringUtils.h"
+
+//************************ Forward Declarations ****************************/
+
+class ProgramOption;
+
+//************************* Main Driver Routine ****************************/
+
+//---------------------------------------------------------------------------
+//
+//  Class: ProgramOptions
+//
+//  Base Classes: none
+//
+//  Class Data Members:
+//     ProgramOptionsRepr*     Internal representation of program options,
+//                             accessible to derived classes.
+//  Purpose:
+//     Base class for representing the set of options for a program.
+//
+//---------------------------------------------------------------------------
+
+class ProgramOptions: public Unique {
+public:
+  /*ctor*/     ProgramOptions  (int _argc,
+                                const char* _argv[],
+                                const char* _envp[]);
+  /*dtor*/     ~ProgramOptions ()      {}      
+  
+  //--------------------------------------------------------------------
+  // Retrieving different kinds of arguments.
+  // The required argument is specified by the optionString.
+  //--------------------------------------------------------------------
+    
+  const char*  StringOptionValue(const char* optionString) const;
+  bool         FlagOptionValue  (const char* optionString) const;
+  double       RealOptionValue  (const char* optionString) const;
+  int          IntOptionValue   (const char* optionString) const;
+  
+  bool         OptionSpecified  (const char* optionString) const;
+    
+  //--------------------------------------------------------------------
+  // The name used to invoke this program.
+  //--------------------------------------------------------------------
+  const char* ProgramName       () const;
+    
+  //--------------------------------------------------------------------
+  // Access to unparsed arguments
+  //--------------------------------------------------------------------
+  int         NumberOfOtherOptions() const;
+  const char* OtherOption(int i) const;
+
+  //--------------------------------------------------------------------
+  // Access to the original arguments
+  //--------------------------------------------------------------------
+  const char**         GetOriginalArgs() const;
+  void PrintArgs(ostream &out) const;
+
+  //--------------------------------------------------------------------
+  // Derived classes may use PrintOptions in their own PrintUsage() fct 
+  // to print information about optional, required, or additional
+  // arguments 
+  //--------------------------------------------------------------------
+  virtual void PrintOptions    (ostream& stream) const;
+  virtual void Usage           () const;
+
+  //--------------------------------------------------------------------
+  // Generate a human-friendly description of the options actually set.
+  // The vector returned contains a multiple of 3 of entries, entry 3n is
+  // the name of the option, entry 3n + 1 contains the description of
+  // the option and entry 3n + 2 contains the ascii value of the option.
+  // All entries are allocated using malloc and can be freed with 'free'.
+  //--------------------------------------------------------------------
+  virtual vector<char*> GetDescription () const;
+  
+protected:
+  //--------------------------------------------------------------------
+  // Called by the subclass to register each possible option
+  // used by the program.  Assumes ownership of the ProgramOption.
+  //--------------------------------------------------------------------
+  void                         Register        (ProgramOption* option);
+
+  //--------------------------------------------------------------------
+  // Parses the options.
+  //--------------------------------------------------------------------
+  void ParseArgs       (int argc,
+                        const char* argv[],
+                        const char* envp[]);
+  
+  inline ProgramOption* OptionHandler(const char* optString) {
+     ProgramOption** poPtr = optionRegistry.query(optString);
+     return poPtr? *poPtr : NULL;
+  }
+
+  inline const ProgramOption* OptionHandler(const char* optString) const {
+     const ProgramOption* const* poPtr = optionRegistry.query(optString);
+     return poPtr? *poPtr : NULL;
+  }
+  
+protected:
+  //--------------------------------------------------------------------
+  // Functions that must be overridden by the subclass.
+  //--------------------------------------------------------------------
+  
+  virtual void ParseExtraArgs  () = 0; // called after successful ParseArgs
+  
+  virtual void PrintUsage      (ostream& stream) const = 0;
+  
+protected:
+  StringMap<ProgramOption*> optionRegistry;
+  int                  argc;
+  const char**         argv;
+  const char**         envp;
+  int                  argsConsumed;
+};
+
+//**************************************************************************/
+
+#endif
diff --git a/include/llvm/Support/StringUtils.h b/include/llvm/Support/StringUtils.h
new file mode 100644 (file)
index 0000000..db42964
--- /dev/null
@@ -0,0 +1,75 @@
+// $Id$ -*-c++-*-
+//***************************************************************************
+//
+// File:
+//     ProgramOptions.h
+//
+// Purpose:
+//     A representation of options for any program.
+//
+// History:
+//     08/08/95 - adve  - Created in the dHPF compiler
+//      10/10/96 - mpal, dbaker - converted to const member functions.
+//      10/19/96 - meven - slightly changed interface to accomodate 
+//                         arguments other than -X type options
+//     07/15/01 - vadve - Copied to LLVM system and modified
+//
+//**************************************************************************/
+
+#ifndef LLVM_SUPPORT_PROGRAMOPTIONS_h
+#define LLVM_SUPPORT_PROGRAMOPTIONS_h
+
+//************************** System Include Files **************************/
+
+#include <string>
+#include <hash_map>
+
+//*************************** User Include Files ***************************/
+
+#include "llvm/Support/Unique.h"
+class ProgramOption;
+
+//************************ Forward Declarations ****************************/
+
+//***************************** String Functions ****************************/
+
+struct eqstr
+{
+  bool operator()(const char* s1, const char* s2) const
+  {
+    return strcmp(s1, s2) == 0;
+  }
+};
+
+//***************************** String Classes *****************************/
+
+template <class DataType>
+class StringMap:
+  public hash_map<const char*, DataType, hash<const char*>, eqstr>
+{
+public:
+  typedef hash_map<const char*, DataType, hash<const char*>, eqstr>::iterator
+       iterator;
+  typedef hash_map<const char*, DataType, hash<const char*>, eqstr>::const_iterator
+       const_iterator;
+  
+public:
+  DataType*            query(const char* _key)
+  {
+    hash_map<const char*, DataType, hash<const char*>, eqstr>::iterator
+      hashPair = this->find(_key);
+    return (hashPair == this->end())? NULL : & (*hashPair).second;
+  }
+  
+  const DataType*      query(const char* _key) const
+  {
+    hash_map<const char*, DataType, hash<const char*>, eqstr>::const_iterator
+      hashPair = this->find(_key);
+    return (hashPair == this->end())? NULL : & (*hashPair).second;
+  }
+};
+
+//**************************************************************************/
+
+#endif
+
diff --git a/include/llvm/Support/Unique.h b/include/llvm/Support/Unique.h
new file mode 100644 (file)
index 0000000..f555ce1
--- /dev/null
@@ -0,0 +1,56 @@
+//***************************************************************************
+// class Unique:
+//     Mixin class for classes that should never be copied.
+// 
+// Purpose:
+//     This mixin disables both the copy constructor and the
+//     assignment operator.  It also provides a default equality operator.
+// 
+// History:
+//     09/24/96 - vadve - Created (adapted from dHPF).
+//
+//***************************************************************************
+
+#ifndef UNIQUE_H
+#define UNIQUE_H
+
+#include <assert.h>
+
+
+class Unique
+{
+protected:
+  /*ctor*/     Unique          () {}
+  /*dtor*/ virtual ~Unique     () {}
+  
+public:
+  virtual bool operator==      (const Unique& u1) const;
+  virtual bool operator!=      (const Unique& u1) const;
+
+private:
+  // 
+  // Disable the copy constructor and the assignment operator
+  // by making them both private:
+  // 
+  /*ctor*/     Unique          (Unique&)               { assert(0); }
+  virtual Unique& operator=    (const Unique& u1)      { assert(0);
+                                                           return *this; }
+};
+
+
+// Unique object equality.
+inline bool
+Unique::operator==(const Unique& u2) const
+{
+    return (bool) (this == &u2);
+}
+
+
+// Unique object inequality.
+inline bool
+Unique::operator!=(const Unique& u2) const
+{
+    return (bool) !(this == &u2);
+}
+
+#endif