3232#include " RemoteCommandHandler.h"
3333#include < vector>
3434#include < map>
35+ #include < functional>
3536
3637/* * Handle command of server side application
3738 *
@@ -56,9 +57,9 @@ class RemoteCommandHandlerTemplate : public IRemoteCommandHandler
5657 *
5758 * @return the command execution status, @see CommandStatus
5859 */
59- typedef CommandStatus (CommandParser::* RemoteCommandParser)( const IRemoteCommand&
60- remoteCommand ,
61- std::string& strResult) ;
60+ using RemoteCommandParser = std::function<CommandStatus(CommandParser&,
61+ const IRemoteCommand& ,
62+ std::string&)> ;
6263
6364 /* * Parser item definition */
6465 class RemoteCommandParserItem
@@ -119,7 +120,7 @@ class RemoteCommandHandlerTemplate : public IRemoteCommandHandler
119120 return false ;
120121 }
121122
122- switch ((commandParser.* mParser )( remoteCommand, result)) {
123+ switch (mParser (commandParser, remoteCommand, result)) {
123124 case EDone:
124125 result = " Done" ;
125126 // Fall through intentionally
@@ -158,9 +159,18 @@ class RemoteCommandHandlerTemplate : public IRemoteCommandHandler
158159 * @param remoteCommandParserItems supported command parser items
159160 */
160161 RemoteCommandHandlerTemplate (CommandParser& commandParser,
161- const RemoteCommandParserItems& remoteCommandParserItems) :
162+ RemoteCommandParserItems& remoteCommandParserItems) :
162163 _commandParser (commandParser), _remoteCommandParserItems(remoteCommandParserItems)
163164 {
165+ /* Add the help command and specialize the help function
166+ * to match RemoteCommandParser prototype
167+ */
168+ _remoteCommandParserItems.emplace (
169+ " help" ,
170+ RemoteCommandParserItem (
171+ [this ](CommandParser&,
172+ const IRemoteCommand&, std::string& result){ return help (result); },
173+ 0 , " " , " Show commands description and usage" ));
164174 }
165175
166176private:
@@ -181,14 +191,6 @@ class RemoteCommandHandlerTemplate : public IRemoteCommandHandler
181191 return remoteCommandParserItem.parse (_commandParser, remoteCommand, result);
182192 }
183193 catch (const std::out_of_range&) {
184-
185- if (remoteCommand.getCommand () == helpCommand) {
186-
187- help (result);
188-
189- return true ;
190- }
191-
192194 // Not found
193195 result = " Command not found!\n Use \" help\" to show available commands" ;
194196
@@ -199,12 +201,14 @@ class RemoteCommandHandlerTemplate : public IRemoteCommandHandler
199201 /* * Format help display
200202 *
201203 * @param result the formatted help string
204+ *
205+ * @return ESucceeded command status
202206 */
203- void help (std::string& result)
207+ CommandStatus help (std::string& result)
204208 {
205209 struct Help { std::string usage; std::string description; };
206- std::vector<Help> helps{ { helpCommand, helpCommandDescription } } ;
207- size_t maxUsage = helpCommand. length () ;
210+ std::vector<Help> helps;
211+ size_t maxUsage = 0 ;
208212
209213 for (auto & item : _remoteCommandParserItems) {
210214 std::string usage = item.first + ' ' + item.second .getHelp ();
@@ -216,24 +220,12 @@ class RemoteCommandHandlerTemplate : public IRemoteCommandHandler
216220 help.usage .resize (maxUsage, ' ' );
217221 result += help.usage + " => " + help.description + ' \n ' ;
218222 }
223+ return CommandStatus::ESucceeded;
219224 }
220225
221- /* * Help command name */
222- static const std::string helpCommand;
223-
224- /* * Help command description */
225- static const std::string helpCommandDescription;
226-
227226 /* * Command parser used during command during command handling */
228227 CommandParser& _commandParser;
229228
230229 /* * Remote command parser map */
231- const RemoteCommandParserItems& _remoteCommandParserItems;
230+ RemoteCommandParserItems& _remoteCommandParserItems;
232231};
233-
234- template <typename CommandParser>
235- const std::string RemoteCommandHandlerTemplate<CommandParser>::helpCommand = " help" ;
236-
237- template <typename CommandParser>
238- const std::string RemoteCommandHandlerTemplate<CommandParser>::helpCommandDescription =
239- " Show commands description and usage" ;
0 commit comments