HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
ICommand Struct Referenceabstract

Example application menu entry API. More...

#include <common.h>

+ Inheritance diagram for ICommand:
+ Collaboration diagram for ICommand:

Public Member Functions

virtual const std::string & key () const =0
 Get the command key. More...
 
virtual const std::string usage (bool format=false) const =0
 Get the command help documentation. More...
 
virtual void run (std::vector< std::string > &args)=0
 Execute the command code. More...
 

Static Public Member Functions

static void add (ICommand *command)
 Add a command to the registry. More...
 
static void remove (ICommand *command)
 Remove a command to the registry. More...
 
static void run (std::string &cmd, std::vector< std::string > &args)
 Find and run the command with the given key, using the arguments in args. More...
 
static std::ostream & help (std::ostream &stream)
 Generate the help screen, based on the commands in the registry. More...
 

Static Protected Attributes

static std::map< std::string, ICommand * > registry
 Command registry.
 

Detailed Description

Example application menu entry API.

Definition at line 37 of file apps/common.h.

Member Function Documentation

◆ add()

void ICommand::add ( ICommand command)
static

Add a command to the registry.

Parameters
[in]commandcommand to be added to the registry.

Definition at line 47 of file common.cpp.

References key(), and registry.

48 {
49  registry.insert(std::pair<std::string, ICommand *>(command->key(), command));
50 }
static std::map< std::string, ICommand * > registry
Command registry.
Definition: apps/common.h:42
virtual const std::string & key() const =0
Get the command key.
+ Here is the call graph for this function:

◆ help()

std::ostream & ICommand::help ( std::ostream &  stream)
static

Generate the help screen, based on the commands in the registry.

Parameters
[in]streamreference to the stream to print the help screen.
Returns
reference to the stream.

Definition at line 71 of file common.cpp.

References registry.

72 {
73  struct entry
74  {
75  std::string cmd;
76  std::string help;
77  };
78 
79  static std::string cache = "";
80  static uint8_t cache_count = 0;
81 
82  if (cache_count == ICommand::registry.size())
83  {
84  _stream << cache;
85  return _stream;
86  }
87 
88  std::stringstream stream;
89 
90  uint16_t size = 0;
91  std::vector<entry> entries;
92 
93  /* *INDENT-OFF* */
94  std::for_each (ICommand::registry.begin (), ICommand::registry.end (),
95  [&entries, &size](std::pair <std::string, ICommand *> e)
96  {
97  // LOG (TRACE) << "E F : " << e.first << NL;
98  // LOG (TRACE) << "E S : " << e.second << NL;
99 
100  const std::string &raw = e.second->usage ();
101 
102  char *temp = new char[raw.size ()+1];
103 
104  strncpy (temp, raw.c_str (), raw.size ())[raw.size ()] = 0;
105 
106  std::vector <char *> lines;
107 
108  char *saveptr = NULL;
109  for (char *p = strtok_r (temp, "\n", &saveptr); p != NULL; p = strtok_r (NULL, "\n", &saveptr))
110  {
111  lines.push_back (p);
112  }
113 
114  std::for_each (lines.begin (), lines.end (), [&entries, &size](char *line)
115  {
116  entry e;
117 
118  char *saveptr = NULL;
119  char *p = strtok_r (line, ":", &saveptr);
120  e.cmd = std::string (p);
121 
122  p = strtok_r (NULL, ":", &saveptr);
123  e.help = std::string (p);
124 
125  entries.push_back (e);
126 
127  if (size < e.cmd.size ())
128  {
129  size = e.cmd.size ();
130  }
131  });
132 
133  delete[] temp;
134  });
135  /* *INDENT-ON* */
136 
137  size++;
138 
139  stream << std::endl;
140 
141  stream << "================================================" << std::endl;
142  stream << "HAN-FUN Example Application : v" << HF_VERSION << std::endl;
143  stream << "================================================" << std::endl << std::endl;
144 
145  stream << std::setfill(' ');
146  /* *INDENT-OFF* */
147  std::for_each (entries.begin (), entries.end (), [&stream, size](entry &e)
148  {
149  stream << std::left << std::setw (size) << e.cmd << "\t: " << e.help << std::endl;
150  });
151  /* *INDENT-ON* */
152 
153  stream << std::left << std::setw(size) << "h/?" << "\t: " << "this help menu."
154  << std::endl << std::endl;
155  stream << "Select an Option (Q/q to exit): " << std::endl;
156 
157  cache = stream.str();
158 
159  _stream << cache;
160 
161  return _stream;
162 }
static std::map< std::string, ICommand * > registry
Command registry.
Definition: apps/common.h:42
static std::ostream & help(std::ostream &stream)
Generate the help screen, based on the commands in the registry.
Definition: common.cpp:71

◆ key()

virtual const std::string& ICommand::key ( ) const
pure virtual

Get the command key.

Returns
the string that represents the command key.

Implemented in Command.

Referenced by add(), remove(), and run().

+ Here is the caller graph for this function:

◆ remove()

void ICommand::remove ( ICommand command)
static

Remove a command to the registry.

Parameters
[in]commandcommand to be removed from the registry.

Definition at line 59 of file common.cpp.

References key(), and registry.

60 {
61  registry.erase(command->key());
62 }
static std::map< std::string, ICommand * > registry
Command registry.
Definition: apps/common.h:42
virtual const std::string & key() const =0
Get the command key.
+ Here is the call graph for this function:

◆ run() [1/2]

virtual void ICommand::run ( std::vector< std::string > &  args)
pure virtual

Execute the command code.

Parameters
[in]argsthe arguments passed to the code.

◆ run() [2/2]

void ICommand::run ( std::string &  cmd,
std::vector< std::string > &  args 
)
static

Find and run the command with the given key, using the arguments in args.

Parameters
[in]cmdthe key for the command to be run.
[in]argsvector containing the arguments to call the command with.

Definition at line 171 of file common.cpp.

References key(), LOG, and registry.

172 {
173  auto it = registry.find(key);
174 
175  if (it == registry.end())
176  {
177  LOG(ERROR) << "Command '" << key << "' not found !" << NL;
178  ICommand::help(std::cout);
179  }
180  else
181  {
182  it->second->run(args);
183  }
184 }
static std::map< std::string, ICommand * > registry
Command registry.
Definition: apps/common.h:42
#define NL
Helper define for new-line and stream clear.
Definition: debug.h:34
virtual const std::string & key() const =0
Get the command key.
#define LOG(X)
Log messages with the level given by X.
Definition: debug.h:81
static std::ostream & help(std::ostream &stream)
Generate the help screen, based on the commands in the registry.
Definition: common.cpp:71
+ Here is the call graph for this function:

◆ usage()

virtual const std::string ICommand::usage ( bool  format = false) const
pure virtual

Get the command help documentation.

Parameters
[in]formatif true the usage string SHOULD be formatted to be displayed to the user.
Returns
command help documentation.

Implemented in Command.


The documentation for this struct was generated from the following files: