mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-30 02:31: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:
@@ -26,8 +26,7 @@
|
||||
#include <opencv2/highgui/highgui.hpp>
|
||||
#include <opencv2/imgproc/imgproc.hpp>
|
||||
#include <wpi/MemAlloc.h>
|
||||
#include <wpi/SmallString.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
#include <wpi/StringExtras.h>
|
||||
#include <wpi/timestamp.h>
|
||||
|
||||
#include "COMCreators.h"
|
||||
@@ -69,17 +68,17 @@ using namespace cs;
|
||||
|
||||
namespace cs {
|
||||
|
||||
UsbCameraImpl::UsbCameraImpl(const wpi::Twine& name, wpi::Logger& logger,
|
||||
UsbCameraImpl::UsbCameraImpl(std::string_view name, wpi::Logger& logger,
|
||||
Notifier& notifier, Telemetry& telemetry,
|
||||
const wpi::Twine& path)
|
||||
: SourceImpl{name, logger, notifier, telemetry}, m_path{path.str()} {
|
||||
std::string_view path)
|
||||
: SourceImpl{name, logger, notifier, telemetry}, m_path{path} {
|
||||
std::wstring_convert<std::codecvt_utf8<wchar_t>> utf8_conv;
|
||||
m_widePath = utf8_conv.from_bytes(m_path.c_str());
|
||||
m_deviceId = -1;
|
||||
StartMessagePump();
|
||||
}
|
||||
|
||||
UsbCameraImpl::UsbCameraImpl(const wpi::Twine& name, wpi::Logger& logger,
|
||||
UsbCameraImpl::UsbCameraImpl(std::string_view name, wpi::Logger& logger,
|
||||
Notifier& notifier, Telemetry& telemetry,
|
||||
int deviceId)
|
||||
: SourceImpl{name, logger, notifier, telemetry}, m_deviceId(deviceId) {
|
||||
@@ -99,11 +98,11 @@ void UsbCameraImpl::SetProperty(int property, int value, CS_Status* status) {
|
||||
SetCameraMessage, msg.kind, &msg);
|
||||
*status = result;
|
||||
}
|
||||
void UsbCameraImpl::SetStringProperty(int property, const wpi::Twine& value,
|
||||
void UsbCameraImpl::SetStringProperty(int property, std::string_view value,
|
||||
CS_Status* status) {
|
||||
Message msg{Message::kCmdSetPropertyStr};
|
||||
msg.data[0] = property;
|
||||
msg.dataStr = value.str();
|
||||
msg.dataStr = value;
|
||||
auto result =
|
||||
m_messagePump->SendWindowMessage<CS_Status, Message::Kind, Message*>(
|
||||
SetCameraMessage, msg.kind, &msg);
|
||||
@@ -197,9 +196,9 @@ void UsbCameraImpl::NumSinksEnabledChanged() {
|
||||
SetCameraMessage, Message::kNumSinksEnabledChanged, nullptr);
|
||||
}
|
||||
|
||||
void UsbCameraImpl::SetPath(const wpi::Twine& path, CS_Status* status) {
|
||||
void UsbCameraImpl::SetPath(std::string_view path, CS_Status* status) {
|
||||
Message msg{Message::kCmdSetPath};
|
||||
msg.dataStr = path.str();
|
||||
msg.dataStr = path;
|
||||
auto result =
|
||||
m_messagePump->SendWindowMessage<CS_Status, Message::Kind, Message*>(
|
||||
SetCameraMessage, msg.kind, &msg);
|
||||
@@ -257,7 +256,7 @@ bool UsbCameraImpl::CheckDeviceChange(WPARAM wParam, DEV_BROADCAST_HDR* pHdr,
|
||||
|
||||
void UsbCameraImpl::DeviceDisconnect() {
|
||||
if (m_connectVerbose)
|
||||
SINFO("Disconnected from " << m_path);
|
||||
SINFO("Disconnected from {}", m_path);
|
||||
m_sourceReader.Reset();
|
||||
m_mediaSource.Reset();
|
||||
if (m_imageCallback) {
|
||||
@@ -268,8 +267,8 @@ void UsbCameraImpl::DeviceDisconnect() {
|
||||
SetConnected(false);
|
||||
}
|
||||
|
||||
static bool IsPercentageProperty(wpi::StringRef name) {
|
||||
if (name.startswith("raw_"))
|
||||
static bool IsPercentageProperty(std::string_view name) {
|
||||
if (wpi::starts_with(name, "raw_"))
|
||||
name = name.substr(4);
|
||||
return name == "Brightness" || name == "Contrast" || name == "Saturation" ||
|
||||
name == "Hue" || name == "Sharpness" || name == "Gain" ||
|
||||
@@ -341,8 +340,7 @@ void UsbCameraImpl::ProcessFrame(IMFSample* videoSample,
|
||||
case cs::VideoMode::PixelFormat::kMJPEG: {
|
||||
// Special case
|
||||
PutFrame(VideoMode::kMJPEG, mode.width, mode.height,
|
||||
wpi::StringRef(reinterpret_cast<char*>(ptr), length),
|
||||
wpi::Now());
|
||||
{reinterpret_cast<char*>(ptr), length}, wpi::Now());
|
||||
doFinalSet = false;
|
||||
break;
|
||||
}
|
||||
@@ -477,9 +475,9 @@ bool UsbCameraImpl::DeviceConnect() {
|
||||
return true;
|
||||
|
||||
if (m_connectVerbose)
|
||||
SINFO("Connecting to USB camera on " << m_path);
|
||||
SINFO("Connecting to USB camera on {}", m_path);
|
||||
|
||||
SDEBUG3("opening device");
|
||||
SDEBUG3("{}", "opening device");
|
||||
|
||||
const wchar_t* path = m_widePath.c_str();
|
||||
m_mediaSource = CreateVideoCaptureDevice(path);
|
||||
@@ -506,13 +504,13 @@ bool UsbCameraImpl::DeviceConnect() {
|
||||
}
|
||||
|
||||
if (!m_properties_cached) {
|
||||
SDEBUG3("caching properties");
|
||||
SDEBUG3("{}", "caching properties");
|
||||
DeviceCacheProperties();
|
||||
DeviceCacheVideoModes();
|
||||
DeviceCacheMode();
|
||||
m_properties_cached = true;
|
||||
} else {
|
||||
SDEBUG3("restoring video mode");
|
||||
SDEBUG3("{}", "restoring video mode");
|
||||
DeviceSetMode();
|
||||
}
|
||||
|
||||
@@ -526,7 +524,7 @@ bool UsbCameraImpl::DeviceConnect() {
|
||||
}
|
||||
|
||||
std::unique_ptr<PropertyImpl> UsbCameraImpl::CreateEmptyProperty(
|
||||
const wpi::Twine& name) const {
|
||||
std::string_view name) const {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -545,7 +543,7 @@ bool UsbCameraImpl::CacheProperties(CS_Status* status) const {
|
||||
}
|
||||
|
||||
template <typename TagProperty, typename IAM>
|
||||
void UsbCameraImpl::DeviceAddProperty(const wpi::Twine& name_, TagProperty tag,
|
||||
void UsbCameraImpl::DeviceAddProperty(std::string_view name_, TagProperty tag,
|
||||
IAM* pProcAmp) {
|
||||
// First see if properties exist
|
||||
bool isValid = false;
|
||||
@@ -556,11 +554,11 @@ void UsbCameraImpl::DeviceAddProperty(const wpi::Twine& name_, TagProperty tag,
|
||||
}
|
||||
}
|
||||
|
||||
template void UsbCameraImpl::DeviceAddProperty(const wpi::Twine& name_,
|
||||
template void UsbCameraImpl::DeviceAddProperty(std::string_view name_,
|
||||
tagVideoProcAmpProperty tag,
|
||||
IAMVideoProcAmp* pProcAmp);
|
||||
|
||||
template void UsbCameraImpl::DeviceAddProperty(const wpi::Twine& name_,
|
||||
template void UsbCameraImpl::DeviceAddProperty(std::string_view name_,
|
||||
tagCameraControlProperty tag,
|
||||
IAMCameraControl* pProcAmp);
|
||||
|
||||
@@ -669,7 +667,7 @@ void UsbCameraImpl::DeviceCacheProperty(
|
||||
} else {
|
||||
// Read current raw value and set percentage from it
|
||||
if (!rawProp->DeviceGet(lock, pProcAmp))
|
||||
SWARNING("failed to get property " << rawProp->name);
|
||||
SWARNING("failed to get property {}", rawProp->name);
|
||||
|
||||
if (perProp) {
|
||||
perProp->SetValue(RawToPercentage(*rawProp, rawProp->value));
|
||||
@@ -680,7 +678,7 @@ void UsbCameraImpl::DeviceCacheProperty(
|
||||
// Set value on device if user-configured
|
||||
if (rawProp->valueSet) {
|
||||
if (!rawProp->DeviceSet(lock, pProcAmp))
|
||||
SWARNING("failed to set property " << rawProp->name);
|
||||
SWARNING("failed to set property {}", rawProp->name);
|
||||
}
|
||||
|
||||
// Update pointers since we released the lock
|
||||
@@ -764,7 +762,7 @@ CS_StatusValue UsbCameraImpl::DeviceCmdSetProperty(
|
||||
bool setString = (msg.kind == Message::kCmdSetPropertyStr);
|
||||
int property = msg.data[0];
|
||||
int value = msg.data[1];
|
||||
wpi::StringRef valueStr = msg.dataStr;
|
||||
std::string_view valueStr = msg.dataStr;
|
||||
|
||||
// Look up
|
||||
auto prop = static_cast<UsbCameraProperty*>(GetProperty(property));
|
||||
@@ -1022,23 +1020,21 @@ void UsbCameraImpl::DeviceCacheVideoModes() {
|
||||
m_notifier.NotifySource(*this, CS_SOURCE_VIDEOMODES_UPDATED);
|
||||
}
|
||||
|
||||
static void ParseVidAndPid(wpi::StringRef path, int* pid, int* vid) {
|
||||
auto vidIndex = path.find_lower("vid_");
|
||||
auto pidIndex = path.find_lower("pid_");
|
||||
static void ParseVidAndPid(std::string_view path, int* pid, int* vid) {
|
||||
auto vidIndex = wpi::find_lower(path, "vid_");
|
||||
auto pidIndex = wpi::find_lower(path, "pid_");
|
||||
|
||||
if (vidIndex != wpi::StringRef::npos) {
|
||||
auto vidSlice = path.slice(vidIndex + 4, vidIndex + 8);
|
||||
uint16_t val = 0;
|
||||
if (!vidSlice.getAsInteger(16, val)) {
|
||||
*vid = val;
|
||||
if (vidIndex != std::string_view::npos) {
|
||||
auto vidSlice = wpi::slice(path, vidIndex + 4, vidIndex + 8);
|
||||
if (auto v = wpi::parse_integer<uint16_t>(vidSlice, 16)) {
|
||||
*vid = v.value();
|
||||
}
|
||||
}
|
||||
|
||||
if (pidIndex != wpi::StringRef::npos) {
|
||||
auto pidSlice = path.slice(pidIndex + 4, pidIndex + 8);
|
||||
uint16_t val = 0;
|
||||
if (!pidSlice.getAsInteger(16, val)) {
|
||||
*pid = val;
|
||||
if (pidIndex != std::string_view::npos) {
|
||||
auto pidSlice = wpi::slice(path, pidIndex + 4, pidIndex + 8);
|
||||
if (auto v = wpi::parse_integer<uint16_t>(pidSlice, 16)) {
|
||||
*pid = v.value();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1112,7 +1108,7 @@ done:
|
||||
return retval;
|
||||
}
|
||||
|
||||
CS_Source CreateUsbCameraDev(const wpi::Twine& name, int dev,
|
||||
CS_Source CreateUsbCameraDev(std::string_view name, int dev,
|
||||
CS_Status* status) {
|
||||
// First check if device exists
|
||||
auto devices = cs::EnumerateUsbCameras(status);
|
||||
@@ -1125,7 +1121,7 @@ CS_Source CreateUsbCameraDev(const wpi::Twine& name, int dev,
|
||||
return inst.CreateSource(CS_SOURCE_USB, source);
|
||||
}
|
||||
|
||||
CS_Source CreateUsbCameraPath(const wpi::Twine& name, const wpi::Twine& path,
|
||||
CS_Source CreateUsbCameraPath(std::string_view name, std::string_view path,
|
||||
CS_Status* status) {
|
||||
auto& inst = Instance::GetInstance();
|
||||
auto source = std::make_shared<UsbCameraImpl>(
|
||||
@@ -1133,7 +1129,7 @@ CS_Source CreateUsbCameraPath(const wpi::Twine& name, const wpi::Twine& path,
|
||||
return inst.CreateSource(CS_SOURCE_USB, source);
|
||||
}
|
||||
|
||||
void SetUsbCameraPath(CS_Source source, const wpi::Twine& path,
|
||||
void SetUsbCameraPath(CS_Source source, std::string_view path,
|
||||
CS_Status* status) {
|
||||
auto data = Instance::GetInstance().GetSource(source);
|
||||
if (!data || data->kind != CS_SOURCE_USB) {
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -23,7 +24,6 @@
|
||||
#include <Dbt.h>
|
||||
#include <wpi/STLExtras.h>
|
||||
#include <wpi/SmallVector.h>
|
||||
#include <wpi/Twine.h>
|
||||
#include <wpi/condition_variable.h>
|
||||
#include <wpi/mutex.h>
|
||||
#include <wpi/raw_istream.h>
|
||||
@@ -40,9 +40,9 @@ namespace cs {
|
||||
class UsbCameraImpl : public SourceImpl,
|
||||
public std::enable_shared_from_this<UsbCameraImpl> {
|
||||
public:
|
||||
UsbCameraImpl(const wpi::Twine& name, wpi::Logger& logger, Notifier& notifier,
|
||||
Telemetry& telemetry, const wpi::Twine& path);
|
||||
UsbCameraImpl(const wpi::Twine& name, wpi::Logger& logger, Notifier& notifier,
|
||||
UsbCameraImpl(std::string_view name, wpi::Logger& logger, Notifier& notifier,
|
||||
Telemetry& telemetry, std::string_view path);
|
||||
UsbCameraImpl(std::string_view name, wpi::Logger& logger, Notifier& notifier,
|
||||
Telemetry& telemetry, int deviceId);
|
||||
~UsbCameraImpl() override;
|
||||
|
||||
@@ -50,7 +50,7 @@ class UsbCameraImpl : public SourceImpl,
|
||||
|
||||
// Property functions
|
||||
void SetProperty(int property, int value, CS_Status* status) override;
|
||||
void SetStringProperty(int property, const wpi::Twine& value,
|
||||
void SetStringProperty(int property, std::string_view value,
|
||||
CS_Status* status) override;
|
||||
|
||||
// Standard common camera properties
|
||||
@@ -75,7 +75,7 @@ class UsbCameraImpl : public SourceImpl,
|
||||
void ProcessFrame(IMFSample* sample, const VideoMode& mode);
|
||||
void PostRequestNewFrame();
|
||||
|
||||
void SetPath(const wpi::Twine& path, CS_Status* status);
|
||||
void SetPath(std::string_view path, CS_Status* status);
|
||||
std::string GetPath() const;
|
||||
|
||||
// Messages passed to/from camera thread
|
||||
@@ -107,7 +107,7 @@ class UsbCameraImpl : public SourceImpl,
|
||||
|
||||
protected:
|
||||
std::unique_ptr<PropertyImpl> CreateEmptyProperty(
|
||||
const wpi::Twine& name) const override;
|
||||
std::string_view name) const override;
|
||||
|
||||
// Cache properties. Immediately successful if properties are already cached.
|
||||
// If they are not, tries to connect to the camera to do so; returns false and
|
||||
@@ -132,7 +132,7 @@ class UsbCameraImpl : public SourceImpl,
|
||||
void DeviceCacheProperties();
|
||||
void DeviceCacheVideoModes();
|
||||
template <typename TagProperty, typename IAM>
|
||||
void DeviceAddProperty(const wpi::Twine& name_, TagProperty tag,
|
||||
void DeviceAddProperty(std::string_view name_, TagProperty tag,
|
||||
IAM* pProcAmp);
|
||||
|
||||
ComPtr<IMFMediaType> DeviceCheckModeValid(const VideoMode& toCheck);
|
||||
|
||||
@@ -4,14 +4,16 @@
|
||||
|
||||
#include "UsbCameraProperty.h"
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "ComPtr.h"
|
||||
|
||||
using namespace cs;
|
||||
|
||||
UsbCameraProperty::UsbCameraProperty(const wpi::Twine& name_,
|
||||
UsbCameraProperty::UsbCameraProperty(std::string_view name_,
|
||||
tagVideoProcAmpProperty tag, bool autoProp,
|
||||
IAMVideoProcAmp* pProcAmp, bool* isValid)
|
||||
: PropertyImpl{autoProp ? name_ + "_auto" : name_} {
|
||||
: PropertyImpl{autoProp ? fmt::format("{}_auto", name_) : name_} {
|
||||
this->tagVideoProc = tag;
|
||||
this->isControlProperty = false;
|
||||
this->isAutoProp = autoProp;
|
||||
@@ -71,11 +73,11 @@ bool UsbCameraProperty::DeviceSet(std::unique_lock<wpi::mutex>& lock,
|
||||
return false;
|
||||
}
|
||||
|
||||
UsbCameraProperty::UsbCameraProperty(const wpi::Twine& name_,
|
||||
UsbCameraProperty::UsbCameraProperty(std::string_view name_,
|
||||
tagCameraControlProperty tag,
|
||||
bool autoProp, IAMCameraControl* pProcAmp,
|
||||
bool* isValid)
|
||||
: PropertyImpl{autoProp ? name_ + "_auto" : name_} {
|
||||
: PropertyImpl{autoProp ? fmt::format("{}_auto", name_) : name_} {
|
||||
this->tagCameraControl = tag;
|
||||
this->isControlProperty = true;
|
||||
this->isAutoProp = autoProp;
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <mfreadwrite.h>
|
||||
|
||||
#include <memory>
|
||||
#include <string_view>
|
||||
|
||||
#include <Dshow.h>
|
||||
#include <wpi/mutex.h>
|
||||
@@ -21,19 +22,19 @@ namespace cs {
|
||||
class UsbCameraProperty : public PropertyImpl {
|
||||
public:
|
||||
UsbCameraProperty() = default;
|
||||
explicit UsbCameraProperty(const wpi::Twine& name_) : PropertyImpl{name_} {}
|
||||
explicit UsbCameraProperty(std::string_view name_) : PropertyImpl{name_} {}
|
||||
|
||||
// Software property constructor
|
||||
UsbCameraProperty(const wpi::Twine& name_, unsigned id_,
|
||||
CS_PropertyKind kind_, int minimum_, int maximum_,
|
||||
int step_, int defaultValue_, int value_)
|
||||
UsbCameraProperty(std::string_view name_, unsigned id_, CS_PropertyKind kind_,
|
||||
int minimum_, int maximum_, int step_, int defaultValue_,
|
||||
int value_)
|
||||
: PropertyImpl(name_, kind_, minimum_, maximum_, step_, defaultValue_,
|
||||
value_),
|
||||
device{false},
|
||||
id{id_} {}
|
||||
|
||||
// Normalized property constructor
|
||||
UsbCameraProperty(const wpi::Twine& name_, int rawIndex_,
|
||||
UsbCameraProperty(std::string_view name_, int rawIndex_,
|
||||
const UsbCameraProperty& rawProp, int defaultValue_,
|
||||
int value_)
|
||||
: PropertyImpl(name_, rawProp.propKind, 1, defaultValue_, value_),
|
||||
@@ -47,10 +48,10 @@ class UsbCameraProperty : public PropertyImpl {
|
||||
maximum = 100;
|
||||
}
|
||||
|
||||
UsbCameraProperty(const wpi::Twine& name_, tagVideoProcAmpProperty tag,
|
||||
UsbCameraProperty(std::string_view name_, tagVideoProcAmpProperty tag,
|
||||
bool autoProp, IAMVideoProcAmp* pProcAmp, bool* isValid);
|
||||
|
||||
UsbCameraProperty(const wpi::Twine& name_, tagCameraControlProperty tag,
|
||||
UsbCameraProperty(std::string_view name_, tagCameraControlProperty tag,
|
||||
bool autoProp, IAMCameraControl* pProcAmp, bool* isValid);
|
||||
|
||||
bool DeviceGet(std::unique_lock<wpi::mutex>& lock,
|
||||
|
||||
Reference in New Issue
Block a user