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