From 2caef4aca707bfc7e58fbab9d6a2e905e6cd1bda Mon Sep 17 00:00:00 2001 From: braykoff <99614905+Braykoff@users.noreply.github.com> Date: Sun, 22 Dec 2024 22:22:47 -0500 Subject: [PATCH 1/9] Initial updates --- .../frc/smartdashboard/SendableChooser.h | 39 +++++++++++++++--- .../smartdashboard/SendableChooserTest.cpp | 8 +++- .../smartdashboard/SendableChooser.java | 40 ++++++++++++++++--- .../smartdashboard/SendableChooserTest.java | 3 ++ 4 files changed, 79 insertions(+), 11 deletions(-) diff --git a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h index 40588dd58ab..262ac0d8b70 100644 --- a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h +++ b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h @@ -37,7 +37,7 @@ template requires std::copy_constructible && std::default_initializable class SendableChooser : public SendableChooserBase { wpi::StringMap m_choices; - std::function m_listener; + std::function m_listener; template static U _unwrap_smart_ptr(const U& value) { return value; @@ -113,17 +113,46 @@ class SendableChooser : public SendableChooserBase { } } + /** + * Returns the name of the selected option. + * + * If there is none selected, it will return the default option's name. If there is none selected + * and no default, it will return an empty string. + * + * @return The name of the option selected + */ + std::string GetSelectedName() const { + std::scoped_lock lock(m_mutex); + if (m_haveSelected) { + return m_selected; + } else { + return m_defaultChoice; + } + } + /** * Bind a listener that's called when the selected value changes. * Only one listener can be bound. Calling this function will replace the * previous listener. - * @param listener The function to call that accepts the new value + * @param listener The function to call that accepts the new name and new value */ - void OnChange(std::function listener) { + void OnChange(std::function listener) { std::scoped_lock lock(m_mutex); m_listener = listener; } + /** + * Bind a listener that's called when the selected value changes. + * Only one listener can be bound. Calling this function will replace the + * previous listener. + * @param listener The function to call that accepts the new value + */ + void OnChange(std::function listener) { + OnChange([listener](std::string, T choice) { + listener(choice); + }); + } + void InitSendable(wpi::SendableBuilder& builder) override { builder.SetSmartDashboardType("String Chooser"); builder.PublishConstInteger(kInstance, m_instance); @@ -158,7 +187,7 @@ class SendableChooser : public SendableChooserBase { builder.AddStringProperty(kSelected, nullptr, [=, this](std::string_view val) { T choice{}; - std::function listener; + std::function listener; { std::scoped_lock lock(m_mutex); m_haveSelected = true; @@ -170,7 +199,7 @@ class SendableChooser : public SendableChooserBase { m_previousVal = val; } if (listener) { - listener(choice); + listener(val, choice); } }); } diff --git a/wpilibc/src/test/native/cpp/smartdashboard/SendableChooserTest.cpp b/wpilibc/src/test/native/cpp/smartdashboard/SendableChooserTest.cpp index 702735c3eaf..1c127e78bb5 100644 --- a/wpilibc/src/test/native/cpp/smartdashboard/SendableChooserTest.cpp +++ b/wpilibc/src/test/native/cpp/smartdashboard/SendableChooserTest.cpp @@ -31,6 +31,7 @@ TEST_P(SendableChooserTest, ReturnsSelected) { chooserSim.SetSelected(std::to_string(GetParam())); frc::SmartDashboard::UpdateValues(); EXPECT_EQ(GetParam(), chooser.GetSelected()); + EXPECT_EQ(std::to_string(GetParam()), chooser.GetSelectedName()); } TEST(SendableChooserTest, DefaultIsReturnedOnNoSelect) { @@ -44,6 +45,7 @@ TEST(SendableChooserTest, DefaultIsReturnedOnNoSelect) { chooser.SetDefaultOption("4", 4); EXPECT_EQ(4, chooser.GetSelected()); + EXPECT_EQ("4",, chooser.GetSelectedName()); } TEST(SendableChooserTest, @@ -55,6 +57,7 @@ TEST(SendableChooserTest, } EXPECT_EQ(0, chooser.GetSelected()); + EXPECT_EQ("", chooser.GetSelectedName()); } TEST(SendableChooserTest, ChangeListener) { @@ -65,8 +68,10 @@ TEST(SendableChooserTest, ChangeListener) { for (int i = 1; i <= 3; i++) { chooser.AddOption(std::to_string(i), i); } + + std::string currentName = ""; int currentVal = 0; - chooser.OnChange([&](int val) { currentVal = val; }); + chooser.OnChange([&](std::string name, int val) { currentName = name; currentVal = val; }); frc::SmartDashboard::PutData("ChangeListenerChooser", &chooser); frc::SmartDashboard::UpdateValues(); @@ -74,6 +79,7 @@ TEST(SendableChooserTest, ChangeListener) { frc::SmartDashboard::UpdateValues(); EXPECT_EQ(3, currentVal); + EXPECT_EQ("3", currentName); } INSTANTIATE_TEST_SUITE_P(SendableChooserTests, SendableChooserTest, diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooser.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooser.java index e905ebc6350..63380c3abf0 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooser.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooser.java @@ -13,6 +13,7 @@ import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReentrantLock; +import java.util.function.BiConsumer; import java.util.function.Consumer; /** @@ -49,7 +50,7 @@ public class SendableChooser implements Sendable, AutoCloseable { private String m_defaultChoice = ""; private final int m_instance; private String m_previousVal; - private Consumer m_listener; + private BiConsumer m_listener; private static final AtomicInteger s_instances = new AtomicInteger(); /** Instantiates a {@link SendableChooser}. */ @@ -109,19 +110,48 @@ public V getSelected() { } } + /** + * Returns the name of the selected option. If there is none selected, it will return the default. + * If there is none selected and no default, it will return {@code null}. + * + * @return the name of the option selected + */ + public String getSelectedName() { + m_mutex.lock(); + try { + if (m_selected != null) { + return m_selected; + } else { + return m_defaultChoice; + } + } finally { + m_mutex.unlock(); + } + } + /** * Bind a listener that's called when the selected value changes. Only one listener can be bound. * Calling this function will replace the previous listener. * - * @param listener The function to call that accepts the new value + * @param listener The function to call that accepts the new name and new value */ - public void onChange(Consumer listener) { + public void onChange(BiConsumer listener) { requireNonNullParam(listener, "listener", "onChange"); m_mutex.lock(); m_listener = listener; m_mutex.unlock(); } + /** + * Bind a listener that's called when the selected value changes. Only one listener can be bound. + * Calling this function will replace the previous listener. + * + * @param listener The function to call that accepts the new value + */ + public void onChange(Consumer listener) { + onChange((String value, V choice) -> listener.accept(choice)); + } + private String m_selected; private final ReentrantLock m_mutex = new ReentrantLock(); @@ -151,7 +181,7 @@ public void initSendable(SendableBuilder builder) { null, val -> { V choice; - Consumer listener; + BiConsumer listener; m_mutex.lock(); try { m_selected = val; @@ -167,7 +197,7 @@ public void initSendable(SendableBuilder builder) { m_mutex.unlock(); } if (listener != null) { - listener.accept(choice); + listener.accept(val, choice); } }); } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooserTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooserTest.java index d8e3f8fbb98..48471925298 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooserTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooserTest.java @@ -42,6 +42,7 @@ void returnsSelected(int toSelect) { chooserSim.setSelected(String.valueOf(toSelect)); SmartDashboard.updateValues(); assertEquals(toSelect, chooser.getSelected()); + assertEquals(String.valueOf(toSelect), chooser.getSelectedName()); } } @@ -54,6 +55,7 @@ void defaultIsReturnedOnNoSelect() { chooser.setDefaultOption(String.valueOf(0), 0); assertEquals(0, chooser.getSelected()); + assertEquals(String.valueOf(0), chooser.getSelectedName()); } } @@ -65,6 +67,7 @@ void nullIsReturnedOnNoSelectAndNoDefault() { } assertNull(chooser.getSelected()); + assertNull(chooser.getSelectedName()); } } From 034b89c307d0d56bb1748fa065f840ee141ad19e Mon Sep 17 00:00:00 2001 From: braykoff <99614905+Braykoff@users.noreply.github.com> Date: Sun, 22 Dec 2024 22:49:33 -0500 Subject: [PATCH 2/9] Bug fixes --- .../include/frc/smartdashboard/SendableChooser.h | 10 +++++----- .../cpp/smartdashboard/SendableChooserTest.cpp | 4 ++-- .../wpilibj/smartdashboard/SendableChooser.java | 15 +++------------ .../smartdashboard/SendableChooserTest.java | 2 +- 4 files changed, 11 insertions(+), 20 deletions(-) diff --git a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h index 262ac0d8b70..3f0ae83b5a9 100644 --- a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h +++ b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h @@ -37,7 +37,7 @@ template requires std::copy_constructible && std::default_initializable class SendableChooser : public SendableChooserBase { wpi::StringMap m_choices; - std::function m_listener; + std::function m_listener; template static U _unwrap_smart_ptr(const U& value) { return value; @@ -121,7 +121,7 @@ class SendableChooser : public SendableChooserBase { * * @return The name of the option selected */ - std::string GetSelectedName() const { + std::string_view GetSelectedName() const { std::scoped_lock lock(m_mutex); if (m_haveSelected) { return m_selected; @@ -136,7 +136,7 @@ class SendableChooser : public SendableChooserBase { * previous listener. * @param listener The function to call that accepts the new name and new value */ - void OnChange(std::function listener) { + void OnChange(std::function listener) { std::scoped_lock lock(m_mutex); m_listener = listener; } @@ -148,7 +148,7 @@ class SendableChooser : public SendableChooserBase { * @param listener The function to call that accepts the new value */ void OnChange(std::function listener) { - OnChange([listener](std::string, T choice) { + OnChange([listener](std::string_view, T choice) { listener(choice); }); } @@ -187,7 +187,7 @@ class SendableChooser : public SendableChooserBase { builder.AddStringProperty(kSelected, nullptr, [=, this](std::string_view val) { T choice{}; - std::function listener; + std::function listener; { std::scoped_lock lock(m_mutex); m_haveSelected = true; diff --git a/wpilibc/src/test/native/cpp/smartdashboard/SendableChooserTest.cpp b/wpilibc/src/test/native/cpp/smartdashboard/SendableChooserTest.cpp index 1c127e78bb5..83ffd44578b 100644 --- a/wpilibc/src/test/native/cpp/smartdashboard/SendableChooserTest.cpp +++ b/wpilibc/src/test/native/cpp/smartdashboard/SendableChooserTest.cpp @@ -45,7 +45,7 @@ TEST(SendableChooserTest, DefaultIsReturnedOnNoSelect) { chooser.SetDefaultOption("4", 4); EXPECT_EQ(4, chooser.GetSelected()); - EXPECT_EQ("4",, chooser.GetSelectedName()); + EXPECT_EQ("4", chooser.GetSelectedName()); } TEST(SendableChooserTest, @@ -71,7 +71,7 @@ TEST(SendableChooserTest, ChangeListener) { std::string currentName = ""; int currentVal = 0; - chooser.OnChange([&](std::string name, int val) { currentName = name; currentVal = val; }); + chooser.OnChange([&](std::string_view name, int val) { currentName = std::string(name); currentVal = val; }); frc::SmartDashboard::PutData("ChangeListenerChooser", &chooser); frc::SmartDashboard::UpdateValues(); diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooser.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooser.java index 63380c3abf0..a73b7eea5c5 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooser.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooser.java @@ -98,22 +98,13 @@ public void setDefaultOption(String name, V object) { * @return the option selected */ public V getSelected() { - m_mutex.lock(); - try { - if (m_selected != null) { - return m_map.get(m_selected); - } else { - return m_map.get(m_defaultChoice); - } - } finally { - m_mutex.unlock(); - } + return m_map.get(getSelectedName()); } /** * Returns the name of the selected option. If there is none selected, it will return the default. - * If there is none selected and no default, it will return {@code null}. - * + * If there is none selected and no default, it will return an empty String. + * * @return the name of the option selected */ public String getSelectedName() { diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooserTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooserTest.java index 48471925298..655c09ef512 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooserTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooserTest.java @@ -67,7 +67,7 @@ void nullIsReturnedOnNoSelectAndNoDefault() { } assertNull(chooser.getSelected()); - assertNull(chooser.getSelectedName()); + assertEquals("", chooser.getSelectedName()); } } From 7923415969273c82c28416c7cb848b8f7552aff5 Mon Sep 17 00:00:00 2001 From: braykoff <99614905+Braykoff@users.noreply.github.com> Date: Sun, 22 Dec 2024 23:04:17 -0500 Subject: [PATCH 3/9] sendable-chooser-value --- .../frc/smartdashboard/SendableChooser.h | 57 +++++++++---------- .../smartdashboard/SendableChooserTest.cpp | 5 +- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h index 3f0ae83b5a9..a931af09fae 100644 --- a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h +++ b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h @@ -114,19 +114,19 @@ class SendableChooser : public SendableChooserBase { } /** - * Returns the name of the selected option. - * - * If there is none selected, it will return the default option's name. If there is none selected - * and no default, it will return an empty string. - * + * Returns the name of the selected option. + * + * If there is none selected, it will return the default option's name. If + * there is none selected and no default, it will return an empty string. + * * @return The name of the option selected */ std::string_view GetSelectedName() const { std::scoped_lock lock(m_mutex); if (m_haveSelected) { - return m_selected; + return m_selected; } else { - return m_defaultChoice; + return m_defaultChoice; } } @@ -134,7 +134,8 @@ class SendableChooser : public SendableChooserBase { * Bind a listener that's called when the selected value changes. * Only one listener can be bound. Calling this function will replace the * previous listener. - * @param listener The function to call that accepts the new name and new value + * @param listener The function to call that accepts the new name and new + * value */ void OnChange(std::function listener) { std::scoped_lock lock(m_mutex); @@ -148,9 +149,7 @@ class SendableChooser : public SendableChooserBase { * @param listener The function to call that accepts the new value */ void OnChange(std::function listener) { - OnChange([listener](std::string_view, T choice) { - listener(choice); - }); + OnChange([listener](std::string_view, T choice) { listener(choice); }); } void InitSendable(wpi::SendableBuilder& builder) override { @@ -184,24 +183,24 @@ class SendableChooser : public SendableChooserBase { } }, nullptr); - builder.AddStringProperty(kSelected, nullptr, - [=, this](std::string_view val) { - T choice{}; - std::function listener; - { - std::scoped_lock lock(m_mutex); - m_haveSelected = true; - m_selected = val; - if (m_previousVal != val && m_listener) { - choice = m_choices[val]; - listener = m_listener; - } - m_previousVal = val; - } - if (listener) { - listener(val, choice); - } - }); + builder.AddStringProperty( + kSelected, nullptr, [=, this](std::string_view val) { + T choice{}; + std::function listener; + { + std::scoped_lock lock(m_mutex); + m_haveSelected = true; + m_selected = val; + if (m_previousVal != val && m_listener) { + choice = m_choices[val]; + listener = m_listener; + } + m_previousVal = val; + } + if (listener) { + listener(val, choice); + } + }); } }; diff --git a/wpilibc/src/test/native/cpp/smartdashboard/SendableChooserTest.cpp b/wpilibc/src/test/native/cpp/smartdashboard/SendableChooserTest.cpp index 83ffd44578b..824ab416491 100644 --- a/wpilibc/src/test/native/cpp/smartdashboard/SendableChooserTest.cpp +++ b/wpilibc/src/test/native/cpp/smartdashboard/SendableChooserTest.cpp @@ -71,7 +71,10 @@ TEST(SendableChooserTest, ChangeListener) { std::string currentName = ""; int currentVal = 0; - chooser.OnChange([&](std::string_view name, int val) { currentName = std::string(name); currentVal = val; }); + chooser.OnChange([&](std::string_view name, int val) { + currentName = std::string(name); + currentVal = val; + }); frc::SmartDashboard::PutData("ChangeListenerChooser", &chooser); frc::SmartDashboard::UpdateValues(); From e7fd08aa24f72fe77b6f1db6f3fe49aa897f09dd Mon Sep 17 00:00:00 2001 From: braykoff <99614905+Braykoff@users.noreply.github.com> Date: Sun, 22 Dec 2024 23:12:40 -0500 Subject: [PATCH 4/9] linted again --- .../main/native/include/frc/smartdashboard/SendableChooser.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h index a931af09fae..0aa1c801bce 100644 --- a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h +++ b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h @@ -149,7 +149,7 @@ class SendableChooser : public SendableChooserBase { * @param listener The function to call that accepts the new value */ void OnChange(std::function listener) { - OnChange([listener](std::string_view, T choice) { listener(choice); }); + OnChange([listener](std::string_view val, T choice) { listener(choice); }); } void InitSendable(wpi::SendableBuilder& builder) override { From 263e3f8838de81e7634ba6324e8d7efeab436dc4 Mon Sep 17 00:00:00 2001 From: braykoff <99614905+Braykoff@users.noreply.github.com> Date: Mon, 23 Dec 2024 14:48:33 -0500 Subject: [PATCH 5/9] Optimization and java test case --- .../include/frc/smartdashboard/SendableChooser.h | 8 +------- .../wpilibj/smartdashboard/SendableChooserTest.java | 10 +++++++++- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h index 0aa1c801bce..47c2c91f397 100644 --- a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h +++ b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h @@ -95,13 +95,7 @@ class SendableChooser : public SendableChooserBase { * @return The option selected */ CopyType GetSelected() const { - std::string selected = m_defaultChoice; - { - std::scoped_lock lock(m_mutex); - if (m_haveSelected) { - selected = m_selected; - } - } + std::string_view selected = GetSelectedName(); if (selected.empty()) { return CopyType{}; } else { diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooserTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooserTest.java index 655c09ef512..4846bbc85a2 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooserTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooserTest.java @@ -10,6 +10,7 @@ import edu.wpi.first.networktables.NetworkTableInstance; import edu.wpi.first.wpilibj.simulation.SendableChooserSim; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -78,14 +79,21 @@ void testChangeListener() { for (int i = 1; i <= 3; i++) { chooser.addOption(String.valueOf(i), i); } + + AtomicReference currentName = new AtomicReference<>(""); AtomicInteger currentVal = new AtomicInteger(); - chooser.onChange(currentVal::set); + chooser.onChange( + (String name, Integer value) -> { + currentName.set(name); + currentVal.set(value); + }); SmartDashboard.putData("changeListenerChooser", chooser); SmartDashboard.updateValues(); chooserSim.setSelected("3"); SmartDashboard.updateValues(); assertEquals(3, currentVal.get()); + assertEquals("3", currentName.get()); } } From bc03f9f52d6520db65b1f9fee2faa72613484144 Mon Sep 17 00:00:00 2001 From: Brendan Raykoff <99614905+Braykoff@users.noreply.github.com> Date: Mon, 23 Dec 2024 17:52:44 -0500 Subject: [PATCH 6/9] c++, move listener instead of copying Co-authored-by: Joseph Eng <91924258+KangarooKoala@users.noreply.github.com> --- .../main/native/include/frc/smartdashboard/SendableChooser.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h index 47c2c91f397..9f2f5167875 100644 --- a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h +++ b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h @@ -143,7 +143,7 @@ class SendableChooser : public SendableChooserBase { * @param listener The function to call that accepts the new value */ void OnChange(std::function listener) { - OnChange([listener](std::string_view val, T choice) { listener(choice); }); + OnChange([listener = std::move(listener)](std::string_view val, T choice) { listener(choice); }); } void InitSendable(wpi::SendableBuilder& builder) override { From 3a4bb75917ffb23222a14d4bc73d2496f6a46f5e Mon Sep 17 00:00:00 2001 From: braykoff <99614905+Braykoff@users.noreply.github.com> Date: Mon, 23 Dec 2024 18:07:36 -0500 Subject: [PATCH 7/9] linted cpp --- .../main/native/include/frc/smartdashboard/SendableChooser.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h index 9f2f5167875..9db047a4599 100644 --- a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h +++ b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h @@ -143,7 +143,9 @@ class SendableChooser : public SendableChooserBase { * @param listener The function to call that accepts the new value */ void OnChange(std::function listener) { - OnChange([listener = std::move(listener)](std::string_view val, T choice) { listener(choice); }); + OnChange([listener = std::move(listener)](std::string_view val, T choice) { + listener(choice); + }); } void InitSendable(wpi::SendableBuilder& builder) override { From a79936a5880bc2c51e11ef611b133907c1a0ff9d Mon Sep 17 00:00:00 2001 From: Brendan Raykoff <99614905+Braykoff@users.noreply.github.com> Date: Mon, 30 Dec 2024 17:21:31 -0500 Subject: [PATCH 8/9] Switch string_view to string (1/2) Co-authored-by: Peter Johnson --- .../main/native/include/frc/smartdashboard/SendableChooser.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h index 9db047a4599..ec34ecd3171 100644 --- a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h +++ b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h @@ -115,7 +115,7 @@ class SendableChooser : public SendableChooserBase { * * @return The name of the option selected */ - std::string_view GetSelectedName() const { + std::string GetSelectedName() const { std::scoped_lock lock(m_mutex); if (m_haveSelected) { return m_selected; From 0d88d6c36ea9f0857cc4866aa15a7303c8d52982 Mon Sep 17 00:00:00 2001 From: Brendan Raykoff <99614905+Braykoff@users.noreply.github.com> Date: Mon, 30 Dec 2024 17:21:42 -0500 Subject: [PATCH 9/9] Switch string_view to string (2/2) Co-authored-by: Peter Johnson --- .../main/native/include/frc/smartdashboard/SendableChooser.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h index ec34ecd3171..bf7fff55630 100644 --- a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h +++ b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h @@ -95,7 +95,7 @@ class SendableChooser : public SendableChooserBase { * @return The option selected */ CopyType GetSelected() const { - std::string_view selected = GetSelectedName(); + std::string selected = GetSelectedName(); if (selected.empty()) { return CopyType{}; } else {