[commands] Add convenience factories (#4460)

Co-authored-by: Starlight220 <53231611+Starlight220@users.noreply.github.com>
This commit is contained in:
Eli Barnett
2022-11-28 10:41:25 -05:00
committed by GitHub
parent 42b6d4e3f7
commit 1a59737f40
31 changed files with 1240 additions and 1 deletions

View File

@@ -282,6 +282,15 @@ safe) semantics.
*/
[[nodiscard]] CommandPtr HandleInterrupt(std::function<void()> handler) &&;
/**
* Decorates this Command with a name. Is an inline function for
* #SetName(std::string_view);
*
* @param name name
* @return the decorated Command
*/
[[nodiscard]] CommandPtr WithName(std::string_view name) &&;
/**
* Schedules this command.
*/
@@ -343,8 +352,21 @@ safe) semantics.
return InterruptionBehavior::kCancelSelf;
}
/**
* Gets the name of this Command. Defaults to the simple class name if not
* overridden.
*
* @return The display name of the Command
*/
virtual std::string GetName() const;
/**
* Sets the name of this Command. Nullop if not overridden.
*
* @param name The display name of the Command.
*/
virtual void SetName(std::string_view name);
/**
* Transfers ownership of this command to a unique pointer. Used for
* decorator methods.

View File

@@ -65,7 +65,7 @@ class CommandBase : public Command,
*
* @param name name
*/
void SetName(std::string_view name);
void SetName(std::string_view name) override;
/**
* Gets the name of this Command.

View File

@@ -8,6 +8,7 @@
#include <initializer_list>
#include <memory>
#include <span>
#include <string>
#include <utility>
#include <vector>
@@ -219,6 +220,15 @@ class CommandPtr final {
*/
[[nodiscard]] CommandPtr HandleInterrupt(std::function<void()> handler) &&;
/**
* Decorates this Command with a name. Is an inline function for
* Command::SetName(std::string_view);
*
* @param name name
* @return the decorated Command
*/
[[nodiscard]] CommandPtr WithName(std::string_view name) &&;
/**
* Get a raw pointer to the held command.
*/

View File

@@ -104,5 +104,41 @@ class Subsystem {
* Periodic() method to be called when the scheduler runs.
*/
void Register();
/**
* Constructs a command that runs an action once and finishes. Requires this
* subsystem.
*
* @param action the action to run
*/
[[nodiscard]] CommandPtr RunOnce(std::function<void()> action);
/**
* Constructs a command that runs an action every iteration until interrupted.
* Requires this subsystem.
*
* @param action the action to run
*/
[[nodiscard]] CommandPtr Run(std::function<void()> action);
/**
* Constructs a command that runs an action once and another action when the
* command is interrupted. Requires this subsystem.
*
* @param start the action to run on start
* @param end the action to run on interrupt
*/
[[nodiscard]] CommandPtr StartEnd(std::function<void()> start,
std::function<void()> end);
/**
* Constructs a command that runs an action every iteration until interrupted,
* and then runs a second action. Requires this subsystem.
*
* @param run the action to run every iteration
* @param end the action to run on interrupt
*/
[[nodiscard]] CommandPtr RunEnd(std::function<void()> run,
std::function<void()> end);
};
} // namespace frc2