mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-29 02:21:44 +00:00
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:
@@ -6,7 +6,7 @@
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include <wpi/raw_ostream.h>
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "frc/spline/SplineHelper.h"
|
||||
#include "frc/spline/SplineParameterizer.h"
|
||||
@@ -22,7 +22,7 @@ void TrajectoryGenerator::ReportError(const char* error) {
|
||||
if (s_errorFunc) {
|
||||
s_errorFunc(error);
|
||||
} else {
|
||||
wpi::errs() << "TrajectoryGenerator error: " << error << "\n";
|
||||
fmt::print(stderr, "TrajectoryGenerator error: {}\n", error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include <system_error>
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <wpi/SmallString.h>
|
||||
#include <wpi/json.h>
|
||||
#include <wpi/raw_istream.h>
|
||||
@@ -14,13 +15,12 @@
|
||||
using namespace frc;
|
||||
|
||||
void TrajectoryUtil::ToPathweaverJson(const Trajectory& trajectory,
|
||||
const wpi::Twine& path) {
|
||||
std::string_view path) {
|
||||
std::error_code error_code;
|
||||
|
||||
wpi::SmallString<128> buf;
|
||||
wpi::raw_fd_ostream output{path.toStringRef(buf), error_code};
|
||||
wpi::raw_fd_ostream output{path, error_code};
|
||||
if (error_code) {
|
||||
throw std::runtime_error(("Cannot open file: " + path).str());
|
||||
throw std::runtime_error(fmt::format("Cannot open file: {}", path));
|
||||
}
|
||||
|
||||
wpi::json json = trajectory.States();
|
||||
@@ -28,13 +28,12 @@ void TrajectoryUtil::ToPathweaverJson(const Trajectory& trajectory,
|
||||
output.flush();
|
||||
}
|
||||
|
||||
Trajectory TrajectoryUtil::FromPathweaverJson(const wpi::Twine& path) {
|
||||
Trajectory TrajectoryUtil::FromPathweaverJson(std::string_view path) {
|
||||
std::error_code error_code;
|
||||
|
||||
wpi::SmallString<128> buf;
|
||||
wpi::raw_fd_istream input{path.toStringRef(buf), error_code};
|
||||
wpi::raw_fd_istream input{path, error_code};
|
||||
if (error_code) {
|
||||
throw std::runtime_error(("Cannot open file: " + path).str());
|
||||
throw std::runtime_error(fmt::format("Cannot open file: {}", path));
|
||||
}
|
||||
|
||||
wpi::json json;
|
||||
@@ -48,8 +47,7 @@ std::string TrajectoryUtil::SerializeTrajectory(const Trajectory& trajectory) {
|
||||
return json.dump();
|
||||
}
|
||||
|
||||
Trajectory TrajectoryUtil::DeserializeTrajectory(
|
||||
const wpi::StringRef json_str) {
|
||||
Trajectory TrajectoryUtil::DeserializeTrajectory(std::string_view json_str) {
|
||||
wpi::json json = wpi::json::parse(json_str);
|
||||
return Trajectory{json.get<std::vector<Trajectory::State>>()};
|
||||
}
|
||||
|
||||
@@ -33,8 +33,6 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <wpi/Twine.h>
|
||||
|
||||
#include "frc/spline/Spline.h"
|
||||
#include "units/angle.h"
|
||||
#include "units/curvature.h"
|
||||
|
||||
@@ -5,9 +5,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <wpi/StringRef.h>
|
||||
#include <wpi/Twine.h>
|
||||
#include <string_view>
|
||||
|
||||
#include "frc/trajectory/Trajectory.h"
|
||||
|
||||
@@ -25,7 +23,7 @@ class TrajectoryUtil {
|
||||
* @return The interpolated state.
|
||||
*/
|
||||
static void ToPathweaverJson(const Trajectory& trajectory,
|
||||
const wpi::Twine& path);
|
||||
std::string_view path);
|
||||
/**
|
||||
* Imports a Trajectory from a PathWeaver-style JSON file.
|
||||
*
|
||||
@@ -33,7 +31,7 @@ class TrajectoryUtil {
|
||||
*
|
||||
* @return The trajectory represented by the file.
|
||||
*/
|
||||
static Trajectory FromPathweaverJson(const wpi::Twine& path);
|
||||
static Trajectory FromPathweaverJson(std::string_view path);
|
||||
|
||||
/**
|
||||
* Deserializes a Trajectory from PathWeaver-style JSON.
|
||||
@@ -51,6 +49,6 @@ class TrajectoryUtil {
|
||||
*
|
||||
* @return the string containing the serialized JSON
|
||||
*/
|
||||
static Trajectory DeserializeTrajectory(wpi::StringRef json_str);
|
||||
static Trajectory DeserializeTrajectory(std::string_view json_str);
|
||||
};
|
||||
} // namespace frc
|
||||
|
||||
Reference in New Issue
Block a user