Components/CommandProcessing/src/CommandProcessing/Handler.h
Go to the documentation of this file.
1 /*
2  * CommandHandler.h
3  *
4  * Created on: 2 jul. 2015
5  * Author: Herman
6  */
14 #pragma once
15 
16 #include <WHashMap.h>
19 #include <Data/Buffer/LineBuffer.h>
20 #include <memory>
21 #include "Command.h"
22 
23 namespace CommandProcessing
24 {
25 constexpr size_t MAX_COMMANDSIZE = 64;
26 
31 class Handler
32 {
33 public:
37  Handler();
38 
39  Handler(ReadWriteStream* stream, bool owned = true) : outputStream(stream), ownedStream(owned)
40  {
41  }
42 
43  Handler(const Handler& rhs) = delete;
44 
46  {
47  if(ownedStream) {
48  delete outputStream;
49  }
50  }
51 
52  // I/O methods
53 
59  void setOutputStream(ReadWriteStream* stream, bool owned = true)
60  {
61  if(outputStream != nullptr && ownedStream) {
62  delete outputStream;
63  }
64 
65  outputStream = stream;
66  ownedStream = owned;
67  }
68 
70  {
71  if(outputStream == nullptr) {
72  outputStream = new MemoryDataStream();
73  ownedStream = true;
74  }
75 
76  return *outputStream;
77  }
78 
79  size_t process(char charToWrite);
80 
86  size_t process(const char* buffer, size_t size)
87  {
88  size_t retval = 0;
89  for(size_t i = 0; i < size; i++) {
90  if(process(buffer[i]) != 1) {
91  break;
92  }
93  retval++;
94  }
95  return retval;
96  }
97 
98  String processNow(const char* buffer, size_t size);
99 
100  // Command registration/de-registration methods
101 
108  bool registerCommand(Command reqDelegate);
109 
113  bool unregisterCommand(Command reqDelegate);
114 
124  void registerSystemCommands();
125 
130  Command getCommandDelegate(const String& commandString);
131 
135  bool isVerbose() const
136  {
137  return verboseMode;
138  }
139 
143  void setVerbose(bool mode)
144  {
145  verboseMode = mode;
146  }
147 
153  const String& getCommandPrompt() const
154  {
155  return currentPrompt;
156  }
157 
163  void setCommandPrompt(const String& reqPrompt)
164  {
165  currentPrompt = reqPrompt;
166  }
167 
172  char getCommandEOL() const
173  {
174  return currentEOL;
175  }
176 
181  void setCommandEOL(char reqEOL)
182  {
183  currentEOL = reqEOL;
184  }
185 
191  {
192  return currentWelcomeMessage;
193  }
194 
199  void setCommandWelcomeMessage(const String& reqWelcomeMessage)
200  {
201  currentWelcomeMessage = reqWelcomeMessage;
202  }
203 
204 private:
205  HashMap<String, Command> registeredCommands;
206  String currentPrompt;
207 #ifdef ARCH_HOST
208  char currentEOL{'\n'};
209 #else
210  char currentEOL{'\r'};
211 #endif
212  bool verboseMode{false};
213  bool localEcho{true};
214  String currentWelcomeMessage;
215 
216  ReadWriteStream* outputStream{nullptr};
217  bool ownedStream = true;
219 
220  void procesHelpCommand(String commandLine, ReadWriteStream& outputStream);
221  void procesStatusCommand(String commandLine, ReadWriteStream& outputStream);
222  void procesEchoCommand(String commandLine, ReadWriteStream& outputStream);
223  void procesDebugOnCommand(String commandLine, ReadWriteStream& outputStream);
224  void procesDebugOffCommand(String commandLine, ReadWriteStream& outputStream);
225  void processCommandOptions(String commandLine, ReadWriteStream& outputStream);
226 
227  void processCommandLine(const String& cmdString);
228 };
229 
230 } // namespace CommandProcessing
231 
const String & getCommandWelcomeMessage() const
Get the welcome message.
Definition: Components/CommandProcessing/src/CommandProcessing/Handler.h:190
HashMap class template.
Definition: WHashMap.h:41
size_t process(char charToWrite)
Read/write stream using expandable memory buffer.
Definition: MemoryDataStream.h:26
Command delegate class.
Definition: Components/CommandProcessing/src/CommandProcessing/Command.h:19
Verbose mode.
Definition: Components/CommandProcessing/src/CommandProcessing/Handler.h:31
String processNow(const char *buffer, size_t size)
The String class.
Definition: WString.h:136
const String & getCommandPrompt() const
Get the command line prompt.
Definition: Components/CommandProcessing/src/CommandProcessing/Handler.h:153
Handler(ReadWriteStream *stream, bool owned=true)
Definition: Components/CommandProcessing/src/CommandProcessing/Handler.h:39
Definition: Components/CommandProcessing/src/CommandProcessing/Command.h:16
constexpr size_t MAX_COMMANDSIZE
Definition: Components/CommandProcessing/src/CommandProcessing/Handler.h:25
bool registerCommand(Command reqDelegate)
Add a new command to the command handler.
CommandLine commandLine
bool unregisterCommand(Command reqDelegate)
Remove a command from the command handler.
void setVerbose(bool mode)
Set the verbose mode.
Definition: Components/CommandProcessing/src/CommandProcessing/Handler.h:143
void setCommandEOL(char reqEOL)
Set the end of line character.
Definition: Components/CommandProcessing/src/CommandProcessing/Handler.h:181
void registerSystemCommands()
Register default system commands.
void setCommandPrompt(const String &reqPrompt)
Set the command line prompt.
Definition: Components/CommandProcessing/src/CommandProcessing/Handler.h:163
size_t process(const char *buffer, size_t size)
Write chars to stream.
Definition: Components/CommandProcessing/src/CommandProcessing/Handler.h:86
char getCommandEOL() const
Get the end of line character.
Definition: Components/CommandProcessing/src/CommandProcessing/Handler.h:172
void setCommandWelcomeMessage(const String &reqWelcomeMessage)
Set the welcome message.
Definition: Components/CommandProcessing/src/CommandProcessing/Handler.h:199
Command getCommandDelegate(const String &commandString)
Get the command delegate for a command.
void setOutputStream(ReadWriteStream *stream, bool owned=true)
sets the output stream
Definition: Components/CommandProcessing/src/CommandProcessing/Handler.h:59
Handler()
Instantiate a CommandHandler.
ReadWriteStream & getOutputStream()
Definition: Components/CommandProcessing/src/CommandProcessing/Handler.h:69
bool isVerbose() const
Get the verbose mode.
Definition: Components/CommandProcessing/src/CommandProcessing/Handler.h:135
Base class for read/write stream.
Definition: ReadWriteStream.h:19
~Handler()
Definition: Components/CommandProcessing/src/CommandProcessing/Handler.h:45