mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-04 03:11:43 +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:
@@ -8,8 +8,9 @@
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
HALSimWSBaseProvider::HALSimWSBaseProvider(std::string key, std::string type)
|
||||
: m_key(std::move(key)), m_type(std::move(type)) {}
|
||||
HALSimWSBaseProvider::HALSimWSBaseProvider(std::string_view key,
|
||||
std::string_view type)
|
||||
: m_key(key), m_type(type) {}
|
||||
|
||||
void HALSimWSBaseProvider::OnNetValueChanged(const wpi::json& json) {
|
||||
// empty
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
#include "WSHalProviders.h"
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
void HALSimWSHalProvider::OnNetworkConnected(
|
||||
@@ -30,10 +32,10 @@ void HALSimWSHalProvider::ProcessHalCallback(const wpi::json& payload) {
|
||||
}
|
||||
|
||||
HALSimWSHalChanProvider::HALSimWSHalChanProvider(int32_t channel,
|
||||
const std::string& key,
|
||||
const std::string& type)
|
||||
std::string_view key,
|
||||
std::string_view type)
|
||||
: HALSimWSHalProvider(key, type), m_channel(channel) {
|
||||
m_deviceId = std::to_string(channel);
|
||||
m_deviceId = fmt::format("{}", channel);
|
||||
}
|
||||
|
||||
} // namespace wpilibws
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <string_view>
|
||||
|
||||
#include <hal/DriverStation.h>
|
||||
#include <hal/Extensions.h>
|
||||
@@ -32,7 +33,7 @@ void HALSimWSProviderDriverStation::Initialize(WSRegisterFunc webRegisterFunc) {
|
||||
registered = true;
|
||||
HAL_RegisterExtensionListener(
|
||||
nullptr, [](void*, const char* name, void* data) {
|
||||
if (wpi::StringRef{name} == "ds_socket") {
|
||||
if (std::string_view{name} == "ds_socket") {
|
||||
gDSSocketConnected = static_cast<std::atomic<bool>*>(data);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -7,7 +7,9 @@
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <hal/Ports.h>
|
||||
#include <wpi/StringExtras.h>
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
@@ -140,7 +142,7 @@ void HALSimWSProviderSimDevice::OnValueCreated(const char* name,
|
||||
break;
|
||||
}
|
||||
}
|
||||
std::string key = (wpi::Twine(prefix) + name).str();
|
||||
std::string key = fmt::format("{}{}", prefix, name);
|
||||
auto data = std::make_unique<SimDeviceValueData>();
|
||||
data->device = this;
|
||||
data->handle = handle;
|
||||
@@ -258,15 +260,15 @@ HALSimWSProviderSimDevices::~HALSimWSProviderSimDevices() {
|
||||
void HALSimWSProviderSimDevices::DeviceCreatedCallback(
|
||||
const char* name, HAL_SimDeviceHandle handle) {
|
||||
// Map "Accel:Foo" -> type=Accel, device=Foo
|
||||
auto [type, id] = wpi::StringRef{name}.split(':');
|
||||
auto [type, id] = wpi::split(name, ':');
|
||||
std::shared_ptr<HALSimWSProviderSimDevice> dev;
|
||||
if (id.empty()) {
|
||||
auto key = ("SimDevice/" + type).str();
|
||||
auto key = fmt::format("SimDevice/{}", type);
|
||||
dev = std::make_shared<HALSimWSProviderSimDevice>(handle, key, "SimDevice",
|
||||
type);
|
||||
m_providers.Add(key, dev);
|
||||
} else {
|
||||
auto key = (type + "/" + id).str();
|
||||
auto key = fmt::format("{}/{}", type, id);
|
||||
dev = std::make_shared<HALSimWSProviderSimDevice>(handle, key, type, id);
|
||||
m_providers.Add(key, dev);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "WSProvider_Solenoid.h"
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <hal/Ports.h>
|
||||
#include <hal/simulation/CTREPCMData.h>
|
||||
|
||||
@@ -22,9 +23,7 @@ void HALSimWSProviderSolenoid::Initialize(WSRegisterFunc webRegisterFunc) {
|
||||
++CTREPCMIndex) {
|
||||
for (int32_t solenoidIndex = 0;
|
||||
solenoidIndex < HAL_GetNumSolenoidChannels(); ++solenoidIndex) {
|
||||
auto key = ("Solenoid/" + wpi::Twine(CTREPCMIndex) + "," +
|
||||
wpi::Twine(solenoidIndex))
|
||||
.str();
|
||||
auto key = fmt::format("Solenoid/{},{}", CTREPCMIndex, solenoidIndex);
|
||||
auto ptr = std::make_unique<HALSimWSProviderSolenoid>(
|
||||
CTREPCMIndex, solenoidIndex, key, "Solenoid");
|
||||
webRegisterFunc(key, std::move(ptr));
|
||||
@@ -39,8 +38,7 @@ HALSimWSProviderSolenoid::HALSimWSProviderSolenoid(int32_t CTREPCMChannel,
|
||||
: HALSimWSHalProvider(key, type),
|
||||
m_pcmIndex(CTREPCMChannel),
|
||||
m_solenoidIndex(solenoidChannel) {
|
||||
m_deviceId =
|
||||
std::to_string(m_pcmIndex) + "," + std::to_string(solenoidChannel);
|
||||
m_deviceId = fmt::format("{},{}", m_pcmIndex, solenoidChannel);
|
||||
}
|
||||
|
||||
HALSimWSProviderSolenoid::~HALSimWSProviderSolenoid() {
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <wpi/json.h>
|
||||
|
||||
@@ -16,7 +17,8 @@ namespace wpilibws {
|
||||
|
||||
class HALSimWSBaseProvider {
|
||||
public:
|
||||
explicit HALSimWSBaseProvider(std::string key, std::string type = "");
|
||||
explicit HALSimWSBaseProvider(std::string_view key,
|
||||
std::string_view type = "");
|
||||
virtual ~HALSimWSBaseProvider() = default;
|
||||
|
||||
HALSimWSBaseProvider(const HALSimWSBaseProvider&) = delete;
|
||||
@@ -34,8 +36,8 @@ class HALSimWSBaseProvider {
|
||||
// network -> sim
|
||||
virtual void OnNetValueChanged(const wpi::json& json);
|
||||
|
||||
const std::string GetDeviceType() { return m_type; }
|
||||
const std::string GetDeviceId() { return m_deviceId; }
|
||||
const std::string& GetDeviceType() { return m_type; }
|
||||
const std::string& GetDeviceId() { return m_deviceId; }
|
||||
|
||||
protected:
|
||||
// sim -> network
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <hal/simulation/NotifyListener.h>
|
||||
#include <wpi/json.h>
|
||||
@@ -41,8 +42,8 @@ class HALSimWSHalProvider : public HALSimWSBaseProvider {
|
||||
// provider generates per-channel diffs
|
||||
class HALSimWSHalChanProvider : public HALSimWSHalProvider {
|
||||
public:
|
||||
explicit HALSimWSHalChanProvider(int32_t channel, const std::string& key,
|
||||
const std::string& type);
|
||||
explicit HALSimWSHalChanProvider(int32_t channel, std::string_view key,
|
||||
std::string_view type);
|
||||
|
||||
int32_t GetChannel() { return m_channel; }
|
||||
|
||||
@@ -51,15 +52,14 @@ class HALSimWSHalChanProvider : public HALSimWSHalProvider {
|
||||
};
|
||||
|
||||
using WSRegisterFunc = std::function<void(
|
||||
const std::string&, std::shared_ptr<HALSimWSBaseProvider>)>;
|
||||
std::string_view, std::shared_ptr<HALSimWSBaseProvider>)>;
|
||||
|
||||
template <typename T>
|
||||
void CreateProviders(const std::string& prefix, int32_t numChannels,
|
||||
void CreateProviders(std::string_view prefix, int32_t numChannels,
|
||||
WSRegisterFunc webRegisterFunc);
|
||||
|
||||
template <typename T>
|
||||
void CreateSingleProvider(const std::string& key,
|
||||
WSRegisterFunc webRegisterFunc);
|
||||
void CreateSingleProvider(std::string_view key, WSRegisterFunc webRegisterFunc);
|
||||
|
||||
} // namespace wpilibws
|
||||
|
||||
|
||||
@@ -5,25 +5,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "WSHalProviders.h"
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
template <typename T>
|
||||
void CreateProviders(const std::string& prefix, int numChannels,
|
||||
void CreateProviders(std::string_view prefix, int numChannels,
|
||||
WSRegisterFunc webRegisterFunc) {
|
||||
for (int32_t i = 0; i < numChannels; i++) {
|
||||
auto key = (prefix + "/" + wpi::Twine(i)).str();
|
||||
auto key = fmt::format("{}/{}", prefix, i);
|
||||
auto ptr = std::make_unique<T>(i, key, prefix);
|
||||
webRegisterFunc(key, std::move(ptr));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void CreateSingleProvider(const std::string& key,
|
||||
void CreateSingleProvider(std::string_view key,
|
||||
WSRegisterFunc webRegisterFunc) {
|
||||
auto ptr = std::make_unique<T>(key, key);
|
||||
webRegisterFunc(key, std::move(ptr));
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <shared_mutex>
|
||||
#include <string_view>
|
||||
|
||||
#include <wpi/StringMap.h>
|
||||
|
||||
@@ -25,12 +26,13 @@ class ProviderContainer {
|
||||
ProviderContainer(const ProviderContainer&) = delete;
|
||||
ProviderContainer& operator=(const ProviderContainer&) = delete;
|
||||
|
||||
void Add(wpi::StringRef key, std::shared_ptr<HALSimWSBaseProvider> provider) {
|
||||
void Add(std::string_view key,
|
||||
std::shared_ptr<HALSimWSBaseProvider> provider) {
|
||||
std::unique_lock lock(m_mutex);
|
||||
m_providers[key] = provider;
|
||||
}
|
||||
|
||||
void Delete(wpi::StringRef key) {
|
||||
void Delete(std::string_view key) {
|
||||
std::unique_lock lock(m_mutex);
|
||||
m_providers.erase(key);
|
||||
}
|
||||
@@ -42,7 +44,7 @@ class ProviderContainer {
|
||||
}
|
||||
}
|
||||
|
||||
ProviderPtr Get(wpi::StringRef key) {
|
||||
ProviderPtr Get(std::string_view key) {
|
||||
std::shared_lock lock(m_mutex);
|
||||
auto fiter = m_providers.find(key);
|
||||
return fiter != m_providers.end() ? fiter->second : ProviderPtr();
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
#include <hal/SimDevice.h>
|
||||
@@ -34,9 +35,8 @@ struct SimDeviceValueData {
|
||||
|
||||
class HALSimWSProviderSimDevice : public HALSimWSBaseProvider {
|
||||
public:
|
||||
HALSimWSProviderSimDevice(HAL_SimDeviceHandle handle, const std::string& key,
|
||||
const std::string& type,
|
||||
const std::string& deviceId)
|
||||
HALSimWSProviderSimDevice(HAL_SimDeviceHandle handle, std::string_view key,
|
||||
std::string_view type, std::string_view deviceId)
|
||||
: HALSimWSBaseProvider(key, type), m_handle(handle) {
|
||||
m_deviceId = deviceId;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user