From e506e09a060ade64d63d5c60f8a731a1e29a3fd3 Mon Sep 17 00:00:00 2001 From: Joseph Eng <91924258+KangarooKoala@users.noreply.github.com> Date: Sat, 10 Feb 2024 10:42:53 -0800 Subject: [PATCH] [wpilibc] Const-qualify SendableChooser::GetSelected() (#6356) --- .../native/include/frc/smartdashboard/SendableChooser.h | 4 +++- .../native/include/frc/smartdashboard/SendableChooser.inc | 7 +++---- .../include/frc/smartdashboard/SendableChooserBase.h | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h index 2ca15160d7..0c17fdefe9 100644 --- a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h +++ b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.h @@ -40,6 +40,8 @@ class SendableChooser : public SendableChooserBase { static std::weak_ptr _unwrap_smart_ptr(const std::shared_ptr& value); public: + using CopyType = decltype(_unwrap_smart_ptr(m_choices.lookup(""))); + SendableChooser() = default; ~SendableChooser() override = default; SendableChooser(SendableChooser&& rhs) = default; @@ -78,7 +80,7 @@ class SendableChooser : public SendableChooserBase { * * @return The option selected */ - auto GetSelected() -> decltype(_unwrap_smart_ptr(m_choices[""])); + CopyType GetSelected() const; /** * Bind a listener that's called when the selected value changes. diff --git a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.inc b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.inc index 57f4a1d118..2b93629959 100644 --- a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.inc +++ b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooser.inc @@ -33,8 +33,7 @@ void SendableChooser::SetDefaultOption(std::string_view name, T object) { template requires std::copy_constructible && std::default_initializable -auto SendableChooser::GetSelected() - -> decltype(_unwrap_smart_ptr(m_choices[""])) { +typename SendableChooser::CopyType SendableChooser::GetSelected() const { std::string selected = m_defaultChoice; { std::scoped_lock lock(m_mutex); @@ -43,9 +42,9 @@ auto SendableChooser::GetSelected() } } if (selected.empty()) { - return decltype(_unwrap_smart_ptr(m_choices[""])){}; + return CopyType{}; } else { - return _unwrap_smart_ptr(m_choices[selected]); + return _unwrap_smart_ptr(m_choices.lookup(selected)); } } diff --git a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooserBase.h b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooserBase.h index f0b4fedcbb..3263f92f6b 100644 --- a/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooserBase.h +++ b/wpilibc/src/main/native/include/frc/smartdashboard/SendableChooserBase.h @@ -38,7 +38,7 @@ class SendableChooserBase : public wpi::Sendable, std::string m_defaultChoice; std::string m_selected; bool m_haveSelected = false; - wpi::mutex m_mutex; + mutable wpi::mutex m_mutex; int m_instance; std::string m_previousVal; static std::atomic_int s_instances;