Skip to content

Commit

Permalink
Store RAM strings in CStringArray
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 committed Mar 26, 2024
1 parent 961702c commit 2086535
Showing 1 changed file with 13 additions and 40 deletions.
53 changes: 13 additions & 40 deletions Sming/Libraries/CommandProcessing/src/CommandProcessing/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ struct CommandDef {
*/
using Callback = Delegate<void(String commandLine, ReadWriteStream& commandOutput)>;

#ifdef CMDPROC_FLASHSTRINGS

operator bool() const
{
return strings;
Expand All @@ -61,27 +59,17 @@ struct CommandDef {

String get(StringIndex index) const
{
#ifdef CMDPROC_FLASHSTRINGS
return strings ? CStringArray(*strings)[unsigned(index)] : nullptr;
#else
return strings[unsigned(index)];
#endif
}

#ifdef CMDPROC_FLASHSTRINGS
const FlashString* strings{};

#else

operator bool() const
{
return name;
}

bool operator==(const String& name) const
{
return name == this->name;
}

String name;
String help;
String group;

CStringArray strings;
#endif

Callback callback;
Expand All @@ -99,41 +87,27 @@ class Command : public CommandDef
Command(const FlashString& strings, Command::Callback callback) : Command({&strings, callback})
{
}

Command(const CommandDef& def) : CommandDef(def), name{*this}, help{*this}, group{*this}
{
}
#else
/** Instantiate a command delegate using set of wiring Strings
* @param name Command name - the text a user types to invoke the command
* @param help Help message shown by CLI "help" command
* @param group The command group to which this command belongs
* @param callback Delegate that should be invoked (triggered) when the command is entered by a user
*/
Command(String name, String help, String group, Callback callback) : CommandDef{name, help, group, callback}
{
}

Command(const CommandDef& def) : CommandDef(def)
Command(const String& name, const String& help, const String& group, Callback callback)
: Command({nullptr, callback})
{
strings.reserve(name.length() + help.length() + group.length() + 3);
strings += name;
strings += help;
strings += group;
}
#endif

/**
* @brief Invoke registered callback
* @retval bool false if no callback registered
*/
bool operator()(String commandLine, ReadWriteStream& commandOutput) const
Command(const CommandDef& def) : CommandDef(def), name{*this}, help{*this}, group{*this}
{
if(!callback) {
return false;
}

callback(commandLine, commandOutput);
return true;
}

#ifdef CMDPROC_FLASHSTRINGS
/**
* @brief Helper class for accessing individual strings
*/
Expand All @@ -149,7 +123,6 @@ class Command : public CommandDef
const StringAccessor<StringIndex::name> name;
const StringAccessor<StringIndex::help> help;
const StringAccessor<StringIndex::group> group;
#endif
};

} // namespace CommandProcessing
Expand Down

0 comments on commit 2086535

Please sign in to comment.