Skip to content

Commit

Permalink
Add factory method
Browse files Browse the repository at this point in the history
  • Loading branch information
rzblue committed Aug 25, 2023
1 parent cc11f2a commit e27188f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static edu.wpi.first.util.ErrorMessages.requireNonNullParam;

import java.util.Map;
import java.util.Set;
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;

Expand Down Expand Up @@ -142,6 +143,20 @@ public static Command select(Map<Object, Command> commands, Supplier<Object> sel
return new SelectCommand(commands, selector);
}

/**
* Runs the command supplied by the supplier.
*
* @param supplier the command supplier
* @param requirements the set of requirements for this command
* @return the command
* @see DeferredCommand
*/
public static Command defer(Supplier<Command> supplier, Set<Subsystem> requirements) {
return new DeferredCommand(supplier, requirements);
}

// Command Groups

/**
* Runs a group of commands in series, one after the other.
*
Expand All @@ -153,8 +168,6 @@ public static Command sequence(Command... commands) {
return new SequentialCommandGroup(commands);
}

// Command Groups

/**
* Runs a group of commands in series, one after the other. Once the last command ends, the group
* is restarted.
Expand Down
11 changes: 11 additions & 0 deletions wpilibNewCommands/src/main/native/cpp/frc2/command/Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "frc2/command/Commands.h"

#include "frc2/command/ConditionalCommand.h"
#include "frc2/command/DeferredCommand.h"
#include "frc2/command/FunctionalCommand.h"
#include "frc2/command/InstantCommand.h"
#include "frc2/command/ParallelCommandGroup.h"
Expand Down Expand Up @@ -97,6 +98,16 @@ CommandPtr cmd::Either(CommandPtr&& onTrue, CommandPtr&& onFalse,
.ToPtr();
}

CommandPtr cmd::Defer(wpi::unique_function<Command*()> supplier,
std::span<Subsystem* const> requirements) {
return DeferredCommand(std::move(supplier), requirements).ToPtr();
}

CommandPtr cmd::Defer(wpi::unique_function<Command*()> supplier,
std::initializer_list<Subsystem*> requirements) {
return DeferredCommand(std::move(supplier), requirements).ToPtr();
}

CommandPtr cmd::Sequence(std::vector<CommandPtr>&& commands) {
return SequentialCommandGroup(CommandPtr::UnwrapVector(std::move(commands)))
.ToPtr();
Expand Down
20 changes: 20 additions & 0 deletions wpilibNewCommands/src/main/native/include/frc2/command/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,26 @@ CommandPtr Select(std::function<Key()> selector,
return SelectCommand(std::move(selector), std::move(vec)).ToPtr();
}

/**
* Runs the command supplied by the supplier.
*
* @param supplier the command supplier
* @param requirements the set of requirements for this command
*/
[[nodiscard]]
CommandPtr Defer(wpi::unique_function<Command*()> supplier,
std::span<Subsystem* const> requirements);

/**
* Runs the command supplied by the supplier.
*
* @param supplier the command supplier
* @param requirements the set of requirements for this command
*/
[[nodiscard]]
CommandPtr Defer(wpi::unique_function<Command*()> supplier,
std::initializer_list<Subsystem*> requirements);

// Command Groups

namespace impl {
Expand Down

0 comments on commit e27188f

Please sign in to comment.