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