Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit 1dcfbc2

Browse files
committed
Refactor command callback definition in RemoteCommandHandler
RemoteCommandHandler was using old style function pointer to keep commands. This patch introduces the use of function object. The help command is now handled as any other commands thanks to partial function definition. Signed-off-by: Jules Clero <julesx.clero@intel.com>
1 parent 4b71d26 commit 1dcfbc2

File tree

5 files changed

+27
-35
lines changed

5 files changed

+27
-35
lines changed

parameter/command/include/command/Parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class Parser
215215
//@}
216216

217217
/** Parser items getter */
218-
const CommandHandler::RemoteCommandParserItems &getRemoteParserItems() const;
218+
CommandHandler::RemoteCommandParserItems &getRemoteParserItems() const;
219219

220220
/** Parameter Manager used to delegate parsed commands */
221221
CParameterMgr& mParameterMgr;

parameter/command/src/Parser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ Parser::Parser(CParameterMgr& parameterMgr) :
4949
{
5050
}
5151

52-
const Parser::CommandHandler::RemoteCommandParserItems &Parser::getRemoteParserItems() const
52+
Parser::CommandHandler::RemoteCommandParserItems &Parser::getRemoteParserItems() const
5353
{
54-
static const CommandHandler::RemoteCommandParserItems remoteCommandParserItems = {
54+
static CommandHandler::RemoteCommandParserItems remoteCommandParserItems = {
5555
{ "version",
5656
{ &Parser::version, 0, "",
5757
"Show version" } },

remote-processor/RemoteCommandHandlerTemplate.h

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
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

166176
private:
@@ -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!\nUse \"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";

test/test-platform/command/include/command/Parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class Parser
171171
CommandReturn getter(const IRemoteCommand& remoteCommand, std::string& strResult);
172172

173173
/** Parser items map */
174-
static const CommandHandler::RemoteCommandParserItems gRemoteCommandParserItems;
174+
static CommandHandler::RemoteCommandParserItems gRemoteCommandParserItems;
175175

176176
/** Test PLatform used to delegate parsed commands */
177177
CTestPlatform& mTestPlatform;

test/test-platform/command/src/Parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace platform
4141
namespace command
4242
{
4343

44-
const Parser::CommandHandler::RemoteCommandParserItems Parser::gRemoteCommandParserItems = {
44+
Parser::CommandHandler::RemoteCommandParserItems Parser::gRemoteCommandParserItems = {
4545
{ "exit",
4646
{ &Parser::exit, 0, "",
4747
"Exit TestPlatform" } },

0 commit comments

Comments
 (0)