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