Initial checkin of the LLVM Source-Level Debugger. This is incomplete, but
[oota-llvm.git] / tools / llvm-db / CLICommand.h
1 //===- CLICommand.h - Classes used to represent commands --------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a small class hierarchy used to represent the various types
11 // of commands in the CLI debugger front-end.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef CLICOMMAND_H
16 #define CLICOMMAND_H
17
18 #include <string>
19 #include <vector>
20
21 namespace llvm {
22   class CLIDebugger;
23
24   /// CLICommand - Base class of the hierarchy, used to provide the abstract
25   /// interface common to all commands.
26   ///
27   class CLICommand {
28     /// ShortHelp, LongHelp - The short and long helps strings printed for the
29     /// command.  The ShortHelp string should be a single line of text without a
30     /// newline.  The LongHelp string should be a full description with
31     /// terminating newline.
32     std::string ShortHelp, LongHelp;
33
34     /// RefCount - This contains the number of entries in the CLIDebugger
35     /// CommandTable that points to this command.
36     unsigned RefCount;
37
38     /// OptionNames - This contains a list of names for the option.  Keeping
39     /// track of this is done just to make the help output more helpful.
40     ///
41     std::vector<std::string> OptionNames;
42   public:
43     CLICommand(const std::string &SH, const std::string &LH)
44       : ShortHelp(SH), LongHelp(LH), RefCount(0) {}
45
46     virtual ~CLICommand() {}
47
48     /// addRef/dropRef - Implement a simple reference counting scheme to make
49     /// sure we delete commands that are no longer used.
50     void addRef() { ++RefCount; }
51     void dropRef() {
52       if (--RefCount == 0) delete this;
53     }
54
55     /// getPrimaryOptionName - Return the first name the option was added under.
56     /// This is the name we report for the option in the help output.
57     std::string getPrimaryOptionName() const {
58       return OptionNames.empty() ? "" : OptionNames[0];
59     }
60
61     /// getOptionName - Return all of the names the option is registered as.
62     ///
63     const std::vector<std::string> &getOptionNames() const {
64       return OptionNames;
65     }
66
67     /// addOptionName - Add a name that this option is known as.
68     ///
69     void addOptionName(const std::string &Name) {
70       OptionNames.push_back(Name);
71     }
72
73     /// removeOptionName - Eliminate one of the names for this option.
74     ///
75     void removeOptionName(const std::string &Name) {
76       unsigned i = 0; 
77       for (; OptionNames[i] != Name; ++i)
78         assert(i+1 < OptionNames.size() && "Didn't find option name!");
79       OptionNames.erase(OptionNames.begin()+i);
80     }
81
82
83     /// getShortHelp - Return the short help string for this command.
84     ///
85     const std::string &getShortHelp() { return ShortHelp; }
86
87     /// getLongHelp - Return the long help string for this command, if it
88     /// exists.
89     const std::string &getLongHelp() { return LongHelp; }
90
91     virtual void runCommand(CLIDebugger &D, std::string &Arguments) = 0;
92   };
93
94   /// BuiltinCLICommand - This class represents commands that are built directly
95   /// into the debugger.
96   class BuiltinCLICommand : public CLICommand {
97     // Impl - Pointer to the method that implements the command
98     void (CLIDebugger::*Impl)(std::string&);
99   public:
100     BuiltinCLICommand(const std::string &ShortHelp, const std::string &LongHelp,
101                       void (CLIDebugger::*impl)(std::string&))
102       : CLICommand(ShortHelp, LongHelp), Impl(impl) {}
103     
104     void runCommand(CLIDebugger &D, std::string &Arguments) {
105       (D.*Impl)(Arguments);
106     }
107   };
108 }
109
110 #endif