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

@@ -50,12 +50,16 @@ FieldObject2d* Field2d::GetObject(const wpi::Twine& name) {
std::scoped_lock lock(m_mutex);
std::string nameStr = name.str();
for (auto&& obj : m_objects) {
if (obj->m_name == nameStr) return obj.get();
if (obj->m_name == nameStr) {
return obj.get();
}
}
m_objects.emplace_back(std::make_unique<FieldObject2d>(
std::move(nameStr), FieldObject2d::private_init{}));
auto obj = m_objects.back().get();
if (m_table) obj->m_entry = m_table->GetEntry(obj->m_name);
if (m_table) {
obj->m_entry = m_table->GetEntry(obj->m_name);
}
return obj;
}

View File

@@ -32,7 +32,9 @@ void FieldObject2d::SetPose(units::meter_t x, units::meter_t y,
Pose2d FieldObject2d::GetPose() const {
std::scoped_lock lock(m_mutex);
UpdateFromEntry();
if (m_poses.empty()) return {};
if (m_poses.empty()) {
return {};
}
return m_poses[0];
}
@@ -61,7 +63,9 @@ wpi::ArrayRef<Pose2d> FieldObject2d::GetPoses(
}
void FieldObject2d::UpdateEntry(bool setDefault) {
if (!m_entry) return;
if (!m_entry) {
return;
}
wpi::SmallVector<double, 9> arr;
for (auto&& pose : m_poses) {
auto& translation = pose.Translation();
@@ -69,19 +73,26 @@ void FieldObject2d::UpdateEntry(bool setDefault) {
arr.push_back(translation.Y().to<double>());
arr.push_back(pose.Rotation().Degrees().to<double>());
}
if (setDefault)
if (setDefault) {
m_entry.SetDefaultDoubleArray(arr);
else
} else {
m_entry.SetDoubleArray(arr);
}
}
void FieldObject2d::UpdateFromEntry() const {
if (!m_entry) return;
if (!m_entry) {
return;
}
auto val = m_entry.GetValue();
if (!val || !val->IsDoubleArray()) return;
if (!val || !val->IsDoubleArray()) {
return;
}
auto arr = val->GetDoubleArray();
auto size = arr.size();
if ((size % 3) != 0) return;
if ((size % 3) != 0) {
return;
}
m_poses.resize(size / 3);
for (size_t i = 0; i < size / 3; ++i) {
m_poses[i] =

View File

@@ -9,8 +9,9 @@
using namespace frc;
SendableBase::SendableBase(bool addLiveWindow) {
if (addLiveWindow)
if (addLiveWindow) {
SendableRegistry::GetInstance().AddLW(this, "");
else
} else {
SendableRegistry::GetInstance().Add(this, "");
}
}

View File

@@ -20,14 +20,20 @@ std::shared_ptr<nt::NetworkTable> SendableBuilderImpl::GetTable() {
return m_table;
}
bool SendableBuilderImpl::HasTable() const { return m_table != nullptr; }
bool SendableBuilderImpl::HasTable() const {
return m_table != nullptr;
}
bool SendableBuilderImpl::IsActuator() const { return m_actuator; }
bool SendableBuilderImpl::IsActuator() const {
return m_actuator;
}
void SendableBuilderImpl::UpdateTable() {
uint64_t time = nt::Now();
for (auto& property : m_properties) {
if (property.update) property.update(property.entry, time);
if (property.update) {
property.update(property.entry, time);
}
}
for (auto& updateTable : m_updateTables) {
updateTable();
@@ -35,26 +41,40 @@ void SendableBuilderImpl::UpdateTable() {
}
void SendableBuilderImpl::StartListeners() {
for (auto& property : m_properties) property.StartListener();
if (m_controllableEntry) m_controllableEntry.SetBoolean(true);
for (auto& property : m_properties) {
property.StartListener();
}
if (m_controllableEntry) {
m_controllableEntry.SetBoolean(true);
}
}
void SendableBuilderImpl::StopListeners() {
for (auto& property : m_properties) property.StopListener();
if (m_controllableEntry) m_controllableEntry.SetBoolean(false);
for (auto& property : m_properties) {
property.StopListener();
}
if (m_controllableEntry) {
m_controllableEntry.SetBoolean(false);
}
}
void SendableBuilderImpl::StartLiveWindowMode() {
if (m_safeState) m_safeState();
if (m_safeState) {
m_safeState();
}
StartListeners();
}
void SendableBuilderImpl::StopLiveWindowMode() {
StopListeners();
if (m_safeState) m_safeState();
if (m_safeState) {
m_safeState();
}
}
void SendableBuilderImpl::ClearProperties() { m_properties.clear(); }
void SendableBuilderImpl::ClearProperties() {
m_properties.clear();
}
void SendableBuilderImpl::SetSmartDashboardType(const wpi::Twine& type) {
m_table->GetEntry(".type").SetString(type);
@@ -92,7 +112,9 @@ void SendableBuilderImpl::AddBooleanProperty(const wpi::Twine& key,
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsBoolean()) return;
if (!event.value->IsBoolean()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetBoolean()); });
},
@@ -116,7 +138,9 @@ void SendableBuilderImpl::AddDoubleProperty(
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsDouble()) return;
if (!event.value->IsDouble()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetDouble()); });
},
@@ -140,7 +164,9 @@ void SendableBuilderImpl::AddStringProperty(
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsString()) return;
if (!event.value->IsString()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetString()); });
},
@@ -164,7 +190,9 @@ void SendableBuilderImpl::AddBooleanArrayProperty(
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsBooleanArray()) return;
if (!event.value->IsBooleanArray()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetBooleanArray()); });
},
@@ -188,7 +216,9 @@ void SendableBuilderImpl::AddDoubleArrayProperty(
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsDoubleArray()) return;
if (!event.value->IsDoubleArray()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetDoubleArray()); });
},
@@ -212,7 +242,9 @@ void SendableBuilderImpl::AddStringArrayProperty(
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsStringArray()) return;
if (!event.value->IsStringArray()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetStringArray()); });
},
@@ -236,7 +268,9 @@ void SendableBuilderImpl::AddRawProperty(
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsRaw()) return;
if (!event.value->IsRaw()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetRaw()); });
},
@@ -284,7 +318,9 @@ void SendableBuilderImpl::AddSmallStringProperty(
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsString()) return;
if (!event.value->IsString()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetString()); });
},
@@ -310,7 +346,9 @@ void SendableBuilderImpl::AddSmallBooleanArrayProperty(
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsBooleanArray()) return;
if (!event.value->IsBooleanArray()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetBooleanArray()); });
},
@@ -337,7 +375,9 @@ void SendableBuilderImpl::AddSmallDoubleArrayProperty(
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsDoubleArray()) return;
if (!event.value->IsDoubleArray()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetDoubleArray()); });
},
@@ -365,7 +405,9 @@ void SendableBuilderImpl::AddSmallStringArrayProperty(
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsStringArray()) return;
if (!event.value->IsStringArray()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetStringArray()); });
},
@@ -391,7 +433,9 @@ void SendableBuilderImpl::AddSmallRawProperty(
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
return entry.AddListener(
[=](const nt::EntryNotification& event) {
if (!event.value->IsRaw()) return;
if (!event.value->IsRaw()) {
return;
}
SmartDashboard::PostListenerTask(
[=] { setter(event.value->GetRaw()); });
},

View File

@@ -51,9 +51,12 @@ struct SendableRegistry::Impl {
SendableRegistry::Impl::Component& SendableRegistry::Impl::GetOrAdd(
void* sendable, UID* uid) {
UID& compUid = componentMap[sendable];
if (compUid == 0)
if (compUid == 0) {
compUid = components.emplace_back(std::make_unique<Component>()) + 1;
if (uid) *uid = compUid;
}
if (uid) {
*uid = compUid;
}
return *components[compUid - 1];
}
@@ -146,13 +149,17 @@ void SendableRegistry::AddChild(Sendable* parent, void* child) {
bool SendableRegistry::Remove(Sendable* sendable) {
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->componentMap.find(sendable);
if (it == m_impl->componentMap.end()) return false;
if (it == m_impl->componentMap.end()) {
return false;
}
UID compUid = it->getSecond();
m_impl->components.erase(compUid - 1);
m_impl->componentMap.erase(it);
// update any parent pointers
for (auto&& comp : m_impl->components) {
if (comp->parent == sendable) comp->parent = nullptr;
if (comp->parent == sendable) {
comp->parent = nullptr;
}
}
return true;
}
@@ -161,8 +168,9 @@ void SendableRegistry::Move(Sendable* to, Sendable* from) {
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->componentMap.find(from);
if (it == m_impl->componentMap.end() ||
!m_impl->components[it->getSecond() - 1])
!m_impl->components[it->getSecond() - 1]) {
return;
}
UID compUid = it->getSecond();
m_impl->componentMap.erase(it);
m_impl->componentMap[to] = compUid;
@@ -175,7 +183,9 @@ void SendableRegistry::Move(Sendable* to, Sendable* from) {
}
// update any parent pointers
for (auto&& comp : m_impl->components) {
if (comp->parent == from) comp->parent = to;
if (comp->parent == from) {
comp->parent = to;
}
}
}
@@ -188,8 +198,9 @@ std::string SendableRegistry::GetName(const Sendable* sendable) const {
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->componentMap.find(sendable);
if (it == m_impl->componentMap.end() ||
!m_impl->components[it->getSecond() - 1])
return std::string{};
!m_impl->components[it->getSecond() - 1]) {
return {};
}
return m_impl->components[it->getSecond() - 1]->name;
}
@@ -197,8 +208,9 @@ void SendableRegistry::SetName(Sendable* sendable, const wpi::Twine& name) {
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->componentMap.find(sendable);
if (it == m_impl->componentMap.end() ||
!m_impl->components[it->getSecond() - 1])
!m_impl->components[it->getSecond() - 1]) {
return;
}
m_impl->components[it->getSecond() - 1]->name = name.str();
}
@@ -207,8 +219,9 @@ void SendableRegistry::SetName(Sendable* sendable, const wpi::Twine& moduleType,
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->componentMap.find(sendable);
if (it == m_impl->componentMap.end() ||
!m_impl->components[it->getSecond() - 1])
!m_impl->components[it->getSecond() - 1]) {
return;
}
m_impl->components[it->getSecond() - 1]->SetName(moduleType, channel);
}
@@ -217,8 +230,9 @@ void SendableRegistry::SetName(Sendable* sendable, const wpi::Twine& moduleType,
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->componentMap.find(sendable);
if (it == m_impl->componentMap.end() ||
!m_impl->components[it->getSecond() - 1])
!m_impl->components[it->getSecond() - 1]) {
return;
}
m_impl->components[it->getSecond() - 1]->SetName(moduleType, moduleNumber,
channel);
}
@@ -228,8 +242,9 @@ void SendableRegistry::SetName(Sendable* sendable, const wpi::Twine& subsystem,
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->componentMap.find(sendable);
if (it == m_impl->componentMap.end() ||
!m_impl->components[it->getSecond() - 1])
!m_impl->components[it->getSecond() - 1]) {
return;
}
auto& comp = *m_impl->components[it->getSecond() - 1];
comp.name = name.str();
comp.subsystem = subsystem.str();
@@ -239,8 +254,9 @@ std::string SendableRegistry::GetSubsystem(const Sendable* sendable) const {
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->componentMap.find(sendable);
if (it == m_impl->componentMap.end() ||
!m_impl->components[it->getSecond() - 1])
return std::string{};
!m_impl->components[it->getSecond() - 1]) {
return {};
}
return m_impl->components[it->getSecond() - 1]->subsystem;
}
@@ -249,8 +265,9 @@ void SendableRegistry::SetSubsystem(Sendable* sendable,
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->componentMap.find(sendable);
if (it == m_impl->componentMap.end() ||
!m_impl->components[it->getSecond() - 1])
!m_impl->components[it->getSecond() - 1]) {
return;
}
m_impl->components[it->getSecond() - 1]->subsystem = subsystem.str();
}
@@ -265,14 +282,16 @@ std::shared_ptr<void> SendableRegistry::SetData(Sendable* sendable, int handle,
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->componentMap.find(sendable);
if (it == m_impl->componentMap.end() ||
!m_impl->components[it->getSecond() - 1])
!m_impl->components[it->getSecond() - 1]) {
return nullptr;
}
auto& comp = *m_impl->components[it->getSecond() - 1];
std::shared_ptr<void> rv;
if (static_cast<size_t>(handle) < comp.data.size())
if (static_cast<size_t>(handle) < comp.data.size()) {
rv = std::move(comp.data[handle]);
else
} else {
comp.data.resize(handle + 1);
}
comp.data[handle] = std::move(data);
return rv;
}
@@ -283,10 +302,13 @@ std::shared_ptr<void> SendableRegistry::GetData(Sendable* sendable,
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->componentMap.find(sendable);
if (it == m_impl->componentMap.end() ||
!m_impl->components[it->getSecond() - 1])
!m_impl->components[it->getSecond() - 1]) {
return nullptr;
}
auto& comp = *m_impl->components[it->getSecond() - 1];
if (static_cast<size_t>(handle) >= comp.data.size()) return nullptr;
if (static_cast<size_t>(handle) >= comp.data.size()) {
return nullptr;
}
return comp.data[handle];
}
@@ -294,8 +316,9 @@ void SendableRegistry::EnableLiveWindow(Sendable* sendable) {
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->componentMap.find(sendable);
if (it == m_impl->componentMap.end() ||
!m_impl->components[it->getSecond() - 1])
!m_impl->components[it->getSecond() - 1]) {
return;
}
m_impl->components[it->getSecond() - 1]->liveWindow = true;
}
@@ -303,8 +326,9 @@ void SendableRegistry::DisableLiveWindow(Sendable* sendable) {
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->componentMap.find(sendable);
if (it == m_impl->componentMap.end() ||
!m_impl->components[it->getSecond() - 1])
!m_impl->components[it->getSecond() - 1]) {
return;
}
m_impl->components[it->getSecond() - 1]->liveWindow = false;
}
@@ -317,10 +341,13 @@ SendableRegistry::UID SendableRegistry::GetUniqueId(Sendable* sendable) {
}
Sendable* SendableRegistry::GetSendable(UID uid) {
if (uid == 0) return nullptr;
std::scoped_lock lock(m_impl->mutex);
if ((uid - 1) >= m_impl->components.size() || !m_impl->components[uid - 1])
if (uid == 0) {
return nullptr;
}
std::scoped_lock lock(m_impl->mutex);
if ((uid - 1) >= m_impl->components.size() || !m_impl->components[uid - 1]) {
return nullptr;
}
return m_impl->components[uid - 1]->sendable;
}
@@ -328,8 +355,9 @@ void SendableRegistry::Publish(UID sendableUid,
std::shared_ptr<NetworkTable> table) {
std::scoped_lock lock(m_impl->mutex);
if (sendableUid == 0 || (sendableUid - 1) >= m_impl->components.size() ||
!m_impl->components[sendableUid - 1])
!m_impl->components[sendableUid - 1]) {
return;
}
auto& comp = *m_impl->components[sendableUid - 1];
comp.builder = SendableBuilderImpl{}; // clear any current builder
comp.builder.SetTable(table);
@@ -339,11 +367,14 @@ void SendableRegistry::Publish(UID sendableUid,
}
void SendableRegistry::Update(UID sendableUid) {
if (sendableUid == 0) return;
if (sendableUid == 0) {
return;
}
std::scoped_lock lock(m_impl->mutex);
if ((sendableUid - 1) >= m_impl->components.size() ||
!m_impl->components[sendableUid - 1])
!m_impl->components[sendableUid - 1]) {
return;
}
m_impl->components[sendableUid - 1]->builder.UpdateTable();
}
@@ -353,11 +384,14 @@ void SendableRegistry::ForeachLiveWindow(
assert(dataHandle >= 0);
std::scoped_lock lock(m_impl->mutex);
wpi::SmallVector<Impl::Component*, 128> components;
for (auto&& comp : m_impl->components) components.emplace_back(comp.get());
for (auto&& comp : m_impl->components) {
components.emplace_back(comp.get());
}
for (auto comp : components) {
if (comp && comp->sendable && comp->liveWindow) {
if (static_cast<size_t>(dataHandle) >= comp->data.size())
if (static_cast<size_t>(dataHandle) >= comp->data.size()) {
comp->data.resize(dataHandle + 1);
}
CallbackData cbdata{comp->sendable, comp->name,
comp->subsystem, comp->parent,
comp->data[dataHandle], comp->builder};

View File

@@ -40,7 +40,9 @@ Singleton& Singleton::GetInstance() {
return instance;
}
void SmartDashboard::init() { Singleton::GetInstance(); }
void SmartDashboard::init() {
Singleton::GetInstance();
}
bool SmartDashboard::ContainsKey(wpi::StringRef key) {
return Singleton::GetInstance().table->ContainsKey(key);
@@ -106,7 +108,9 @@ void SmartDashboard::PutData(Sendable* value) {
return;
}
auto name = SendableRegistry::GetInstance().GetName(value);
if (!name.empty()) PutData(name, value);
if (!name.empty()) {
PutData(name, value);
}
}
Sendable* SmartDashboard::GetData(wpi::StringRef key) {
@@ -256,5 +260,7 @@ void SmartDashboard::UpdateValues() {
auto& inst = Singleton::GetInstance();
listenerExecutor.RunListenerTasks();
std::scoped_lock lock(inst.tablesToDataMutex);
for (auto& i : inst.tablesToData) registry.Update(i.getValue());
for (auto& i : inst.tablesToData) {
registry.Update(i.getValue());
}
}