Files
hasherezade-bearparser/commander/Commander.h
T
2014-08-16 18:50:39 +02:00

85 lines
1.6 KiB
C++

#pragma once
#include <stdio.h>
#include <iostream>
#include <string>
#include <map>
#include "../parser/bearparser.h"
class Commander;
class CmdException : public CustomException
{
public:
CmdException(const QString info) : CustomException(info) {}
};
class CmdParams
{
public:
CmdParams() {}
virtual ~CmdParams() {}
};
class CmdContext
{
public:
CmdContext() : endProcessing(false) {}
virtual ~CmdContext() {}
void stopProcessing() { endProcessing = true; }
bool isEndProcessing() { return endProcessing; }
protected:
bool endProcessing;
friend class Commander;
};
class Command
{
public:
Command(std::string v_desc = "") : desc(v_desc) {}
virtual ~Command() {}
virtual std::string getDescription() { return desc; }
virtual void execute(CmdParams *params, CmdContext *context_ptr) = 0; // throws exceptions
virtual CmdParams* fetchParams(std::string stream)// throws exception
{
return NULL;
}
protected:
std::string desc;
};
class QuitCommand : public Command
{
public:
QuitCommand() : Command("Quit") {}
virtual void execute(CmdParams *params, CmdContext *context_ptr) { if (context_ptr != NULL) context_ptr->stopProcessing(); }
};
class Commander
{
public:
Commander(CmdContext *v_context);
virtual ~Commander() { clearCommands(); }
void printHelp();
bool addCommand(std::string name, Command *cmd, bool overwrite = true);
virtual void parseCommands(); // main loop
protected:
Command* getCommand(std::string name);
void clearCommands();
std::map<std::string, Command*> cmds;
CmdContext *context;
};