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

@@ -3,23 +3,27 @@
// the WPILib BSD license file in the root directory of this project.
#include <chrono>
#include <cstdio>
#include <thread>
#include <wpi/SmallString.h>
#include <wpi/raw_ostream.h>
#include <fmt/format.h>
#include <wpi/StringExtras.h>
#include "cscore.h"
int main(int argc, char** argv) {
if (argc < 2) {
wpi::errs() << "Usage: settings camera [prop val] ... -- [prop val]...\n";
wpi::errs() << " Example: settings 1 brightness 30 raw_contrast 10\n";
std::fputs("Usage: settings camera [prop val] ... -- [prop val]...\n",
stderr);
std::fputs(" Example: settings 1 brightness 30 raw_contrast 10\n", stderr);
return 1;
}
int id;
if (wpi::StringRef{argv[1]}.getAsInteger(10, id)) {
wpi::errs() << "Expected number for camera\n";
if (auto v = wpi::parse_integer<int>(argv[1], 10)) {
id = v.value();
} else {
std::fputs("Expected number for camera\n", stderr);
return 2;
}
@@ -27,22 +31,21 @@ int main(int argc, char** argv) {
// Set prior to connect
int arg = 2;
wpi::StringRef propName;
for (; arg < argc && wpi::StringRef{argv[arg]} != "--"; ++arg) {
std::string_view propName;
for (; arg < argc && std::string_view{argv[arg]} != "--"; ++arg) {
if (propName.empty()) {
propName = argv[arg];
} else {
wpi::StringRef propVal{argv[arg]};
int intVal;
if (propVal.getAsInteger(10, intVal)) {
camera.GetProperty(propName).SetString(propVal);
std::string_view propVal{argv[arg]};
if (auto v = wpi::parse_integer<int>(propVal, 10)) {
camera.GetProperty(propName).Set(v.value());
} else {
camera.GetProperty(propName).Set(intVal);
camera.GetProperty(propName).SetString(propVal);
}
propName = wpi::StringRef{};
propName = {};
}
}
if (arg < argc && wpi::StringRef{argv[arg]} == "--") {
if (arg < argc && std::string_view{argv[arg]} == "--") {
++arg;
}
@@ -52,57 +55,51 @@ int main(int argc, char** argv) {
}
// Set rest
propName = wpi::StringRef{};
propName = {};
for (; arg < argc; ++arg) {
if (propName.empty()) {
propName = argv[arg];
} else {
wpi::StringRef propVal{argv[arg]};
int intVal;
if (propVal.getAsInteger(10, intVal)) {
camera.GetProperty(propName).SetString(propVal);
std::string_view propVal{argv[arg]};
if (auto v = wpi::parse_integer<int>(propVal, 10)) {
camera.GetProperty(propName).Set(v.value());
} else {
camera.GetProperty(propName).Set(intVal);
camera.GetProperty(propName).SetString(propVal);
}
propName = wpi::StringRef{};
propName = {};
}
}
// Print settings
wpi::SmallString<64> buf;
wpi::outs() << "Properties:\n";
std::puts("Properties:");
for (const auto& prop : camera.EnumerateProperties()) {
wpi::outs() << " " << prop.GetName();
fmt::print(" {}", prop.GetName());
switch (prop.GetKind()) {
case cs::VideoProperty::kBoolean:
wpi::outs() << " (bool): "
<< "value=" << prop.Get()
<< " default=" << prop.GetDefault();
fmt::print(" (bool): value={} default={}", prop.Get(),
prop.GetDefault());
break;
case cs::VideoProperty::kInteger:
wpi::outs() << " (int): "
<< "value=" << prop.Get() << " min=" << prop.GetMin()
<< " max=" << prop.GetMax() << " step=" << prop.GetStep()
<< " default=" << prop.GetDefault();
fmt::print(" (int): value={} min={} max={} step={} default={}",
prop.Get(), prop.GetMin(), prop.GetMax(), prop.GetStep(),
prop.GetDefault());
break;
case cs::VideoProperty::kString:
wpi::outs() << " (string): " << prop.GetString(buf);
fmt::print(" (string): {}", prop.GetString());
break;
case cs::VideoProperty::kEnum: {
wpi::outs() << " (enum): "
<< "value=" << prop.Get();
fmt::print(" (enum): value={}", prop.Get());
auto choices = prop.GetChoices();
for (size_t i = 0; i < choices.size(); ++i) {
if (choices[i].empty()) {
continue;
if (!choices[i].empty()) {
fmt::print("\n {}: {}", i, choices[i]);
}
wpi::outs() << "\n " << i << ": " << choices[i];
}
break;
}
default:
break;
}
wpi::outs() << '\n';
std::fputc('\n', stdout);
}
}