Class to handle a loose connection between user input and the program logic. (Documentation is not ready yet)

Dependents:   Sensor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CommandHandler.h Source File

CommandHandler.h

00001 #ifndef COMMANDHANDLER_H_INCLUDED
00002 #define COMMANDHANDLER_H_INCLUDED
00003 
00004 /** Base class for every command
00005 */
00006 class CommandBase
00007 {
00008 public:
00009     // Type definitions
00010     typedef uint8_t IDType;
00011     typedef uint32_t ParameterType;
00012     typedef bool(*CallbackType)(ParameterType);
00013 
00014     CommandBase(IDType id)
00015         : id_(id), nextCommand_(NULL)
00016     {}
00017 
00018     CommandBase * next()
00019     {
00020         return nextCommand_;
00021     }
00022     void setNext(CommandBase * command)
00023     {
00024         nextCommand_ = command;
00025     }
00026     IDType id()
00027     {
00028         return id_;
00029     }
00030     void operator()(ParameterType param)
00031     {
00032         exec(param);
00033     }
00034     virtual bool exec(ParameterType param) = 0;
00035 private:
00036 
00037     IDType id_;
00038     CommandBase * nextCommand_;
00039 };
00040 
00041 /** Class to create a command that points to a class member.
00042  *
00043  */
00044 template <typename T>
00045 class MemberCommand : public CommandBase
00046 {
00047 public:
00048      MemberCommand(CommandBase::IDType id, T* target, bool (T::*func)(CommandBase::ParameterType))
00049         : CommandBase(id), target_(target), function_(func)
00050      {}
00051 
00052      bool exec(ParameterType param)
00053      {
00054         return (target_->*function_)(param);
00055      }
00056 
00057 private:
00058      T * target_;
00059      bool (T::*function_)(CommandBase::ParameterType);
00060 };
00061 
00062 /** Class to create a command that points to function.
00063  *
00064  */
00065 class FunctionCommand : public CommandBase
00066 {
00067 public:
00068      FunctionCommand(CommandBase::IDType id, CommandBase::CallbackType func)
00069         :  CommandBase(id), function_(func)
00070      {}
00071 
00072      bool exec(ParameterType param)
00073      {
00074         return function_(param);
00075      }
00076 
00077 private:
00078      CommandBase::CallbackType function_;
00079 };
00080 
00081 /** Class that manages the commands
00082 */
00083 class CommandHandler
00084 {
00085 public:
00086     CommandHandler()
00087         : firstCommand_(NULL), lastCommand_(NULL)
00088     {}
00089     void addCommand(CommandBase * command)
00090     {
00091         if (firstCommand_ == NULL)
00092         {
00093             firstCommand_ = command;
00094             lastCommand_ = command;
00095         }
00096         else
00097         {
00098             lastCommand_->setNext(command);
00099             lastCommand_ = command;
00100         }
00101     }
00102     void handleCommand(CommandBase::IDType id, CommandBase::ParameterType param)
00103     {
00104         CommandBase * cmd = firstCommand_;
00105         while (cmd != 0)
00106         {
00107             if (cmd->id() == id && cmd->exec(param))
00108             {
00109                 return;
00110             }
00111             cmd = cmd->next();
00112         }
00113     }
00114 private:
00115     CommandBase * nextCommand();
00116     CommandBase * firstCommand_;
00117     CommandBase * lastCommand_;
00118 };
00119 
00120 #endif /* COMMANDHANDLER_H_INCLUDED */