mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-06 03:31:43 +00:00
[ntcore] Pass pub/sub options as a unified PubSubOptions struct (#4794)
In Java, PubSubOption is still used for passing options, but this simplifies C++ use substantially, as it allows aggregate construction.
This commit is contained in:
@@ -311,7 +311,7 @@ class {{ TypeName }}Topic final : public Topic {
|
||||
[[nodiscard]]
|
||||
SubscriberType Subscribe(
|
||||
{% if not TypeString %}std::string_view typeString, {% endif %}ParamType defaultValue,
|
||||
std::span<const PubSubOption> options = {});
|
||||
const PubSubOptions& options = kDefaultPubSubOptions);
|
||||
{%- if TypeString %}
|
||||
/**
|
||||
* Create a new subscriber to the topic, with specific type string.
|
||||
@@ -332,7 +332,7 @@ class {{ TypeName }}Topic final : public Topic {
|
||||
[[nodiscard]]
|
||||
SubscriberType SubscribeEx(
|
||||
std::string_view typeString, ParamType defaultValue,
|
||||
std::span<const PubSubOption> options = {});
|
||||
const PubSubOptions& options = kDefaultPubSubOptions);
|
||||
{% endif %}
|
||||
/**
|
||||
* Create a new publisher to the topic.
|
||||
@@ -353,7 +353,7 @@ class {{ TypeName }}Topic final : public Topic {
|
||||
* @return publisher
|
||||
*/
|
||||
[[nodiscard]]
|
||||
PublisherType Publish({% if not TypeString %}std::string_view typeString, {% endif %}std::span<const PubSubOption> options = {});
|
||||
PublisherType Publish({% if not TypeString %}std::string_view typeString, {% endif %}const PubSubOptions& options = kDefaultPubSubOptions);
|
||||
|
||||
/**
|
||||
* Create a new publisher to the topic, with type string and initial
|
||||
@@ -375,7 +375,7 @@ class {{ TypeName }}Topic final : public Topic {
|
||||
*/
|
||||
[[nodiscard]]
|
||||
PublisherType PublishEx(std::string_view typeString,
|
||||
const wpi::json& properties, std::span<const PubSubOption> options = {});
|
||||
const wpi::json& properties, const PubSubOptions& options = kDefaultPubSubOptions);
|
||||
|
||||
/**
|
||||
* Create a new entry for the topic.
|
||||
@@ -402,7 +402,7 @@ class {{ TypeName }}Topic final : public Topic {
|
||||
*/
|
||||
[[nodiscard]]
|
||||
EntryType GetEntry({% if not TypeString %}std::string_view typeString, {% endif %}ParamType defaultValue,
|
||||
std::span<const PubSubOption> options = {});
|
||||
const PubSubOptions& options = kDefaultPubSubOptions);
|
||||
{%- if TypeString %}
|
||||
/**
|
||||
* Create a new entry for the topic, with specific type string.
|
||||
@@ -427,7 +427,7 @@ class {{ TypeName }}Topic final : public Topic {
|
||||
*/
|
||||
[[nodiscard]]
|
||||
EntryType GetEntryEx(std::string_view typeString, ParamType defaultValue,
|
||||
std::span<const PubSubOption> options = {});
|
||||
const PubSubOptions& options = kDefaultPubSubOptions);
|
||||
{% endif %}
|
||||
};
|
||||
|
||||
|
||||
@@ -89,7 +89,7 @@ inline void {{ TypeName }}Entry::Unpublish() {
|
||||
|
||||
inline {{ TypeName }}Subscriber {{ TypeName }}Topic::Subscribe(
|
||||
{% if not TypeString %}std::string_view typeString, {% endif %}{{ cpp.ParamType }} defaultValue,
|
||||
std::span<const PubSubOption> options) {
|
||||
const PubSubOptions& options) {
|
||||
return {{ TypeName }}Subscriber{
|
||||
::nt::Subscribe(m_handle, NT_{{ cpp.TYPE_NAME }}, {{ TypeString|default('typeString') }}, options),
|
||||
defaultValue};
|
||||
@@ -97,28 +97,28 @@ inline {{ TypeName }}Subscriber {{ TypeName }}Topic::Subscribe(
|
||||
{%- if TypeString %}
|
||||
inline {{ TypeName }}Subscriber {{ TypeName }}Topic::SubscribeEx(
|
||||
std::string_view typeString, {{ cpp.ParamType }} defaultValue,
|
||||
std::span<const PubSubOption> options) {
|
||||
const PubSubOptions& options) {
|
||||
return {{ TypeName }}Subscriber{
|
||||
::nt::Subscribe(m_handle, NT_{{ cpp.TYPE_NAME }}, typeString, options),
|
||||
defaultValue};
|
||||
}
|
||||
{% endif %}
|
||||
inline {{ TypeName }}Publisher {{ TypeName }}Topic::Publish(
|
||||
{% if not TypeString %}std::string_view typeString, {% endif %}std::span<const PubSubOption> options) {
|
||||
{% if not TypeString %}std::string_view typeString, {% endif %}const PubSubOptions& options) {
|
||||
return {{ TypeName }}Publisher{
|
||||
::nt::Publish(m_handle, NT_{{ cpp.TYPE_NAME }}, {{ TypeString|default('typeString') }}, options)};
|
||||
}
|
||||
|
||||
inline {{ TypeName }}Publisher {{ TypeName }}Topic::PublishEx(
|
||||
std::string_view typeString,
|
||||
const wpi::json& properties, std::span<const PubSubOption> options) {
|
||||
const wpi::json& properties, const PubSubOptions& options) {
|
||||
return {{ TypeName }}Publisher{
|
||||
::nt::PublishEx(m_handle, NT_{{ cpp.TYPE_NAME }}, typeString, properties, options)};
|
||||
}
|
||||
|
||||
inline {{ TypeName }}Entry {{ TypeName }}Topic::GetEntry(
|
||||
{% if not TypeString %}std::string_view typeString, {% endif %}{{ cpp.ParamType }} defaultValue,
|
||||
std::span<const PubSubOption> options) {
|
||||
const PubSubOptions& options) {
|
||||
return {{ TypeName }}Entry{
|
||||
::nt::GetEntry(m_handle, NT_{{ cpp.TYPE_NAME }}, {{ TypeString|default('typeString') }}, options),
|
||||
defaultValue};
|
||||
@@ -126,7 +126,7 @@ inline {{ TypeName }}Entry {{ TypeName }}Topic::GetEntry(
|
||||
{%- if TypeString %}
|
||||
inline {{ TypeName }}Entry {{ TypeName }}Topic::GetEntryEx(
|
||||
std::string_view typeString, {{ cpp.ParamType }} defaultValue,
|
||||
std::span<const PubSubOption> options) {
|
||||
const PubSubOptions& options) {
|
||||
return {{ TypeName }}Entry{
|
||||
::nt::GetEntry(m_handle, NT_{{ cpp.TYPE_NAME }}, typeString, options),
|
||||
defaultValue};
|
||||
|
||||
@@ -57,20 +57,11 @@ public final class NetworkTablesJNI {
|
||||
libraryLoaded = true;
|
||||
}
|
||||
|
||||
private static int[] pubSubOptionTypes(PubSubOption... options) {
|
||||
int[] rv = new int[options.length];
|
||||
for (int i = 0; i < options.length; i++) {
|
||||
rv[i] = options[i].m_type;
|
||||
private static PubSubOptions buildOptions(PubSubOption... options) {
|
||||
if (options.length == 0) {
|
||||
return null; // optimize common case (JNI checks for null)
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
private static int[] pubSubOptionValues(PubSubOption... options) {
|
||||
int[] rv = new int[options.length];
|
||||
for (int i = 0; i < options.length; i++) {
|
||||
rv[i] = options[i].m_value;
|
||||
}
|
||||
return rv;
|
||||
return new PubSubOptions(options);
|
||||
}
|
||||
|
||||
public static native int getDefaultInstance();
|
||||
@@ -81,13 +72,19 @@ public final class NetworkTablesJNI {
|
||||
|
||||
public static native int getInstanceFromHandle(int handle);
|
||||
|
||||
private static native int getEntryImpl(
|
||||
int topic, int type, String typeStr, PubSubOptions options);
|
||||
|
||||
public static native int getEntry(int inst, String key);
|
||||
|
||||
private static native int getEntry(
|
||||
int topic, int type, String typeStr, int[] optionTypes, int[] optionValues);
|
||||
public static int getEntry(
|
||||
int topic, int type, String typeStr, PubSubOptions options) {
|
||||
return getEntryImpl(topic, type, typeStr, options);
|
||||
}
|
||||
|
||||
public static int getEntry(int topic, int type, String typeStr, PubSubOption... options) {
|
||||
return getEntry(topic, type, typeStr, pubSubOptionTypes(options), pubSubOptionValues(options));
|
||||
public static int getEntry(
|
||||
int topic, int type, String typeStr, PubSubOption... options) {
|
||||
return getEntryImpl(topic, type, typeStr, buildOptions(options));
|
||||
}
|
||||
|
||||
public static native String getEntryName(int entry);
|
||||
@@ -136,27 +133,30 @@ public final class NetworkTablesJNI {
|
||||
|
||||
public static native void setTopicProperties(int topic, String properties);
|
||||
|
||||
private static native int subscribe(
|
||||
int topic, int type, String typeStr, int[] optionTypes, int[] optionValues);
|
||||
public static native int subscribe(
|
||||
int topic, int type, String typeStr, PubSubOptions options);
|
||||
|
||||
public static int subscribe(int topic, int type, String typeStr, PubSubOption... options) {
|
||||
return subscribe(topic, type, typeStr, pubSubOptionTypes(options), pubSubOptionValues(options));
|
||||
public static int subscribe(
|
||||
int topic, int type, String typeStr, PubSubOption... options) {
|
||||
return subscribe(topic, type, typeStr, buildOptions(options));
|
||||
}
|
||||
|
||||
public static native void unsubscribe(int sub);
|
||||
|
||||
private static native int publish(
|
||||
int topic, int type, String typeStr, int[] optionTypes, int[] optionValues);
|
||||
public static native int publish(
|
||||
int topic, int type, String typeStr, PubSubOptions options);
|
||||
|
||||
public static int publish(int topic, int type, String typeStr, PubSubOption... options) {
|
||||
return publish(topic, type, typeStr, pubSubOptionTypes(options), pubSubOptionValues(options));
|
||||
public static int publish(
|
||||
int topic, int type, String typeStr, PubSubOption... options) {
|
||||
return publish(topic, type, typeStr, buildOptions(options));
|
||||
}
|
||||
|
||||
private static native int publishEx(
|
||||
int topic, int type, String typeStr, String properties, int[] optionTypes, int[] optionValues);
|
||||
public static native int publishEx(
|
||||
int topic, int type, String typeStr, String properties, PubSubOptions options);
|
||||
|
||||
public static int publishEx(int topic, int type, String typeStr, String properties, PubSubOption... options) {
|
||||
return publishEx(topic, type, typeStr, properties, pubSubOptionTypes(options), pubSubOptionValues(options));
|
||||
public static int publishEx(
|
||||
int topic, int type, String typeStr, String properties, PubSubOption... options) {
|
||||
return publishEx(topic, type, typeStr, properties, buildOptions(options));
|
||||
}
|
||||
|
||||
public static native void unpublish(int pubentry);
|
||||
@@ -167,12 +167,10 @@ public final class NetworkTablesJNI {
|
||||
|
||||
public static native int getTopicFromHandle(int pubsubentry);
|
||||
|
||||
private static native int subscribeMultiple(
|
||||
int inst, String[] prefixes, int[] optionTypes, int[] optionValues);
|
||||
public static native int subscribeMultiple(int inst, String[] prefixes, PubSubOptions options);
|
||||
|
||||
public static int subscribeMultiple(int inst, String[] prefixes, PubSubOption... options) {
|
||||
return subscribeMultiple(
|
||||
inst, prefixes, pubSubOptionTypes(options), pubSubOptionValues(options));
|
||||
return subscribeMultiple(inst, prefixes, buildOptions(options));
|
||||
}
|
||||
|
||||
public static native void unsubscribeMultiple(int sub);
|
||||
|
||||
Reference in New Issue
Block a user