Use std::string_view and fmtlib across all libraries (#3402)

- Twine, StringRef, Format, and NativeFormatting have been removed
- Logging now uses fmtlib style formatting
- Nearly all uses of wpi::outs/errs have been replaced with fmt::print() or
std::puts()/std::fputs() (for unformatted strings).
- A wpi/fmt/raw_ostream.h header has been added to enable
fmt::print() with wpi::raw_ostream
This commit is contained in:
Peter Johnson
2021-06-06 16:13:58 -07:00
committed by GitHub
parent 4f1cecb8e7
commit b2c3b2dd8e
441 changed files with 5061 additions and 9749 deletions

View File

@@ -9,7 +9,6 @@
#include <hal/Ports.h>
#include <hal/simulation/AddressableLEDData.h>
#include <imgui.h>
#include <wpi/StringRef.h>
#include "HALSimGui.h"

View File

@@ -13,15 +13,18 @@
#include <cstring>
#include <memory>
#include <string>
#include <string_view>
#include <vector>
#include <GLFW/glfw3.h>
#include <fmt/format.h>
#include <hal/DriverStationTypes.h>
#include <hal/simulation/DriverStationData.h>
#include <hal/simulation/MockHooks.h>
#include <imgui.h>
#include <imgui_internal.h>
#include <wpi/SmallVector.h>
#include <wpi/StringRef.h>
#include <wpi/StringExtras.h>
#include <wpigui.h>
#include "HALDataSource.h"
@@ -104,7 +107,7 @@ class KeyboardJoystick : public SystemJoystick {
void ClearKey(int key);
virtual const char* GetKeyName(int key) const = 0;
void ReadIni(wpi::StringRef name, wpi::StringRef value);
void ReadIni(std::string_view name, std::string_view value);
void WriteIni(ImGuiTextBuffer* out_buf) const;
protected:
@@ -210,14 +213,14 @@ class FMSSimModel : public glass::FMSModel {
glass::DataSource* GetEnabledData() override { return &m_enabled; }
glass::DataSource* GetTestData() override { return &m_test; }
glass::DataSource* GetAutonomousData() override { return &m_autonomous; }
wpi::StringRef GetGameSpecificMessage(
std::string_view GetGameSpecificMessage(
wpi::SmallVectorImpl<char>& buf) override {
HAL_MatchInfo info;
HALSIM_GetMatchInfo(&info);
buf.clear();
buf.append(info.gameSpecificMessage,
info.gameSpecificMessage + info.gameSpecificMessageSize);
return wpi::StringRef(buf.begin(), buf.size());
return std::string_view(buf.begin(), buf.size());
}
void SetFmsAttached(bool val) override {
@@ -299,17 +302,15 @@ JoystickModel::JoystickModel(int index) : m_index{index} {
axisCount = halAxes.count;
for (int i = 0; i < axisCount; ++i) {
axes[i] = std::make_unique<glass::DataSource>(
"Joystick[" + wpi::Twine{index} + "] Axis[" + wpi::Twine{i} +
wpi::Twine{']'});
fmt::format("Joystick[{}] Axis[{}]", index, i));
}
HAL_JoystickButtons halButtons;
HALSIM_GetJoystickButtons(index, &halButtons);
buttonCount = halButtons.count;
for (int i = 0; i < buttonCount; ++i) {
buttons[i] =
new glass::DataSource("Joystick[" + wpi::Twine{index} + "] Button[" +
wpi::Twine{i + 1} + wpi::Twine{']'});
buttons[i] = new glass::DataSource(
fmt::format("Joystick[{}] Button[{}]", index, i + 1));
buttons[i]->SetDigital(true);
}
for (int i = buttonCount; i < 32; ++i) {
@@ -321,8 +322,7 @@ JoystickModel::JoystickModel(int index) : m_index{index} {
povCount = halPOVs.count;
for (int i = 0; i < povCount; ++i) {
povs[i] = std::make_unique<glass::DataSource>(
"Joystick[" + wpi::Twine{index} + "] POV[" + wpi::Twine{i} +
wpi::Twine{']'});
fmt::format("Joystick[{}] POV [{}]", index, i));
}
m_callback =
@@ -360,10 +360,7 @@ void JoystickModel::CallbackFunc(const char*, void* param, const HAL_Value*) {
// read/write joystick mapping to ini file
static void* JoystickReadOpen(ImGuiContext* ctx, ImGuiSettingsHandler* handler,
const char* name) {
int num;
if (wpi::StringRef{name}.getAsInteger(10, num)) {
return nullptr;
}
int num = wpi::parse_integer<int>(name, 10).value_or(-1);
if (num < 0 || num >= HAL_kMaxJoysticks) {
return nullptr;
}
@@ -371,21 +368,18 @@ static void* JoystickReadOpen(ImGuiContext* ctx, ImGuiSettingsHandler* handler,
}
static void JoystickReadLine(ImGuiContext* ctx, ImGuiSettingsHandler* handler,
void* entry, const char* lineStr) {
void* entry, const char* line) {
RobotJoystick* joy = static_cast<RobotJoystick*>(entry);
// format: guid=guid or useGamepad=0/1
wpi::StringRef line{lineStr};
auto [name, value] = line.split('=');
name = name.trim();
value = value.trim();
auto [name, value] = wpi::split(line, '=');
name = wpi::trim(name);
value = wpi::trim(value);
if (name == "guid") {
joy->guid = value;
} else if (name == "useGamepad") {
int num;
if (value.getAsInteger(10, num)) {
return;
if (auto num = wpi::parse_integer<int>(value, 10)) {
joy->useGamepad = num.value();
}
joy->useGamepad = num;
} else {
joy->name.ReadIni(name, value);
}
@@ -417,10 +411,7 @@ static void JoystickWriteAll(ImGuiContext* ctx, ImGuiSettingsHandler* handler,
static void* KeyboardJoystickReadOpen(ImGuiContext* ctx,
ImGuiSettingsHandler* handler,
const char* name) {
int num;
if (wpi::StringRef{name}.getAsInteger(10, num)) {
return nullptr;
}
int num = wpi::parse_integer<int>(name, 10).value_or(-1);
if (num < 0 || num >= static_cast<int>(gKeyboardJoysticks.size())) {
return nullptr;
}
@@ -431,12 +422,11 @@ static void* KeyboardJoystickReadOpen(ImGuiContext* ctx,
static void KeyboardJoystickReadLine(ImGuiContext* ctx,
ImGuiSettingsHandler* handler, void* entry,
const char* lineStr) {
const char* line) {
auto joy = static_cast<KeyboardJoystick*>(entry);
// format: guid=guid or useGamepad=0/1
wpi::StringRef line{lineStr};
auto [name, value] = line.split('=');
joy->ReadIni(name.trim(), value.trim());
auto [name, value] = wpi::split(line, '=');
joy->ReadIni(wpi::trim(name), wpi::trim(value));
}
static void KeyboardJoystickWriteAll(ImGuiContext* ctx,
@@ -453,7 +443,7 @@ static void KeyboardJoystickWriteAll(ImGuiContext* ctx,
static void* DriverStationReadOpen(ImGuiContext* ctx,
ImGuiSettingsHandler* handler,
const char* name) {
if (name == wpi::StringRef{"Main"}) {
if (name == std::string_view{"Main"}) {
return &gDisableDS;
}
return nullptr;
@@ -461,35 +451,26 @@ static void* DriverStationReadOpen(ImGuiContext* ctx,
static void DriverStationReadLine(ImGuiContext* ctx,
ImGuiSettingsHandler* handler, void* entry,
const char* lineStr) {
wpi::StringRef line{lineStr};
auto [name, value] = line.split('=');
name = name.trim();
value = value.trim();
const char* line) {
auto [name, value] = wpi::split(line, '=');
name = wpi::trim(name);
value = wpi::trim(value);
if (name == "disable") {
int num;
if (value.getAsInteger(10, num)) {
return;
if (auto num = wpi::parse_integer<int>(value, 10)) {
gDisableDS = num.value();
}
gDisableDS = num;
} else if (name == "zeroDisconnectedJoysticks") {
int num;
if (value.getAsInteger(10, num)) {
return;
if (auto num = wpi::parse_integer<int>(value, 10)) {
gZeroDisconnectedJoysticks = num.value();
}
gZeroDisconnectedJoysticks = num;
} else if (name == "enableDisableKeys") {
int num;
if (value.getAsInteger(10, num)) {
return;
if (auto num = wpi::parse_integer<int>(value, 10)) {
gUseEnableDisableHotkeys = num.value();
}
gUseEnableDisableHotkeys = num;
} else if (name == "estopKey") {
int num;
if (value.getAsInteger(10, num)) {
return;
if (auto num = wpi::parse_integer<int>(value, 10)) {
gUseEstopHotkey = num.value();
}
gUseEstopHotkey = num;
}
}
@@ -914,92 +895,76 @@ void KeyboardJoystick::ClearKey(int key) {
}
}
void KeyboardJoystick::ReadIni(wpi::StringRef name, wpi::StringRef value) {
if (name.startswith("axis")) {
name = name.drop_front(4);
void KeyboardJoystick::ReadIni(std::string_view name, std::string_view value) {
if (wpi::starts_with(name, "axis")) {
name.remove_prefix(4);
if (name == "Count") {
int v;
if (value.getAsInteger(10, v)) {
return;
if (auto v = wpi::parse_integer<int>(value, 10)) {
m_data.axes.count = (std::min)(v.value(), HAL_kMaxJoystickAxes);
}
m_data.axes.count = (std::min)(v, HAL_kMaxJoystickAxes);
return;
}
unsigned int index;
if (name.consumeInteger(10, index)) {
return;
}
auto index = wpi::consume_integer<unsigned int>(&name, 10).value_or(
HAL_kMaxJoystickAxes);
if (index >= HAL_kMaxJoystickAxes) {
return;
}
if (name == "incKey") {
int v;
if (value.getAsInteger(10, v)) {
return;
if (auto v = wpi::parse_integer<int>(value, 10)) {
m_axisConfig[index].incKey = v.value();
}
m_axisConfig[index].incKey = v;
} else if (name == "decKey") {
int v;
if (value.getAsInteger(10, v)) {
return;
if (auto v = wpi::parse_integer<int>(value, 10)) {
m_axisConfig[index].decKey = v.value();
}
m_axisConfig[index].decKey = v;
} else if (name == "keyRate") {
std::sscanf(value.data(), "%f", &m_axisConfig[index].keyRate);
} else if (name == "decayRate") {
std::sscanf(value.data(), "%f", &m_axisConfig[index].decayRate);
} else if (name == "maxAbsValue") {
std::sscanf(value.data(), "%f", &m_axisConfig[index].maxAbsValue);
}
} else if (name.startswith("button")) {
name = name.drop_front(6);
if (name == "Count") {
int v;
if (value.getAsInteger(10, v)) {
return;
if (auto v = wpi::parse_float<float>(value)) {
m_axisConfig[index].keyRate = v.value();
}
} else if (name == "decayRate") {
if (auto v = wpi::parse_float<float>(value)) {
m_axisConfig[index].decayRate = v.value();
}
} else if (name == "maxAbsValue") {
if (auto v = wpi::parse_float<float>(value)) {
m_axisConfig[index].maxAbsValue = v.value();
}
}
} else if (wpi::starts_with(name, "button")) {
name.remove_prefix(6);
if (name == "Count") {
if (auto v = wpi::parse_integer<int>(value, 10)) {
m_data.buttons.count = (std::min)(v.value(), kMaxButtonCount);
}
m_data.buttons.count = (std::min)(v, kMaxButtonCount);
return;
}
unsigned int index;
if (name.getAsInteger(10, index)) {
return;
}
auto index =
wpi::parse_integer<unsigned int>(name, 10).value_or(kMaxButtonCount);
if (index >= kMaxButtonCount) {
return;
}
int v;
if (value.getAsInteger(10, v)) {
return;
}
int v = wpi::parse_integer<int>(value, 10).value_or(-1);
if (v < 0 || v >= IM_ARRAYSIZE(ImGuiIO::KeysDown)) {
return;
}
m_buttonKey[index] = v;
} else if (name.startswith("pov")) {
name = name.drop_front(3);
} else if (wpi::starts_with(name, "pov")) {
name.remove_prefix(3);
if (name == "Count") {
int v;
if (value.getAsInteger(10, v)) {
return;
if (auto v = wpi::parse_integer<int>(value, 10)) {
m_data.povs.count = (std::min)(v.value(), HAL_kMaxJoystickPOVs);
}
m_data.povs.count = (std::min)(v, HAL_kMaxJoystickPOVs);
return;
}
unsigned int index;
if (name.consumeInteger(10, index)) {
return;
}
auto index = wpi::consume_integer<unsigned int>(&name, 10).value_or(
HAL_kMaxJoystickPOVs);
if (index >= HAL_kMaxJoystickPOVs) {
return;
}
int v;
if (value.getAsInteger(10, v)) {
return;
}
int v = wpi::parse_integer<int>(value, 10).value_or(-1);
if (v < 0 || v >= IM_ARRAYSIZE(ImGuiIO::KeysDown)) {
return;
}
@@ -1409,8 +1374,9 @@ static void DisplayJoysticks() {
}
joy.sys = payload_sys;
joy.guid.clear();
wpi::StringRef name{payload_sys->GetName()};
joy.useGamepad = name.startswith("Xbox") || name.contains("pad");
std::string_view name{payload_sys->GetName()};
joy.useGamepad =
wpi::starts_with(name, "Xbox") || wpi::contains(name, "pad");
}
ImGui::EndDragDropTarget();
}

View File

@@ -6,11 +6,13 @@
#include <glass/WindowManager.h>
#include <string_view>
namespace halsimgui {
class DSManager : public glass::WindowManager {
public:
explicit DSManager(const wpi::Twine& iniName) : WindowManager{iniName} {}
explicit DSManager(std::string_view iniName) : WindowManager{iniName} {}
void DisplayMenu() override;
};

View File

@@ -9,8 +9,10 @@
#include <limits>
#include <memory>
#include <string_view>
#include <vector>
#include <fmt/format.h>
#include <hal/Ports.h>
#include <hal/simulation/EncoderData.h>
#include <hal/simulation/SimDeviceData.h>
@@ -24,14 +26,14 @@ namespace {
class EncoderSimModel : public glass::EncoderModel {
public:
EncoderSimModel(const wpi::Twine& id, int32_t index, int channelA,
EncoderSimModel(std::string_view id, int32_t index, int channelA,
int channelB)
: m_distancePerPulse(id + " Dist/Count"),
m_count(id + " Count"),
m_period(id + " Period"),
m_direction(id + " Direction"),
m_distance(id + " Distance"),
m_rate(id + " Rate"),
: m_distancePerPulse(fmt::format("{} Dist/Count", id)),
m_count(fmt::format("{} Count", id)),
m_period(fmt::format("{} Period", id)),
m_direction(fmt::format("{} Direction", id)),
m_distance(fmt::format("{} Distance", id)),
m_rate(fmt::format("{} Rate", id)),
m_index{index},
m_channelA{channelA},
m_channelB{channelB},
@@ -48,8 +50,7 @@ class EncoderSimModel : public glass::EncoderModel {
}
EncoderSimModel(int32_t index, int channelA, int channelB)
: EncoderSimModel("Encoder[" + wpi::Twine(channelA) + wpi::Twine(',') +
wpi::Twine(channelB) + wpi::Twine(']'),
: EncoderSimModel(fmt::format("Encoder[{},{}]", channelA, channelB),
index, channelA, channelB) {}
explicit EncoderSimModel(int32_t index)

View File

@@ -56,7 +56,7 @@ void HALProvider::Update() {
}
}
glass::Model* HALProvider::GetModel(wpi::StringRef name) {
glass::Model* HALProvider::GetModel(std::string_view name) {
auto it = FindModelEntry(name);
if (it == m_modelEntries.end() || (*it)->name != name) {
return nullptr;

View File

@@ -7,9 +7,11 @@
#include <glass/other/DeviceTree.h>
#include <stdint.h>
#include <fmt/format.h>
#include <hal/SimDevice.h>
#include <hal/simulation/SimDeviceData.h>
#include <wpi/DenseMap.h>
#include <wpi/StringExtras.h>
#include "HALDataSource.h"
#include "HALSimGui.h"
@@ -21,7 +23,7 @@ class SimValueSource : public glass::DataSource {
public:
explicit SimValueSource(HAL_SimValueHandle handle, const char* device,
const char* name)
: DataSource(wpi::Twine{device} + wpi::Twine{'-'} + name),
: DataSource(fmt::format("{}-{}", device, name)),
m_callback{HALSIM_RegisterSimValueChangedCallback(
handle, this, CallbackFunc, true)} {}
~SimValueSource() override {
@@ -137,11 +139,11 @@ static void DisplaySimValue(const char* name, void* data,
static void DisplaySimDevice(const char* name, void* data,
HAL_SimDeviceHandle handle) {
wpi::StringRef id{name};
std::string_view id{name};
if (!gSimDevicesShowPrefix) {
// only show "Foo" portion of "Accel:Foo"
wpi::StringRef type;
std::tie(type, id) = id.split(':');
std::string_view type;
std::tie(type, id) = wpi::split(id, ':');
if (id.empty()) {
id = type;
}

View File

@@ -5,11 +5,12 @@
#include <glass/Context.h>
#include <glass/other/Plot.h>
#include <cstdio>
#include <string_view>
#include <hal/Extensions.h>
#include <hal/Main.h>
#include <imgui.h>
#include <wpi/StringRef.h>
#include <wpi/raw_ostream.h>
#include <wpigui.h>
#include "AccelerometerSimGui.h"
@@ -41,7 +42,7 @@ extern "C" {
__declspec(dllexport)
#endif
int HALSIM_InitExtension(void) {
wpi::outs() << "Simulator GUI Initializing.\n";
std::puts("Simulator GUI Initializing.");
gui::CreateContext();
glass::CreateContext();
@@ -102,7 +103,7 @@ __declspec(dllexport)
}
HAL_RegisterExtensionListener(
nullptr, [](void*, const char* name, void* data) {
if (wpi::StringRef{name} == "ds_socket") {
if (std::string_view{name} == "ds_socket") {
DriverStationGui::SetDSSocketExtension(data);
}
});
@@ -114,7 +115,7 @@ __declspec(dllexport)
gui::DestroyContext();
},
[](void*) { gui::Exit(); });
wpi::outs() << "Simulator GUI Initialized!\n";
std::puts("Simulator GUI Initialized!");
return 0;
}