From 3eb372c25ad6079d6edfbdb4bb099a7bc00e4350 Mon Sep 17 00:00:00 2001 From: Starlight220 <53231611+Starlight220@users.noreply.github.com> Date: Mon, 2 Oct 2023 18:23:11 +0300 Subject: [PATCH] [wpiutil] SendableBuilder: Add PublishConst methods (#5158) --- .../smartdashboard/SendableBuilderImpl.cpp | 67 ++++++++++++ .../frc/smartdashboard/SendableBuilderImpl.h | 32 ++++++ .../smartdashboard/SendableBuilderImpl.java | 100 ++++++++++++++++++ .../first/util/sendable/SendableBuilder.java | 89 ++++++++++++++++ .../include/wpi/sendable/SendableBuilder.h | 97 +++++++++++++++++ 5 files changed, 385 insertions(+) diff --git a/wpilibc/src/main/native/cpp/smartdashboard/SendableBuilderImpl.cpp b/wpilibc/src/main/native/cpp/smartdashboard/SendableBuilderImpl.cpp index 79b0e6e896..c063850411 100644 --- a/wpilibc/src/main/native/cpp/smartdashboard/SendableBuilderImpl.cpp +++ b/wpilibc/src/main/native/cpp/smartdashboard/SendableBuilderImpl.cpp @@ -141,6 +141,14 @@ void SendableBuilderImpl::AddPropertyImpl(Topic topic, Getter getter, m_properties.emplace_back(std::move(prop)); } +template +void SendableBuilderImpl::PublishConstImpl(Topic topic, Value value) { + auto prop = std::make_unique>(); + prop->pub = topic.Publish(); + prop->pub.Set(value); + m_properties.emplace_back(std::move(prop)); +} + void SendableBuilderImpl::AddBooleanProperty(std::string_view key, std::function getter, std::function setter) { @@ -148,6 +156,11 @@ void SendableBuilderImpl::AddBooleanProperty(std::string_view key, std::move(setter)); } +void SendableBuilderImpl::PublishConstBoolean(std::string_view key, + bool value) { + PublishConstImpl(m_table->GetBooleanTopic(key), value); +} + void SendableBuilderImpl::AddIntegerProperty( std::string_view key, std::function getter, std::function setter) { @@ -155,6 +168,11 @@ void SendableBuilderImpl::AddIntegerProperty( std::move(setter)); } +void SendableBuilderImpl::PublishConstInteger(std::string_view key, + int64_t value) { + PublishConstImpl(m_table->GetIntegerTopic(key), value); +} + void SendableBuilderImpl::AddFloatProperty(std::string_view key, std::function getter, std::function setter) { @@ -162,6 +180,10 @@ void SendableBuilderImpl::AddFloatProperty(std::string_view key, std::move(setter)); } +void SendableBuilderImpl::PublishConstFloat(std::string_view key, float value) { + PublishConstImpl(m_table->GetFloatTopic(key), value); +} + void SendableBuilderImpl::AddDoubleProperty( std::string_view key, std::function getter, std::function setter) { @@ -169,6 +191,11 @@ void SendableBuilderImpl::AddDoubleProperty( std::move(setter)); } +void SendableBuilderImpl::PublishConstDouble(std::string_view key, + double value) { + PublishConstImpl(m_table->GetDoubleTopic(key), value); +} + void SendableBuilderImpl::AddStringProperty( std::string_view key, std::function getter, std::function setter) { @@ -176,6 +203,11 @@ void SendableBuilderImpl::AddStringProperty( std::move(setter)); } +void SendableBuilderImpl::PublishConstString(std::string_view key, + std::string_view value) { + PublishConstImpl(m_table->GetStringTopic(key), value); +} + void SendableBuilderImpl::AddBooleanArrayProperty( std::string_view key, std::function()> getter, std::function)> setter) { @@ -183,6 +215,11 @@ void SendableBuilderImpl::AddBooleanArrayProperty( std::move(setter)); } +void SendableBuilderImpl::PublishConstBooleanArray(std::string_view key, + std::span value) { + PublishConstImpl(m_table->GetBooleanArrayTopic(key), value); +} + void SendableBuilderImpl::AddIntegerArrayProperty( std::string_view key, std::function()> getter, std::function)> setter) { @@ -190,6 +227,11 @@ void SendableBuilderImpl::AddIntegerArrayProperty( std::move(setter)); } +void SendableBuilderImpl::PublishConstIntegerArray( + std::string_view key, std::span value) { + PublishConstImpl(m_table->GetIntegerArrayTopic(key), value); +} + void SendableBuilderImpl::AddFloatArrayProperty( std::string_view key, std::function()> getter, std::function)> setter) { @@ -197,6 +239,11 @@ void SendableBuilderImpl::AddFloatArrayProperty( std::move(setter)); } +void SendableBuilderImpl::PublishConstFloatArray(std::string_view key, + std::span value) { + PublishConstImpl(m_table->GetFloatArrayTopic(key), value); +} + void SendableBuilderImpl::AddDoubleArrayProperty( std::string_view key, std::function()> getter, std::function)> setter) { @@ -204,6 +251,11 @@ void SendableBuilderImpl::AddDoubleArrayProperty( std::move(setter)); } +void SendableBuilderImpl::PublishConstDoubleArray( + std::string_view key, std::span value) { + PublishConstImpl(m_table->GetDoubleArrayTopic(key), value); +} + void SendableBuilderImpl::AddStringArrayProperty( std::string_view key, std::function()> getter, std::function)> setter) { @@ -211,6 +263,11 @@ void SendableBuilderImpl::AddStringArrayProperty( std::move(setter)); } +void SendableBuilderImpl::PublishConstStringArray( + std::string_view key, std::span value) { + PublishConstImpl(m_table->GetStringArrayTopic(key), value); +} + void SendableBuilderImpl::AddRawProperty( std::string_view key, std::string_view typeString, std::function()> getter, @@ -235,6 +292,16 @@ void SendableBuilderImpl::AddRawProperty( m_properties.emplace_back(std::move(prop)); } +void SendableBuilderImpl::PublishConstRaw(std::string_view key, + std::string_view typeString, + std::span value) { + auto topic = m_table->GetRawTopic(key); + auto prop = std::make_unique>(); + prop->pub = topic.Publish(typeString); + prop->pub.Set(value); + m_properties.emplace_back(std::move(prop)); +} + template void SendableBuilderImpl::AddSmallPropertyImpl(Topic topic, Getter getter, diff --git a/wpilibc/src/main/native/include/frc/smartdashboard/SendableBuilderImpl.h b/wpilibc/src/main/native/include/frc/smartdashboard/SendableBuilderImpl.h index 4bd42896e2..baee6ffaa4 100644 --- a/wpilibc/src/main/native/include/frc/smartdashboard/SendableBuilderImpl.h +++ b/wpilibc/src/main/native/include/frc/smartdashboard/SendableBuilderImpl.h @@ -96,44 +96,73 @@ class SendableBuilderImpl : public nt::NTSendableBuilder { void AddBooleanProperty(std::string_view key, std::function getter, std::function setter) override; + void PublishConstBoolean(std::string_view key, bool value) override; + void AddIntegerProperty(std::string_view key, std::function getter, std::function setter) override; + void PublishConstInteger(std::string_view key, int64_t value) override; + void AddFloatProperty(std::string_view key, std::function getter, std::function setter) override; + void PublishConstFloat(std::string_view key, float value) override; + void AddDoubleProperty(std::string_view key, std::function getter, std::function setter) override; + void PublishConstDouble(std::string_view key, double value) override; + void AddStringProperty(std::string_view key, std::function getter, std::function setter) override; + void PublishConstString(std::string_view key, + std::string_view value) override; + void AddBooleanArrayProperty( std::string_view key, std::function()> getter, std::function)> setter) override; + void PublishConstBooleanArray(std::string_view key, + std::span value) override; + void AddIntegerArrayProperty( std::string_view key, std::function()> getter, std::function)> setter) override; + void PublishConstIntegerArray(std::string_view key, + std::span value) override; + void AddFloatArrayProperty( std::string_view key, std::function()> getter, std::function)> setter) override; + void PublishConstFloatArray(std::string_view key, + std::span value) override; + void AddDoubleArrayProperty( std::string_view key, std::function()> getter, std::function)> setter) override; + void PublishConstDoubleArray(std::string_view key, + std::span value) override; + void AddStringArrayProperty( std::string_view key, std::function()> getter, std::function)> setter) override; + void PublishConstStringArray(std::string_view key, + std::span value) override; + void AddRawProperty( std::string_view key, std::string_view typeString, std::function()> getter, std::function)> setter) override; + void PublishConstRaw(std::string_view key, std::string_view typeString, + std::span value) override; + void AddSmallStringProperty( std::string_view key, std::function& buf)> getter, @@ -198,6 +227,9 @@ class SendableBuilderImpl : public nt::NTSendableBuilder { template void AddPropertyImpl(Topic topic, Getter getter, Setter setter); + template + void PublishConstImpl(Topic topic, Value value); + template void AddSmallPropertyImpl(Topic topic, Getter getter, Setter setter); diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SendableBuilderImpl.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SendableBuilderImpl.java index 7bb266e658..2c409e8314 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SendableBuilderImpl.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SendableBuilderImpl.java @@ -57,6 +57,7 @@ import java.util.function.LongConsumer; import java.util.function.LongSupplier; import java.util.function.Supplier; +@SuppressWarnings("PMD.CompareObjectsWithEquals") public class SendableBuilderImpl implements NTSendableBuilder { @FunctionalInterface private interface TimedConsumer { @@ -328,6 +329,15 @@ public class SendableBuilderImpl implements NTSendableBuilder { m_properties.add(property); } + @Override + public void publishConstBoolean(String key, boolean value) { + Property property = new Property<>(); + BooleanTopic topic = m_table.getBooleanTopic(key); + property.m_pub = topic.publish(); + property.m_pub.set(value); + m_properties.add(property); + } + /** * Add an integer property. * @@ -355,6 +365,15 @@ public class SendableBuilderImpl implements NTSendableBuilder { m_properties.add(property); } + @Override + public void publishConstInteger(String key, long value) { + Property property = new Property<>(); + IntegerTopic topic = m_table.getIntegerTopic(key); + property.m_pub = topic.publish(); + property.m_pub.set(value); + m_properties.add(property); + } + /** * Add a float property. * @@ -382,6 +401,15 @@ public class SendableBuilderImpl implements NTSendableBuilder { m_properties.add(property); } + @Override + public void publishConstFloat(String key, float value) { + Property property = new Property<>(); + FloatTopic topic = m_table.getFloatTopic(key); + property.m_pub = topic.publish(); + property.m_pub.set(value); + m_properties.add(property); + } + /** * Add a double property. * @@ -409,6 +437,15 @@ public class SendableBuilderImpl implements NTSendableBuilder { m_properties.add(property); } + @Override + public void publishConstDouble(String key, double value) { + Property property = new Property<>(); + DoubleTopic topic = m_table.getDoubleTopic(key); + property.m_pub = topic.publish(); + property.m_pub.set(value); + m_properties.add(property); + } + /** * Add a string property. * @@ -436,6 +473,15 @@ public class SendableBuilderImpl implements NTSendableBuilder { m_properties.add(property); } + @Override + public void publishConstString(String key, String value) { + Property property = new Property<>(); + StringTopic topic = m_table.getStringTopic(key); + property.m_pub = topic.publish(); + property.m_pub.set(value); + m_properties.add(property); + } + /** * Add a boolean array property. * @@ -465,6 +511,15 @@ public class SendableBuilderImpl implements NTSendableBuilder { m_properties.add(property); } + @Override + public void publishConstBooleanArray(String key, boolean[] value) { + Property property = new Property<>(); + BooleanArrayTopic topic = m_table.getBooleanArrayTopic(key); + property.m_pub = topic.publish(); + property.m_pub.set(value); + m_properties.add(property); + } + /** * Add an integer array property. * @@ -494,6 +549,15 @@ public class SendableBuilderImpl implements NTSendableBuilder { m_properties.add(property); } + @Override + public void publishConstIntegerArray(String key, long[] value) { + Property property = new Property<>(); + IntegerArrayTopic topic = m_table.getIntegerArrayTopic(key); + property.m_pub = topic.publish(); + property.m_pub.set(value); + m_properties.add(property); + } + /** * Add a float array property. * @@ -523,6 +587,15 @@ public class SendableBuilderImpl implements NTSendableBuilder { m_properties.add(property); } + @Override + public void publishConstFloatArray(String key, float[] value) { + Property property = new Property<>(); + FloatArrayTopic topic = m_table.getFloatArrayTopic(key); + property.m_pub = topic.publish(); + property.m_pub.set(value); + m_properties.add(property); + } + /** * Add a double array property. * @@ -552,6 +625,15 @@ public class SendableBuilderImpl implements NTSendableBuilder { m_properties.add(property); } + @Override + public void publishConstDoubleArray(String key, double[] value) { + Property property = new Property<>(); + DoubleArrayTopic topic = m_table.getDoubleArrayTopic(key); + property.m_pub = topic.publish(); + property.m_pub.set(value); + m_properties.add(property); + } + /** * Add a string array property. * @@ -581,6 +663,15 @@ public class SendableBuilderImpl implements NTSendableBuilder { m_properties.add(property); } + @Override + public void publishConstStringArray(String key, String[] value) { + Property property = new Property<>(); + StringArrayTopic topic = m_table.getStringArrayTopic(key); + property.m_pub = topic.publish(); + property.m_pub.set(value); + m_properties.add(property); + } + /** * Add a raw property. * @@ -610,4 +701,13 @@ public class SendableBuilderImpl implements NTSendableBuilder { } m_properties.add(property); } + + @Override + public void publishConstRaw(String key, String typestring, byte[] value) { + Property property = new Property<>(); + RawTopic topic = m_table.getRawTopic(key); + property.m_pub = topic.publish(typestring); + property.m_pub.set(value); + m_properties.add(property); + } } diff --git a/wpiutil/src/main/java/edu/wpi/first/util/sendable/SendableBuilder.java b/wpiutil/src/main/java/edu/wpi/first/util/sendable/SendableBuilder.java index a58c3a30c2..db822ce441 100644 --- a/wpiutil/src/main/java/edu/wpi/first/util/sendable/SendableBuilder.java +++ b/wpiutil/src/main/java/edu/wpi/first/util/sendable/SendableBuilder.java @@ -55,6 +55,14 @@ public interface SendableBuilder extends AutoCloseable { */ void addBooleanProperty(String key, BooleanSupplier getter, BooleanConsumer setter); + /** + * Add a constant boolean property. + * + * @param key property name + * @param value the value + */ + void publishConstBoolean(String key, boolean value); + /** * Add an integer property. * @@ -64,6 +72,14 @@ public interface SendableBuilder extends AutoCloseable { */ void addIntegerProperty(String key, LongSupplier getter, LongConsumer setter); + /** + * Add a constant integer property. + * + * @param key property name + * @param value the value + */ + void publishConstInteger(String key, long value); + /** * Add a float property. * @@ -73,6 +89,14 @@ public interface SendableBuilder extends AutoCloseable { */ void addFloatProperty(String key, FloatSupplier getter, FloatConsumer setter); + /** + * Add a constant float property. + * + * @param key property name + * @param value the value + */ + void publishConstFloat(String key, float value); + /** * Add a double property. * @@ -82,6 +106,14 @@ public interface SendableBuilder extends AutoCloseable { */ void addDoubleProperty(String key, DoubleSupplier getter, DoubleConsumer setter); + /** + * Add a constant double property. + * + * @param key property name + * @param value the value + */ + void publishConstDouble(String key, double value); + /** * Add a string property. * @@ -91,6 +123,14 @@ public interface SendableBuilder extends AutoCloseable { */ void addStringProperty(String key, Supplier getter, Consumer setter); + /** + * Add a constant string property. + * + * @param key property name + * @param value the value + */ + void publishConstString(String key, String value); + /** * Add a boolean array property. * @@ -100,6 +140,14 @@ public interface SendableBuilder extends AutoCloseable { */ void addBooleanArrayProperty(String key, Supplier getter, Consumer setter); + /** + * Add a constant boolean array property. + * + * @param key property name + * @param value the value + */ + void publishConstBooleanArray(String key, boolean[] value); + /** * Add an integer array property. * @@ -109,6 +157,14 @@ public interface SendableBuilder extends AutoCloseable { */ void addIntegerArrayProperty(String key, Supplier getter, Consumer setter); + /** + * Add a constant integer property. + * + * @param key property name + * @param value the value + */ + void publishConstIntegerArray(String key, long[] value); + /** * Add a float array property. * @@ -118,6 +174,14 @@ public interface SendableBuilder extends AutoCloseable { */ void addFloatArrayProperty(String key, Supplier getter, Consumer setter); + /** + * Add a constant float array property. + * + * @param key property name + * @param value the value + */ + void publishConstFloatArray(String key, float[] value); + /** * Add a double array property. * @@ -127,6 +191,14 @@ public interface SendableBuilder extends AutoCloseable { */ void addDoubleArrayProperty(String key, Supplier getter, Consumer setter); + /** + * Add a constant double array property. + * + * @param key property name + * @param value the value + */ + void publishConstDoubleArray(String key, double[] value); + /** * Add a string array property. * @@ -136,6 +208,14 @@ public interface SendableBuilder extends AutoCloseable { */ void addStringArrayProperty(String key, Supplier getter, Consumer setter); + /** + * Add a constant string array property. + * + * @param key property name + * @param value the value + */ + void publishConstStringArray(String key, String[] value); + /** * Add a raw property. * @@ -147,6 +227,15 @@ public interface SendableBuilder extends AutoCloseable { void addRawProperty( String key, String typeString, Supplier getter, Consumer setter); + /** + * Add a constant raw property. + * + * @param key property name + * @param typeString type string + * @param value the value + */ + void publishConstRaw(String key, String typeString, byte[] value); + /** * Gets the kind of backend being used. * diff --git a/wpiutil/src/main/native/include/wpi/sendable/SendableBuilder.h b/wpiutil/src/main/native/include/wpi/sendable/SendableBuilder.h index 2287b731cc..4115420c79 100644 --- a/wpiutil/src/main/native/include/wpi/sendable/SendableBuilder.h +++ b/wpiutil/src/main/native/include/wpi/sendable/SendableBuilder.h @@ -59,6 +59,14 @@ class SendableBuilder { std::function getter, std::function setter) = 0; + /** + * Add a constant boolean property. + * + * @param key property name + * @param value the value + */ + virtual void PublishConstBoolean(std::string_view key, bool value) = 0; + /** * Add an integer property. * @@ -70,6 +78,14 @@ class SendableBuilder { std::function getter, std::function setter) = 0; + /** + * Add a constant integer property. + * + * @param key property name + * @param value the value + */ + virtual void PublishConstInteger(std::string_view key, int64_t value) = 0; + /** * Add a float property. * @@ -81,6 +97,14 @@ class SendableBuilder { std::function getter, std::function setter) = 0; + /** + * Add a constant float property. + * + * @param key property name + * @param value the value + */ + virtual void PublishConstFloat(std::string_view key, float value) = 0; + /** * Add a double property. * @@ -92,6 +116,14 @@ class SendableBuilder { std::function getter, std::function setter) = 0; + /** + * Add a constant double property. + * + * @param key property name + * @param value the value + */ + virtual void PublishConstDouble(std::string_view key, double value) = 0; + /** * Add a string property. * @@ -103,6 +135,15 @@ class SendableBuilder { std::string_view key, std::function getter, std::function setter) = 0; + /** + * Add a constant string property. + * + * @param key property name + * @param value the value + */ + virtual void PublishConstString(std::string_view key, + std::string_view value) = 0; + /** * Add a boolean array property. * @@ -114,6 +155,15 @@ class SendableBuilder { std::string_view key, std::function()> getter, std::function)> setter) = 0; + /** + * Add a constant boolean array property. + * + * @param key property name + * @param value the value + */ + virtual void PublishConstBooleanArray(std::string_view key, + std::span value) = 0; + /** * Add an integer array property. * @@ -125,6 +175,15 @@ class SendableBuilder { std::string_view key, std::function()> getter, std::function)> setter) = 0; + /** + * Add a constant integer array property. + * + * @param key property name + * @param value the value + */ + virtual void PublishConstIntegerArray(std::string_view key, + std::span value) = 0; + /** * Add a float array property. * @@ -136,6 +195,15 @@ class SendableBuilder { std::string_view key, std::function()> getter, std::function)> setter) = 0; + /** + * Add a constant float array property. + * + * @param key property name + * @param value the value + */ + virtual void PublishConstFloatArray(std::string_view key, + std::span value) = 0; + /** * Add a double array property. * @@ -147,6 +215,15 @@ class SendableBuilder { std::string_view key, std::function()> getter, std::function)> setter) = 0; + /** + * Add a constant double array property. + * + * @param key property name + * @param value the value + */ + virtual void PublishConstDoubleArray(std::string_view key, + std::span value) = 0; + /** * Add a string array property. * @@ -158,6 +235,15 @@ class SendableBuilder { std::string_view key, std::function()> getter, std::function)> setter) = 0; + /** + * Add a constant string array property. + * + * @param key property name + * @param value the value + */ + virtual void PublishConstStringArray(std::string_view key, + std::span value) = 0; + /** * Add a raw property. * @@ -171,6 +257,17 @@ class SendableBuilder { std::function()> getter, std::function)> setter) = 0; + /** + * Add a constant raw property. + * + * @param key property name + * @param typeString type string + * @param value the value + */ + virtual void PublishConstRaw(std::string_view key, + std::string_view typeString, + std::span value) = 0; + /** * Add a string property (SmallString form). *