mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-27 02:01:42 +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:
@@ -4,6 +4,8 @@
|
||||
|
||||
#include "hal/AddressableLED.h"
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "DigitalInternal.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "HALInternal.h"
|
||||
@@ -113,9 +115,11 @@ void HAL_SetAddressableLEDLength(HAL_AddressableLEDHandle handle,
|
||||
}
|
||||
if (length > HAL_kAddressableLEDMaxLength || length < 0) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(status, "LED length must be less than or equal to " +
|
||||
wpi::Twine(HAL_kAddressableLEDMaxLength) +
|
||||
". " + wpi::Twine(length) + " was requested");
|
||||
hal::SetLastError(
|
||||
status,
|
||||
fmt::format(
|
||||
"LED length must be less than or equal to {}. {} was requested",
|
||||
HAL_kAddressableLEDMaxLength, length));
|
||||
return;
|
||||
}
|
||||
SimAddressableLEDData[led->index].length = length;
|
||||
@@ -131,10 +135,11 @@ void HAL_WriteAddressableLEDData(HAL_AddressableLEDHandle handle,
|
||||
}
|
||||
if (length > SimAddressableLEDData[led->index].length) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(status,
|
||||
"Data length must be less than or equal to " +
|
||||
wpi::Twine(SimAddressableLEDData[led->index].length) +
|
||||
". " + wpi::Twine(length) + " was requested");
|
||||
hal::SetLastError(
|
||||
status,
|
||||
fmt::format(
|
||||
"Data length must be less than or equal to {}. {} was requested",
|
||||
SimAddressableLEDData[led->index].length, length));
|
||||
return;
|
||||
}
|
||||
SimAddressableLEDData[led->index].SetData(data, length);
|
||||
|
||||
@@ -13,9 +13,9 @@
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <wpi/condition_variable.h>
|
||||
#include <wpi/mutex.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
#include "HALInitializer.h"
|
||||
#include "hal/cpp/fpga_clock.h"
|
||||
@@ -83,14 +83,16 @@ int32_t HAL_SendError(HAL_Bool isError, int32_t errorCode, HAL_Bool isLVCode,
|
||||
if (i == KEEP_MSGS || (curTime - prevMsgTime[i]) >= std::chrono::seconds(1)) {
|
||||
printMsg = true;
|
||||
if (printMsg) {
|
||||
fmt::memory_buffer buf;
|
||||
if (location && location[0] != '\0') {
|
||||
std::fprintf(stderr, "%s at %s: ", isError ? "Error" : "Warning",
|
||||
location);
|
||||
fmt::format_to(buf, "{} at {}: ", isError ? "Error" : "Warning",
|
||||
location);
|
||||
}
|
||||
std::fprintf(stderr, "%s\n", details);
|
||||
fmt::format_to(buf, "{}\n", details);
|
||||
if (callStack && callStack[0] != '\0') {
|
||||
std::fprintf(stderr, "%s\n", callStack);
|
||||
fmt::format_to(buf, "{}\n", callStack);
|
||||
}
|
||||
std::fwrite(buf.data(), buf.size(), 1, stderr);
|
||||
}
|
||||
if (i == KEEP_MSGS) {
|
||||
// replace the oldest one
|
||||
@@ -114,8 +116,8 @@ int32_t HAL_SendConsoleLine(const char* line) {
|
||||
if (handler) {
|
||||
return handler(line);
|
||||
}
|
||||
wpi::outs() << line << "\n";
|
||||
wpi::outs().flush();
|
||||
std::puts(line);
|
||||
std::fflush(stdout);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,12 +4,14 @@
|
||||
|
||||
#include "hal/Extensions.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
#include <wpi/SmallString.h>
|
||||
#include <wpi/StringRef.h>
|
||||
#include <fmt/format.h>
|
||||
#include <wpi/SmallVector.h>
|
||||
#include <wpi/StringExtras.h>
|
||||
#include <wpi/fs.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
#include <wpi/spinlock.h>
|
||||
|
||||
#if defined(WIN32) || defined(_WIN32)
|
||||
@@ -24,7 +26,7 @@
|
||||
#define DLOPEN(a) LoadLibraryA(a)
|
||||
#define DLSYM GetProcAddress
|
||||
#define DLCLOSE FreeLibrary
|
||||
#define DLERROR "error #" << GetLastError()
|
||||
#define DLERROR fmt::format("error #{}", GetLastError())
|
||||
#else
|
||||
#define DELIM ':'
|
||||
#define HTYPE void*
|
||||
@@ -53,30 +55,26 @@ extern "C" {
|
||||
|
||||
int HAL_LoadOneExtension(const char* library) {
|
||||
int rc = 1; // It is expected and reasonable not to find an extra simulation
|
||||
wpi::outs() << "HAL Extensions: Attempting to load: "
|
||||
<< fs::path{library}.stem().string() << "\n";
|
||||
wpi::outs().flush();
|
||||
fmt::print("HAL Extensions: Attempting to load: {}\n",
|
||||
fs::path{library}.stem().string());
|
||||
std::fflush(stdout);
|
||||
HTYPE handle = DLOPEN(library);
|
||||
#if !defined(WIN32) && !defined(_WIN32)
|
||||
if (!handle) {
|
||||
wpi::SmallString<128> libraryName("lib");
|
||||
libraryName += library;
|
||||
#if defined(__APPLE__)
|
||||
libraryName += ".dylib";
|
||||
auto libraryName = fmt::format("lib{}.dylib", library);
|
||||
#else
|
||||
libraryName += ".so";
|
||||
auto libraryName = fmt::format("lib{}.so", library);
|
||||
#endif
|
||||
wpi::outs() << "HAL Extensions: Load failed: " << DLERROR
|
||||
<< "\nTrying modified name: "
|
||||
<< fs::path{libraryName.str()}.stem() << "\n";
|
||||
wpi::outs().flush();
|
||||
fmt::print("HAL Extensions: Load failed: {}\nTrying modified name: {}\n",
|
||||
DLERROR, fs::path{libraryName}.stem().string());
|
||||
std::fflush(stdout);
|
||||
handle = DLOPEN(libraryName.c_str());
|
||||
}
|
||||
#endif
|
||||
if (!handle) {
|
||||
wpi::outs() << "HAL Extensions: Failed to load library: " << DLERROR
|
||||
<< '\n';
|
||||
wpi::outs().flush();
|
||||
fmt::print("HAL Extensions: Failed to load library: {}\n", DLERROR);
|
||||
std::fflush(stdout);
|
||||
return rc;
|
||||
}
|
||||
|
||||
@@ -88,32 +86,30 @@ int HAL_LoadOneExtension(const char* library) {
|
||||
}
|
||||
|
||||
if (rc != 0) {
|
||||
wpi::outs() << "HAL Extensions: Failed to load extension\n";
|
||||
wpi::outs().flush();
|
||||
std::puts("HAL Extensions: Failed to load extension");
|
||||
std::fflush(stdout);
|
||||
DLCLOSE(handle);
|
||||
} else {
|
||||
wpi::outs() << "HAL Extensions: Successfully loaded extension\n";
|
||||
wpi::outs().flush();
|
||||
std::puts("HAL Extensions: Successfully loaded extension");
|
||||
std::fflush(stdout);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
int HAL_LoadExtensions(void) {
|
||||
int rc = 1;
|
||||
wpi::SmallVector<wpi::StringRef, 2> libraries;
|
||||
wpi::SmallVector<std::string_view, 2> libraries;
|
||||
const char* e = std::getenv("HALSIM_EXTENSIONS");
|
||||
if (!e) {
|
||||
if (GetShowNotFoundMessage()) {
|
||||
wpi::outs() << "HAL Extensions: No extensions found\n";
|
||||
wpi::outs().flush();
|
||||
std::puts("HAL Extensions: No extensions found");
|
||||
std::fflush(stdout);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
wpi::StringRef env{e};
|
||||
env.split(libraries, DELIM, -1, false);
|
||||
for (auto& libref : libraries) {
|
||||
wpi::SmallString<128> library(libref);
|
||||
rc = HAL_LoadOneExtension(library.c_str());
|
||||
wpi::split(e, libraries, DELIM, -1, false);
|
||||
for (auto& library : libraries) {
|
||||
rc = HAL_LoadOneExtension(std::string(library).c_str());
|
||||
if (rc < 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -4,10 +4,10 @@
|
||||
|
||||
#include "hal/HAL.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <vector>
|
||||
|
||||
#include <wpi/mutex.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
#include <wpi/spinlock.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
@@ -351,7 +351,11 @@ HAL_Bool HAL_Initialize(int32_t timeout, int32_t mode) {
|
||||
}
|
||||
#endif // _WIN32
|
||||
|
||||
wpi::outs().SetUnbuffered();
|
||||
#ifndef _WIN32
|
||||
setlinebuf(stdin);
|
||||
setlinebuf(stdout);
|
||||
#endif
|
||||
|
||||
if (HAL_LoadExtensions() < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -6,14 +6,14 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <wpi/Twine.h>
|
||||
#include <string_view>
|
||||
|
||||
namespace hal {
|
||||
void SetLastError(int32_t* status, const wpi::Twine& value);
|
||||
void SetLastErrorIndexOutOfRange(int32_t* status, const wpi::Twine& message,
|
||||
void SetLastError(int32_t* status, std::string_view value);
|
||||
void SetLastErrorIndexOutOfRange(int32_t* status, std::string_view message,
|
||||
int32_t minimum, int32_t maximum,
|
||||
int32_t channel);
|
||||
void SetLastErrorPreviouslyAllocated(int32_t* status, const wpi::Twine& message,
|
||||
void SetLastErrorPreviouslyAllocated(int32_t* status, std::string_view message,
|
||||
int32_t channel,
|
||||
const wpi::Twine& previousAllocation);
|
||||
std::string_view previousAllocation);
|
||||
} // namespace hal
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <cstdio>
|
||||
#include <thread>
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <wpi/timestamp.h>
|
||||
|
||||
#include "MockHooksInternal.h"
|
||||
@@ -83,7 +84,7 @@ void HALSIM_WaitForProgramStart(void) {
|
||||
int count = 0;
|
||||
while (!programStarted) {
|
||||
count++;
|
||||
std::printf("Waiting for program start signal: %d\n", count);
|
||||
fmt::print("Waiting for program start signal: {}\n", count);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
#include "hal/PDP.h"
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "CANAPIInternal.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "HALInternal.h"
|
||||
@@ -28,7 +30,7 @@ extern "C" {
|
||||
HAL_PDPHandle HAL_InitializePDP(int32_t module, int32_t* status) {
|
||||
if (!HAL_CheckPDPModule(module)) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(status, "Invalid pdp module " + wpi::Twine(module));
|
||||
hal::SetLastError(status, fmt::format("Invalid pdp module {}", module));
|
||||
return HAL_kInvalidHandle;
|
||||
}
|
||||
hal::init::CheckInit();
|
||||
|
||||
@@ -4,8 +4,7 @@
|
||||
|
||||
#include "hal/SimDevice.h"
|
||||
|
||||
#include <wpi/SmallString.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "HALInitializer.h"
|
||||
#include "mockdata/SimDeviceDataInternal.h"
|
||||
@@ -66,19 +65,12 @@ void HAL_ResetSimValue(HAL_SimValueHandle handle) {
|
||||
}
|
||||
|
||||
hal::SimDevice::SimDevice(const char* name, int index) {
|
||||
wpi::SmallString<128> fullname;
|
||||
wpi::raw_svector_ostream os(fullname);
|
||||
os << name << '[' << index << ']';
|
||||
|
||||
m_handle = HAL_CreateSimDevice(fullname.c_str());
|
||||
m_handle = HAL_CreateSimDevice(fmt::format("{}[{}]", name, index).c_str());
|
||||
}
|
||||
|
||||
hal::SimDevice::SimDevice(const char* name, int index, int channel) {
|
||||
wpi::SmallString<128> fullname;
|
||||
wpi::raw_svector_ostream os(fullname);
|
||||
os << name << '[' << index << ',' << channel << ']';
|
||||
|
||||
m_handle = HAL_CreateSimDevice(fullname.c_str());
|
||||
m_handle = HAL_CreateSimDevice(
|
||||
fmt::format("{}[{},{}]", name, index, channel).c_str());
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <wpi/StringExtras.h>
|
||||
|
||||
#include "SimDeviceDataInternal.h"
|
||||
|
||||
using namespace hal;
|
||||
@@ -71,7 +73,7 @@ void SimDeviceData::SetDeviceEnabled(const char* prefix, bool enabled) {
|
||||
bool SimDeviceData::IsDeviceEnabled(const char* name) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
for (const auto& elem : m_prefixEnabled) {
|
||||
if (wpi::StringRef{name}.startswith(elem.first)) {
|
||||
if (wpi::starts_with(name, elem.first)) {
|
||||
return elem.second;
|
||||
}
|
||||
}
|
||||
@@ -83,7 +85,7 @@ HAL_SimDeviceHandle SimDeviceData::CreateDevice(const char* name) {
|
||||
|
||||
// don't create if disabled
|
||||
for (const auto& elem : m_prefixEnabled) {
|
||||
if (wpi::StringRef{name}.startswith(elem.first)) {
|
||||
if (wpi::starts_with(name, elem.first)) {
|
||||
if (elem.second) {
|
||||
break; // enabled
|
||||
}
|
||||
@@ -272,7 +274,7 @@ int32_t SimDeviceData::RegisterDeviceCreatedCallback(
|
||||
// initial notifications
|
||||
if (initialNotify) {
|
||||
for (auto&& device : m_devices) {
|
||||
if (wpi::StringRef{device->name}.startswith(prefix)) {
|
||||
if (wpi::starts_with(device->name, prefix)) {
|
||||
callback(device->name.c_str(), param, device->handle);
|
||||
}
|
||||
}
|
||||
@@ -332,7 +334,7 @@ void SimDeviceData::EnumerateDevices(const char* prefix, void* param,
|
||||
HALSIM_SimDeviceCallback callback) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
for (auto&& device : m_devices) {
|
||||
if (wpi::StringRef{device->name}.startswith(prefix)) {
|
||||
if (wpi::starts_with(device->name, prefix)) {
|
||||
callback(device->name.c_str(), param, device->handle);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <wpi/StringExtras.h>
|
||||
#include <wpi/StringMap.h>
|
||||
#include <wpi/UidVector.h>
|
||||
#include <wpi/spinlock.h>
|
||||
@@ -118,7 +119,7 @@ class SimPrefixCallbackRegistry {
|
||||
void Invoke(const char* name, U&&... u) const {
|
||||
if (m_callbacks) {
|
||||
for (auto&& cb : *m_callbacks) {
|
||||
if (wpi::StringRef{name}.startswith(cb.prefix)) {
|
||||
if (wpi::starts_with(name, cb.prefix)) {
|
||||
cb.callback(name, cb.param, std::forward<U>(u)...);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user