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

@@ -24,7 +24,9 @@ class NTField2DModel::GroupModel : public FieldObjectGroupModel {
void NTUpdate(const nt::Value& value);
void Update() override {
if (auto value = nt::GetEntryValue(m_entry)) NTUpdate(*value);
if (auto value = nt::GetEntryValue(m_entry)) {
NTUpdate(*value);
}
}
bool Exists() override { return nt::GetEntryType(m_entry) != NT_UNASSIGNED; }
bool IsReadOnly() override { return false; }
@@ -96,12 +98,14 @@ void NTField2DModel::GroupModel::NTUpdate(const nt::Value& value) {
m_count = arr.size() / 3;
if (m_count > m_objects.size()) {
m_objects.reserve(m_count);
for (size_t i = m_objects.size(); i < m_count; ++i)
for (size_t i = m_objects.size(); i < m_count; ++i) {
m_objects.emplace_back(std::make_unique<ObjectModel>(m_name, m_entry, i));
}
}
if (m_count < m_objects.size()) {
for (size_t i = m_count; i < m_objects.size(); ++i)
for (size_t i = m_count; i < m_objects.size(); ++i) {
m_objects[i]->SetExists(false);
}
}
for (size_t i = 0; i < m_count; ++i) {
@@ -139,19 +143,31 @@ void NTField2DModel::GroupModel::ObjectModel::SetPoseImpl(double x, double y,
bool setRot) {
// get from NT, validate type and size
auto value = nt::GetEntryValue(m_entry);
if (!value || !value->IsDoubleArray()) return;
if (!value || !value->IsDoubleArray()) {
return;
}
auto origArr = value->GetDoubleArray();
if (origArr.size() < static_cast<size_t>((m_index + 1) * 3)) return;
if (origArr.size() < static_cast<size_t>((m_index + 1) * 3)) {
return;
}
// copy existing array
wpi::SmallVector<double, 8> arr;
arr.reserve(origArr.size());
for (auto&& elem : origArr) arr.emplace_back(elem);
for (auto&& elem : origArr) {
arr.emplace_back(elem);
}
// update value
if (setX) arr[m_index * 3 + 0] = x;
if (setY) arr[m_index * 3 + 1] = y;
if (setRot) arr[m_index * 3 + 2] = rot;
if (setX) {
arr[m_index * 3 + 0] = x;
}
if (setY) {
arr[m_index * 3 + 1] = y;
}
if (setRot) {
arr[m_index * 3 + 2] = rot;
}
// set back to NT
nt::SetEntryValue(m_entry, nt::Value::MakeDoubleArray(arr));
@@ -194,19 +210,24 @@ void NTField2DModel::Update() {
// handle create/delete
if (wpi::StringRef{event.name}.startswith(m_path)) {
auto name = wpi::StringRef{event.name}.drop_front(m_path.size());
if (name.empty() || name[0] == '.') continue;
if (name.empty() || name[0] == '.') {
continue;
}
auto it = std::lower_bound(m_groups.begin(), m_groups.end(), name,
[](const auto& e, wpi::StringRef name) {
return e->GetName() < name;
});
bool match = (it != m_groups.end() && (*it)->GetName() == name);
if (event.flags & NT_NOTIFY_DELETE) {
if (match) m_groups.erase(it);
if (match) {
m_groups.erase(it);
}
continue;
} else if (event.flags & NT_NOTIFY_NEW) {
if (!match)
if (!match) {
it = m_groups.emplace(
it, std::make_unique<GroupModel>(event.name, event.entry));
}
} else if (!match) {
continue;
}
@@ -221,13 +242,16 @@ bool NTField2DModel::Exists() {
return m_nt.IsConnected() && nt::GetEntryType(m_name) != NT_UNASSIGNED;
}
bool NTField2DModel::IsReadOnly() { return false; }
bool NTField2DModel::IsReadOnly() {
return false;
}
void NTField2DModel::ForEachFieldObjectGroup(
wpi::function_ref<void(FieldObjectGroupModel& model, wpi::StringRef name)>
func) {
for (auto&& group : m_groups) {
if (group->Exists())
if (group->Exists()) {
func(*group, wpi::StringRef{group->GetName()}.drop_front(m_path.size()));
}
}
}