-
Notifications
You must be signed in to change notification settings - Fork 611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add methods to get name of SendableChooser's selected value #7580
Open
Braykoff
wants to merge
7
commits into
wpilibsuite:main
Choose a base branch
from
Braykoff:sendable-chooser-value
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+102
−37
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2caef4a
Initial updates
Braykoff 034b89c
Bug fixes
Braykoff 7923415
sendable-chooser-value
Braykoff e7fd08a
linted again
Braykoff 263e3f8
Optimization and java test case
Braykoff bc03f9f
c++, move listener instead of copying
Braykoff 3a4bb75
linted cpp
Braykoff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -37,7 +37,7 @@ template <class T> | |||||
requires std::copy_constructible<T> && std::default_initializable<T> | ||||||
class SendableChooser : public SendableChooserBase { | ||||||
wpi::StringMap<T> m_choices; | ||||||
std::function<void(T)> m_listener; | ||||||
std::function<void(std::string_view, T)> m_listener; | ||||||
template <class U> | ||||||
static U _unwrap_smart_ptr(const U& value) { | ||||||
return value; | ||||||
|
@@ -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 { | ||||||
|
@@ -113,17 +107,45 @@ 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_view GetSelectedName() const { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
It's not safe to return a string_view here. It needs to return a copy (a std::string) because the access is mutex-protected, so (theoretically) another thread could come in and modify m_selected before the string_view is accessed. |
||||||
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<void(T)> listener) { | ||||||
void OnChange(std::function<void(std::string_view, T)> 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<void(T)> listener) { | ||||||
OnChange([listener](std::string_view val, T choice) { listener(choice); }); | ||||||
Braykoff marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
|
||||||
void InitSendable(wpi::SendableBuilder& builder) override { | ||||||
builder.SetSmartDashboardType("String Chooser"); | ||||||
builder.PublishConstInteger(kInstance, m_instance); | ||||||
|
@@ -155,24 +177,24 @@ class SendableChooser : public SendableChooserBase { | |||||
} | ||||||
}, | ||||||
nullptr); | ||||||
builder.AddStringProperty(kSelected, nullptr, | ||||||
[=, this](std::string_view val) { | ||||||
T choice{}; | ||||||
std::function<void(T)> 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(choice); | ||||||
} | ||||||
}); | ||||||
builder.AddStringProperty( | ||||||
kSelected, nullptr, [=, this](std::string_view val) { | ||||||
T choice{}; | ||||||
std::function<void(std::string_view, T)> 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); | ||||||
} | ||||||
}); | ||||||
} | ||||||
}; | ||||||
|
||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.