diff --git a/wpilibc/shared/include/Commands/ConditionalCommand.h b/wpilibc/shared/include/Commands/ConditionalCommand.h index e7fcb3c836..7453a631ff 100644 --- a/wpilibc/shared/include/Commands/ConditionalCommand.h +++ b/wpilibc/shared/include/Commands/ConditionalCommand.h @@ -40,10 +40,9 @@ namespace frc { */ class ConditionalCommand : public Command { public: - explicit ConditionalCommand(Command* onTrue, - Command* onFalse = new InstantCommand()); + explicit ConditionalCommand(Command* onTrue, Command* onFalse = nullptr); ConditionalCommand(const std::string& name, Command* onTrue, - Command* onFalse = new InstantCommand()); + Command* onFalse = nullptr); virtual ~ConditionalCommand() = default; protected: diff --git a/wpilibc/shared/src/Commands/ConditionalCommand.cpp b/wpilibc/shared/src/Commands/ConditionalCommand.cpp index 6d36086a50..c72ef15ca3 100644 --- a/wpilibc/shared/src/Commands/ConditionalCommand.cpp +++ b/wpilibc/shared/src/Commands/ConditionalCommand.cpp @@ -11,6 +11,17 @@ using namespace frc; +static void RequireAll(Command& command, Command* onTrue, Command* onFalse) { + if (onTrue != nullptr) { + for (auto requirement : onTrue->GetRequirements()) + command.Requires(requirement); + } + if (onFalse != nullptr) { + for (auto requirement : onFalse->GetRequirements()) + command.Requires(requirement); + } +} + /** * Creates a new ConditionalCommand with given onTrue and onFalse Commands. * @@ -23,8 +34,7 @@ ConditionalCommand::ConditionalCommand(Command* onTrue, Command* onFalse) { m_onTrue = onTrue; m_onFalse = onFalse; - for (auto requirement : m_onTrue->GetRequirements()) Requires(requirement); - for (auto requirement : m_onFalse->GetRequirements()) Requires(requirement); + RequireAll(*this, onTrue, onFalse); } /** @@ -42,8 +52,7 @@ ConditionalCommand::ConditionalCommand(const std::string& name, Command* onTrue, m_onTrue = onTrue; m_onFalse = onFalse; - for (auto requirement : m_onTrue->GetRequirements()) Requires(requirement); - for (auto requirement : m_onFalse->GetRequirements()) Requires(requirement); + RequireAll(*this, onTrue, onFalse); } void ConditionalCommand::_Initialize() { @@ -53,13 +62,15 @@ void ConditionalCommand::_Initialize() { m_chosenCommand = m_onFalse; } - /* - * This is a hack to make cancelling the chosen command inside a CommandGroup - * work properly - */ - m_chosenCommand->ClearRequirements(); + if (m_chosenCommand != nullptr) { + /* + * This is a hack to make cancelling the chosen command inside a + * CommandGroup work properly + */ + m_chosenCommand->ClearRequirements(); - m_chosenCommand->Start(); + m_chosenCommand->Start(); + } } void ConditionalCommand::_Cancel() { diff --git a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/ConditionalCommand.java b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/ConditionalCommand.java index f5c443363e..8e41df63d4 100644 --- a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/ConditionalCommand.java +++ b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/ConditionalCommand.java @@ -47,6 +47,20 @@ public abstract class ConditionalCommand extends Command { */ private Command m_chosenCommand = null; + private void requireAll() { + if (m_onTrue != null) { + for (Enumeration e = m_onTrue.getRequirements(); e.hasMoreElements(); ) { + requires((Subsystem) e.nextElement()); + } + } + + if (m_onFalse != null) { + for (Enumeration e = m_onFalse.getRequirements(); e.hasMoreElements(); ) { + requires((Subsystem) e.nextElement()); + } + } + } + /** * Creates a new ConditionalCommand with given onTrue and onFalse Commands. * @@ -55,7 +69,7 @@ public abstract class ConditionalCommand extends Command { * @param onTrue The Command to execute if {@link ConditionalCommand#condition()} returns true */ public ConditionalCommand(Command onTrue) { - this(onTrue, new InstantCommand()); + this(onTrue, null); } /** @@ -70,13 +84,7 @@ public abstract class ConditionalCommand extends Command { m_onTrue = onTrue; m_onFalse = onFalse; - for (Enumeration e = m_onTrue.getRequirements(); e.hasMoreElements(); ) { - requires((Subsystem) e.nextElement()); - } - - for (Enumeration e = m_onFalse.getRequirements(); e.hasMoreElements(); ) { - requires((Subsystem) e.nextElement()); - } + requireAll(); } /** @@ -88,7 +96,7 @@ public abstract class ConditionalCommand extends Command { * @param onTrue The Command to execute if {@link ConditionalCommand#condition()} returns true */ public ConditionalCommand(String name, Command onTrue) { - this(name, onTrue, new InstantCommand()); + this(name, onTrue, null); } /** @@ -105,13 +113,7 @@ public abstract class ConditionalCommand extends Command { m_onTrue = onTrue; m_onFalse = onFalse; - for (Enumeration e = m_onTrue.getRequirements(); e.hasMoreElements(); ) { - requires((Subsystem) e.nextElement()); - } - - for (Enumeration e = m_onFalse.getRequirements(); e.hasMoreElements(); ) { - requires((Subsystem) e.nextElement()); - } + requireAll(); } /** @@ -132,10 +134,15 @@ public abstract class ConditionalCommand extends Command { m_chosenCommand = m_onFalse; } - // This is a hack to make cancelling the chosen command inside a CommandGroup work properly - m_chosenCommand.clearRequirements(); + if (m_chosenCommand != null) { + /* + * This is a hack to make cancelling the chosen command inside a + * CommandGroup work properly + */ + m_chosenCommand.clearRequirements(); - m_chosenCommand.start(); + m_chosenCommand.start(); + } } @Override