HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
apps/common.h
Go to the documentation of this file.
1 // =============================================================================
16 // =============================================================================
17 
18 #ifndef HF_APP_COMMON_H
19 #define HF_APP_COMMON_H
20 
21 #include <map>
22 
23 #include "hanfun.h"
24 
30 // =============================================================================
31 // Command API
32 // =============================================================================
33 
37 struct ICommand
38 {
39  protected:
40 
42  static std::map<std::string, ICommand *> registry;
43 
44  public:
45 
51  virtual const std::string &key() const = 0;
52 
61  virtual const std::string usage(bool format = false) const = 0;
62 
68  virtual void run(std::vector<std::string> &args) = 0;
69 
75  static void add(ICommand *command);
76 
82  static void remove(ICommand *command);
83 
90  static void run(std::string &cmd, std::vector<std::string> &args);
91 
99  static std::ostream &help(std::ostream &stream);
100 };
101 
105 class Command: public ICommand
106 {
107  protected:
108 
109  const std::string _key;
110  const std::string _usage;
111 
112  public:
113 
120  Command(const char *__key, const char *__usage):
121  _key(__key), _usage(__usage)
122  {}
123 
124  const std::string &key() const
125  {
126  return _key;
127  }
128 
129  const std::string usage(bool format = false) const
130  {
131  if (format)
132  {
133  std::string result = _usage;
134 
135  for (std::string::size_type pos = 0; (pos = result.find(":", pos)) != std::string::npos;
136  pos += 3 - 1)
137  {
138  result.replace(pos, 1, " : ");
139  }
140 
141  return result;
142  }
143  else
144  {
145  return _usage;
146  }
147  }
148 };
149 
153 #define COMMAND(_name, _key, _help) \
154  struct Command_## _name: public Command \
155  { \
156  Command_##_name(): Command(_key, _help) {} \
157  \
158  void run(std::vector<std::string>&args); \
159  }; \
160  Command_##_name command##_name; \
161  void Command_##_name::run(std::vector<std::string>&args)
162 
166 #define COMMAND_ADD(_name) \
167  { \
168  ICommand::add(&command##_name); \
169  }
170 
171 // =============================================================================
172 // Parser helpers
173 // =============================================================================
174 
176 #define STRTOL(X) strtol(X.c_str(), NULL, 10);
177 
179 #define STRTOL_HEX(X) strtol(X.c_str(), NULL, 16);
180 
183 #endif /* HF_APP_COMMON_H */
const std::string _usage
Command help string.
Definition: apps/common.h:110
This is the top level include file for the HAN-FUN library.
static void add(ICommand *command)
Add a command to the registry.
Definition: common.cpp:47
const std::string & key() const
Get the command key.
Definition: apps/common.h:124
Example application menu entry API.
Definition: apps/common.h:37
const std::string _key
Command key.
Definition: apps/common.h:109
const std::string usage(bool format=false) const
Get the command help documentation.
Definition: apps/common.h:129
static std::map< std::string, ICommand * > registry
Command registry.
Definition: apps/common.h:42
virtual void run(std::vector< std::string > &args)=0
Execute the command code.
Parent class for the commands API implementations.
Definition: apps/common.h:105
virtual const std::string & key() const =0
Get the command key.
virtual const std::string usage(bool format=false) const =0
Get the command help documentation.
Command(const char *__key, const char *__usage)
Constructor.
Definition: apps/common.h:120
static std::ostream & help(std::ostream &stream)
Generate the help screen, based on the commands in the registry.
Definition: common.cpp:71