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 1a337f7 commit bc01e9d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 40 deletions.
52 changes: 13 additions & 39 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 Info {
*/
using Callback = Delegate<void(String commandLine, ReadWriteStream& commandOutput)>;

#ifdef CMDPROC_FLASHSTRINGS

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

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 @@ -100,40 +88,27 @@ class Command : public Info
{
}

Command(const Info& info) : Info(info), 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) : Info{name, help, group, callback}
{
}

Command(const Info& info) : Info(info)
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 Info& info) : Info(info), 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 +124,6 @@ class Command : public Info
const StringAccessor<StringIndex::name> name;
const StringAccessor<StringIndex::help> help;
const StringAccessor<StringIndex::group> group;
#endif
};

} // namespace CommandProcessing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ bool Handler::registerCommand(const Command& command)
return false;
}

registeredCommands.addElement(new Info{command});
registeredCommands.add(command);
debug_d("[CH] Command '%s' registered", name.c_str());
return true;
}
Expand Down

0 comments on commit bc01e9d

Please sign in to comment.