[wpilibc] Use std::string_view instead of Twine (#3380)

Use fmtlib where needed for string formatting into std::string_view.
This commit is contained in:
Peter Johnson
2021-05-26 17:44:18 -07:00
committed by GitHub
parent 50915cb7ed
commit 4e2c3051be
76 changed files with 387 additions and 419 deletions

View File

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

View File

@@ -8,7 +8,7 @@
using namespace frc;
MechanismLigament2d::MechanismLigament2d(const wpi::Twine& name, double length,
MechanismLigament2d::MechanismLigament2d(std::string_view name, double length,
units::degree_t angle,
double lineWeight,
const frc::Color8Bit& color)

View File

@@ -6,8 +6,7 @@
using namespace frc;
MechanismObject2d::MechanismObject2d(const wpi::Twine& name)
: m_name{name.str()} {}
MechanismObject2d::MechanismObject2d(std::string_view name) : m_name{name} {}
const std::string& MechanismObject2d::GetName() const {
return m_name;

View File

@@ -12,9 +12,9 @@ using namespace frc;
static constexpr char kPosition[] = "pos";
MechanismRoot2d::MechanismRoot2d(const wpi::Twine& name, double x, double y,
MechanismRoot2d::MechanismRoot2d(std::string_view name, double x, double y,
const private_init&)
: MechanismObject2d(name.str()), m_x{x}, m_y{y} {}
: MechanismObject2d(name), m_x{x}, m_y{y} {}
void MechanismRoot2d::SetPosition(double x, double y) {
std::scoped_lock lock(m_mutex);

View File

@@ -76,7 +76,7 @@ void SendableBuilderImpl::ClearProperties() {
m_properties.clear();
}
void SendableBuilderImpl::SetSmartDashboardType(const wpi::Twine& type) {
void SendableBuilderImpl::SetSmartDashboardType(std::string_view type) {
m_table->GetEntry(".type").SetString(type);
}
@@ -93,11 +93,11 @@ void SendableBuilderImpl::SetUpdateTable(std::function<void()> func) {
m_updateTables.emplace_back(std::move(func));
}
nt::NetworkTableEntry SendableBuilderImpl::GetEntry(const wpi::Twine& key) {
nt::NetworkTableEntry SendableBuilderImpl::GetEntry(std::string_view key) {
return m_table->GetEntry(key);
}
void SendableBuilderImpl::AddBooleanProperty(const wpi::Twine& key,
void SendableBuilderImpl::AddBooleanProperty(std::string_view key,
std::function<bool()> getter,
std::function<void(bool)> setter) {
m_properties.emplace_back(*m_table, key);
@@ -124,7 +124,7 @@ void SendableBuilderImpl::AddBooleanProperty(const wpi::Twine& key,
}
void SendableBuilderImpl::AddDoubleProperty(
const wpi::Twine& key, std::function<double()> getter,
std::string_view key, std::function<double()> getter,
std::function<void(double)> setter) {
m_properties.emplace_back(*m_table, key);
if (getter) {
@@ -150,7 +150,7 @@ void SendableBuilderImpl::AddDoubleProperty(
}
void SendableBuilderImpl::AddStringProperty(
const wpi::Twine& key, std::function<std::string()> getter,
std::string_view key, std::function<std::string()> getter,
std::function<void(wpi::StringRef)> setter) {
m_properties.emplace_back(*m_table, key);
if (getter) {
@@ -176,7 +176,7 @@ void SendableBuilderImpl::AddStringProperty(
}
void SendableBuilderImpl::AddBooleanArrayProperty(
const wpi::Twine& key, std::function<std::vector<int>()> getter,
std::string_view key, std::function<std::vector<int>()> getter,
std::function<void(wpi::ArrayRef<int>)> setter) {
m_properties.emplace_back(*m_table, key);
if (getter) {
@@ -202,7 +202,7 @@ void SendableBuilderImpl::AddBooleanArrayProperty(
}
void SendableBuilderImpl::AddDoubleArrayProperty(
const wpi::Twine& key, std::function<std::vector<double>()> getter,
std::string_view key, std::function<std::vector<double>()> getter,
std::function<void(wpi::ArrayRef<double>)> setter) {
m_properties.emplace_back(*m_table, key);
if (getter) {
@@ -228,7 +228,7 @@ void SendableBuilderImpl::AddDoubleArrayProperty(
}
void SendableBuilderImpl::AddStringArrayProperty(
const wpi::Twine& key, std::function<std::vector<std::string>()> getter,
std::string_view key, std::function<std::vector<std::string>()> getter,
std::function<void(wpi::ArrayRef<std::string>)> setter) {
m_properties.emplace_back(*m_table, key);
if (getter) {
@@ -254,7 +254,7 @@ void SendableBuilderImpl::AddStringArrayProperty(
}
void SendableBuilderImpl::AddRawProperty(
const wpi::Twine& key, std::function<std::string()> getter,
std::string_view key, std::function<std::string()> getter,
std::function<void(wpi::StringRef)> setter) {
m_properties.emplace_back(*m_table, key);
if (getter) {
@@ -280,7 +280,7 @@ void SendableBuilderImpl::AddRawProperty(
}
void SendableBuilderImpl::AddValueProperty(
const wpi::Twine& key, std::function<std::shared_ptr<nt::Value>()> getter,
std::string_view key, std::function<std::shared_ptr<nt::Value>()> getter,
std::function<void(std::shared_ptr<nt::Value>)> setter) {
m_properties.emplace_back(*m_table, key);
if (getter) {
@@ -302,7 +302,7 @@ void SendableBuilderImpl::AddValueProperty(
}
void SendableBuilderImpl::AddSmallStringProperty(
const wpi::Twine& key,
std::string_view key,
std::function<wpi::StringRef(wpi::SmallVectorImpl<char>& buf)> getter,
std::function<void(wpi::StringRef)> setter) {
m_properties.emplace_back(*m_table, key);
@@ -330,7 +330,7 @@ void SendableBuilderImpl::AddSmallStringProperty(
}
void SendableBuilderImpl::AddSmallBooleanArrayProperty(
const wpi::Twine& key,
std::string_view key,
std::function<wpi::ArrayRef<int>(wpi::SmallVectorImpl<int>& buf)> getter,
std::function<void(wpi::ArrayRef<int>)> setter) {
m_properties.emplace_back(*m_table, key);
@@ -358,7 +358,7 @@ void SendableBuilderImpl::AddSmallBooleanArrayProperty(
}
void SendableBuilderImpl::AddSmallDoubleArrayProperty(
const wpi::Twine& key,
std::string_view key,
std::function<wpi::ArrayRef<double>(wpi::SmallVectorImpl<double>& buf)>
getter,
std::function<void(wpi::ArrayRef<double>)> setter) {
@@ -387,7 +387,7 @@ void SendableBuilderImpl::AddSmallDoubleArrayProperty(
}
void SendableBuilderImpl::AddSmallStringArrayProperty(
const wpi::Twine& key,
std::string_view key,
std::function<
wpi::ArrayRef<std::string>(wpi::SmallVectorImpl<std::string>& buf)>
getter,
@@ -417,7 +417,7 @@ void SendableBuilderImpl::AddSmallStringArrayProperty(
}
void SendableBuilderImpl::AddSmallRawProperty(
const wpi::Twine& key,
std::string_view key,
std::function<wpi::StringRef(wpi::SmallVectorImpl<char>& buf)> getter,
std::function<void(wpi::StringRef)> setter) {
m_properties.emplace_back(*m_table, key);

View File

@@ -6,6 +6,7 @@
#include <memory>
#include <fmt/format.h>
#include <wpi/DenseMap.h>
#include <wpi/SmallVector.h>
#include <wpi/UidVector.h>
@@ -26,16 +27,12 @@ struct SendableRegistry::Impl {
bool liveWindow = false;
wpi::SmallVector<std::shared_ptr<void>, 2> data;
void SetName(const wpi::Twine& moduleType, int channel) {
name =
(moduleType + wpi::Twine('[') + wpi::Twine(channel) + wpi::Twine(']'))
.str();
void SetName(std::string_view moduleType, int channel) {
name = fmt::format("{}[{}]", moduleType, channel);
}
void SetName(const wpi::Twine& moduleType, int moduleNumber, int channel) {
name = (moduleType + wpi::Twine('[') + wpi::Twine(moduleNumber) +
wpi::Twine(',') + wpi::Twine(channel) + wpi::Twine(']'))
.str();
void SetName(std::string_view moduleType, int moduleNumber, int channel) {
name = fmt::format("{}[{},{}]", moduleType, moduleNumber, channel);
}
};
@@ -66,14 +63,14 @@ SendableRegistry& SendableRegistry::GetInstance() {
return instance;
}
void SendableRegistry::Add(Sendable* sendable, const wpi::Twine& name) {
void SendableRegistry::Add(Sendable* sendable, std::string_view name) {
std::scoped_lock lock(m_impl->mutex);
auto& comp = m_impl->GetOrAdd(sendable);
comp.sendable = sendable;
comp.name = name.str();
comp.name = name;
}
void SendableRegistry::Add(Sendable* sendable, const wpi::Twine& moduleType,
void SendableRegistry::Add(Sendable* sendable, std::string_view moduleType,
int channel) {
std::scoped_lock lock(m_impl->mutex);
auto& comp = m_impl->GetOrAdd(sendable);
@@ -81,7 +78,7 @@ void SendableRegistry::Add(Sendable* sendable, const wpi::Twine& moduleType,
comp.SetName(moduleType, channel);
}
void SendableRegistry::Add(Sendable* sendable, const wpi::Twine& moduleType,
void SendableRegistry::Add(Sendable* sendable, std::string_view moduleType,
int moduleNumber, int channel) {
std::scoped_lock lock(m_impl->mutex);
auto& comp = m_impl->GetOrAdd(sendable);
@@ -89,24 +86,24 @@ void SendableRegistry::Add(Sendable* sendable, const wpi::Twine& moduleType,
comp.SetName(moduleType, moduleNumber, channel);
}
void SendableRegistry::Add(Sendable* sendable, const wpi::Twine& subsystem,
const wpi::Twine& name) {
void SendableRegistry::Add(Sendable* sendable, std::string_view subsystem,
std::string_view name) {
std::scoped_lock lock(m_impl->mutex);
auto& comp = m_impl->GetOrAdd(sendable);
comp.sendable = sendable;
comp.name = name.str();
comp.subsystem = subsystem.str();
comp.name = name;
comp.subsystem = subsystem;
}
void SendableRegistry::AddLW(Sendable* sendable, const wpi::Twine& name) {
void SendableRegistry::AddLW(Sendable* sendable, std::string_view name) {
std::scoped_lock lock(m_impl->mutex);
auto& comp = m_impl->GetOrAdd(sendable);
comp.sendable = sendable;
comp.liveWindow = true;
comp.name = name.str();
comp.name = name;
}
void SendableRegistry::AddLW(Sendable* sendable, const wpi::Twine& moduleType,
void SendableRegistry::AddLW(Sendable* sendable, std::string_view moduleType,
int channel) {
std::scoped_lock lock(m_impl->mutex);
auto& comp = m_impl->GetOrAdd(sendable);
@@ -115,7 +112,7 @@ void SendableRegistry::AddLW(Sendable* sendable, const wpi::Twine& moduleType,
comp.SetName(moduleType, channel);
}
void SendableRegistry::AddLW(Sendable* sendable, const wpi::Twine& moduleType,
void SendableRegistry::AddLW(Sendable* sendable, std::string_view moduleType,
int moduleNumber, int channel) {
std::scoped_lock lock(m_impl->mutex);
auto& comp = m_impl->GetOrAdd(sendable);
@@ -124,14 +121,14 @@ void SendableRegistry::AddLW(Sendable* sendable, const wpi::Twine& moduleType,
comp.SetName(moduleType, moduleNumber, channel);
}
void SendableRegistry::AddLW(Sendable* sendable, const wpi::Twine& subsystem,
const wpi::Twine& name) {
void SendableRegistry::AddLW(Sendable* sendable, std::string_view subsystem,
std::string_view name) {
std::scoped_lock lock(m_impl->mutex);
auto& comp = m_impl->GetOrAdd(sendable);
comp.sendable = sendable;
comp.liveWindow = true;
comp.name = name.str();
comp.subsystem = subsystem.str();
comp.name = name;
comp.subsystem = subsystem;
}
void SendableRegistry::AddChild(Sendable* parent, Sendable* child) {
@@ -204,17 +201,17 @@ std::string SendableRegistry::GetName(const Sendable* sendable) const {
return m_impl->components[it->getSecond() - 1]->name;
}
void SendableRegistry::SetName(Sendable* sendable, const wpi::Twine& name) {
void SendableRegistry::SetName(Sendable* sendable, std::string_view 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]) {
return;
}
m_impl->components[it->getSecond() - 1]->name = name.str();
m_impl->components[it->getSecond() - 1]->name = name;
}
void SendableRegistry::SetName(Sendable* sendable, const wpi::Twine& moduleType,
void SendableRegistry::SetName(Sendable* sendable, std::string_view moduleType,
int channel) {
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->componentMap.find(sendable);
@@ -225,7 +222,7 @@ void SendableRegistry::SetName(Sendable* sendable, const wpi::Twine& moduleType,
m_impl->components[it->getSecond() - 1]->SetName(moduleType, channel);
}
void SendableRegistry::SetName(Sendable* sendable, const wpi::Twine& moduleType,
void SendableRegistry::SetName(Sendable* sendable, std::string_view moduleType,
int moduleNumber, int channel) {
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->componentMap.find(sendable);
@@ -237,8 +234,8 @@ void SendableRegistry::SetName(Sendable* sendable, const wpi::Twine& moduleType,
channel);
}
void SendableRegistry::SetName(Sendable* sendable, const wpi::Twine& subsystem,
const wpi::Twine& name) {
void SendableRegistry::SetName(Sendable* sendable, std::string_view subsystem,
std::string_view name) {
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->componentMap.find(sendable);
if (it == m_impl->componentMap.end() ||
@@ -246,8 +243,8 @@ void SendableRegistry::SetName(Sendable* sendable, const wpi::Twine& subsystem,
return;
}
auto& comp = *m_impl->components[it->getSecond() - 1];
comp.name = name.str();
comp.subsystem = subsystem.str();
comp.name = name;
comp.subsystem = subsystem;
}
std::string SendableRegistry::GetSubsystem(const Sendable* sendable) const {
@@ -261,14 +258,14 @@ std::string SendableRegistry::GetSubsystem(const Sendable* sendable) const {
}
void SendableRegistry::SetSubsystem(Sendable* sendable,
const wpi::Twine& subsystem) {
std::string_view 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]) {
return;
}
m_impl->components[it->getSecond() - 1]->subsystem = subsystem.str();
m_impl->components[it->getSecond() - 1]->subsystem = subsystem;
}
int SendableRegistry::GetDataHandle() {