[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

@@ -21,7 +21,7 @@ int Command::m_commandCounter = 0;
Command::Command() : Command("", -1.0) {}
Command::Command(const wpi::Twine& name) : Command(name, -1.0) {}
Command::Command(std::string_view name) : Command(name, -1.0) {}
Command::Command(double timeout) : Command("", timeout) {}
@@ -29,7 +29,7 @@ Command::Command(Subsystem& subsystem) : Command("", -1.0) {
Requires(&subsystem);
}
Command::Command(const wpi::Twine& name, double timeout) {
Command::Command(std::string_view name, double timeout) {
// We use -1.0 to indicate no timeout.
if (timeout < 0.0 && timeout != -1.0) {
throw FRC_MakeError(err::ParameterOutOfRange, "timeout {} < 0.0", timeout);
@@ -38,16 +38,15 @@ Command::Command(const wpi::Twine& name, double timeout) {
m_timeout = timeout;
// If name contains an empty string
if (name.isTriviallyEmpty() ||
(name.isSingleStringRef() && name.getSingleStringRef().empty())) {
if (name.empty()) {
SendableRegistry::GetInstance().Add(
this, "Command_" + wpi::Twine(typeid(*this).name()));
this, fmt::format("Command_{}", typeid(*this).name()));
} else {
SendableRegistry::GetInstance().Add(this, name);
}
}
Command::Command(const wpi::Twine& name, Subsystem& subsystem)
Command::Command(std::string_view name, Subsystem& subsystem)
: Command(name, -1.0) {
Requires(&subsystem);
}
@@ -56,7 +55,7 @@ Command::Command(double timeout, Subsystem& subsystem) : Command("", timeout) {
Requires(&subsystem);
}
Command::Command(const wpi::Twine& name, double timeout, Subsystem& subsystem)
Command::Command(std::string_view name, double timeout, Subsystem& subsystem)
: Command(name, timeout) {
Requires(&subsystem);
}
@@ -183,7 +182,7 @@ bool Command::IsTimedOut() const {
return m_timeout != -1 && TimeSinceInitialized() >= m_timeout;
}
bool Command::AssertUnlocked(const std::string& message) {
bool Command::AssertUnlocked(std::string_view message) {
if (m_locked) {
throw FRC_MakeError(
err::CommandIllegalUse,
@@ -277,7 +276,7 @@ std::string Command::GetName() const {
return SendableRegistry::GetInstance().GetName(this);
}
void Command::SetName(const wpi::Twine& name) {
void Command::SetName(std::string_view name) {
SendableRegistry::GetInstance().SetName(this, name);
}
@@ -285,7 +284,7 @@ std::string Command::GetSubsystem() const {
return SendableRegistry::GetInstance().GetSubsystem(this);
}
void Command::SetSubsystem(const wpi::Twine& name) {
void Command::SetSubsystem(std::string_view name) {
SendableRegistry::GetInstance().SetSubsystem(this, name);
}

View File

@@ -8,7 +8,7 @@
using namespace frc;
CommandGroup::CommandGroup(const wpi::Twine& name) : Command(name) {}
CommandGroup::CommandGroup(std::string_view name) : Command(name) {}
void CommandGroup::AddSequential(Command* command) {
if (!command) {

View File

@@ -28,7 +28,7 @@ ConditionalCommand::ConditionalCommand(Command* onTrue, Command* onFalse) {
RequireAll(*this, onTrue, onFalse);
}
ConditionalCommand::ConditionalCommand(const wpi::Twine& name, Command* onTrue,
ConditionalCommand::ConditionalCommand(std::string_view name, Command* onTrue,
Command* onFalse)
: Command(name) {
m_onTrue = onTrue;

View File

@@ -8,11 +8,11 @@
using namespace frc;
InstantCommand::InstantCommand(const wpi::Twine& name) : Command(name) {}
InstantCommand::InstantCommand(std::string_view name) : Command(name) {}
InstantCommand::InstantCommand(Subsystem& subsystem) : Command(subsystem) {}
InstantCommand::InstantCommand(const wpi::Twine& name, Subsystem& subsystem)
InstantCommand::InstantCommand(std::string_view name, Subsystem& subsystem)
: Command(name, subsystem) {}
InstantCommand::InstantCommand(std::function<void()> func)
@@ -23,13 +23,13 @@ InstantCommand::InstantCommand(Subsystem& subsystem, std::function<void()> func)
m_func = func;
}
InstantCommand::InstantCommand(const wpi::Twine& name,
InstantCommand::InstantCommand(std::string_view name,
std::function<void()> func)
: InstantCommand(name) {
m_func = func;
}
InstantCommand::InstantCommand(const wpi::Twine& name, Subsystem& subsystem,
InstantCommand::InstantCommand(std::string_view name, Subsystem& subsystem,
std::function<void()> func)
: InstantCommand(name, subsystem) {
m_func = func;

View File

@@ -8,7 +8,7 @@
using namespace frc;
PIDCommand::PIDCommand(const wpi::Twine& name, double p, double i, double d,
PIDCommand::PIDCommand(std::string_view name, double p, double i, double d,
double f, double period)
: Command(name) {
m_controller = std::make_shared<PIDController>(p, i, d, this, this, period);
@@ -19,12 +19,12 @@ PIDCommand::PIDCommand(double p, double i, double d, double f, double period) {
std::make_shared<PIDController>(p, i, d, f, this, this, period);
}
PIDCommand::PIDCommand(const wpi::Twine& name, double p, double i, double d)
PIDCommand::PIDCommand(std::string_view name, double p, double i, double d)
: Command(name) {
m_controller = std::make_shared<PIDController>(p, i, d, this, this);
}
PIDCommand::PIDCommand(const wpi::Twine& name, double p, double i, double d,
PIDCommand::PIDCommand(std::string_view name, double p, double i, double d,
double period)
: Command(name) {
m_controller = std::make_shared<PIDController>(p, i, d, this, this, period);
@@ -38,7 +38,7 @@ PIDCommand::PIDCommand(double p, double i, double d, double period) {
m_controller = std::make_shared<PIDController>(p, i, d, this, this, period);
}
PIDCommand::PIDCommand(const wpi::Twine& name, double p, double i, double d,
PIDCommand::PIDCommand(std::string_view name, double p, double i, double d,
double f, double period, Subsystem& subsystem)
: Command(name, subsystem) {
m_controller = std::make_shared<PIDController>(p, i, d, this, this, period);
@@ -51,13 +51,13 @@ PIDCommand::PIDCommand(double p, double i, double d, double f, double period,
std::make_shared<PIDController>(p, i, d, f, this, this, period);
}
PIDCommand::PIDCommand(const wpi::Twine& name, double p, double i, double d,
PIDCommand::PIDCommand(std::string_view name, double p, double i, double d,
Subsystem& subsystem)
: Command(name, subsystem) {
m_controller = std::make_shared<PIDController>(p, i, d, this, this);
}
PIDCommand::PIDCommand(const wpi::Twine& name, double p, double i, double d,
PIDCommand::PIDCommand(std::string_view name, double p, double i, double d,
double period, Subsystem& subsystem)
: Command(name, subsystem) {
m_controller = std::make_shared<PIDController>(p, i, d, this, this, period);

View File

@@ -8,20 +8,20 @@
using namespace frc;
PIDSubsystem::PIDSubsystem(const wpi::Twine& name, double p, double i, double d)
PIDSubsystem::PIDSubsystem(std::string_view name, double p, double i, double d)
: Subsystem(name) {
m_controller = std::make_shared<PIDController>(p, i, d, this, this);
AddChild("PIDController", m_controller);
}
PIDSubsystem::PIDSubsystem(const wpi::Twine& name, double p, double i, double d,
PIDSubsystem::PIDSubsystem(std::string_view name, double p, double i, double d,
double f)
: Subsystem(name) {
m_controller = std::make_shared<PIDController>(p, i, d, f, this, this);
AddChild("PIDController", m_controller);
}
PIDSubsystem::PIDSubsystem(const wpi::Twine& name, double p, double i, double d,
PIDSubsystem::PIDSubsystem(std::string_view name, double p, double i, double d,
double f, double period)
: Subsystem(name) {
m_controller =

View File

@@ -4,13 +4,14 @@
#include "frc/commands/PrintCommand.h"
#include <fmt/format.h>
#include <wpi/raw_ostream.h>
using namespace frc;
PrintCommand::PrintCommand(const wpi::Twine& message)
: InstantCommand("Print \"" + message + wpi::Twine('"')) {
m_message = message.str();
PrintCommand::PrintCommand(std::string_view message)
: InstantCommand(fmt::format("Print \"{}\"", message)) {
m_message = message;
}
void PrintCommand::Initialize() {

View File

@@ -13,7 +13,7 @@
using namespace frc;
Subsystem::Subsystem(const wpi::Twine& name) {
Subsystem::Subsystem(std::string_view name) {
SendableRegistry::GetInstance().AddLW(this, name, name);
Scheduler::GetInstance()->RegisterSubsystem(this);
}
@@ -40,12 +40,12 @@ Command* Subsystem::GetDefaultCommand() {
return m_defaultCommand;
}
wpi::StringRef Subsystem::GetDefaultCommandName() {
std::string Subsystem::GetDefaultCommandName() {
Command* defaultCommand = GetDefaultCommand();
if (defaultCommand) {
return SendableRegistry::GetInstance().GetName(defaultCommand);
} else {
return wpi::StringRef();
return {};
}
}
@@ -58,12 +58,12 @@ Command* Subsystem::GetCurrentCommand() const {
return m_currentCommand;
}
wpi::StringRef Subsystem::GetCurrentCommandName() const {
std::string Subsystem::GetCurrentCommandName() const {
Command* currentCommand = GetCurrentCommand();
if (currentCommand) {
return SendableRegistry::GetInstance().GetName(currentCommand);
} else {
return wpi::StringRef();
return {};
}
}
@@ -75,7 +75,7 @@ std::string Subsystem::GetName() const {
return SendableRegistry::GetInstance().GetName(this);
}
void Subsystem::SetName(const wpi::Twine& name) {
void Subsystem::SetName(std::string_view name) {
SendableRegistry::GetInstance().SetName(this, name);
}
@@ -83,20 +83,20 @@ std::string Subsystem::GetSubsystem() const {
return SendableRegistry::GetInstance().GetSubsystem(this);
}
void Subsystem::SetSubsystem(const wpi::Twine& name) {
void Subsystem::SetSubsystem(std::string_view name) {
SendableRegistry::GetInstance().SetSubsystem(this, name);
}
void Subsystem::AddChild(const wpi::Twine& name,
void Subsystem::AddChild(std::string_view name,
std::shared_ptr<Sendable> child) {
AddChild(name, *child);
}
void Subsystem::AddChild(const wpi::Twine& name, Sendable* child) {
void Subsystem::AddChild(std::string_view name, Sendable* child) {
AddChild(name, *child);
}
void Subsystem::AddChild(const wpi::Twine& name, Sendable& child) {
void Subsystem::AddChild(std::string_view name, Sendable& child) {
auto& registry = SendableRegistry::GetInstance();
registry.AddLW(&child, registry.GetSubsystem(this), name);
}

View File

@@ -6,12 +6,12 @@
using namespace frc;
TimedCommand::TimedCommand(const wpi::Twine& name, double timeout)
TimedCommand::TimedCommand(std::string_view name, double timeout)
: Command(name, timeout) {}
TimedCommand::TimedCommand(double timeout) : Command(timeout) {}
TimedCommand::TimedCommand(const wpi::Twine& name, double timeout,
TimedCommand::TimedCommand(std::string_view name, double timeout,
Subsystem& subsystem)
: Command(name, timeout, subsystem) {}

View File

@@ -4,10 +4,12 @@
#include "frc/commands/WaitCommand.h"
#include <fmt/format.h>
using namespace frc;
WaitCommand::WaitCommand(double timeout)
: TimedCommand("Wait(" + std::to_string(timeout) + ")", timeout) {}
: TimedCommand(fmt::format("Wait({})", timeout), timeout) {}
WaitCommand::WaitCommand(const wpi::Twine& name, double timeout)
WaitCommand::WaitCommand(std::string_view name, double timeout)
: TimedCommand(name, timeout) {}

View File

@@ -11,7 +11,7 @@ using namespace frc;
WaitForChildren::WaitForChildren(double timeout)
: Command("WaitForChildren", timeout) {}
WaitForChildren::WaitForChildren(const wpi::Twine& name, double timeout)
WaitForChildren::WaitForChildren(std::string_view name, double timeout)
: Command(name, timeout) {}
bool WaitForChildren::IsFinished() {

View File

@@ -13,7 +13,7 @@ WaitUntilCommand::WaitUntilCommand(double time)
m_time = time;
}
WaitUntilCommand::WaitUntilCommand(const wpi::Twine& name, double time)
WaitUntilCommand::WaitUntilCommand(std::string_view name, double time)
: Command(name, time) {
m_time = time;
}