CommandProcessing/src/CommandProcessing/Command.h
Go to the documentation of this file.
1 /*
2  * CommandDelegate.h
3  *
4  * Created on: 2 jul. 2015
5  * Author: Herman
6  */
11 #pragma once
12 
13 #include <Delegate.h>
15 #include <Data/CStringArray.h>
16 
17 #ifdef CMDPROC_FLASHSTRINGS
24 #define CMDP_STRINGS(name, help, group) FS(name "\0" help "\0" group "\0")
25 #else
26 #define CMDP_STRINGS(name, help, group) F(name), F(help), F(group)
27 #endif
28 
30 {
31 // Order matches CMDP_STRINGS
32 enum class StringIndex {
33  name,
34  help,
35  group,
36 };
37 
41 struct CommandDef {
48  using Callback = Delegate<void(String commandLine, ReadWriteStream& commandOutput)>;
49 
50  operator bool() const
51  {
52  return strings;
53  }
54 
55  bool operator==(const String& name) const
56  {
57  return name == get(StringIndex::name);
58  }
59 
60  String get(StringIndex index) const
61  {
62 #ifdef CMDPROC_FLASHSTRINGS
63  return strings ? CStringArray(*strings)[unsigned(index)] : nullptr;
64 #else
65  return strings[unsigned(index)];
66 #endif
67  }
68 
69 #ifdef CMDPROC_FLASHSTRINGS
70  const FlashString* strings{};
71 #else
73 #endif
74 
76 };
77 
79 class Command : private CommandDef
80 {
81 public:
82  friend class Handler;
83 
84 #ifdef CMDPROC_FLASHSTRINGS
90  {
91  }
92 #else
100  : Command({nullptr, callback})
101  {
102  strings.reserve(name.length() + help.length() + group.length() + 3);
103  strings += name;
104  strings += help;
105  strings += group;
106  }
107 #endif
108 
109  Command(const CommandDef& def) : CommandDef(def), name{*this}, help{*this}, group{*this}
110  {
111  }
112 
116  template <StringIndex index> struct StringAccessor {
117  operator String() const
118  {
119  return def.get(index);
120  }
121 
122  const CommandDef& def;
123  };
124 
128 };
129 
130 } // namespace CommandProcessing
131 
CommandLine commandLine
Class to manage a double null-terminated list of strings, such as "one\0two\0three\0".
Definition: CStringArray.h:22
bool reserve(size_t size)
Pre-allocate String memory.
Command delegate class.
Definition: CommandProcessing/src/CommandProcessing/Command.h:80
const StringAccessor< StringIndex::group > group
Definition: CommandProcessing/src/CommandProcessing/Command.h:127
Command(const CommandDef &def)
Definition: CommandProcessing/src/CommandProcessing/Command.h:109
const StringAccessor< StringIndex::help > help
Definition: CommandProcessing/src/CommandProcessing/Command.h:126
const StringAccessor< StringIndex::name > name
Definition: CommandProcessing/src/CommandProcessing/Command.h:125
Command(const String &name, const String &help, const String &group, Callback callback)
Definition: CommandProcessing/src/CommandProcessing/Command.h:99
Command handler class.
Definition: CommandProcessing/src/CommandProcessing/Handler.h:28
describes a counted string stored in flash memory
Definition: String.hpp:174
Base class for read/write stream.
Definition: ReadWriteStream.h:20
The String class.
Definition: WString.h:137
Definition: CommandProcessing/src/CommandProcessing/Command.h:30
StringIndex
Definition: CommandProcessing/src/CommandProcessing/Command.h:32
Command definition stored by handler.
Definition: CommandProcessing/src/CommandProcessing/Command.h:41
Callback callback
Definition: CommandProcessing/src/CommandProcessing/Command.h:75
String get(StringIndex index) const
Definition: CommandProcessing/src/CommandProcessing/Command.h:60
bool operator==(const String &name) const
Definition: CommandProcessing/src/CommandProcessing/Command.h:55
CStringArray strings
Definition: CommandProcessing/src/CommandProcessing/Command.h:72
Helper class for accessing individual strings.
Definition: CommandProcessing/src/CommandProcessing/Command.h:116
const CommandDef & def
Definition: CommandProcessing/src/CommandProcessing/Command.h:122