]> git.tdb.fi Git - gldbg.git/blobdiff - source/commandinterpreter.h
Add framework necessary to support more modular tools
[gldbg.git] / source / commandinterpreter.h
index 537245fb53c1a0f38bea5b0a652f490e97fa64cf..487abb232fcdfcd6e4cb79cc56214db0720b19cb 100644 (file)
@@ -15,29 +15,66 @@ class GlDbg;
 
 class CommandInterpreter
 {
-private:
+public:
        struct Command
        {
-               typedef void (CommandInterpreter::*Func)(const std::string &);
-
-               Func func;
+       private:
                std::string description;
                std::string help;
-               Command *alias_for;
 
-               Command();
-               Command(Command *);
-               Command(Func, const std::string &);
-               Command(Func, const std::string &, const std::string &);
+       protected:
+               Command() { }
+
+       public:
+               void set_help(const std::string &);
+               void set_help(const std::string &, const std::string &);
+               const std::string &get_description() const { return description; }
+               const std::string &get_help() const { return help; }
+
+               virtual void execute(const std::string &) = 0;
        };
 
-       typedef std::map<std::string, Command> CommandMap;
+private:
+       template<typename T>
+       class CommandFunction: public Command
+       {
+       private:
+               typedef void (T::*Func)(const std::string &);
+
+               T *obj;
+               Func func;
+       public:
+               CommandFunction(T *o, Func f): obj(o), func(f) { }
+
+               virtual void execute(const std::string &a)
+               { (obj->*func)(a); }
+       };
+
+       class CommandAlias: public Command
+       {
+       private:
+               Command *target;
+
+       public:
+               CommandAlias(Command *);
+
+               const Command *get_target() const { return target; }
+
+               virtual void execute(const std::string &);
+       };
+
+       typedef std::map<std::string, Command *> CommandMap;
 
        GlDbg &gldbg;
        CommandMap commands;
 
 public:
        CommandInterpreter(GlDbg &);
+
+       template<typename T>
+       Command &register_command(const std::string &n, T *o, void (T::*f)(const std::string &))
+       { return *(commands[n] = new CommandFunction<T>(o, f)); }
+
        void execute(const std::string &);
 
 private: