]> git.tdb.fi Git - gldbg.git/blob - source/commandinterpreter.h
fa881c6883be885b6e2cc03974448e68783766fd
[gldbg.git] / source / commandinterpreter.h
1 #ifndef COMMANDINTERPRETER_H_
2 #define COMMANDINTERPRETER_H_
3
4 #include <map>
5 #include <string>
6
7 class GlDbg;
8
9 class CommandInterpreter
10 {
11 public:
12         struct Command
13         {
14         private:
15                 std::string description;
16                 std::string help;
17
18         protected:
19                 Command() { }
20         public:
21                 virtual ~Command() { }
22
23                 void set_help(const std::string &);
24                 void set_help(const std::string &, const std::string &);
25                 const std::string &get_description() const { return description; }
26                 const std::string &get_help() const { return help; }
27
28                 virtual void execute(const std::string &) = 0;
29         };
30
31 private:
32         template<typename T>
33         class CommandFunction: public Command
34         {
35         private:
36                 typedef void (T::*Func)(const std::string &);
37
38                 T *obj;
39                 Func func;
40         public:
41                 CommandFunction(T *o, Func f): obj(o), func(f) { }
42
43                 virtual void execute(const std::string &a)
44                 { (obj->*func)(a); }
45         };
46
47         class CommandAlias: public Command
48         {
49         private:
50                 Command *target;
51
52         public:
53                 CommandAlias(Command *);
54
55                 const Command *get_target() const { return target; }
56
57                 virtual void execute(const std::string &);
58         };
59
60         typedef std::map<std::string, Command *> CommandMap;
61
62         GlDbg &gldbg;
63         CommandMap commands;
64
65 public:
66         CommandInterpreter(GlDbg &);
67
68         template<typename T>
69         Command &register_command(const std::string &n, T *o, void (T::*f)(const std::string &))
70         { return *(commands[n] = new CommandFunction<T>(o, f)); }
71
72         void execute(const std::string &);
73
74 private:
75         void cmd_help(const std::string &);
76         void cmd_run(const std::string &);
77         void cmd_break(const std::string &);
78         void cmd_unbreak(const std::string &);
79         void cmd_next(const std::string &);
80         void cmd_finish(const std::string &);
81         void cmd_continue(const std::string &);
82         void cmd_signal(const std::string &);
83         void cmd_kill(const std::string &);
84         void cmd_exit(const std::string &);
85 };
86
87 #endif