Command.h Source File

Back to the index.

Command.h
Go to the documentation of this file.
1 #ifndef COMMAND_H
2 #define COMMAND_H
3 
4 /*
5  * Copyright (C) 2008-2010 Anders Gavare. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  * derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include "misc.h"
32 
33 #include "UnitTest.h"
34 
35 class GXemul;
36 
37 
38 /**
39  * \brief A %Command is a named function, executed by the CommandInterpreter.
40  *
41  * To implement a new %Command, simply create the corresponding .h file
42  * in src/include/commands/ and a .cc file in src/main/commands/ (e.g. by
43  * copying from a similar command), and add the name of the command's .o
44  * file to src/main/commands/Makefile.skel. (The configure script takes care
45  * of adding all commands to the CommandInterpreter, so there is no need
46  * to manually "register" the command anywhere.)
47  */
48 class Command
49  : public ReferenceCountable
50  , public UnitTestable
51 {
52 public:
53  /**
54  * \brief Constructs a %Command.
55  *
56  * @param name The command's name. This should be a unique lower-case
57  * string, consisting only of letters a-z.
58  * @param argumentFormat A string describing the command's arguments.
59  * May be empty, if the command takes no arguments.
60  */
61  Command(const string& name, const string& argumentFormat);
62 
63  virtual ~Command() = 0;
64 
65  /**
66  * \brief Gets the name of the command.
67  *
68  * @return The name of the command.
69  */
70  const string& GetCommandName() const
71  {
72  return m_name;
73  }
74 
75  /**
76  * \brief Gets the argument format for the command.
77  *
78  * @return The argument format for the command.
79  */
80  const string& GetArgumentFormat() const
81  {
82  return m_argumentFormat;
83  }
84 
85  /**
86  * \brief Executes the command on a given GXemul instance.
87  *
88  * @param gxemul A reference to the GXemul instance.
89  * @param arguments A vector of zero or more string arguments.
90  * @return true if the command succeeded, false if it failed.
91  */
92  virtual bool Execute(GXemul& gxemul,
93  const vector<string>& arguments) = 0;
94 
95  /**
96  * \brief Returns whether the command can be quickly re-executed using
97  * an empty command line.
98  *
99  * Typical examples are: step, cpu disassembly, and memory dump,
100  * where re-executing the command without arguments is very natural.
101  *
102  * @return true if the command may be re-executed without arguments,
103  * false otherwise.
104  */
105  virtual bool MayBeReexecutedWithoutArgs() const
106  {
107  return false;
108  }
109 
110  /**
111  * \brief Returns a short (one-line) description of the command.
112  *
113  * @return A short description of the command.
114  */
115  virtual string GetShortDescription() const = 0;
116 
117  /**
118  * \brief Returns a long description/help message for the command.
119  *
120  * @return A long description/help message for the command.
121  */
122  virtual string GetLongDescription() const = 0;
123 
124 
125  /********************************************************************/
126 
127  static void RunUnitTests(int& nSucceeded, int& nFailures);
128 
129 private:
130  string m_name;
131  string m_argumentFormat;
132 };
133 
134 
135 typedef map< string,refcount_ptr<Command> > Commands;
136 
137 
138 #endif // COMMAND_H
Commands
map< string, refcount_ptr< Command > > Commands
Definition: Command.h:135
Command::GetArgumentFormat
const string & GetArgumentFormat() const
Gets the argument format for the command.
Definition: Command.h:80
GXemul
The main emulator class.
Definition: GXemul.h:54
Command
A Command is a named function, executed by the CommandInterpreter.
Definition: Command.h:48
Command::Command
Command(const string &name, const string &argumentFormat)
Constructs a Command.
Definition: Command.cc:32
Command::MayBeReexecutedWithoutArgs
virtual bool MayBeReexecutedWithoutArgs() const
Returns whether the command can be quickly re-executed using an empty command line.
Definition: Command.h:105
Command::~Command
virtual ~Command()=0
Definition: Command.cc:39
misc.h
UnitTest.h
Command::GetCommandName
const string & GetCommandName() const
Gets the name of the command.
Definition: Command.h:70
Command::GetLongDescription
virtual string GetLongDescription() const =0
Returns a long description/help message for the command.
Command::Execute
virtual bool Execute(GXemul &gxemul, const vector< string > &arguments)=0
Executes the command on a given GXemul instance.
UnitTestable
Base class for unit testable classes.
Definition: UnitTest.h:74
ReferenceCountable
Base class for reference countable objects.
Definition: refcount_ptr.h:62
Command::GetShortDescription
virtual string GetShortDescription() const =0
Returns a short (one-line) description of the command.
Command::RunUnitTests
static void RunUnitTests(int &nSucceeded, int &nFailures)

Generated on Tue Mar 24 2020 14:04:48 for GXemul by doxygen 1.8.17