mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
Use StringRef and ArrayRef return values when buf passed.
For functions where a SmallVector is passed to be used as a stack buffer for the return value, have the return value be the appropriate StringRef or ArrayRef type. This allows for both more natural usage and enables directly returning (rather than copying) a permanently stored or constant string.
This commit is contained in:
@@ -258,9 +258,9 @@ HTTPSinkImpl::HTTPSinkImpl(llvm::StringRef name, llvm::StringRef description,
|
||||
|
||||
HTTPSinkImpl::~HTTPSinkImpl() { Stop(); }
|
||||
|
||||
void HTTPSinkImpl::GetDescription(llvm::SmallVectorImpl<char>& desc) const {
|
||||
llvm::raw_svector_ostream oss{desc};
|
||||
oss << m_description;
|
||||
llvm::StringRef HTTPSinkImpl::GetDescription(
|
||||
llvm::SmallVectorImpl<char>& buf) const {
|
||||
return m_description;
|
||||
}
|
||||
|
||||
void HTTPSinkImpl::Stop() {
|
||||
|
||||
@@ -35,7 +35,8 @@ class HTTPSinkImpl : public SinkImpl {
|
||||
std::unique_ptr<wpi::NetworkAcceptor> acceptor);
|
||||
~HTTPSinkImpl() override;
|
||||
|
||||
void GetDescription(llvm::SmallVectorImpl<char>& desc) const override;
|
||||
llvm::StringRef GetDescription(
|
||||
llvm::SmallVectorImpl<char>& buf) const override;
|
||||
|
||||
void SetSourceChannel(int channel) { m_sourceChannel.store(channel); }
|
||||
|
||||
|
||||
@@ -28,7 +28,8 @@ class SinkImpl {
|
||||
SinkImpl& operator=(const SinkImpl& queue) = delete;
|
||||
|
||||
llvm::StringRef GetName() const { return m_name; }
|
||||
virtual void GetDescription(llvm::SmallVectorImpl<char>& desc) const = 0;
|
||||
virtual llvm::StringRef GetDescription(
|
||||
llvm::SmallVectorImpl<char>& buf) const = 0;
|
||||
|
||||
void Enable() {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "llvm/ArrayRef.h"
|
||||
#include "llvm/StringRef.h"
|
||||
#include "cameraserver_c.h"
|
||||
#include "Frame.h"
|
||||
@@ -31,7 +32,8 @@ class SourceImpl {
|
||||
SourceImpl& operator=(const SourceImpl& queue) = delete;
|
||||
|
||||
llvm::StringRef GetName() const { return m_name; }
|
||||
virtual void GetDescription(llvm::SmallVectorImpl<char>& desc) const = 0;
|
||||
virtual llvm::StringRef GetDescription(
|
||||
llvm::SmallVectorImpl<char>& buf) const = 0;
|
||||
int GetNumChannels() const { return m_numChannels; }
|
||||
bool IsConnected() const { return m_connected; }
|
||||
|
||||
@@ -78,19 +80,19 @@ class SourceImpl {
|
||||
|
||||
// Property functions
|
||||
virtual int GetProperty(llvm::StringRef name) const = 0;
|
||||
virtual void EnumerateProperties(
|
||||
llvm::SmallVectorImpl<int>& properties) const = 0;
|
||||
virtual llvm::ArrayRef<int> EnumerateProperties(
|
||||
llvm::SmallVectorImpl<int>& vec) const = 0;
|
||||
virtual CS_PropertyType GetPropertyType(int property) const = 0;
|
||||
virtual void GetPropertyName(int property,
|
||||
llvm::SmallVectorImpl<char>& name) const = 0;
|
||||
virtual llvm::StringRef GetPropertyName(
|
||||
int property, llvm::SmallVectorImpl<char>& buf) const = 0;
|
||||
virtual bool GetBooleanProperty(int property) const = 0;
|
||||
virtual void SetBooleanProperty(int property, bool value) = 0;
|
||||
virtual double GetDoubleProperty(int property) const = 0;
|
||||
virtual void SetDoubleProperty(int property, double value) = 0;
|
||||
virtual double GetPropertyMin(int property) const = 0;
|
||||
virtual double GetPropertyMax(int property) const = 0;
|
||||
virtual void GetStringProperty(int property,
|
||||
llvm::SmallVectorImpl<char>& value) const = 0;
|
||||
virtual llvm::StringRef GetStringProperty(
|
||||
int property, llvm::SmallVectorImpl<char>& buf) const = 0;
|
||||
virtual void SetStringProperty(int property, llvm::StringRef value) = 0;
|
||||
virtual int GetEnumProperty(int property) const = 0;
|
||||
virtual void SetEnumProperty(int property, int value) = 0;
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
#include "llvm/ArrayRef.h"
|
||||
#include "llvm/SmallVector.h"
|
||||
#include "support/atomic_static.h"
|
||||
|
||||
@@ -52,7 +53,7 @@ class UnlimitedHandleResource {
|
||||
void Free(THandle handle);
|
||||
|
||||
template <typename T>
|
||||
void GetAll(llvm::SmallVectorImpl<T>& handles);
|
||||
llvm::ArrayRef<T> GetAll(llvm::SmallVectorImpl<T>& vec);
|
||||
|
||||
private:
|
||||
THandle MakeHandle(size_t i) {
|
||||
@@ -124,13 +125,15 @@ void UnlimitedHandleResource<THandle, TStruct, typeValue, TMutex>::Free(
|
||||
|
||||
template <typename THandle, typename TStruct, int typeValue, typename TMutex>
|
||||
template <typename T>
|
||||
void UnlimitedHandleResource<THandle, TStruct, typeValue, TMutex>::GetAll(
|
||||
llvm::SmallVectorImpl<T>& handles) {
|
||||
llvm::ArrayRef<T>
|
||||
UnlimitedHandleResource<THandle, TStruct, typeValue, TMutex>::GetAll(
|
||||
llvm::SmallVectorImpl<T>& vec) {
|
||||
std::lock_guard<TMutex> sync(m_handleMutex);
|
||||
size_t i;
|
||||
for (i = 0; i < m_structures.size(); i++) {
|
||||
if (m_structures[i] != nullptr) handles.push_back(MakeHandle(i));
|
||||
if (m_structures[i] != nullptr) vec.push_back(MakeHandle(i));
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
template <typename THandle, typename TStruct, int typeValue,
|
||||
|
||||
@@ -32,8 +32,8 @@ CS_PropertyType CS_GetPropertyType(CS_Property property, CS_Status* status) {
|
||||
}
|
||||
|
||||
char* CS_GetPropertyName(CS_Property property, CS_Status* status) {
|
||||
llvm::SmallString<128> str;
|
||||
cs::GetPropertyName(property, str, status);
|
||||
llvm::SmallString<128> buf;
|
||||
auto str = cs::GetPropertyName(property, buf, status);
|
||||
if (*status != 0) return nullptr;
|
||||
return ConvertToC(str);
|
||||
}
|
||||
@@ -65,8 +65,8 @@ double CS_GetDoublePropertyMax(CS_Property property, CS_Status* status) {
|
||||
}
|
||||
|
||||
char* CS_GetStringProperty(CS_Property property, CS_Status* status) {
|
||||
llvm::SmallString<128> str;
|
||||
cs::GetStringProperty(property, str, status);
|
||||
llvm::SmallString<128> buf;
|
||||
auto str = cs::GetStringProperty(property, buf, status);
|
||||
if (*status != 0) return nullptr;
|
||||
return ConvertToC(str);
|
||||
}
|
||||
@@ -114,15 +114,15 @@ CS_Source CS_CreateCvSource(const char* name, int numChannels,
|
||||
}
|
||||
|
||||
char* CS_GetSourceName(CS_Source source, CS_Status* status) {
|
||||
llvm::SmallString<128> str;
|
||||
cs::GetSourceName(source, str, status);
|
||||
llvm::SmallString<128> buf;
|
||||
auto str = cs::GetSourceName(source, buf, status);
|
||||
if (*status != 0) return nullptr;
|
||||
return ConvertToC(str);
|
||||
}
|
||||
|
||||
char* CS_GetSourceDescription(CS_Source source, CS_Status* status) {
|
||||
llvm::SmallString<128> str;
|
||||
cs::GetSourceDescription(source, str, status);
|
||||
llvm::SmallString<128> buf;
|
||||
auto str = cs::GetSourceDescription(source, buf, status);
|
||||
if (*status != 0) return nullptr;
|
||||
return ConvertToC(str);
|
||||
}
|
||||
@@ -146,8 +146,8 @@ CS_Property CS_GetSourceProperty(CS_Source source, const char* name,
|
||||
|
||||
CS_Property* CS_EnumerateSourceProperties(CS_Source source, int* count,
|
||||
CS_Status* status) {
|
||||
llvm::SmallVector<CS_Property, 32> vec;
|
||||
cs::EnumerateSourceProperties(source, vec, status);
|
||||
llvm::SmallVector<CS_Property, 32> buf;
|
||||
auto vec = cs::EnumerateSourceProperties(source, buf, status);
|
||||
CS_Property* out =
|
||||
static_cast<CS_Property*>(std::malloc(vec.size() * sizeof(CS_Property)));
|
||||
*count = vec.size();
|
||||
@@ -224,15 +224,15 @@ CS_Sink CS_CreateCvSinkCallback(const char* name, void* data,
|
||||
}
|
||||
|
||||
char* CS_GetSinkName(CS_Sink sink, CS_Status* status) {
|
||||
llvm::SmallString<128> str;
|
||||
cs::GetSinkName(sink, str, status);
|
||||
llvm::SmallString<128> buf;
|
||||
auto str = cs::GetSinkName(sink, buf, status);
|
||||
if (*status != 0) return nullptr;
|
||||
return ConvertToC(str);
|
||||
}
|
||||
|
||||
char* CS_GetSinkDescription(CS_Sink sink, CS_Status* status) {
|
||||
llvm::SmallString<128> str;
|
||||
cs::GetSinkDescription(sink, str, status);
|
||||
llvm::SmallString<128> buf;
|
||||
auto str = cs::GetSinkDescription(sink, buf, status);
|
||||
if (*status != 0) return nullptr;
|
||||
return ConvertToC(str);
|
||||
}
|
||||
@@ -273,8 +273,8 @@ uint64_t CS_GrabSinkFrame(CS_Sink sink, struct CvMat* image,
|
||||
}
|
||||
|
||||
char* CS_GetSinkError(CS_Sink sink, CS_Status* status) {
|
||||
llvm::SmallString<128> str;
|
||||
cs::GetSinkError(sink, str, status);
|
||||
llvm::SmallString<128> buf;
|
||||
auto str = cs::GetSinkError(sink, buf, status);
|
||||
if (*status != 0) return nullptr;
|
||||
return ConvertToC(str);
|
||||
}
|
||||
@@ -349,8 +349,8 @@ void CS_FreeEnumeratedUSBCameras(CS_USBCameraInfo* cameras, int count) {
|
||||
}
|
||||
|
||||
CS_Source* CS_EnumerateSources(int* count, CS_Status* status) {
|
||||
llvm::SmallVector<CS_Source, 32> handles;
|
||||
cs::EnumerateSourceHandles(handles, status);
|
||||
llvm::SmallVector<CS_Source, 32> buf;
|
||||
auto handles = cs::EnumerateSourceHandles(buf, status);
|
||||
CS_Source* sources =
|
||||
static_cast<CS_Source*>(std::malloc(handles.size() * sizeof(CS_Source)));
|
||||
*count = handles.size();
|
||||
@@ -368,8 +368,8 @@ void CS_ReleaseEnumeratedSources(CS_Source* sources, int count) {
|
||||
}
|
||||
|
||||
CS_Sink* CS_EnumerateSinks(int* count, CS_Status* status) {
|
||||
llvm::SmallVector<CS_Sink, 32> handles;
|
||||
cs::EnumerateSinkHandles(handles, status);
|
||||
llvm::SmallVector<CS_Sink, 32> buf;
|
||||
auto handles = cs::EnumerateSinkHandles(buf, status);
|
||||
CS_Sink* sinks =
|
||||
static_cast<CS_Sink*>(std::malloc(handles.size() * sizeof(CS_Sink)));
|
||||
*count = handles.size();
|
||||
|
||||
@@ -55,12 +55,13 @@ std::string GetPropertyName(CS_Property property, CS_Status* status) {
|
||||
return name.str();
|
||||
}
|
||||
|
||||
void GetPropertyName(CS_Property property, llvm::SmallVectorImpl<char>& name,
|
||||
CS_Status* status) {
|
||||
llvm::StringRef GetPropertyName(CS_Property property,
|
||||
llvm::SmallVectorImpl<char>& buf,
|
||||
CS_Status* status) {
|
||||
int propertyIndex;
|
||||
auto source = GetPropertySource(property, &propertyIndex, status);
|
||||
if (!source) return;
|
||||
source->GetPropertyName(propertyIndex, name);
|
||||
if (!source) return llvm::StringRef{};
|
||||
return source->GetPropertyName(propertyIndex, buf);
|
||||
}
|
||||
|
||||
bool GetBooleanProperty(CS_Property property, CS_Status* status) {
|
||||
@@ -114,12 +115,13 @@ std::string GetStringProperty(CS_Property property, CS_Status* status) {
|
||||
return value.str();
|
||||
}
|
||||
|
||||
void GetStringProperty(CS_Property property, llvm::SmallVectorImpl<char>& value,
|
||||
CS_Status* status) {
|
||||
llvm::StringRef GetStringProperty(CS_Property property,
|
||||
llvm::SmallVectorImpl<char>& buf,
|
||||
CS_Status* status) {
|
||||
int propertyIndex;
|
||||
auto source = GetPropertySource(property, &propertyIndex, status);
|
||||
if (!source) return;
|
||||
source->GetStringProperty(propertyIndex, value);
|
||||
if (!source) return llvm::StringRef{};
|
||||
return source->GetStringProperty(propertyIndex, buf);
|
||||
}
|
||||
|
||||
void SetStringProperty(CS_Property property, llvm::StringRef value,
|
||||
@@ -188,36 +190,36 @@ std::string GetSourceName(CS_Source source, CS_Status* status) {
|
||||
return data->source->GetName();
|
||||
}
|
||||
|
||||
void GetSourceName(CS_Source source, llvm::SmallVectorImpl<char>& name,
|
||||
CS_Status* status) {
|
||||
llvm::StringRef GetSourceName(CS_Source source,
|
||||
llvm::SmallVectorImpl<char>& buf,
|
||||
CS_Status* status) {
|
||||
auto data = Sources::GetInstance().Get(source);
|
||||
if (!data) {
|
||||
*status = CS_INVALID_HANDLE;
|
||||
return;
|
||||
return llvm::StringRef{};
|
||||
}
|
||||
auto str = data->source->GetName();
|
||||
name.append(str.begin(), str.end());
|
||||
return data->source->GetName();
|
||||
}
|
||||
|
||||
std::string GetSourceDescription(CS_Source source, CS_Status* status) {
|
||||
llvm::SmallString<128> desc;
|
||||
auto data = Sources::GetInstance().Get(source);
|
||||
if (!data) {
|
||||
*status = CS_INVALID_HANDLE;
|
||||
return std::string{};
|
||||
}
|
||||
data->source->GetDescription(desc);
|
||||
return desc.str();
|
||||
llvm::SmallString<128> buf;
|
||||
return data->source->GetDescription(buf);
|
||||
}
|
||||
|
||||
void GetSourceDescription(CS_Source source, llvm::SmallVectorImpl<char>& desc,
|
||||
CS_Status* status) {
|
||||
llvm::StringRef GetSourceDescription(CS_Source source,
|
||||
llvm::SmallVectorImpl<char>& buf,
|
||||
CS_Status* status) {
|
||||
auto data = Sources::GetInstance().Get(source);
|
||||
if (!data) {
|
||||
*status = CS_INVALID_HANDLE;
|
||||
return;
|
||||
return llvm::StringRef{};
|
||||
}
|
||||
data->source->GetDescription(desc);
|
||||
return data->source->GetDescription(buf);
|
||||
}
|
||||
|
||||
uint64_t GetSourceLastFrameTime(CS_Source source, CS_Status* status) {
|
||||
@@ -257,10 +259,11 @@ CS_Property GetSourceProperty(CS_Source source, llvm::StringRef name,
|
||||
return Handle{source, property, Handle::kProperty};
|
||||
}
|
||||
|
||||
void EnumerateSourceProperties(CS_Source source,
|
||||
llvm::SmallVectorImpl<CS_Property>& properties,
|
||||
CS_Status* status) {
|
||||
llvm::ArrayRef<CS_Property> EnumerateSourceProperties(
|
||||
CS_Source source, llvm::SmallVectorImpl<CS_Property>& vec,
|
||||
CS_Status* status) {
|
||||
// TODO
|
||||
return llvm::ArrayRef<CS_Property>{};
|
||||
}
|
||||
|
||||
CS_Source CopySource(CS_Source source, CS_Status* status) {
|
||||
@@ -358,36 +361,35 @@ std::string GetSinkName(CS_Sink sink, CS_Status* status) {
|
||||
return data->sink->GetName();
|
||||
}
|
||||
|
||||
void GetSinkName(CS_Sink sink, llvm::SmallVectorImpl<char>& name,
|
||||
CS_Status* status) {
|
||||
llvm::StringRef GetSinkName(CS_Sink sink, llvm::SmallVectorImpl<char>& buf,
|
||||
CS_Status* status) {
|
||||
auto data = Sinks::GetInstance().Get(sink);
|
||||
if (!data) {
|
||||
*status = CS_INVALID_HANDLE;
|
||||
return;
|
||||
return llvm::StringRef{};
|
||||
}
|
||||
auto str = data->sink->GetName();
|
||||
name.append(str.begin(), str.end());
|
||||
return data->sink->GetName();
|
||||
}
|
||||
|
||||
std::string GetSinkDescription(CS_Sink sink, CS_Status* status) {
|
||||
llvm::SmallString<128> desc;
|
||||
auto data = Sinks::GetInstance().Get(sink);
|
||||
if (!data) {
|
||||
*status = CS_INVALID_HANDLE;
|
||||
return std::string{};
|
||||
}
|
||||
data->sink->GetDescription(desc);
|
||||
return desc.str();
|
||||
llvm::SmallString<128> buf;
|
||||
return data->sink->GetDescription(buf);
|
||||
}
|
||||
|
||||
void GetSinkDescription(CS_Sink sink, llvm::SmallVectorImpl<char>& desc,
|
||||
CS_Status* status) {
|
||||
llvm::StringRef GetSinkDescription(CS_Sink sink,
|
||||
llvm::SmallVectorImpl<char>& buf,
|
||||
CS_Status* status) {
|
||||
auto data = Sinks::GetInstance().Get(sink);
|
||||
if (!data) {
|
||||
*status = CS_INVALID_HANDLE;
|
||||
return;
|
||||
return llvm::StringRef{};
|
||||
}
|
||||
data->sink->GetDescription(desc);
|
||||
return data->sink->GetDescription(buf);
|
||||
}
|
||||
|
||||
void SetSinkSource(CS_Sink sink, CS_Source source, CS_Status* status) {
|
||||
@@ -464,12 +466,12 @@ uint64_t GrabSinkFrame(CS_Sink sink, cv::Mat* image, CS_Status* status) {
|
||||
}
|
||||
|
||||
std::string GetSinkError(CS_Sink sink, CS_Status* status) {
|
||||
return ""; // TODO
|
||||
return std::string{}; // TODO
|
||||
}
|
||||
|
||||
void GetSinkError(CS_Sink sink, llvm::SmallVectorImpl<char>& msg,
|
||||
CS_Status* status) {
|
||||
// TODO
|
||||
llvm::StringRef GetSinkError(CS_Sink sink, llvm::SmallVectorImpl<char>& buf,
|
||||
CS_Status* status) {
|
||||
return llvm::StringRef{}; // TODO
|
||||
}
|
||||
|
||||
void SetSinkEnabled(CS_Sink sink, bool enabled, CS_Status* status) {
|
||||
@@ -509,14 +511,14 @@ std::vector<USBCameraInfo> EnumerateUSBCameras(CS_Status* status) {
|
||||
return std::vector<USBCameraInfo>{}; // TODO
|
||||
}
|
||||
|
||||
void EnumerateSourceHandles(llvm::SmallVectorImpl<CS_Source>& handles,
|
||||
CS_Status* status) {
|
||||
Sources::GetInstance().GetAll(handles);
|
||||
llvm::ArrayRef<CS_Source> EnumerateSourceHandles(
|
||||
llvm::SmallVectorImpl<CS_Source>& vec, CS_Status* status) {
|
||||
return Sources::GetInstance().GetAll(vec);
|
||||
}
|
||||
|
||||
void EnumerateSinkHandles(llvm::SmallVectorImpl<CS_Sink>& handles,
|
||||
CS_Status* status) {
|
||||
Sinks::GetInstance().GetAll(handles);
|
||||
llvm::ArrayRef<CS_Sink> EnumerateSinkHandles(
|
||||
llvm::SmallVectorImpl<CS_Sink>& vec, CS_Status* status) {
|
||||
return Sinks::GetInstance().GetAll(vec);
|
||||
}
|
||||
|
||||
} // namespace cs
|
||||
|
||||
Reference in New Issue
Block a user