Skip to content

Commit

Permalink
Const-qualify SendableChooser.GetSelected()
Browse files Browse the repository at this point in the history
  • Loading branch information
KangarooKoala committed Feb 10, 2024
1 parent 1db3936 commit fde508b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class SendableChooser : public SendableChooserBase {
static std::weak_ptr<U> _unwrap_smart_ptr(const std::shared_ptr<U>& value);

public:
using CopyType = decltype(_unwrap_smart_ptr(m_choices.lookup("")));

SendableChooser() = default;
~SendableChooser() override = default;
SendableChooser(SendableChooser&& rhs) = default;
Expand Down Expand Up @@ -81,7 +83,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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ void SendableChooser<T>::SetDefaultOption(std::string_view name, T object) {

template <class T>
requires std::copy_constructible<T> && std::default_initializable<T>
auto SendableChooser<T>::GetSelected()
-> decltype(_unwrap_smart_ptr(m_choices[""])) {
typename SendableChooser<T>::CopyType SendableChooser<T>::GetSelected() const {
std::string selected = m_defaultChoice;
{
std::scoped_lock lock(m_mutex);
Expand All @@ -43,9 +42,9 @@ auto SendableChooser<T>::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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit fde508b

Please sign in to comment.