Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/shell/shell_command.h @ 5637

Last change on this file since 5637 was 5637, checked in by bensch, 18 years ago

orxonox/trunk: using Executor successfully (but with runtime-errors)

File size: 6.5 KB
Line 
1/*!
2 * @file shell_command.h
3 * Definition of a on-screen-shell
4 */
5
6#ifndef _SHELL_COMMAND_H
7#define _SHELL_COMMAND_H
8
9#include "base_object.h"
10
11#include "helper_functions.h"
12#include "multi_type.h"
13#include "substring.h"
14#include "functor_list.h"
15#include "executor/executor.h"
16#include <stdarg.h>
17
18#define     SHELL_COMMAND_MAX_SIZE      //!< The maximum size of a Shell Command
19
20
21
22// FORWARD DECLARATION
23template<class T> class tList;
24
25/**
26 * an easy to use Macro to create a Command
27 * @param command the name of the command (without "" around the string)
28 * @param class the name of the class to apply this command to (without the "" around the string)
29 * @param function the function to call
30 *
31 * MEANING:
32 *  ShellCommand* someUniqueVarName =
33 *       ShellCommand<ClassName>::registerCommand("commandNameInShell", "ClassName", &ClassName::FunctionToCall);
34 *
35 * In the Shell you would call this Command using:
36 * $ ClassName [ObjectName] commandNameInShell [parameters]
37 */
38//#define SHELL_COMMAND(command, class, function) \
39//        ShellCommand* shell_command_##class##_##command = ShellCommand<class>::registerCommand(#command, #class, &class::function)
40#define SHELL_COMMAND(command, class, function) \
41           ShellCommand* shell_command_##class##_##command = ShellCommand::registerCommand(#command, #class, new ExecutorObjective<class>(&class::function))
42
43/**
44 * an easy to use Macro to create a Command
45 * @param command the name of the command (without "" around the string)
46 * @param class the name of the class to apply this command to (without the "" around the string)
47 * @param function the function to call
48 *
49 * MEANING:
50 *  ShellCommand* someUniqueVarName =
51 *       ShellCommand<ClassName>::registerCommand("commandNameInShell", "ClassName", &ClassName::FunctionToCall);
52 *
53 * In the Shell you would call this Command using:
54 * $ ClassName [ObjectName] commandNameInShell [parameters]
55 */
56#define SHELL_COMMAND_STATIC(command, class, function) \
57           ShellCommand* shell_command_##class##_##command = ShellCommand::registerCommand(#command, #class, new ExecutorStatic<class>(function))
58
59
60//! an enumerator for the definition of the Type.
61typedef enum {
62  ShellCommand_Objective = 1,
63  ShellCommand_Static    = 2,
64} ShellCommand_Type;
65
66////////////////
67// BASE CLASS //
68////////////////
69class ShellCommand;
70class ShellCommandAlias;
71
72//! A class to hold all Classes that have (once) registered Commands.
73class ShellCommandClass : public BaseObject
74{
75  friend class ShellCommand;
76
77  public:
78    /** @returns the CommandClassList */
79    static const tList<ShellCommandClass>* getCommandClassList() { return ShellCommandClass::commandClassList; };
80    static bool getCommandListOfClass(const char* className, tList<const char>* stringList);
81    static bool getCommandListOfAlias(tList<const char>* aliasList);
82
83    static ShellCommandClass* getCommandClass(const char* className);
84    static void unregisterAllCommands();
85
86    static void help (const char* className);
87
88  private:
89    ShellCommandClass(const char* className);
90    ~ShellCommandClass();
91
92    static const ShellCommandClass* isRegistered(const char* className);
93    static void initCommandClassList();
94
95  private:
96    const char*                      className;                 //!< The Name of the Class. This should match the ClassName of the Commands Class.
97    long                             classID;                   //!< The classID of this Class
98    tList<ShellCommand>*         commandList;               //!< A list of Commands from this Class
99    static tList<ShellCommandClass>* commandClassList;          //!< A list of Classes
100    static tList<ShellCommandAlias>* aliasList;                 //!< An Alias to A Command. (only for classes with one Instance)
101};
102
103
104//! a baseClass for all possible ShellCommands
105class ShellCommand : public BaseObject
106{
107  friend class ShellCommandClass;
108  public:
109    static bool execute (const char* executionString);
110
111    ShellCommand* describe(const char* description);
112    ShellCommand* setAlias(const char* alias);
113    ShellCommand* defaultValues(unsigned int count, ...);
114
115    static ShellCommand* registerCommand(const char* commandName, const char* className, Executor* executor);
116
117    static void unregisterCommand(const char* commandName, const char* className);
118
119    static void debug();
120
121  protected:
122    ShellCommand(const char* commandName, const char* className, Executor* executor);
123    ~ShellCommand();
124
125    /** @returns the Type of this Function (either static or objective) */
126    inline ShellCommand_Type getType() { return this->functorType; };
127
128    static bool isRegistered(const char* commandName, const char* className, Executor* executor);
129    static const char* paramToString(long parameter);
130
131  protected:
132    ShellCommand_Type                functorType;                          //!< The type of Function we've got (either static or objective).
133    void*                            functionPointer;                      //!< The pointeer to the function of the Class (or static pointer if ClassID == CL_NULL )
134    unsigned int                     paramCount;                           //!< the count of parameters.
135    unsigned int*                    parameters;                           //!< Parameters the function of this Command takes.
136    MultiType*                       defaultValue;                         //!< Default Values.
137
138  private:
139    ShellCommandClass*               shellClass;                           //!< A Pointer to the Shell-Class this Command belongs to.
140    ShellCommandAlias*               alias;                                //!< An Alias for the Class.
141
142    const char*                      description;                          //!< A description for this commnand. (initially NULL). Assigned with (create)->describe("blablabla");
143    Executor*                        executor;
144
145};
146
147//! A Class, that handles aliases.
148class ShellCommandAlias
149{
150  friend class ShellCommand;
151  public:
152    /** @returns the Name of the Alias. */
153    const char* getName() const { return this->aliasName; };
154    /** @returns the Command, this Alias is asociated with */
155    ShellCommand* getCommand() const { return this->command; };
156
157  private:
158    /** @param aliasName the name of the Alias @param command the Command, to associate this alias with */
159    ShellCommandAlias(const char* aliasName, ShellCommand* command) { this->aliasName = aliasName; this->command = command; };
160
161  private:
162    const char*         aliasName;       //!< the name of the Alias
163    ShellCommand*   command;         //!< a pointer to the command, this alias executes.
164};
165
166#endif /* _SHELL_COMMAND_H */
Note: See TracBrowser for help on using the repository browser.