Skip to content

Commit

Permalink
[ntcore] Pass pub/sub options as a unified PubSubOptions struct (#4794)
Browse files Browse the repository at this point in the history
In Java, PubSubOption is still used for passing options, but this
simplifies C++ use substantially, as it allows aggregate construction.
  • Loading branch information
PeterJohnson authored Dec 13, 2022
1 parent f66a667 commit a865f48
Show file tree
Hide file tree
Showing 53 changed files with 696 additions and 624 deletions.
4 changes: 2 additions & 2 deletions glass/src/libnt/native/cpp/NTDigitalInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ NTDigitalInputModel::NTDigitalInputModel(std::string_view path)
NTDigitalInputModel::NTDigitalInputModel(nt::NetworkTableInstance inst,
std::string_view path)
: m_inst{inst},
m_value{inst.GetBooleanTopic(fmt::format("{}/Value", path))
.Subscribe(false, {{nt::PubSubOption::SendAll(true)}})},
m_value{
inst.GetBooleanTopic(fmt::format("{}/Value", path)).Subscribe(false)},
m_name{inst.GetStringTopic(fmt::format("{}/.name", path)).Subscribe("")},
m_valueData{fmt::format("NT_DIn:{}", path)},
m_nameValue{wpi::rsplit(path, '/').second} {
Expand Down
4 changes: 2 additions & 2 deletions glass/src/libnt/native/cpp/NTDigitalOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ NTDigitalOutputModel::NTDigitalOutputModel(std::string_view path)
NTDigitalOutputModel::NTDigitalOutputModel(nt::NetworkTableInstance inst,
std::string_view path)
: m_inst{inst},
m_value{inst.GetBooleanTopic(fmt::format("{}/Value", path))
.GetEntry(false, {{nt::PubSubOption::SendAll(true)}})},
m_value{
inst.GetBooleanTopic(fmt::format("{}/Value", path)).GetEntry(false)},
m_name{inst.GetStringTopic(fmt::format("{}/.name", path)).Subscribe("")},
m_controllable{inst.GetBooleanTopic(fmt::format("{}/.controllable", path))
.Subscribe(false)},
Expand Down
6 changes: 3 additions & 3 deletions glass/src/libnt/native/cpp/NTFMS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ NTFMSModel::NTFMSModel(nt::NetworkTableInstance inst, std::string_view path)
inst.GetStringTopic(fmt::format("{}/GameSpecificMessage", path))
.Subscribe("")},
m_alliance{inst.GetBooleanTopic(fmt::format("{}/IsRedAlliance", path))
.Subscribe(false, {{nt::PubSubOption::SendAll(true)}})},
.Subscribe(false)},
m_station{inst.GetIntegerTopic(fmt::format("{}/StationNumber", path))
.Subscribe(0, {{nt::PubSubOption::SendAll(true)}})},
.Subscribe(0)},
m_controlWord{inst.GetIntegerTopic(fmt::format("{}/FMSControlData", path))
.Subscribe(0, {{nt::PubSubOption::SendAll(true)}})},
.Subscribe(0)},
m_fmsAttached{fmt::format("NT_FMS:FMSAttached:{}", path)},
m_dsAttached{fmt::format("NT_FMS:DSAttached:{}", path)},
m_allianceStationId{fmt::format("NT_FMS:AllianceStationID:{}", path)},
Expand Down
5 changes: 1 addition & 4 deletions glass/src/libnt/native/cpp/NTField2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ NTField2DModel::NTField2DModel(nt::NetworkTableInstance inst,
std::string_view path)
: m_path{fmt::format("{}/", path)},
m_inst{inst},
m_tableSub{inst,
{{m_path}},
{{nt::PubSubOption::SendAll(true),
nt::PubSubOption::Periodic(0.05)}}},
m_tableSub{inst, {{m_path}}, {.periodic = 0.05, .sendAll = true}},
m_nameTopic{inst.GetTopic(fmt::format("{}/.name", path))},
m_poller{inst} {
m_poller.AddListener(m_tableSub, nt::EventFlags::kTopic |
Expand Down
3 changes: 1 addition & 2 deletions glass/src/libnt/native/cpp/NTGyro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ NTGyroModel::NTGyroModel(std::string_view path)

NTGyroModel::NTGyroModel(nt::NetworkTableInstance inst, std::string_view path)
: m_inst{inst},
m_angle{inst.GetDoubleTopic(fmt::format("{}/Value", path))
.Subscribe(0, {{nt::PubSubOption::SendAll(true)}})},
m_angle{inst.GetDoubleTopic(fmt::format("{}/Value", path)).Subscribe(0)},
m_name{inst.GetStringTopic(fmt::format("{}/.name", path)).Subscribe({})},
m_angleData{fmt::format("NT_Gyro:{}", path)},
m_nameValue{wpi::rsplit(path, '/').second} {}
Expand Down
8 changes: 4 additions & 4 deletions glass/src/libnt/native/cpp/NTMecanumDrive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ NTMecanumDriveModel::NTMecanumDriveModel(nt::NetworkTableInstance inst,
.Subscribe(0)},
m_flPercent{
inst.GetDoubleTopic(fmt::format("{}/Front Left Motor Speed", path))
.GetEntry(0, {{nt::PubSubOption::SendAll(true)}})},
.GetEntry(0)},
m_frPercent{
inst.GetDoubleTopic(fmt::format("{}/Front Right Motor Speed", path))
.GetEntry(0, {{nt::PubSubOption::SendAll(true)}})},
.GetEntry(0)},
m_rlPercent{
inst.GetDoubleTopic(fmt::format("{}/Rear Left Motor Speed", path))
.GetEntry(0, {{nt::PubSubOption::SendAll(true)}})},
.GetEntry(0)},
m_rrPercent{
inst.GetDoubleTopic(fmt::format("{}/Rear Right Motor Speed", path))
.GetEntry(0, {{nt::PubSubOption::SendAll(true)}})},
.GetEntry(0)},
m_nameValue{wpi::rsplit(path, '/').second},
m_flPercentData{fmt::format("NTMcnmDriveFL:{}", path)},
m_frPercentData{fmt::format("NTMcnmDriveFR:{}", path)},
Expand Down
5 changes: 1 addition & 4 deletions glass/src/libnt/native/cpp/NTMechanism2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,7 @@ NTMechanism2DModel::NTMechanism2DModel(nt::NetworkTableInstance inst,
std::string_view path)
: m_inst{inst},
m_path{fmt::format("{}/", path)},
m_tableSub{inst,
{{m_path}},
{{nt::PubSubOption::SendAll(true),
nt::PubSubOption::Periodic(0.05)}}},
m_tableSub{inst, {{m_path}}, {.periodic = 0.05, .sendAll = true}},
m_nameTopic{m_inst.GetTopic(fmt::format("{}/.name", path))},
m_dimensionsTopic{m_inst.GetTopic(fmt::format("{}/dims", path))},
m_bgColorTopic{m_inst.GetTopic(fmt::format("{}/backgroundColor", path))},
Expand Down
3 changes: 1 addition & 2 deletions glass/src/libnt/native/cpp/NTMotorController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ NTMotorControllerModel::NTMotorControllerModel(std::string_view path)
NTMotorControllerModel::NTMotorControllerModel(nt::NetworkTableInstance inst,
std::string_view path)
: m_inst{inst},
m_value{inst.GetDoubleTopic(fmt::format("{}/Value", path))
.GetEntry(0, {{nt::PubSubOption::SendAll(true)}})},
m_value{inst.GetDoubleTopic(fmt::format("{}/Value", path)).GetEntry(0)},
m_name{inst.GetStringTopic(fmt::format("{}/.name", path)).Subscribe("")},
m_controllable{inst.GetBooleanTopic(fmt::format("{}/.controllable", path))
.Subscribe(false)},
Expand Down
13 changes: 5 additions & 8 deletions glass/src/libnt/native/cpp/NTPIDController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@ NTPIDControllerModel::NTPIDControllerModel(nt::NetworkTableInstance inst,
m_name{inst.GetStringTopic(fmt::format("{}/.name", path)).Subscribe("")},
m_controllable{inst.GetBooleanTopic(fmt::format("{}/.controllable", path))
.Subscribe(false)},
m_p{inst.GetDoubleTopic(fmt::format("{}/p", path))
.GetEntry(0, {{nt::PubSubOption::SendAll(true)}})},
m_i{inst.GetDoubleTopic(fmt::format("{}/i", path))
.GetEntry(0, {{nt::PubSubOption::SendAll(true)}})},
m_d{inst.GetDoubleTopic(fmt::format("{}/d", path))
.GetEntry(0, {{nt::PubSubOption::SendAll(true)}})},
m_setpoint{inst.GetDoubleTopic(fmt::format("{}/setpoint", path))
.GetEntry(0, {{nt::PubSubOption::SendAll(true)}})},
m_p{inst.GetDoubleTopic(fmt::format("{}/p", path)).GetEntry(0)},
m_i{inst.GetDoubleTopic(fmt::format("{}/i", path)).GetEntry(0)},
m_d{inst.GetDoubleTopic(fmt::format("{}/d", path)).GetEntry(0)},
m_setpoint{
inst.GetDoubleTopic(fmt::format("{}/setpoint", path)).GetEntry(0)},
m_pData{fmt::format("NTPIDCtrlP:{}", path)},
m_iData{fmt::format("NTPIDCtrlI:{}", path)},
m_dData{fmt::format("NTPIDCtrlD:{}", path)},
Expand Down
3 changes: 1 addition & 2 deletions ntcore/src/dev/native/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ void bench() {
// add "typical" set of subscribers on client and server
nt::SubscribeMultiple(client, {{std::string_view{}}});
nt::Subscribe(nt::GetTopic(client, "highrate"), NT_DOUBLE, "double",
{{nt::PubSubOption::KeepDuplicates(true),
nt::PubSubOption::SendAll(true)}});
{.sendAll = true, .keepDuplicates = true});
nt::SubscribeMultiple(server, {{std::string_view{}}});
auto pub = nt::Publish(nt::GetTopic(server, "highrate"), NT_DOUBLE, "double");
nt::SetDouble(pub, 0);
Expand Down
12 changes: 6 additions & 6 deletions ntcore/src/generate/include/networktables/Topic.h.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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 %}
};

Expand Down
12 changes: 6 additions & 6 deletions ntcore/src/generate/include/networktables/Topic.inc.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -89,44 +89,44 @@ 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};
}
{%- 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};
}
{%- 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};
Expand Down
64 changes: 31 additions & 33 deletions ntcore/src/generate/java/NetworkTablesJNI.java.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Loading

0 comments on commit a865f48

Please sign in to comment.