[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

@@ -6,6 +6,7 @@
#include <chrono>
#include <string>
#include <string_view>
#include <type_traits>
#include <hal/DriverStation.h>
@@ -26,7 +27,7 @@ template <class T>
class MatchDataSenderEntry {
public:
MatchDataSenderEntry(const std::shared_ptr<nt::NetworkTable>& table,
const wpi::Twine& key, const T& initialVal) {
std::string_view key, const T& initialVal) {
static_assert(std::is_same_v<T, bool> || std::is_same_v<T, double> ||
std::is_same_v<T, std::string>,
"Invalid type for MatchDataSenderEntry - must be "
@@ -56,7 +57,7 @@ class MatchDataSenderEntry {
void SetValue(bool val) { ntEntry.SetBoolean(val); }
void SetValue(double val) { ntEntry.SetDouble(val); }
void SetValue(const wpi::Twine& val) { ntEntry.SetString(val); }
void SetValue(std::string_view val) { ntEntry.SetString(val); }
};
class MatchDataSender {

View File

@@ -6,10 +6,10 @@
#include <utility>
#include <fmt/format.h>
#include <hal/FRCUsageReporting.h>
#include <hal/Notifier.h>
#include <hal/Threads.h>
#include <wpi/SmallString.h>
#include "frc/Errors.h"
#include "frc/Timer.h"
@@ -138,11 +138,12 @@ Notifier& Notifier::operator=(Notifier&& rhs) {
return *this;
}
void Notifier::SetName(const wpi::Twine& name) {
wpi::SmallString<64> nameBuf;
void Notifier::SetName(std::string_view name) {
fmt::memory_buffer buf;
fmt::format_to(buf, "{}", name);
buf.push_back('\0'); // null terminate
int32_t status = 0;
HAL_SetNotifierName(m_notifier,
name.toNullTerminatedStringRef(nameBuf).data(), &status);
HAL_SetNotifierName(m_notifier, buf.data(), &status);
}
void Notifier::SetHandler(std::function<void()> handler) {

View File

@@ -4,6 +4,7 @@
#include "frc/PowerDistributionPanel.h"
#include <fmt/format.h>
#include <hal/FRCUsageReporting.h>
#include <hal/PDP.h>
#include <hal/Ports.h>
@@ -99,7 +100,7 @@ void PowerDistributionPanel::InitSendable(SendableBuilder& builder) {
builder.SetSmartDashboardType("PowerDistributionPanel");
for (int i = 0; i < SensorUtil::kPDPChannels; ++i) {
builder.AddDoubleProperty(
"Chan" + wpi::Twine(i), [=]() { return GetCurrent(i); }, nullptr);
fmt::format("Chan{}", i), [=]() { return GetCurrent(i); }, nullptr);
}
builder.AddDoubleProperty(
"Voltage", [=]() { return GetVoltage(); }, nullptr);

View File

@@ -8,8 +8,8 @@
using namespace frc;
ScopedTracer::ScopedTracer(wpi::Twine name, wpi::raw_ostream& os)
: m_name(name.str()), m_os(os) {
ScopedTracer::ScopedTracer(std::string_view name, wpi::raw_ostream& os)
: m_name(name), m_os(os) {
m_tracer.ResetTimer();
}

View File

@@ -42,16 +42,14 @@ SerialPort::SerialPort(int baudRate, Port port, int dataBits,
static_cast<uint8_t>(port) + 1);
}
SerialPort::SerialPort(int baudRate, const wpi::Twine& portName, Port port,
SerialPort::SerialPort(int baudRate, std::string_view portName, Port port,
int dataBits, SerialPort::Parity parity,
SerialPort::StopBits stopBits) {
int32_t status = 0;
wpi::SmallVector<char, 64> buf;
const char* portNameC = portName.toNullTerminatedStringRef(buf).data();
m_portHandle = HAL_InitializeSerialPortDirect(
static_cast<HAL_SerialPort>(port), portNameC, &status);
m_portHandle =
HAL_InitializeSerialPortDirect(static_cast<HAL_SerialPort>(port),
std::string(portName).c_str(), &status);
FRC_CheckErrorStatus(status, "Port {}", port);
HAL_SetSerialBaudRate(m_portHandle, baudRate, &status);
FRC_CheckErrorStatus(status, "SetSerialBaudRate {}", baudRate);
@@ -113,10 +111,10 @@ int SerialPort::Read(char* buffer, int count) {
}
int SerialPort::Write(const char* buffer, int count) {
return Write(wpi::StringRef(buffer, static_cast<size_t>(count)));
return Write(std::string_view(buffer, static_cast<size_t>(count)));
}
int SerialPort::Write(wpi::StringRef buffer) {
int SerialPort::Write(std::string_view buffer) {
int32_t status = 0;
int retVal =
HAL_WriteSerial(m_portHandle, buffer.data(), buffer.size(), &status);

View File

@@ -15,8 +15,6 @@
using namespace frc;
using wpi::Twine;
struct LiveWindow::Impl {
Impl();

View File

@@ -42,7 +42,7 @@ int PWMMotorController::GetChannel() const {
return m_pwm.GetChannel();
}
PWMMotorController::PWMMotorController(const wpi::Twine& name, int channel)
PWMMotorController::PWMMotorController(std::string_view name, int channel)
: m_pwm(channel, false) {
SendableRegistry::GetInstance().AddLW(this, name, channel);
}

View File

@@ -9,7 +9,7 @@
using namespace frc;
ComplexWidget::ComplexWidget(ShuffleboardContainer& parent,
const wpi::Twine& title, Sendable& sendable)
std::string_view title, Sendable& sendable)
: ShuffleboardValue(title),
ShuffleboardWidget(parent, title),
m_sendable(sendable) {}

View File

@@ -4,21 +4,15 @@
#include "frc/shuffleboard/ShuffleboardComponentBase.h"
#include <wpi/SmallVector.h>
using namespace frc;
ShuffleboardComponentBase::ShuffleboardComponentBase(
ShuffleboardContainer& parent, const wpi::Twine& title,
const wpi::Twine& type)
: ShuffleboardValue(title), m_parent(parent) {
wpi::SmallVector<char, 16> storage;
m_type = type.toStringRef(storage);
}
ShuffleboardContainer& parent, std::string_view title,
std::string_view type)
: ShuffleboardValue(title), m_parent(parent), m_type(type) {}
void ShuffleboardComponentBase::SetType(const wpi::Twine& type) {
wpi::SmallVector<char, 16> storage;
m_type = type.toStringRef(storage);
void ShuffleboardComponentBase::SetType(std::string_view type) {
m_type = type;
m_metadataDirty = true;
}

View File

@@ -4,7 +4,6 @@
#include "frc/shuffleboard/ShuffleboardContainer.h"
#include <wpi/SmallVector.h>
#include <wpi/raw_ostream.h>
#include "frc/Errors.h"
@@ -22,7 +21,7 @@ static constexpr const char* GetStringFromBuiltInLayout(BuiltInLayouts layout) {
return layoutStrings[static_cast<int>(layout)];
}
ShuffleboardContainer::ShuffleboardContainer(const wpi::Twine& title)
ShuffleboardContainer::ShuffleboardContainer(std::string_view title)
: ShuffleboardValue(title) {}
const std::vector<std::unique_ptr<ShuffleboardComponentBase>>&
@@ -30,40 +29,36 @@ ShuffleboardContainer::GetComponents() const {
return m_components;
}
ShuffleboardLayout& ShuffleboardContainer::GetLayout(const wpi::Twine& title,
ShuffleboardLayout& ShuffleboardContainer::GetLayout(std::string_view title,
BuiltInLayouts type) {
return GetLayout(title, GetStringFromBuiltInLayout(type));
}
ShuffleboardLayout& ShuffleboardContainer::GetLayout(const wpi::Twine& title,
ShuffleboardLayout& ShuffleboardContainer::GetLayout(std::string_view title,
const LayoutType& type) {
return GetLayout(title, type.GetLayoutName());
}
ShuffleboardLayout& ShuffleboardContainer::GetLayout(const wpi::Twine& title,
const wpi::Twine& type) {
wpi::SmallVector<char, 16> storage;
auto titleRef = title.toStringRef(storage);
if (m_layouts.count(titleRef) == 0) {
auto layout = std::make_unique<ShuffleboardLayout>(*this, titleRef, type);
ShuffleboardLayout& ShuffleboardContainer::GetLayout(std::string_view title,
std::string_view type) {
if (m_layouts.count(title) == 0) {
auto layout = std::make_unique<ShuffleboardLayout>(*this, title, type);
auto ptr = layout.get();
m_components.emplace_back(std::move(layout));
m_layouts.insert(std::make_pair(titleRef, ptr));
m_layouts.insert(std::make_pair(title, ptr));
}
return *m_layouts[titleRef];
return *m_layouts[title];
}
ShuffleboardLayout& ShuffleboardContainer::GetLayout(const wpi::Twine& title) {
wpi::SmallVector<char, 16> storage;
auto titleRef = title.toStringRef(storage);
if (m_layouts.count(titleRef) == 0) {
ShuffleboardLayout& ShuffleboardContainer::GetLayout(std::string_view title) {
if (m_layouts.count(title) == 0) {
throw FRC_MakeError(err::InvalidParameter,
"No layout with title {} has been defined", titleRef);
"No layout with title {} has been defined", title);
}
return *m_layouts[titleRef];
return *m_layouts[title];
}
ComplexWidget& ShuffleboardContainer::Add(const wpi::Twine& title,
ComplexWidget& ShuffleboardContainer::Add(std::string_view title,
Sendable& sendable) {
CheckTitle(title);
auto widget = std::make_unique<ComplexWidget>(*this, title, sendable);
@@ -81,7 +76,7 @@ ComplexWidget& ShuffleboardContainer::Add(Sendable& sendable) {
}
SimpleWidget& ShuffleboardContainer::Add(
const wpi::Twine& title, std::shared_ptr<nt::Value> defaultValue) {
std::string_view title, std::shared_ptr<nt::Value> defaultValue) {
CheckTitle(title);
auto widget = std::make_unique<SimpleWidget>(*this, title);
@@ -91,48 +86,48 @@ SimpleWidget& ShuffleboardContainer::Add(
return *ptr;
}
SimpleWidget& ShuffleboardContainer::Add(const wpi::Twine& title,
SimpleWidget& ShuffleboardContainer::Add(std::string_view title,
bool defaultValue) {
return Add(title, nt::Value::MakeBoolean(defaultValue));
}
SimpleWidget& ShuffleboardContainer::Add(const wpi::Twine& title,
SimpleWidget& ShuffleboardContainer::Add(std::string_view title,
double defaultValue) {
return Add(title, nt::Value::MakeDouble(defaultValue));
}
SimpleWidget& ShuffleboardContainer::Add(const wpi::Twine& title,
SimpleWidget& ShuffleboardContainer::Add(std::string_view title,
int defaultValue) {
return Add(title, nt::Value::MakeDouble(defaultValue));
}
SimpleWidget& ShuffleboardContainer::Add(const wpi::Twine& title,
const wpi::Twine& defaultValue) {
SimpleWidget& ShuffleboardContainer::Add(std::string_view title,
std::string_view defaultValue) {
return Add(title, nt::Value::MakeString(defaultValue));
}
SimpleWidget& ShuffleboardContainer::Add(const wpi::Twine& title,
SimpleWidget& ShuffleboardContainer::Add(std::string_view title,
const char* defaultValue) {
return Add(title, nt::Value::MakeString(defaultValue));
}
SimpleWidget& ShuffleboardContainer::Add(const wpi::Twine& title,
SimpleWidget& ShuffleboardContainer::Add(std::string_view title,
wpi::ArrayRef<bool> defaultValue) {
return Add(title, nt::Value::MakeBooleanArray(defaultValue));
}
SimpleWidget& ShuffleboardContainer::Add(const wpi::Twine& title,
SimpleWidget& ShuffleboardContainer::Add(std::string_view title,
wpi::ArrayRef<double> defaultValue) {
return Add(title, nt::Value::MakeDoubleArray(defaultValue));
}
SimpleWidget& ShuffleboardContainer::Add(
const wpi::Twine& title, wpi::ArrayRef<std::string> defaultValue) {
std::string_view title, wpi::ArrayRef<std::string> defaultValue) {
return Add(title, nt::Value::MakeStringArray(defaultValue));
}
SuppliedValueWidget<std::string>& ShuffleboardContainer::AddString(
const wpi::Twine& title, std::function<std::string()> supplier) {
std::string_view title, std::function<std::string()> supplier) {
static auto setter = [](nt::NetworkTableEntry entry, std::string value) {
entry.SetString(value);
};
@@ -146,7 +141,7 @@ SuppliedValueWidget<std::string>& ShuffleboardContainer::AddString(
}
SuppliedValueWidget<double>& ShuffleboardContainer::AddNumber(
const wpi::Twine& title, std::function<double()> supplier) {
std::string_view title, std::function<double()> supplier) {
static auto setter = [](nt::NetworkTableEntry entry, double value) {
entry.SetDouble(value);
};
@@ -160,7 +155,7 @@ SuppliedValueWidget<double>& ShuffleboardContainer::AddNumber(
}
SuppliedValueWidget<bool>& ShuffleboardContainer::AddBoolean(
const wpi::Twine& title, std::function<bool()> supplier) {
std::string_view title, std::function<bool()> supplier) {
static auto setter = [](nt::NetworkTableEntry entry, bool value) {
entry.SetBoolean(value);
};
@@ -175,7 +170,7 @@ SuppliedValueWidget<bool>& ShuffleboardContainer::AddBoolean(
SuppliedValueWidget<std::vector<std::string>>&
ShuffleboardContainer::AddStringArray(
const wpi::Twine& title,
std::string_view title,
std::function<std::vector<std::string>()> supplier) {
static auto setter = [](nt::NetworkTableEntry entry,
std::vector<std::string> value) {
@@ -191,7 +186,7 @@ ShuffleboardContainer::AddStringArray(
}
SuppliedValueWidget<std::vector<double>>& ShuffleboardContainer::AddNumberArray(
const wpi::Twine& title, std::function<std::vector<double>()> supplier) {
std::string_view title, std::function<std::vector<double>()> supplier) {
static auto setter = [](nt::NetworkTableEntry entry,
std::vector<double> value) {
entry.SetDoubleArray(value);
@@ -206,7 +201,7 @@ SuppliedValueWidget<std::vector<double>>& ShuffleboardContainer::AddNumberArray(
}
SuppliedValueWidget<std::vector<int>>& ShuffleboardContainer::AddBooleanArray(
const wpi::Twine& title, std::function<std::vector<int>()> supplier) {
std::string_view title, std::function<std::vector<int>()> supplier) {
static auto setter = [](nt::NetworkTableEntry entry, std::vector<int> value) {
entry.SetBooleanArray(value);
};
@@ -219,14 +214,14 @@ SuppliedValueWidget<std::vector<int>>& ShuffleboardContainer::AddBooleanArray(
return *ptr;
}
SuppliedValueWidget<wpi::StringRef>& ShuffleboardContainer::AddRaw(
const wpi::Twine& title, std::function<wpi::StringRef()> supplier) {
static auto setter = [](nt::NetworkTableEntry entry, wpi::StringRef value) {
SuppliedValueWidget<std::string_view>& ShuffleboardContainer::AddRaw(
std::string_view title, std::function<std::string_view()> supplier) {
static auto setter = [](nt::NetworkTableEntry entry, std::string_view value) {
entry.SetRaw(value);
};
CheckTitle(title);
auto widget = std::make_unique<SuppliedValueWidget<wpi::StringRef>>(
auto widget = std::make_unique<SuppliedValueWidget<std::string_view>>(
*this, title, supplier, setter);
auto ptr = widget.get();
m_components.emplace_back(std::move(widget));
@@ -234,44 +229,44 @@ SuppliedValueWidget<wpi::StringRef>& ShuffleboardContainer::AddRaw(
}
SimpleWidget& ShuffleboardContainer::AddPersistent(
const wpi::Twine& title, std::shared_ptr<nt::Value> defaultValue) {
std::string_view title, std::shared_ptr<nt::Value> defaultValue) {
auto& widget = Add(title, defaultValue);
widget.GetEntry().SetPersistent();
return widget;
}
SimpleWidget& ShuffleboardContainer::AddPersistent(const wpi::Twine& title,
SimpleWidget& ShuffleboardContainer::AddPersistent(std::string_view title,
bool defaultValue) {
return AddPersistent(title, nt::Value::MakeBoolean(defaultValue));
}
SimpleWidget& ShuffleboardContainer::AddPersistent(const wpi::Twine& title,
SimpleWidget& ShuffleboardContainer::AddPersistent(std::string_view title,
double defaultValue) {
return AddPersistent(title, nt::Value::MakeDouble(defaultValue));
}
SimpleWidget& ShuffleboardContainer::AddPersistent(const wpi::Twine& title,
SimpleWidget& ShuffleboardContainer::AddPersistent(std::string_view title,
int defaultValue) {
return AddPersistent(title, nt::Value::MakeDouble(defaultValue));
}
SimpleWidget& ShuffleboardContainer::AddPersistent(
const wpi::Twine& title, const wpi::Twine& defaultValue) {
std::string_view title, std::string_view defaultValue) {
return AddPersistent(title, nt::Value::MakeString(defaultValue));
}
SimpleWidget& ShuffleboardContainer::AddPersistent(
const wpi::Twine& title, wpi::ArrayRef<bool> defaultValue) {
std::string_view title, wpi::ArrayRef<bool> defaultValue) {
return AddPersistent(title, nt::Value::MakeBooleanArray(defaultValue));
}
SimpleWidget& ShuffleboardContainer::AddPersistent(
const wpi::Twine& title, wpi::ArrayRef<double> defaultValue) {
std::string_view title, wpi::ArrayRef<double> defaultValue) {
return AddPersistent(title, nt::Value::MakeDoubleArray(defaultValue));
}
SimpleWidget& ShuffleboardContainer::AddPersistent(
const wpi::Twine& title, wpi::ArrayRef<std::string> defaultValue) {
std::string_view title, wpi::ArrayRef<std::string> defaultValue) {
return AddPersistent(title, nt::Value::MakeStringArray(defaultValue));
}
@@ -287,12 +282,11 @@ void ShuffleboardContainer::DisableIfActuator() {
}
}
void ShuffleboardContainer::CheckTitle(const wpi::Twine& title) {
wpi::SmallVector<char, 16> storage;
auto titleRef = title.toStringRef(storage);
if (m_usedTitles.count(titleRef) > 0) {
void ShuffleboardContainer::CheckTitle(std::string_view title) {
std::string titleStr{title};
if (m_usedTitles.count(titleStr) > 0) {
wpi::errs() << "Title is already in use: " << title << "\n";
return;
}
m_usedTitles.insert(titleRef);
m_usedTitles.insert(titleStr);
}

View File

@@ -7,8 +7,8 @@
using namespace frc;
ShuffleboardLayout::ShuffleboardLayout(ShuffleboardContainer& parent,
const wpi::Twine& title,
const wpi::Twine& type)
std::string_view title,
std::string_view type)
: ShuffleboardValue(title),
ShuffleboardComponent(parent, title, type),
ShuffleboardContainer(title) {

View File

@@ -11,7 +11,7 @@
using namespace frc;
SimpleWidget::SimpleWidget(ShuffleboardContainer& parent,
const wpi::Twine& title)
std::string_view title)
: ShuffleboardValue(title), ShuffleboardWidget(parent, title), m_entry() {}
nt::NetworkTableEntry SimpleWidget::GetEntry() {

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() {