Add braces to C++ single-line loops and conditionals (NFC) (#2973)

This makes code easier to read and more consistent between C++ and Java.
Also update clang-format settings to always add a line break (even if no braces are used).
This commit is contained in:
Peter Johnson
2020-12-28 12:58:06 -08:00
committed by GitHub
parent 0291a3ff56
commit 2aed432b4b
634 changed files with 10716 additions and 3938 deletions

View File

@@ -31,8 +31,9 @@ Command::Command(Subsystem& subsystem) : Command("", -1.0) {
Command::Command(const wpi::Twine& name, double timeout) {
// We use -1.0 to indicate no timeout.
if (timeout < 0.0 && timeout != -1.0)
if (timeout < 0.0 && timeout != -1.0) {
wpi_setWPIErrorWithContext(ParameterOutOfRange, "timeout < 0.0");
}
m_timeout = timeout;
@@ -61,37 +62,45 @@ Command::Command(const wpi::Twine& name, double timeout, Subsystem& subsystem)
}
double Command::TimeSinceInitialized() const {
if (m_startTime < 0.0)
if (m_startTime < 0.0) {
return 0.0;
else
} else {
return Timer::GetFPGATimestamp() - m_startTime;
}
}
void Command::Requires(Subsystem* subsystem) {
if (!AssertUnlocked("Can not add new requirement to command")) return;
if (!AssertUnlocked("Can not add new requirement to command")) {
return;
}
if (subsystem != nullptr)
if (subsystem != nullptr) {
m_requirements.insert(subsystem);
else
} else {
wpi_setWPIErrorWithContext(NullParameter, "subsystem");
}
}
void Command::Start() {
LockChanges();
if (m_parent != nullptr)
if (m_parent != nullptr) {
wpi_setWPIErrorWithContext(
CommandIllegalUse,
"Can not start a command that is part of a command group");
}
m_completed = false;
Scheduler::GetInstance()->AddCommand(this);
}
bool Command::Run() {
if (!m_runWhenDisabled && m_parent == nullptr && RobotState::IsDisabled())
if (!m_runWhenDisabled && m_parent == nullptr && RobotState::IsDisabled()) {
Cancel();
}
if (IsCanceled()) return false;
if (IsCanceled()) {
return false;
}
if (!m_initialized) {
m_initialized = true;
@@ -105,23 +114,34 @@ bool Command::Run() {
}
void Command::Cancel() {
if (m_parent != nullptr)
if (m_parent != nullptr) {
wpi_setWPIErrorWithContext(
CommandIllegalUse,
"Can not cancel a command that is part of a command group");
}
_Cancel();
}
bool Command::IsRunning() const { return m_running; }
bool Command::IsRunning() const {
return m_running;
}
bool Command::IsInitialized() const { return m_initialized; }
bool Command::IsInitialized() const {
return m_initialized;
}
bool Command::IsCompleted() const { return m_completed; }
bool Command::IsCompleted() const {
return m_completed;
}
bool Command::IsCanceled() const { return m_canceled; }
bool Command::IsCanceled() const {
return m_canceled;
}
bool Command::IsInterruptible() const { return m_interruptible; }
bool Command::IsInterruptible() const {
return m_interruptible;
}
void Command::SetInterruptible(bool interruptible) {
m_interruptible = interruptible;
@@ -135,19 +155,28 @@ const Command::SubsystemSet& Command::GetRequirements() const {
return m_requirements;
}
CommandGroup* Command::GetGroup() const { return m_parent; }
CommandGroup* Command::GetGroup() const {
return m_parent;
}
void Command::SetRunWhenDisabled(bool run) { m_runWhenDisabled = run; }
void Command::SetRunWhenDisabled(bool run) {
m_runWhenDisabled = run;
}
bool Command::WillRunWhenDisabled() const { return m_runWhenDisabled; }
bool Command::WillRunWhenDisabled() const {
return m_runWhenDisabled;
}
int Command::GetID() const { return m_commandID; }
int Command::GetID() const {
return m_commandID;
}
void Command::SetTimeout(double timeout) {
if (timeout < 0.0)
if (timeout < 0.0) {
wpi_setWPIErrorWithContext(ParameterOutOfRange, "timeout < 0.0");
else
} else {
m_timeout = timeout;
}
}
bool Command::IsTimedOut() const {
@@ -177,9 +206,13 @@ void Command::SetParent(CommandGroup* parent) {
}
}
bool Command::IsParented() const { return m_parent != nullptr; }
bool Command::IsParented() const {
return m_parent != nullptr;
}
void Command::ClearRequirements() { m_requirements.clear(); }
void Command::ClearRequirements() {
m_requirements.clear();
}
void Command::Initialize() {}
@@ -187,21 +220,33 @@ void Command::Execute() {}
void Command::End() {}
void Command::Interrupted() { End(); }
void Command::Interrupted() {
End();
}
void Command::_Initialize() { m_completed = false; }
void Command::_Initialize() {
m_completed = false;
}
void Command::_Interrupted() { m_completed = true; }
void Command::_Interrupted() {
m_completed = true;
}
void Command::_Execute() {}
void Command::_End() { m_completed = true; }
void Command::_Cancel() {
if (IsRunning()) m_canceled = true;
void Command::_End() {
m_completed = true;
}
void Command::LockChanges() { m_locked = true; }
void Command::_Cancel() {
if (IsRunning()) {
m_canceled = true;
}
}
void Command::LockChanges() {
m_locked = true;
}
void Command::Removed() {
if (m_initialized) {
@@ -225,7 +270,9 @@ void Command::StartRunning() {
m_completed = false;
}
void Command::StartTiming() { m_startTime = Timer::GetFPGATimestamp(); }
void Command::StartTiming() {
m_startTime = Timer::GetFPGATimestamp();
}
std::string Command::GetName() const {
return SendableRegistry::GetInstance().GetName(this);
@@ -252,9 +299,13 @@ void Command::InitSendable(SendableBuilder& builder) {
"running", [=]() { return IsRunning(); },
[=](bool value) {
if (value) {
if (!IsRunning()) Start();
if (!IsRunning()) {
Start();
}
} else {
if (IsRunning()) Cancel();
if (IsRunning()) {
Cancel();
}
}
});
builder.AddBooleanProperty(

View File

@@ -15,7 +15,9 @@ void CommandGroup::AddSequential(Command* command) {
wpi_setWPIErrorWithContext(NullParameter, "command");
return;
}
if (!AssertUnlocked("Cannot add new command to command group")) return;
if (!AssertUnlocked("Cannot add new command to command group")) {
return;
}
m_commands.emplace_back(command, CommandGroupEntry::kSequence_InSequence);
@@ -23,7 +25,9 @@ void CommandGroup::AddSequential(Command* command) {
// Iterate through command->GetRequirements() and call Requires() on each
// required subsystem
for (auto&& requirement : command->GetRequirements()) Requires(requirement);
for (auto&& requirement : command->GetRequirements()) {
Requires(requirement);
}
}
void CommandGroup::AddSequential(Command* command, double timeout) {
@@ -31,7 +35,9 @@ void CommandGroup::AddSequential(Command* command, double timeout) {
wpi_setWPIErrorWithContext(NullParameter, "command");
return;
}
if (!AssertUnlocked("Cannot add new command to command group")) return;
if (!AssertUnlocked("Cannot add new command to command group")) {
return;
}
if (timeout < 0.0) {
wpi_setWPIErrorWithContext(ParameterOutOfRange, "timeout < 0.0");
return;
@@ -44,7 +50,9 @@ void CommandGroup::AddSequential(Command* command, double timeout) {
// Iterate through command->GetRequirements() and call Requires() on each
// required subsystem
for (auto&& requirement : command->GetRequirements()) Requires(requirement);
for (auto&& requirement : command->GetRequirements()) {
Requires(requirement);
}
}
void CommandGroup::AddParallel(Command* command) {
@@ -52,7 +60,9 @@ void CommandGroup::AddParallel(Command* command) {
wpi_setWPIErrorWithContext(NullParameter, "command");
return;
}
if (!AssertUnlocked("Cannot add new command to command group")) return;
if (!AssertUnlocked("Cannot add new command to command group")) {
return;
}
m_commands.emplace_back(command, CommandGroupEntry::kSequence_BranchChild);
@@ -60,7 +70,9 @@ void CommandGroup::AddParallel(Command* command) {
// Iterate through command->GetRequirements() and call Requires() on each
// required subsystem
for (auto&& requirement : command->GetRequirements()) Requires(requirement);
for (auto&& requirement : command->GetRequirements()) {
Requires(requirement);
}
}
void CommandGroup::AddParallel(Command* command, double timeout) {
@@ -68,7 +80,9 @@ void CommandGroup::AddParallel(Command* command, double timeout) {
wpi_setWPIErrorWithContext(NullParameter, "command");
return;
}
if (!AssertUnlocked("Cannot add new command to command group")) return;
if (!AssertUnlocked("Cannot add new command to command group")) {
return;
}
if (timeout < 0.0) {
wpi_setWPIErrorWithContext(ParameterOutOfRange, "timeout < 0.0");
return;
@@ -81,26 +95,36 @@ void CommandGroup::AddParallel(Command* command, double timeout) {
// Iterate through command->GetRequirements() and call Requires() on each
// required subsystem
for (auto&& requirement : command->GetRequirements()) Requires(requirement);
for (auto&& requirement : command->GetRequirements()) {
Requires(requirement);
}
}
bool CommandGroup::IsInterruptible() const {
if (!Command::IsInterruptible()) return false;
if (!Command::IsInterruptible()) {
return false;
}
if (m_currentCommandIndex != -1 &&
static_cast<size_t>(m_currentCommandIndex) < m_commands.size()) {
Command* cmd = m_commands[m_currentCommandIndex].m_command;
if (!cmd->IsInterruptible()) return false;
if (!cmd->IsInterruptible()) {
return false;
}
}
for (const auto& child : m_children) {
if (!child->m_command->IsInterruptible()) return false;
if (!child->m_command->IsInterruptible()) {
return false;
}
}
return true;
}
int CommandGroup::GetSize() const { return m_children.size(); }
int CommandGroup::GetSize() const {
return m_children.size();
}
void CommandGroup::Initialize() {}
@@ -115,7 +139,9 @@ void CommandGroup::End() {}
void CommandGroup::Interrupted() {}
void CommandGroup::_Initialize() { m_currentCommandIndex = -1; }
void CommandGroup::_Initialize() {
m_currentCommandIndex = -1;
}
void CommandGroup::_Execute() {
CommandGroupEntry* entry;
@@ -132,7 +158,9 @@ void CommandGroup::_Execute() {
// If a command is prepared to run
if (cmd != nullptr) {
// If command timed out, cancel it so it's removed from the Scheduler
if (entry->IsTimedOut()) cmd->_Cancel();
if (entry->IsTimedOut()) {
cmd->_Cancel();
}
// If command finished or was canceled, remove it from Scheduler
if (cmd->Run()) {
@@ -219,7 +247,9 @@ void CommandGroup::_End() {
m_children.clear();
}
void CommandGroup::_Interrupted() { _End(); }
void CommandGroup::_Interrupted() {
_End();
}
void CommandGroup::CancelConflicts(Command* command) {
for (auto childIter = m_children.begin(); childIter != m_children.end();) {
@@ -235,6 +265,8 @@ void CommandGroup::CancelConflicts(Command* command) {
break;
}
}
if (!erased) childIter++;
if (!erased) {
childIter++;
}
}
}

View File

@@ -13,8 +13,12 @@ CommandGroupEntry::CommandGroupEntry(Command* command, Sequence state,
: m_timeout(timeout), m_command(command), m_state(state) {}
bool CommandGroupEntry::IsTimedOut() const {
if (m_timeout < 0.0) return false;
if (m_timeout < 0.0) {
return false;
}
double time = m_command->TimeSinceInitialized();
if (time == 0.0) return false;
if (time == 0.0) {
return false;
}
return time >= m_timeout;
}

View File

@@ -10,12 +10,14 @@ using namespace frc;
static void RequireAll(Command& command, Command* onTrue, Command* onFalse) {
if (onTrue != nullptr) {
for (auto requirement : onTrue->GetRequirements())
for (auto requirement : onTrue->GetRequirements()) {
command.Requires(requirement);
}
}
if (onFalse != nullptr) {
for (auto requirement : onFalse->GetRequirements())
for (auto requirement : onFalse->GetRequirements()) {
command.Requires(requirement);
}
}
}

View File

@@ -39,4 +39,6 @@ void InstantCommand::_Initialize() {
}
}
bool InstantCommand::IsFinished() { return true; }
bool InstantCommand::IsFinished() {
return true;
}

View File

@@ -72,19 +72,29 @@ PIDCommand::PIDCommand(double p, double i, double d, double period,
m_controller = std::make_shared<PIDController>(p, i, d, this, this, period);
}
void PIDCommand::_Initialize() { m_controller->Enable(); }
void PIDCommand::_Initialize() {
m_controller->Enable();
}
void PIDCommand::_End() { m_controller->Disable(); }
void PIDCommand::_End() {
m_controller->Disable();
}
void PIDCommand::_Interrupted() { _End(); }
void PIDCommand::_Interrupted() {
_End();
}
void PIDCommand::SetSetpointRelative(double deltaSetpoint) {
SetSetpoint(GetSetpoint() + deltaSetpoint);
}
void PIDCommand::PIDWrite(double output) { UsePIDOutput(output); }
void PIDCommand::PIDWrite(double output) {
UsePIDOutput(output);
}
double PIDCommand::PIDGet() { return ReturnPIDInput(); }
double PIDCommand::PIDGet() {
return ReturnPIDInput();
}
std::shared_ptr<PIDController> PIDCommand::GetPIDController() const {
return m_controller;
@@ -94,9 +104,13 @@ void PIDCommand::SetSetpoint(double setpoint) {
m_controller->SetSetpoint(setpoint);
}
double PIDCommand::GetSetpoint() const { return m_controller->GetSetpoint(); }
double PIDCommand::GetSetpoint() const {
return m_controller->GetSetpoint();
}
double PIDCommand::GetPosition() { return ReturnPIDInput(); }
double PIDCommand::GetPosition() {
return ReturnPIDInput();
}
void PIDCommand::InitSendable(SendableBuilder& builder) {
m_controller->InitSendable(builder);

View File

@@ -49,13 +49,21 @@ PIDSubsystem::PIDSubsystem(double p, double i, double d, double f,
AddChild("PIDController", m_controller);
}
void PIDSubsystem::Enable() { m_controller->Enable(); }
void PIDSubsystem::Enable() {
m_controller->Enable();
}
void PIDSubsystem::Disable() { m_controller->Disable(); }
void PIDSubsystem::Disable() {
m_controller->Disable();
}
void PIDSubsystem::PIDWrite(double output) { UsePIDOutput(output); }
void PIDSubsystem::PIDWrite(double output) {
UsePIDOutput(output);
}
double PIDSubsystem::PIDGet() { return ReturnPIDInput(); }
double PIDSubsystem::PIDGet() {
return ReturnPIDInput();
}
void PIDSubsystem::SetSetpoint(double setpoint) {
m_controller->SetSetpoint(setpoint);
@@ -73,11 +81,17 @@ void PIDSubsystem::SetOutputRange(double minimumOutput, double maximumOutput) {
m_controller->SetOutputRange(minimumOutput, maximumOutput);
}
double PIDSubsystem::GetSetpoint() { return m_controller->GetSetpoint(); }
double PIDSubsystem::GetSetpoint() {
return m_controller->GetSetpoint();
}
double PIDSubsystem::GetPosition() { return ReturnPIDInput(); }
double PIDSubsystem::GetPosition() {
return ReturnPIDInput();
}
double PIDSubsystem::GetRate() { return ReturnPIDInput(); }
double PIDSubsystem::GetRate() {
return ReturnPIDInput();
}
void PIDSubsystem::SetAbsoluteTolerance(double absValue) {
m_controller->SetAbsoluteTolerance(absValue);
@@ -87,7 +101,9 @@ void PIDSubsystem::SetPercentTolerance(double percent) {
m_controller->SetPercentTolerance(percent);
}
bool PIDSubsystem::OnTarget() const { return m_controller->OnTarget(); }
bool PIDSubsystem::OnTarget() const {
return m_controller->OnTarget();
}
std::shared_ptr<PIDController> PIDSubsystem::GetPIDController() {
return m_controller;

View File

@@ -13,4 +13,6 @@ PrintCommand::PrintCommand(const wpi::Twine& message)
m_message = message.str();
}
void PrintCommand::Initialize() { wpi::outs() << m_message << '\n'; }
void PrintCommand::Initialize() {
wpi::outs() << m_message << '\n';
}

View File

@@ -52,8 +52,9 @@ Scheduler* Scheduler::GetInstance() {
void Scheduler::AddCommand(Command* command) {
std::scoped_lock lock(m_impl->additionsMutex);
if (std::find(m_impl->additions.begin(), m_impl->additions.end(), command) !=
m_impl->additions.end())
m_impl->additions.end()) {
return;
}
m_impl->additions.push_back(command);
}
@@ -73,7 +74,9 @@ void Scheduler::RegisterSubsystem(Subsystem* subsystem) {
void Scheduler::Run() {
// Get button input (going backwards preserves button priority)
{
if (!m_impl->enabled) return;
if (!m_impl->enabled) {
return;
}
std::scoped_lock lock(m_impl->buttonsMutex);
for (auto& button : m_impl->buttons) {
@@ -152,7 +155,9 @@ void Scheduler::ResetAll() {
m_impl->commands.clear();
}
void Scheduler::SetEnabled(bool enabled) { m_impl->enabled = enabled; }
void Scheduler::SetEnabled(bool enabled) {
m_impl->enabled = enabled;
}
void Scheduler::InitSendable(SendableBuilder& builder) {
builder.SetSmartDashboardType("Scheduler");
@@ -163,7 +168,9 @@ void Scheduler::InitSendable(SendableBuilder& builder) {
// Get the list of possible commands to cancel
auto new_toCancel = cancelEntry.GetValue();
wpi::ArrayRef<double> toCancel;
if (new_toCancel) toCancel = new_toCancel->GetDoubleArray();
if (new_toCancel) {
toCancel = new_toCancel->GetDoubleArray();
}
// Cancel commands whose cancel buttons were pressed on the SmartDashboard
if (!toCancel.empty()) {
@@ -212,7 +219,9 @@ Scheduler::~Scheduler() {
}
void Scheduler::Impl::Remove(Command* command) {
if (!commands.erase(command)) return;
if (!commands.erase(command)) {
return;
}
for (auto&& requirement : command->GetRequirements()) {
requirement->SetCurrentCommand(nullptr);
@@ -222,7 +231,9 @@ void Scheduler::Impl::Remove(Command* command) {
}
void Scheduler::Impl::ProcessCommandAddition(Command* command) {
if (command == nullptr) return;
if (command == nullptr) {
return;
}
// Only add if not already in
auto found = commands.find(command);
@@ -231,8 +242,9 @@ void Scheduler::Impl::ProcessCommandAddition(Command* command) {
const auto& requirements = command->GetRequirements();
for (const auto requirement : requirements) {
if (requirement->GetCurrentCommand() != nullptr &&
!requirement->GetCurrentCommand()->IsInterruptible())
!requirement->GetCurrentCommand()->IsInterruptible()) {
return;
}
}
// Give it the requirements

View File

@@ -11,4 +11,6 @@ StartCommand::StartCommand(Command* commandToStart)
m_commandToFork = commandToStart;
}
void StartCommand::Initialize() { m_commandToFork->Start(); }
void StartCommand::Initialize() {
m_commandToFork->Start();
}

View File

@@ -55,7 +55,9 @@ void Subsystem::SetCurrentCommand(Command* command) {
m_currentCommandChanged = true;
}
Command* Subsystem::GetCurrentCommand() const { return m_currentCommand; }
Command* Subsystem::GetCurrentCommand() const {
return m_currentCommand;
}
wpi::StringRef Subsystem::GetCurrentCommandName() const {
Command* currentCommand = GetCurrentCommand();
@@ -100,9 +102,13 @@ void Subsystem::AddChild(const wpi::Twine& name, Sendable& child) {
registry.AddLW(&child, registry.GetSubsystem(this), name);
}
void Subsystem::AddChild(std::shared_ptr<Sendable> child) { AddChild(*child); }
void Subsystem::AddChild(std::shared_ptr<Sendable> child) {
AddChild(*child);
}
void Subsystem::AddChild(Sendable* child) { AddChild(*child); }
void Subsystem::AddChild(Sendable* child) {
AddChild(*child);
}
void Subsystem::AddChild(Sendable& child) {
auto& registry = SendableRegistry::GetInstance();
@@ -111,7 +117,9 @@ void Subsystem::AddChild(Sendable& child) {
}
void Subsystem::ConfirmCommand() {
if (m_currentCommandChanged) m_currentCommandChanged = false;
if (m_currentCommandChanged) {
m_currentCommandChanged = false;
}
}
void Subsystem::InitSendable(SendableBuilder& builder) {

View File

@@ -18,4 +18,6 @@ TimedCommand::TimedCommand(const wpi::Twine& name, double timeout,
TimedCommand::TimedCommand(double timeout, Subsystem& subsystem)
: Command(timeout, subsystem) {}
bool TimedCommand::IsFinished() { return IsTimedOut(); }
bool TimedCommand::IsFinished() {
return IsTimedOut();
}

View File

@@ -18,4 +18,6 @@ WaitUntilCommand::WaitUntilCommand(const wpi::Twine& name, double time)
m_time = time;
}
bool WaitUntilCommand::IsFinished() { return Timer::GetMatchTime() >= m_time; }
bool WaitUntilCommand::IsFinished() {
return Timer::GetMatchTime() >= m_time;
}