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.
This commit is contained in:
Thad House
2024-05-13 05:35:14 -07:00
committed by GitHub
parent 178fe99f12
commit 4ce8f3f935
60 changed files with 990 additions and 914 deletions

View File

@@ -16,11 +16,11 @@ static inline std::span<const T> ConvertFromC(const T* arr, size_t size) {
return {arr, size};
}
static inline std::string_view ConvertFromC(const char* arr, size_t size) {
return {arr, size};
static inline std::string_view ConvertFromC(const struct WPI_String* str) {
return wpi::to_string_view(str);
}
static std::vector<std::string> ConvertFromC(const NT_String* arr, size_t size) {
static std::vector<std::string> ConvertFromC(const struct WPI_String* arr, size_t size) {
std::vector<std::string> v;
v.reserve(size);
for (size_t i = 0; i < size; ++i) {
@@ -57,7 +57,7 @@ static void ConvertToC(const nt::TimestampedDouble& in, NT_TimestampedDouble* ou
static void ConvertToC(const nt::TimestampedString& in, NT_TimestampedString* out) {
out->time = in.time;
out->serverTime = in.serverTime;
out->value = ConvertToC<char>(in.value, &out->len);
ConvertToC(in.value, &out->value);
}
static void ConvertToC(const nt::TimestampedRaw& in, NT_TimestampedRaw* out) {
@@ -93,7 +93,7 @@ static void ConvertToC(const nt::TimestampedDoubleArray& in, NT_TimestampedDoubl
static void ConvertToC(const nt::TimestampedStringArray& in, NT_TimestampedStringArray* out) {
out->time = in.time;
out->serverTime = in.serverTime;
out->value = ConvertToC<struct NT_String>(in.value, &out->len);
out->value = ConvertToC<struct WPI_String>(in.value, &out->len);
}
@@ -247,26 +247,26 @@ double* NT_ReadQueueValuesDouble(NT_Handle subentry, size_t* len) {
}
NT_Bool NT_SetString(NT_Handle pubentry, int64_t time, const char* value, size_t len) {
return nt::SetString(pubentry, ConvertFromC(value, len), time);
NT_Bool NT_SetString(NT_Handle pubentry, int64_t time, const struct WPI_String* value) {
return nt::SetString(pubentry, ConvertFromC(value), time);
}
NT_Bool NT_SetDefaultString(NT_Handle pubentry, const char* defaultValue, size_t defaultValueLen) {
return nt::SetDefaultString(pubentry, ConvertFromC(defaultValue, defaultValueLen));
NT_Bool NT_SetDefaultString(NT_Handle pubentry, const struct WPI_String* defaultValue) {
return nt::SetDefaultString(pubentry, ConvertFromC(defaultValue));
}
char* NT_GetString(NT_Handle subentry, const char* defaultValue, size_t defaultValueLen, size_t* len) {
auto cppValue = nt::GetString(subentry, ConvertFromC(defaultValue, defaultValueLen));
return ConvertToC<char>(cppValue, len);
void NT_GetString(NT_Handle subentry, const struct WPI_String* defaultValue, struct WPI_String* value) {
auto cppValue = nt::GetString(subentry, ConvertFromC(defaultValue));
ConvertToC(cppValue, value);
}
void NT_GetAtomicString(NT_Handle subentry, const char* defaultValue, size_t defaultValueLen, struct NT_TimestampedString* value) {
auto cppValue = nt::GetAtomicString(subentry, ConvertFromC(defaultValue, defaultValueLen));
void NT_GetAtomicString(NT_Handle subentry, const struct WPI_String* defaultValue, struct NT_TimestampedString* value) {
auto cppValue = nt::GetAtomicString(subentry, ConvertFromC(defaultValue));
ConvertToC(cppValue, value);
}
void NT_DisposeTimestampedString(struct NT_TimestampedString* value) {
std::free(value->value);
WPI_FreeString(&value->value);
}
struct NT_TimestampedString* NT_ReadQueueString(NT_Handle subentry, size_t* len) {
@@ -457,26 +457,26 @@ void NT_FreeQueueDoubleArray(struct NT_TimestampedDoubleArray* arr, size_t len)
}
NT_Bool NT_SetStringArray(NT_Handle pubentry, int64_t time, const struct NT_String* value, size_t len) {
NT_Bool NT_SetStringArray(NT_Handle pubentry, int64_t time, const struct WPI_String* value, size_t len) {
return nt::SetStringArray(pubentry, ConvertFromC(value, len), time);
}
NT_Bool NT_SetDefaultStringArray(NT_Handle pubentry, const struct NT_String* defaultValue, size_t defaultValueLen) {
NT_Bool NT_SetDefaultStringArray(NT_Handle pubentry, const struct WPI_String* defaultValue, size_t defaultValueLen) {
return nt::SetDefaultStringArray(pubentry, ConvertFromC(defaultValue, defaultValueLen));
}
struct NT_String* NT_GetStringArray(NT_Handle subentry, const struct NT_String* defaultValue, size_t defaultValueLen, size_t* len) {
struct WPI_String* NT_GetStringArray(NT_Handle subentry, const struct WPI_String* defaultValue, size_t defaultValueLen, size_t* len) {
auto cppValue = nt::GetStringArray(subentry, ConvertFromC(defaultValue, defaultValueLen));
return ConvertToC<struct NT_String>(cppValue, len);
return ConvertToC<struct WPI_String>(cppValue, len);
}
void NT_GetAtomicStringArray(NT_Handle subentry, const struct NT_String* defaultValue, size_t defaultValueLen, struct NT_TimestampedStringArray* value) {
void NT_GetAtomicStringArray(NT_Handle subentry, const struct WPI_String* defaultValue, size_t defaultValueLen, struct NT_TimestampedStringArray* value) {
auto cppValue = nt::GetAtomicStringArray(subentry, ConvertFromC(defaultValue, defaultValueLen));
ConvertToC(cppValue, value);
}
void NT_DisposeTimestampedStringArray(struct NT_TimestampedStringArray* value) {
NT_FreeStringArray(value->value, value->len);
WPI_FreeStringArray(value->value, value->len);
}
struct NT_TimestampedStringArray* NT_ReadQueueStringArray(NT_Handle subentry, size_t* len) {

View File

@@ -473,12 +473,7 @@ struct NT_TimestampedString {
/**
* Value.
*/
char* value;
/**
* Value length.
*/
size_t len;
struct WPI_String value;
};
/**
@@ -493,10 +488,8 @@ struct NT_TimestampedString {
* @param pubentry publisher or entry handle
* @param time timestamp; 0 indicates current NT time should be used
* @param value value to publish
* @param len length of value
*/
NT_Bool NT_SetString(NT_Handle pubentry, int64_t time, const char* value, size_t len);
NT_Bool NT_SetString(NT_Handle pubentry, int64_t time, const struct WPI_String* value);
/**
* Publish a default value.
@@ -505,10 +498,8 @@ NT_Bool NT_SetString(NT_Handle pubentry, int64_t time, const char* value, size_t
*
* @param pubentry publisher or entry handle
* @param defaultValue default value
* @param defaultValueLen length of default value
*/
NT_Bool NT_SetDefaultString(NT_Handle pubentry, const char* defaultValue, size_t defaultValueLen);
NT_Bool NT_SetDefaultString(NT_Handle pubentry, const struct WPI_String* defaultValue);
/**
* Get the last published value.
@@ -516,12 +507,10 @@ NT_Bool NT_SetDefaultString(NT_Handle pubentry, const char* defaultValue, size_t
*
* @param subentry subscriber or entry handle
* @param defaultValue default value to return if no value has been published
* @param defaultValueLen length of default value
* @param len length of returned value (output)
* @param value returned value (output)
* @return value
*/
char* NT_GetString(NT_Handle subentry, const char* defaultValue, size_t defaultValueLen, size_t* len);
void NT_GetString(NT_Handle subentry, const struct WPI_String* defaultValue, struct WPI_String* value);
/**
* Get the last published value along with its timestamp.
@@ -530,11 +519,9 @@ char* NT_GetString(NT_Handle subentry, const char* defaultValue, size_t defaultV
*
* @param subentry subscriber or entry handle
* @param defaultValue default value to return if no value has been published
* @param defaultValueLen length of default value
* @param value timestamped value (output)
*/
void NT_GetAtomicString(NT_Handle subentry, const char* defaultValue, size_t defaultValueLen, struct NT_TimestampedString* value);
void NT_GetAtomicString(NT_Handle subentry, const struct WPI_String* defaultValue, struct NT_TimestampedString* value);
/**
* Disposes a timestamped value (as returned by NT_GetAtomicString).
@@ -1145,7 +1132,7 @@ struct NT_TimestampedStringArray {
/**
* Value.
*/
struct NT_String* value;
struct WPI_String* value;
/**
* Value length.
*/
@@ -1168,7 +1155,7 @@ struct NT_TimestampedStringArray {
* @param len length of value
*/
NT_Bool NT_SetStringArray(NT_Handle pubentry, int64_t time, const struct NT_String* value, size_t len);
NT_Bool NT_SetStringArray(NT_Handle pubentry, int64_t time, const struct WPI_String* value, size_t len);
/**
* Publish a default value.
@@ -1180,7 +1167,7 @@ NT_Bool NT_SetStringArray(NT_Handle pubentry, int64_t time, const struct NT_Stri
* @param defaultValueLen length of default value
*/
NT_Bool NT_SetDefaultStringArray(NT_Handle pubentry, const struct NT_String* defaultValue, size_t defaultValueLen);
NT_Bool NT_SetDefaultStringArray(NT_Handle pubentry, const struct WPI_String* defaultValue, size_t defaultValueLen);
/**
* Get the last published value.
@@ -1193,7 +1180,7 @@ NT_Bool NT_SetDefaultStringArray(NT_Handle pubentry, const struct NT_String* def
* @return value
*/
struct NT_String* NT_GetStringArray(NT_Handle subentry, const struct NT_String* defaultValue, size_t defaultValueLen, size_t* len);
struct WPI_String* NT_GetStringArray(NT_Handle subentry, const struct WPI_String* defaultValue, size_t defaultValueLen, size_t* len);
/**
* Get the last published value along with its timestamp.
@@ -1206,7 +1193,7 @@ struct NT_String* NT_GetStringArray(NT_Handle subentry, const struct NT_String*
* @param value timestamped value (output)
*/
void NT_GetAtomicStringArray(NT_Handle subentry, const struct NT_String* defaultValue, size_t defaultValueLen, struct NT_TimestampedStringArray* value);
void NT_GetAtomicStringArray(NT_Handle subentry, const struct WPI_String* defaultValue, size_t defaultValueLen, struct NT_TimestampedStringArray* value);
/**
* Disposes a timestamped value (as returned by NT_GetAtomicStringArray).