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

@@ -6,9 +6,12 @@
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <memory>
#include <string_view>
#include <utility>
#include <fmt/format.h>
#include <frc/geometry/Pose2d.h>
#include <frc/geometry/Rotation2d.h>
#include <frc/geometry/Translation2d.h>
@@ -21,11 +24,11 @@
#include <units/angle.h>
#include <units/length.h>
#include <wpi/SmallString.h>
#include <wpi/StringExtras.h>
#include <wpi/StringMap.h>
#include <wpi/fs.h>
#include <wpi/json.h>
#include <wpi/raw_istream.h>
#include <wpi/raw_ostream.h>
#include <wpigui.h>
#include "glass/Context.h"
@@ -229,7 +232,7 @@ class FieldInfo {
private:
void Reset();
bool LoadImageImpl(const char* fn);
void LoadJson(const wpi::Twine& jsonfile);
void LoadJson(std::string_view jsonfile);
std::unique_ptr<pfd::open_file> m_fileOpener;
@@ -377,7 +380,7 @@ void FieldInfo::LoadImage() {
if (m_fileOpener && m_fileOpener->ready(0)) {
auto result = m_fileOpener->result();
if (!result.empty()) {
if (wpi::StringRef(result[0]).endswith(".json")) {
if (wpi::ends_with(result[0], ".json")) {
LoadJson(result[0]);
} else {
LoadImageImpl(result[0].c_str());
@@ -396,11 +399,11 @@ void FieldInfo::LoadImage() {
}
}
void FieldInfo::LoadJson(const wpi::Twine& jsonfile) {
void FieldInfo::LoadJson(std::string_view jsonfile) {
std::error_code ec;
wpi::raw_fd_istream f(jsonfile, ec);
if (ec) {
wpi::errs() << "GUI: could not open field JSON file\n";
std::fputs("GUI: could not open field JSON file\n", stderr);
return;
}
@@ -409,12 +412,12 @@ void FieldInfo::LoadJson(const wpi::Twine& jsonfile) {
try {
j = wpi::json::parse(f);
} catch (const wpi::json::parse_error& e) {
wpi::errs() << "GUI: JSON: could not parse: " << e.what() << '\n';
fmt::print(stderr, "GUI: JSON: could not parse: {}\n", e.what());
}
// top level must be an object
if (!j.is_object()) {
wpi::errs() << "GUI: JSON: does not contain a top object\n";
std::fputs("GUI: JSON: does not contain a top object\n", stderr);
return;
}
@@ -423,8 +426,7 @@ void FieldInfo::LoadJson(const wpi::Twine& jsonfile) {
try {
image = j.at("field-image").get<std::string>();
} catch (const wpi::json::exception& e) {
wpi::errs() << "GUI: JSON: could not read field-image: " << e.what()
<< '\n';
fmt::print(stderr, "GUI: JSON: could not read field-image: {}\n", e.what());
return;
}
@@ -436,8 +438,8 @@ void FieldInfo::LoadJson(const wpi::Twine& jsonfile) {
bottom = j.at("field-corners").at("bottom-right").at(1).get<int>();
right = j.at("field-corners").at("bottom-right").at(0).get<int>();
} catch (const wpi::json::exception& e) {
wpi::errs() << "GUI: JSON: could not read field-corners: " << e.what()
<< '\n';
fmt::print(stderr, "GUI: JSON: could not read field-corners: {}\n",
e.what());
return;
}
@@ -448,7 +450,7 @@ void FieldInfo::LoadJson(const wpi::Twine& jsonfile) {
width = j.at("field-size").at(0).get<float>();
height = j.at("field-size").at(1).get<float>();
} catch (const wpi::json::exception& e) {
wpi::errs() << "GUI: JSON: could not read field-size: " << e.what() << '\n';
fmt::print(stderr, "GUI: JSON: could not read field-size: {}\n", e.what());
return;
}
@@ -457,7 +459,7 @@ void FieldInfo::LoadJson(const wpi::Twine& jsonfile) {
try {
unit = j.at("field-unit").get<std::string>();
} catch (const wpi::json::exception& e) {
wpi::errs() << "GUI: JSON: could not read field-unit: " << e.what() << '\n';
fmt::print(stderr, "GUI: JSON: could not read field-unit: {}\n", e.what());
return;
}
@@ -468,7 +470,7 @@ void FieldInfo::LoadJson(const wpi::Twine& jsonfile) {
}
// the image filename is relative to the json file
auto pathname = fs::path{jsonfile.str()}.replace_filename(image).string();
auto pathname = fs::path{jsonfile}.replace_filename(image).string();
// load field image
if (!LoadImageImpl(pathname.c_str())) {
@@ -486,10 +488,10 @@ void FieldInfo::LoadJson(const wpi::Twine& jsonfile) {
}
bool FieldInfo::LoadImageImpl(const char* fn) {
wpi::outs() << "GUI: loading field image '" << fn << "'\n";
fmt::print("GUI: loading field image '{}'\n", fn);
auto texture = gui::Texture::CreateFromFile(fn);
if (!texture) {
wpi::errs() << "GUI: could not read field image\n";
std::puts("GUI: could not read field image");
return false;
}
m_texture = std::move(texture);
@@ -679,10 +681,10 @@ void ObjectInfo::LoadImage() {
}
bool ObjectInfo::LoadImageImpl(const char* fn) {
wpi::outs() << "GUI: loading object image '" << fn << "'\n";
fmt::print("GUI: loading object image '{}'\n", fn);
auto texture = gui::Texture::CreateFromFile(fn);
if (!texture) {
wpi::errs() << "GUI: could not read object image\n";
std::fputs("GUI: could not read object image\n", stderr);
return false;
}
m_texture = std::move(texture);
@@ -883,7 +885,7 @@ void glass::DisplayField2DSettings(Field2DModel* model) {
}
auto obj = objRef.get();
wpi::SmallString<64> nameBuf = name;
wpi::SmallString<64> nameBuf{name};
if (ImGui::CollapsingHeader(nameBuf.c_str())) {
obj->DisplaySettings();
}
@@ -899,7 +901,7 @@ class FieldDisplay {
const ImVec2& contentSize);
private:
void DisplayObject(FieldObjectModel& model, wpi::StringRef name);
void DisplayObject(FieldObjectModel& model, std::string_view name);
FieldInfo* m_field;
ImVec2 m_mousePos;
@@ -1018,7 +1020,8 @@ void FieldDisplay::Display(FieldInfo* field, Field2DModel* model,
}
}
void FieldDisplay::DisplayObject(FieldObjectModel& model, wpi::StringRef name) {
void FieldDisplay::DisplayObject(FieldObjectModel& model,
std::string_view name) {
PushID(name);
auto& objRef = m_field->m_objects[name];
if (!objRef) {

View File

@@ -5,7 +5,6 @@
#include "glass/other/Log.h"
#include <imgui.h>
#include <wpi/raw_ostream.h>
using namespace glass;
@@ -17,14 +16,13 @@ void LogData::Clear() {
m_lineOffsets.push_back(0);
}
void LogData::Append(const wpi::Twine& msg) {
void LogData::Append(std::string_view msg) {
if (m_lineOffsets.size() >= m_maxLines) {
Clear();
}
size_t oldSize = m_buf.size();
wpi::raw_string_ostream os{m_buf};
msg.print(os);
m_buf.append(msg);
for (size_t newSize = m_buf.size(); oldSize < newSize; ++oldSize) {
if (m_buf[oldSize] == '\n') {
m_lineOffsets.push_back(oldSize + 1);

View File

@@ -6,9 +6,12 @@
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <memory>
#include <string_view>
#include <utility>
#include <fmt/format.h>
#include <frc/geometry/Pose2d.h>
#include <frc/geometry/Rotation2d.h>
#include <frc/geometry/Transform2d.h>
@@ -21,7 +24,6 @@
#include <portable-file-dialogs.h>
#include <units/angle.h>
#include <units/length.h>
#include <wpi/raw_ostream.h>
#include <wpigui.h>
#include "glass/Context.h"
@@ -125,10 +127,10 @@ void BackgroundInfo::LoadImage() {
}
bool BackgroundInfo::LoadImageImpl(const char* fn) {
wpi::outs() << "GUI: loading background image '" << fn << "'\n";
fmt::print("GUI: loading background image '{}'\n", fn);
auto texture = gui::Texture::CreateFromFile(fn);
if (!texture) {
wpi::errs() << "GUI: could not read background image\n";
std::puts("GUI: could not read background image");
return false;
}
m_texture = std::move(texture);
@@ -182,7 +184,7 @@ void glass::DisplayMechanism2DSettings(Mechanism2DModel* model) {
void FrameData::DrawObject(ImDrawList* drawList, MechanismObjectModel& objModel,
const frc::Pose2d& pose) const {
const char* type = objModel.GetType();
if (wpi::StringRef{type} == "line") {
if (std::string_view{type} == "line") {
auto startPose =
pose + frc::Transform2d{frc::Translation2d{}, objModel.GetAngle()};
auto endPose =

View File

@@ -12,8 +12,11 @@
#include <cstring>
#include <memory>
#include <string>
#include <string_view>
#include <vector>
#include <fmt/format.h>
#define IMGUI_DEFINE_MATH_OPERATORS
#include <imgui.h>
#include <imgui_internal.h>
@@ -23,6 +26,7 @@
#include <wpi/Signal.h>
#include <wpi/SmallString.h>
#include <wpi/SmallVector.h>
#include <wpi/StringExtras.h>
#include <wpi/timestamp.h>
#include "glass/Context.h"
@@ -42,7 +46,7 @@ struct PlotSeriesRef {
class PlotSeries {
public:
explicit PlotSeries(wpi::StringRef id);
explicit PlotSeries(std::string_view id);
explicit PlotSeries(DataSource* source, int yAxis = 0);
const std::string& GetId() const { return m_id; }
@@ -52,7 +56,7 @@ class PlotSeries {
void SetSource(DataSource* source);
DataSource* GetSource() const { return m_source; }
bool ReadIni(wpi::StringRef name, wpi::StringRef value);
bool ReadIni(std::string_view name, std::string_view value);
void WriteIni(ImGuiTextBuffer* out);
enum Action { kNone, kMoveUp, kMoveDown, kDelete };
@@ -102,7 +106,7 @@ class Plot {
public:
Plot();
bool ReadIni(wpi::StringRef name, wpi::StringRef value);
bool ReadIni(std::string_view name, std::string_view value);
void WriteIni(ImGuiTextBuffer* out);
void DragDropTarget(PlotView& view, size_t i, bool inPlot);
@@ -164,7 +168,7 @@ class PlotView : public View {
} // namespace
PlotSeries::PlotSeries(wpi::StringRef id) : m_id(id) {
PlotSeries::PlotSeries(std::string_view id) : m_id(id) {
if (DataSource* source = DataSource::Find(id)) {
SetSource(source);
return;
@@ -241,55 +245,45 @@ void PlotSeries::AppendValue(double value, uint64_t timeUs) {
}
}
bool PlotSeries::ReadIni(wpi::StringRef name, wpi::StringRef value) {
bool PlotSeries::ReadIni(std::string_view name, std::string_view value) {
if (name == "name") {
m_name = value;
return true;
}
if (name == "yAxis") {
int num;
if (value.getAsInteger(10, num)) {
return true;
if (auto num = wpi::parse_integer<int>(value, 10)) {
m_yAxis = num.value();
}
m_yAxis = num;
return true;
} else if (name == "color") {
unsigned int num;
if (value.getAsInteger(10, num)) {
return true;
if (auto num = wpi::parse_integer<unsigned int>(value, 10)) {
m_color = ImColor(num.value());
}
m_color = ImColor(num);
return true;
} else if (name == "marker") {
int num;
if (value.getAsInteger(10, num)) {
return true;
if (auto num = wpi::parse_integer<int>(value, 10)) {
m_marker = num.value();
}
m_marker = num;
return true;
} else if (name == "weight") {
std::sscanf(value.data(), "%f", &m_weight);
if (auto num = wpi::parse_float<float>(value)) {
m_weight = num.value();
}
return true;
} else if (name == "digital") {
int num;
if (value.getAsInteger(10, num)) {
return true;
if (auto num = wpi::parse_integer<int>(value, 10)) {
m_digital = num.value();
}
m_digital = num;
return true;
} else if (name == "digitalBitHeight") {
int num;
if (value.getAsInteger(10, num)) {
return true;
if (auto num = wpi::parse_integer<int>(value, 10)) {
m_digitalBitHeight = num.value();
}
m_digitalBitHeight = num;
return true;
} else if (name == "digitalBitGap") {
int num;
if (value.getAsInteger(10, num)) {
return true;
if (auto num = wpi::parse_integer<int>(value, 10)) {
m_digitalBitGap = num.value();
}
m_digitalBitGap = num;
return true;
}
return false;
@@ -478,121 +472,93 @@ Plot::Plot() {
}
}
bool Plot::ReadIni(wpi::StringRef name, wpi::StringRef value) {
bool Plot::ReadIni(std::string_view name, std::string_view value) {
if (name == "name") {
m_name = value;
return true;
} else if (name == "visible") {
int num;
if (value.getAsInteger(10, num)) {
return true;
if (auto num = wpi::parse_integer<int>(value, 10)) {
m_visible = num.value() != 0;
}
m_visible = num != 0;
return true;
} else if (name == "showPause") {
int num;
if (value.getAsInteger(10, num)) {
return true;
if (auto num = wpi::parse_integer<int>(value, 10)) {
m_showPause = num.value() != 0;
}
m_showPause = num != 0;
return true;
} else if (name == "lockPrevX") {
int num;
if (value.getAsInteger(10, num)) {
return true;
if (auto num = wpi::parse_integer<int>(value, 10)) {
m_lockPrevX = num.value() != 0;
}
m_lockPrevX = num != 0;
return true;
} else if (name == "legend") {
int num;
if (value.getAsInteger(10, num)) {
return true;
}
if (num == 0) {
m_plotFlags |= ImPlotFlags_NoLegend;
} else {
m_plotFlags &= ~ImPlotFlags_NoLegend;
if (auto num = wpi::parse_integer<int>(value, 10)) {
if (num.value() == 0) {
m_plotFlags |= ImPlotFlags_NoLegend;
} else {
m_plotFlags &= ~ImPlotFlags_NoLegend;
}
}
return true;
} else if (name == "yaxis2") {
int num;
if (value.getAsInteger(10, num)) {
return true;
}
if (num == 0) {
m_plotFlags &= ~ImPlotFlags_YAxis2;
} else {
m_plotFlags |= ImPlotFlags_YAxis2;
if (auto num = wpi::parse_integer<int>(value, 10)) {
if (num.value() == 0) {
m_plotFlags &= ~ImPlotFlags_YAxis2;
} else {
m_plotFlags |= ImPlotFlags_YAxis2;
}
}
return true;
} else if (name == "yaxis3") {
int num;
if (value.getAsInteger(10, num)) {
return true;
}
if (num == 0) {
m_plotFlags &= ~ImPlotFlags_YAxis3;
} else {
m_plotFlags |= ImPlotFlags_YAxis3;
if (auto num = wpi::parse_integer<int>(value, 10)) {
if (num.value() == 0) {
m_plotFlags &= ~ImPlotFlags_YAxis3;
} else {
m_plotFlags |= ImPlotFlags_YAxis3;
}
}
return true;
} else if (name == "viewTime") {
int num;
if (value.getAsInteger(10, num)) {
return true;
if (auto num = wpi::parse_integer<int>(value, 10)) {
m_viewTime = num.value() / 1000.0;
}
m_viewTime = num / 1000.0;
return true;
} else if (name == "autoHeight") {
int num;
if (value.getAsInteger(10, num)) {
return true;
if (auto num = wpi::parse_integer<int>(value, 10)) {
m_autoHeight = num.value() != 0;
}
m_autoHeight = num != 0;
return true;
} else if (name == "height") {
int num;
if (value.getAsInteger(10, num)) {
return true;
if (auto num = wpi::parse_integer<int>(value, 10)) {
m_height = num.value();
}
m_height = num;
return true;
} else if (name.startswith("y")) {
auto [yAxisStr, yName] = name.split('_');
int yAxis;
if (yAxisStr.substr(1).getAsInteger(10, yAxis)) {
return false;
}
} else if (wpi::starts_with(name, 'y')) {
auto [yAxisStr, yName] = wpi::split(name, '_');
int yAxis =
wpi::parse_integer<int>(wpi::drop_front(yAxisStr), 10).value_or(-1);
if (yAxis < 0 || yAxis > 3) {
return false;
}
if (yName == "min") {
int num;
if (value.getAsInteger(10, num)) {
return true;
if (auto num = wpi::parse_integer<int>(value, 10)) {
m_axisRange[yAxis].min = num.value() / 1000.0;
}
m_axisRange[yAxis].min = num / 1000.0;
return true;
} else if (yName == "max") {
int num;
if (value.getAsInteger(10, num)) {
return true;
if (auto num = wpi::parse_integer<int>(value, 10)) {
m_axisRange[yAxis].max = num.value() / 1000.0;
}
m_axisRange[yAxis].max = num / 1000.0;
return true;
} else if (yName == "lockMin") {
int num;
if (value.getAsInteger(10, num)) {
return true;
if (auto num = wpi::parse_integer<int>(value, 10)) {
m_axisRange[yAxis].lockMin = num.value() != 0;
}
m_axisRange[yAxis].lockMin = num != 0;
return true;
} else if (yName == "lockMax") {
int num;
if (value.getAsInteger(10, num)) {
return true;
if (auto num = wpi::parse_integer<int>(value, 10)) {
m_axisRange[yAxis].lockMax = num.value() != 0;
}
m_axisRange[yAxis].lockMax = num != 0;
return true;
} else if (yName == "label") {
m_axisLabel[yAxis] = value;
@@ -960,10 +926,10 @@ void PlotView::MovePlotSeries(PlotView* fromView, size_t fromPlotIndex,
}
}
PlotProvider::PlotProvider(const wpi::Twine& iniName)
: WindowManager{iniName + "Window"},
PlotProvider::PlotProvider(std::string_view iniName)
: WindowManager{fmt::format("{}Window", iniName)},
m_plotSaver{iniName, this, false},
m_seriesSaver{iniName + "Series", this, true} {}
m_seriesSaver{fmt::format("{}Series", iniName), this, true} {}
PlotProvider::~PlotProvider() = default;
@@ -1021,21 +987,23 @@ void PlotProvider::DisplayWindows() {
WindowManager::DisplayWindows();
}
PlotProvider::IniSaver::IniSaver(const wpi::Twine& typeName,
PlotProvider::IniSaver::IniSaver(std::string_view typeName,
PlotProvider* provider, bool forSeries)
: IniSaverBase{typeName}, m_provider{provider}, m_forSeries{forSeries} {}
void* PlotProvider::IniSaver::IniReadOpen(const char* name) {
auto [viewId, plotNumStr] = wpi::StringRef{name}.split('#');
wpi::StringRef seriesId;
auto [viewId, plotNumStr] = wpi::split(name, '#');
std::string_view seriesId;
if (m_forSeries) {
std::tie(plotNumStr, seriesId) = plotNumStr.split('#');
std::tie(plotNumStr, seriesId) = wpi::split(plotNumStr, '#');
if (seriesId.empty()) {
return nullptr;
}
}
unsigned int plotNum;
if (plotNumStr.getAsInteger(10, plotNum)) {
if (auto plotNumOpt = wpi::parse_integer<unsigned int>(plotNumStr, 10)) {
plotNum = plotNumOpt.value();
} else {
return nullptr;
}
@@ -1071,10 +1039,10 @@ void* PlotProvider::IniSaver::IniReadOpen(const char* name) {
.get();
}
void PlotProvider::IniSaver::IniReadLine(void* entry, const char* lineStr) {
auto [name, value] = wpi::StringRef{lineStr}.split('=');
name = name.trim();
value = value.trim();
void PlotProvider::IniSaver::IniReadLine(void* entry, const char* line) {
auto [name, value] = wpi::split(line, '=');
name = wpi::trim(name);
value = wpi::trim(value);
if (m_forSeries) {
static_cast<PlotSeries*>(entry)->ReadIni(name, value);
} else {