2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2019-05-30 19:12:05 -07:00
|
|
|
|
|
|
|
|
#include "ConfigurableSourceImpl.h"
|
|
|
|
|
|
2024-09-20 17:43:39 -07:00
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
2019-05-30 19:12:05 -07:00
|
|
|
#include <wpi/timestamp.h>
|
|
|
|
|
|
|
|
|
|
#include "Handle.h"
|
|
|
|
|
#include "Instance.h"
|
|
|
|
|
#include "Notifier.h"
|
|
|
|
|
|
|
|
|
|
using namespace cs;
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
ConfigurableSourceImpl::ConfigurableSourceImpl(std::string_view name,
|
2019-05-30 19:12:05 -07:00
|
|
|
wpi::Logger& logger,
|
|
|
|
|
Notifier& notifier,
|
|
|
|
|
Telemetry& telemetry,
|
|
|
|
|
const VideoMode& mode)
|
|
|
|
|
: SourceImpl{name, logger, notifier, telemetry} {
|
|
|
|
|
m_mode = mode;
|
|
|
|
|
m_videoModes.push_back(m_mode);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 00:37:33 -08:00
|
|
|
ConfigurableSourceImpl::~ConfigurableSourceImpl() = default;
|
2019-05-30 19:12:05 -07:00
|
|
|
|
|
|
|
|
void ConfigurableSourceImpl::Start() {
|
|
|
|
|
m_notifier.NotifySource(*this, CS_SOURCE_CONNECTED);
|
|
|
|
|
m_notifier.NotifySource(*this, CS_SOURCE_VIDEOMODES_UPDATED);
|
|
|
|
|
m_notifier.NotifySourceVideoMode(*this, m_mode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ConfigurableSourceImpl::SetVideoMode(const VideoMode& mode,
|
|
|
|
|
CS_Status* status) {
|
|
|
|
|
{
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_mutex);
|
2019-05-30 19:12:05 -07:00
|
|
|
m_mode = mode;
|
|
|
|
|
m_videoModes[0] = mode;
|
|
|
|
|
}
|
|
|
|
|
m_notifier.NotifySourceVideoMode(*this, mode);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigurableSourceImpl::NumSinksChanged() {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigurableSourceImpl::NumSinksEnabledChanged() {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void ConfigurableSourceImpl::NotifyError(std::string_view msg) {
|
2019-05-30 19:12:05 -07:00
|
|
|
PutError(msg, wpi::Now());
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
int ConfigurableSourceImpl::CreateProperty(std::string_view name,
|
2019-05-30 19:12:05 -07:00
|
|
|
CS_PropertyKind kind, int minimum,
|
|
|
|
|
int maximum, int step,
|
|
|
|
|
int defaultValue, int value) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_mutex);
|
2020-06-27 20:39:00 -07:00
|
|
|
int ndx = CreateOrUpdateProperty(
|
|
|
|
|
name,
|
|
|
|
|
[=] {
|
|
|
|
|
return std::make_unique<PropertyImpl>(name, kind, minimum, maximum,
|
|
|
|
|
step, defaultValue, value);
|
|
|
|
|
},
|
|
|
|
|
[&](PropertyImpl& prop) {
|
|
|
|
|
// update all but value
|
|
|
|
|
prop.propKind = kind;
|
|
|
|
|
prop.minimum = minimum;
|
|
|
|
|
prop.maximum = maximum;
|
|
|
|
|
prop.step = step;
|
|
|
|
|
prop.defaultValue = defaultValue;
|
|
|
|
|
value = prop.value;
|
|
|
|
|
});
|
2019-05-30 19:12:05 -07:00
|
|
|
m_notifier.NotifySourceProperty(*this, CS_SOURCE_PROPERTY_CREATED, name, ndx,
|
2021-06-06 16:13:58 -07:00
|
|
|
kind, value, {});
|
2019-05-30 19:12:05 -07:00
|
|
|
return ndx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ConfigurableSourceImpl::CreateProperty(
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view name, CS_PropertyKind kind, int minimum, int maximum,
|
2019-05-30 19:12:05 -07:00
|
|
|
int step, int defaultValue, int value,
|
|
|
|
|
std::function<void(CS_Property property)> onChange) {
|
|
|
|
|
// TODO
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigurableSourceImpl::SetEnumPropertyChoices(
|
2022-10-15 16:33:14 -07:00
|
|
|
int property, std::span<const std::string> choices, CS_Status* status) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_mutex);
|
2019-05-30 19:12:05 -07:00
|
|
|
auto prop = GetProperty(property);
|
|
|
|
|
if (!prop) {
|
|
|
|
|
*status = CS_INVALID_PROPERTY;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (prop->propKind != CS_PROP_ENUM) {
|
|
|
|
|
*status = CS_WRONG_PROPERTY_TYPE;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-06 19:51:14 -07:00
|
|
|
prop->enumChoices.assign(choices.begin(), choices.end());
|
2019-05-30 19:12:05 -07:00
|
|
|
m_notifier.NotifySourceProperty(*this, CS_SOURCE_PROPERTY_CHOICES_UPDATED,
|
|
|
|
|
prop->name, property, CS_PROP_ENUM,
|
2021-06-06 16:13:58 -07:00
|
|
|
prop->value, {});
|
2019-05-30 19:12:05 -07:00
|
|
|
}
|
2024-02-12 22:33:03 -08:00
|
|
|
|
|
|
|
|
namespace cs {
|
|
|
|
|
static constexpr unsigned SourceMask = CS_SOURCE_CV | CS_SOURCE_RAW;
|
|
|
|
|
|
|
|
|
|
void NotifySourceError(CS_Source source, std::string_view msg,
|
|
|
|
|
CS_Status* status) {
|
|
|
|
|
auto data = Instance::GetInstance().GetSource(source);
|
|
|
|
|
if (!data || (data->kind & SourceMask) == 0) {
|
|
|
|
|
*status = CS_INVALID_HANDLE;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
static_cast<ConfigurableSourceImpl&>(*data->source).NotifyError(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetSourceConnected(CS_Source source, bool connected, CS_Status* status) {
|
|
|
|
|
auto data = Instance::GetInstance().GetSource(source);
|
|
|
|
|
if (!data || (data->kind & SourceMask) == 0) {
|
|
|
|
|
*status = CS_INVALID_HANDLE;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
static_cast<ConfigurableSourceImpl&>(*data->source).SetConnected(connected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetSourceDescription(CS_Source source, std::string_view description,
|
|
|
|
|
CS_Status* status) {
|
|
|
|
|
auto data = Instance::GetInstance().GetSource(source);
|
|
|
|
|
if (!data || (data->kind & SourceMask) == 0) {
|
|
|
|
|
*status = CS_INVALID_HANDLE;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
static_cast<ConfigurableSourceImpl&>(*data->source)
|
|
|
|
|
.SetDescription(description);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CS_Property CreateSourceProperty(CS_Source source, std::string_view name,
|
|
|
|
|
CS_PropertyKind kind, int minimum, int maximum,
|
|
|
|
|
int step, int defaultValue, int value,
|
|
|
|
|
CS_Status* status) {
|
|
|
|
|
auto data = Instance::GetInstance().GetSource(source);
|
|
|
|
|
if (!data || (data->kind & SourceMask) == 0) {
|
|
|
|
|
*status = CS_INVALID_HANDLE;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
int property = static_cast<ConfigurableSourceImpl&>(*data->source)
|
|
|
|
|
.CreateProperty(name, kind, minimum, maximum, step,
|
|
|
|
|
defaultValue, value);
|
|
|
|
|
return Handle{source, property, Handle::kProperty};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CS_Property CreateSourcePropertyCallback(
|
|
|
|
|
CS_Source source, std::string_view name, CS_PropertyKind kind, int minimum,
|
|
|
|
|
int maximum, int step, int defaultValue, int value,
|
|
|
|
|
std::function<void(CS_Property property)> onChange, CS_Status* status) {
|
|
|
|
|
auto data = Instance::GetInstance().GetSource(source);
|
|
|
|
|
if (!data || (data->kind & SourceMask) == 0) {
|
|
|
|
|
*status = CS_INVALID_HANDLE;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
int property = static_cast<ConfigurableSourceImpl&>(*data->source)
|
|
|
|
|
.CreateProperty(name, kind, minimum, maximum, step,
|
|
|
|
|
defaultValue, value, onChange);
|
|
|
|
|
return Handle{source, property, Handle::kProperty};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetSourceEnumPropertyChoices(CS_Source source, CS_Property property,
|
|
|
|
|
std::span<const std::string> choices,
|
|
|
|
|
CS_Status* status) {
|
|
|
|
|
auto data = Instance::GetInstance().GetSource(source);
|
|
|
|
|
if (!data || (data->kind & SourceMask) == 0) {
|
|
|
|
|
*status = CS_INVALID_HANDLE;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get property index; also validate the source owns this property
|
|
|
|
|
Handle handle{property};
|
|
|
|
|
int i = handle.GetParentIndex();
|
|
|
|
|
if (i < 0) {
|
|
|
|
|
*status = CS_INVALID_HANDLE;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
auto data2 = Instance::GetInstance().GetSource(Handle{i, Handle::kSource});
|
|
|
|
|
if (!data2 || data->source.get() != data2->source.get()) {
|
|
|
|
|
*status = CS_INVALID_HANDLE;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int propertyIndex = handle.GetIndex();
|
|
|
|
|
static_cast<ConfigurableSourceImpl&>(*data->source)
|
|
|
|
|
.SetEnumPropertyChoices(propertyIndex, choices, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace cs
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
Change C APIs to a unified string implementation (#6299)
Currently in the entire C API of WPILib we have ~8 different ways of handling strings. The C API actually isn't built for pure C callers (We don't actually have any of those). Instead, they're built for interop between languages like LabVIEW and C# which can talk to C API's directly.
For output parameters, the choice was fairly obvious. An output struct containing a const string pointer and a length makes the most sense. Its easy to use these from most other languages, and doesn't require special null termination handling. Freeing these is also easy, as if you ever receive one of these string structures, theres just a single function call to free it.
Input parameters are a bit more complex. To be used from pure C, and from LabVIEW, a null terminated string is the best in most cases. However, null terminated strings in general have a lot of downsides. Additionally, from LabVIEW there are other considerations around encoding that having a wrapper struct helps make a bit easier. From a language like C#, a wrapper struct is by far the easiest, as custom marshalling can make it trivial to marshal both UTF8 and UTF16 strings down.
The final consideration is its nice to have an identical concept for both input and output. It makes the rules fairly easy to understand.
WPILib will not have any APIs that manipulate a string allocated externally. This means WPI_String can be const, as across the boundary it is always const.
If a WPILib API takes a const WPI_String*, WPILib will not manipulate or attempt to free that string, and that string is treated as an input. It is up to the caller to handle that memory, WPILib will never hold onto that memory longer than the call.
If a WPILib API takes a WPI_String*, that string is an output. WPILib will allocate that API with WPI_AllocateString(), fill in the string, and return to the caller. When the caller is done with the string, they must free it with WPI_FreeString().
If an output struct contains a WPI_String member, that member is considered read only, and should not be explicitly freed. The caller should call the free function for that struct.
If an array of WPI_Strings are returned, each individual string is considered read only, and should not be explicitly freed. The free function for that array should be called by the caller.
If an input struct containing a WPI_String, or an input array of WPI_Strings is passed to WPILib, the individual strings will not be manipulated or freed by WPILib, and the caller owns and should free that memory.
Callbacks also follow these rules. The most common is a callback either getting passed a const WPI_String* or a struct containing a WPI_String. In both of these cases, the callback target should consider these strings read only, and not attempt to free them or manipulate them.
2024-05-13 05:35:14 -07:00
|
|
|
void CS_NotifySourceError(CS_Source source, const struct WPI_String* msg,
|
2024-02-12 22:33:03 -08:00
|
|
|
CS_Status* status) {
|
Change C APIs to a unified string implementation (#6299)
Currently in the entire C API of WPILib we have ~8 different ways of handling strings. The C API actually isn't built for pure C callers (We don't actually have any of those). Instead, they're built for interop between languages like LabVIEW and C# which can talk to C API's directly.
For output parameters, the choice was fairly obvious. An output struct containing a const string pointer and a length makes the most sense. Its easy to use these from most other languages, and doesn't require special null termination handling. Freeing these is also easy, as if you ever receive one of these string structures, theres just a single function call to free it.
Input parameters are a bit more complex. To be used from pure C, and from LabVIEW, a null terminated string is the best in most cases. However, null terminated strings in general have a lot of downsides. Additionally, from LabVIEW there are other considerations around encoding that having a wrapper struct helps make a bit easier. From a language like C#, a wrapper struct is by far the easiest, as custom marshalling can make it trivial to marshal both UTF8 and UTF16 strings down.
The final consideration is its nice to have an identical concept for both input and output. It makes the rules fairly easy to understand.
WPILib will not have any APIs that manipulate a string allocated externally. This means WPI_String can be const, as across the boundary it is always const.
If a WPILib API takes a const WPI_String*, WPILib will not manipulate or attempt to free that string, and that string is treated as an input. It is up to the caller to handle that memory, WPILib will never hold onto that memory longer than the call.
If a WPILib API takes a WPI_String*, that string is an output. WPILib will allocate that API with WPI_AllocateString(), fill in the string, and return to the caller. When the caller is done with the string, they must free it with WPI_FreeString().
If an output struct contains a WPI_String member, that member is considered read only, and should not be explicitly freed. The caller should call the free function for that struct.
If an array of WPI_Strings are returned, each individual string is considered read only, and should not be explicitly freed. The free function for that array should be called by the caller.
If an input struct containing a WPI_String, or an input array of WPI_Strings is passed to WPILib, the individual strings will not be manipulated or freed by WPILib, and the caller owns and should free that memory.
Callbacks also follow these rules. The most common is a callback either getting passed a const WPI_String* or a struct containing a WPI_String. In both of these cases, the callback target should consider these strings read only, and not attempt to free them or manipulate them.
2024-05-13 05:35:14 -07:00
|
|
|
return cs::NotifySourceError(source, wpi::to_string_view(msg), status);
|
2024-02-12 22:33:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CS_SetSourceConnected(CS_Source source, CS_Bool connected,
|
|
|
|
|
CS_Status* status) {
|
|
|
|
|
return cs::SetSourceConnected(source, connected, status);
|
|
|
|
|
}
|
|
|
|
|
|
Change C APIs to a unified string implementation (#6299)
Currently in the entire C API of WPILib we have ~8 different ways of handling strings. The C API actually isn't built for pure C callers (We don't actually have any of those). Instead, they're built for interop between languages like LabVIEW and C# which can talk to C API's directly.
For output parameters, the choice was fairly obvious. An output struct containing a const string pointer and a length makes the most sense. Its easy to use these from most other languages, and doesn't require special null termination handling. Freeing these is also easy, as if you ever receive one of these string structures, theres just a single function call to free it.
Input parameters are a bit more complex. To be used from pure C, and from LabVIEW, a null terminated string is the best in most cases. However, null terminated strings in general have a lot of downsides. Additionally, from LabVIEW there are other considerations around encoding that having a wrapper struct helps make a bit easier. From a language like C#, a wrapper struct is by far the easiest, as custom marshalling can make it trivial to marshal both UTF8 and UTF16 strings down.
The final consideration is its nice to have an identical concept for both input and output. It makes the rules fairly easy to understand.
WPILib will not have any APIs that manipulate a string allocated externally. This means WPI_String can be const, as across the boundary it is always const.
If a WPILib API takes a const WPI_String*, WPILib will not manipulate or attempt to free that string, and that string is treated as an input. It is up to the caller to handle that memory, WPILib will never hold onto that memory longer than the call.
If a WPILib API takes a WPI_String*, that string is an output. WPILib will allocate that API with WPI_AllocateString(), fill in the string, and return to the caller. When the caller is done with the string, they must free it with WPI_FreeString().
If an output struct contains a WPI_String member, that member is considered read only, and should not be explicitly freed. The caller should call the free function for that struct.
If an array of WPI_Strings are returned, each individual string is considered read only, and should not be explicitly freed. The free function for that array should be called by the caller.
If an input struct containing a WPI_String, or an input array of WPI_Strings is passed to WPILib, the individual strings will not be manipulated or freed by WPILib, and the caller owns and should free that memory.
Callbacks also follow these rules. The most common is a callback either getting passed a const WPI_String* or a struct containing a WPI_String. In both of these cases, the callback target should consider these strings read only, and not attempt to free them or manipulate them.
2024-05-13 05:35:14 -07:00
|
|
|
void CS_SetSourceDescription(CS_Source source,
|
|
|
|
|
const struct WPI_String* description,
|
2024-02-12 22:33:03 -08:00
|
|
|
CS_Status* status) {
|
Change C APIs to a unified string implementation (#6299)
Currently in the entire C API of WPILib we have ~8 different ways of handling strings. The C API actually isn't built for pure C callers (We don't actually have any of those). Instead, they're built for interop between languages like LabVIEW and C# which can talk to C API's directly.
For output parameters, the choice was fairly obvious. An output struct containing a const string pointer and a length makes the most sense. Its easy to use these from most other languages, and doesn't require special null termination handling. Freeing these is also easy, as if you ever receive one of these string structures, theres just a single function call to free it.
Input parameters are a bit more complex. To be used from pure C, and from LabVIEW, a null terminated string is the best in most cases. However, null terminated strings in general have a lot of downsides. Additionally, from LabVIEW there are other considerations around encoding that having a wrapper struct helps make a bit easier. From a language like C#, a wrapper struct is by far the easiest, as custom marshalling can make it trivial to marshal both UTF8 and UTF16 strings down.
The final consideration is its nice to have an identical concept for both input and output. It makes the rules fairly easy to understand.
WPILib will not have any APIs that manipulate a string allocated externally. This means WPI_String can be const, as across the boundary it is always const.
If a WPILib API takes a const WPI_String*, WPILib will not manipulate or attempt to free that string, and that string is treated as an input. It is up to the caller to handle that memory, WPILib will never hold onto that memory longer than the call.
If a WPILib API takes a WPI_String*, that string is an output. WPILib will allocate that API with WPI_AllocateString(), fill in the string, and return to the caller. When the caller is done with the string, they must free it with WPI_FreeString().
If an output struct contains a WPI_String member, that member is considered read only, and should not be explicitly freed. The caller should call the free function for that struct.
If an array of WPI_Strings are returned, each individual string is considered read only, and should not be explicitly freed. The free function for that array should be called by the caller.
If an input struct containing a WPI_String, or an input array of WPI_Strings is passed to WPILib, the individual strings will not be manipulated or freed by WPILib, and the caller owns and should free that memory.
Callbacks also follow these rules. The most common is a callback either getting passed a const WPI_String* or a struct containing a WPI_String. In both of these cases, the callback target should consider these strings read only, and not attempt to free them or manipulate them.
2024-05-13 05:35:14 -07:00
|
|
|
return cs::SetSourceDescription(source, wpi::to_string_view(description),
|
|
|
|
|
status);
|
2024-02-12 22:33:03 -08:00
|
|
|
}
|
|
|
|
|
|
Change C APIs to a unified string implementation (#6299)
Currently in the entire C API of WPILib we have ~8 different ways of handling strings. The C API actually isn't built for pure C callers (We don't actually have any of those). Instead, they're built for interop between languages like LabVIEW and C# which can talk to C API's directly.
For output parameters, the choice was fairly obvious. An output struct containing a const string pointer and a length makes the most sense. Its easy to use these from most other languages, and doesn't require special null termination handling. Freeing these is also easy, as if you ever receive one of these string structures, theres just a single function call to free it.
Input parameters are a bit more complex. To be used from pure C, and from LabVIEW, a null terminated string is the best in most cases. However, null terminated strings in general have a lot of downsides. Additionally, from LabVIEW there are other considerations around encoding that having a wrapper struct helps make a bit easier. From a language like C#, a wrapper struct is by far the easiest, as custom marshalling can make it trivial to marshal both UTF8 and UTF16 strings down.
The final consideration is its nice to have an identical concept for both input and output. It makes the rules fairly easy to understand.
WPILib will not have any APIs that manipulate a string allocated externally. This means WPI_String can be const, as across the boundary it is always const.
If a WPILib API takes a const WPI_String*, WPILib will not manipulate or attempt to free that string, and that string is treated as an input. It is up to the caller to handle that memory, WPILib will never hold onto that memory longer than the call.
If a WPILib API takes a WPI_String*, that string is an output. WPILib will allocate that API with WPI_AllocateString(), fill in the string, and return to the caller. When the caller is done with the string, they must free it with WPI_FreeString().
If an output struct contains a WPI_String member, that member is considered read only, and should not be explicitly freed. The caller should call the free function for that struct.
If an array of WPI_Strings are returned, each individual string is considered read only, and should not be explicitly freed. The free function for that array should be called by the caller.
If an input struct containing a WPI_String, or an input array of WPI_Strings is passed to WPILib, the individual strings will not be manipulated or freed by WPILib, and the caller owns and should free that memory.
Callbacks also follow these rules. The most common is a callback either getting passed a const WPI_String* or a struct containing a WPI_String. In both of these cases, the callback target should consider these strings read only, and not attempt to free them or manipulate them.
2024-05-13 05:35:14 -07:00
|
|
|
CS_Property CS_CreateSourceProperty(CS_Source source,
|
|
|
|
|
const struct WPI_String* name,
|
2024-02-12 22:33:03 -08:00
|
|
|
enum CS_PropertyKind kind, int minimum,
|
|
|
|
|
int maximum, int step, int defaultValue,
|
|
|
|
|
int value, CS_Status* status) {
|
Change C APIs to a unified string implementation (#6299)
Currently in the entire C API of WPILib we have ~8 different ways of handling strings. The C API actually isn't built for pure C callers (We don't actually have any of those). Instead, they're built for interop between languages like LabVIEW and C# which can talk to C API's directly.
For output parameters, the choice was fairly obvious. An output struct containing a const string pointer and a length makes the most sense. Its easy to use these from most other languages, and doesn't require special null termination handling. Freeing these is also easy, as if you ever receive one of these string structures, theres just a single function call to free it.
Input parameters are a bit more complex. To be used from pure C, and from LabVIEW, a null terminated string is the best in most cases. However, null terminated strings in general have a lot of downsides. Additionally, from LabVIEW there are other considerations around encoding that having a wrapper struct helps make a bit easier. From a language like C#, a wrapper struct is by far the easiest, as custom marshalling can make it trivial to marshal both UTF8 and UTF16 strings down.
The final consideration is its nice to have an identical concept for both input and output. It makes the rules fairly easy to understand.
WPILib will not have any APIs that manipulate a string allocated externally. This means WPI_String can be const, as across the boundary it is always const.
If a WPILib API takes a const WPI_String*, WPILib will not manipulate or attempt to free that string, and that string is treated as an input. It is up to the caller to handle that memory, WPILib will never hold onto that memory longer than the call.
If a WPILib API takes a WPI_String*, that string is an output. WPILib will allocate that API with WPI_AllocateString(), fill in the string, and return to the caller. When the caller is done with the string, they must free it with WPI_FreeString().
If an output struct contains a WPI_String member, that member is considered read only, and should not be explicitly freed. The caller should call the free function for that struct.
If an array of WPI_Strings are returned, each individual string is considered read only, and should not be explicitly freed. The free function for that array should be called by the caller.
If an input struct containing a WPI_String, or an input array of WPI_Strings is passed to WPILib, the individual strings will not be manipulated or freed by WPILib, and the caller owns and should free that memory.
Callbacks also follow these rules. The most common is a callback either getting passed a const WPI_String* or a struct containing a WPI_String. In both of these cases, the callback target should consider these strings read only, and not attempt to free them or manipulate them.
2024-05-13 05:35:14 -07:00
|
|
|
return cs::CreateSourceProperty(source, wpi::to_string_view(name), kind,
|
|
|
|
|
minimum, maximum, step, defaultValue, value,
|
|
|
|
|
status);
|
2024-02-12 22:33:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CS_Property CS_CreateSourcePropertyCallback(
|
|
|
|
|
CS_Source source, const char* name, enum CS_PropertyKind kind, int minimum,
|
|
|
|
|
int maximum, int step, int defaultValue, int value, void* data,
|
|
|
|
|
void (*onChange)(void* data, CS_Property property), CS_Status* status) {
|
|
|
|
|
return cs::CreateSourcePropertyCallback(
|
|
|
|
|
source, name, kind, minimum, maximum, step, defaultValue, value,
|
|
|
|
|
[=](CS_Property property) { onChange(data, property); }, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CS_SetSourceEnumPropertyChoices(CS_Source source, CS_Property property,
|
Change C APIs to a unified string implementation (#6299)
Currently in the entire C API of WPILib we have ~8 different ways of handling strings. The C API actually isn't built for pure C callers (We don't actually have any of those). Instead, they're built for interop between languages like LabVIEW and C# which can talk to C API's directly.
For output parameters, the choice was fairly obvious. An output struct containing a const string pointer and a length makes the most sense. Its easy to use these from most other languages, and doesn't require special null termination handling. Freeing these is also easy, as if you ever receive one of these string structures, theres just a single function call to free it.
Input parameters are a bit more complex. To be used from pure C, and from LabVIEW, a null terminated string is the best in most cases. However, null terminated strings in general have a lot of downsides. Additionally, from LabVIEW there are other considerations around encoding that having a wrapper struct helps make a bit easier. From a language like C#, a wrapper struct is by far the easiest, as custom marshalling can make it trivial to marshal both UTF8 and UTF16 strings down.
The final consideration is its nice to have an identical concept for both input and output. It makes the rules fairly easy to understand.
WPILib will not have any APIs that manipulate a string allocated externally. This means WPI_String can be const, as across the boundary it is always const.
If a WPILib API takes a const WPI_String*, WPILib will not manipulate or attempt to free that string, and that string is treated as an input. It is up to the caller to handle that memory, WPILib will never hold onto that memory longer than the call.
If a WPILib API takes a WPI_String*, that string is an output. WPILib will allocate that API with WPI_AllocateString(), fill in the string, and return to the caller. When the caller is done with the string, they must free it with WPI_FreeString().
If an output struct contains a WPI_String member, that member is considered read only, and should not be explicitly freed. The caller should call the free function for that struct.
If an array of WPI_Strings are returned, each individual string is considered read only, and should not be explicitly freed. The free function for that array should be called by the caller.
If an input struct containing a WPI_String, or an input array of WPI_Strings is passed to WPILib, the individual strings will not be manipulated or freed by WPILib, and the caller owns and should free that memory.
Callbacks also follow these rules. The most common is a callback either getting passed a const WPI_String* or a struct containing a WPI_String. In both of these cases, the callback target should consider these strings read only, and not attempt to free them or manipulate them.
2024-05-13 05:35:14 -07:00
|
|
|
const struct WPI_String* choices,
|
|
|
|
|
int count, CS_Status* status) {
|
2024-02-12 22:33:03 -08:00
|
|
|
wpi::SmallVector<std::string, 8> vec;
|
|
|
|
|
vec.reserve(count);
|
|
|
|
|
for (int i = 0; i < count; ++i) {
|
Change C APIs to a unified string implementation (#6299)
Currently in the entire C API of WPILib we have ~8 different ways of handling strings. The C API actually isn't built for pure C callers (We don't actually have any of those). Instead, they're built for interop between languages like LabVIEW and C# which can talk to C API's directly.
For output parameters, the choice was fairly obvious. An output struct containing a const string pointer and a length makes the most sense. Its easy to use these from most other languages, and doesn't require special null termination handling. Freeing these is also easy, as if you ever receive one of these string structures, theres just a single function call to free it.
Input parameters are a bit more complex. To be used from pure C, and from LabVIEW, a null terminated string is the best in most cases. However, null terminated strings in general have a lot of downsides. Additionally, from LabVIEW there are other considerations around encoding that having a wrapper struct helps make a bit easier. From a language like C#, a wrapper struct is by far the easiest, as custom marshalling can make it trivial to marshal both UTF8 and UTF16 strings down.
The final consideration is its nice to have an identical concept for both input and output. It makes the rules fairly easy to understand.
WPILib will not have any APIs that manipulate a string allocated externally. This means WPI_String can be const, as across the boundary it is always const.
If a WPILib API takes a const WPI_String*, WPILib will not manipulate or attempt to free that string, and that string is treated as an input. It is up to the caller to handle that memory, WPILib will never hold onto that memory longer than the call.
If a WPILib API takes a WPI_String*, that string is an output. WPILib will allocate that API with WPI_AllocateString(), fill in the string, and return to the caller. When the caller is done with the string, they must free it with WPI_FreeString().
If an output struct contains a WPI_String member, that member is considered read only, and should not be explicitly freed. The caller should call the free function for that struct.
If an array of WPI_Strings are returned, each individual string is considered read only, and should not be explicitly freed. The free function for that array should be called by the caller.
If an input struct containing a WPI_String, or an input array of WPI_Strings is passed to WPILib, the individual strings will not be manipulated or freed by WPILib, and the caller owns and should free that memory.
Callbacks also follow these rules. The most common is a callback either getting passed a const WPI_String* or a struct containing a WPI_String. In both of these cases, the callback target should consider these strings read only, and not attempt to free them or manipulate them.
2024-05-13 05:35:14 -07:00
|
|
|
vec.emplace_back(wpi::to_string_view(&choices[i]));
|
2024-02-12 22:33:03 -08:00
|
|
|
}
|
|
|
|
|
return cs::SetSourceEnumPropertyChoices(source, property, vec, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // extern "C"
|