WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
wpi::SendableChooser< T > Class Template Reference

The SendableChooser class is a useful tool for presenting a selection of options to the SmartDashboard. More...

#include <wpi/smartdashboard/SendableChooser.hpp>

Inheritance diagram for wpi::SendableChooser< T >:
wpi::SendableChooserBase wpi::util::Sendable wpi::util::SendableHelper< SendableChooserBase >

Public Types

using CopyType = decltype(_unwrap_smart_ptr(m_choices.find("")->second))

Public Member Functions

 SendableChooser ()=default
 ~SendableChooser () override=default
 SendableChooser (SendableChooser &&rhs)=default
SendableChooseroperator= (SendableChooser &&rhs)=default
void AddOption (std::string_view name, T object)
 Adds the given object to the list of options.
void SetDefaultOption (std::string_view name, T object)
 Add the given object to the list of options and marks it as the default.
CopyType GetSelected () const
 Returns a copy of the selected option (a std::weak_ptr<U> if T = std::shared_ptr<U>).
void OnChange (std::function< void(T)> listener)
 Bind a listener that's called when the selected value changes.
void InitSendable (wpi::util::SendableBuilder &builder) override
 Initializes this Sendable object.
Public Member Functions inherited from wpi::SendableChooserBase
 SendableChooserBase ()
 ~SendableChooserBase () override=default
 SendableChooserBase (SendableChooserBase &&oth)
SendableChooserBaseoperator= (SendableChooserBase &&oth)
Public Member Functions inherited from wpi::util::Sendable
virtual constexpr ~Sendable ()=default
Public Member Functions inherited from wpi::util::SendableHelper< SendableChooserBase >
constexpr SendableHelper (const SendableHelper &rhs)=default
constexpr SendableHelperoperator= (const SendableHelper &rhs)=default

Additional Inherited Members

Protected Member Functions inherited from wpi::util::SendableHelper< SendableChooserBase >
constexpr ~SendableHelper ()
Protected Attributes inherited from wpi::SendableChooserBase
std::string m_defaultChoice
std::string m_selected
bool m_haveSelected = false
wpi::util::mutex m_mutex
int m_instance
std::string m_previousVal
Static Protected Attributes inherited from wpi::SendableChooserBase
static constexpr const char * kDefault = "default"
static constexpr const char * kOptions = "options"
static constexpr const char * kSelected = "selected"
static constexpr const char * kActive = "active"
static constexpr const char * kInstance = ".instance"
static std::atomic_int s_instances

Detailed Description

template<class T>
requires std::copy_constructible<T> && std::default_initializable<T>
class wpi::SendableChooser< T >

The SendableChooser class is a useful tool for presenting a selection of options to the SmartDashboard.

For instance, you may wish to be able to select between multiple autonomous modes. You can do this by putting every possible Command you want to run as an autonomous into a SendableChooser and then put it into the SmartDashboard to have a list of options appear on the laptop. Once autonomous starts, simply ask the SendableChooser what the selected value is.

Template Parameters
TThe type of values to be stored
See also
SmartDashboard

Member Typedef Documentation

◆ CopyType

template<class T>
using wpi::SendableChooser< T >::CopyType = decltype(_unwrap_smart_ptr(m_choices.find("")->second))

Constructor & Destructor Documentation

◆ SendableChooser() [1/2]

template<class T>
wpi::SendableChooser< T >::SendableChooser ( )
default

◆ ~SendableChooser()

template<class T>
wpi::SendableChooser< T >::~SendableChooser ( )
overridedefault

◆ SendableChooser() [2/2]

template<class T>
wpi::SendableChooser< T >::SendableChooser ( SendableChooser< T > && rhs)
default

Member Function Documentation

◆ AddOption()

template<class T>
void wpi::SendableChooser< T >::AddOption ( std::string_view name,
T object )
inline

Adds the given object to the list of options.

On the SmartDashboard on the desktop, the object will appear as the given name.

Parameters
namethe name of the option
objectthe option

◆ GetSelected()

template<class T>
CopyType wpi::SendableChooser< T >::GetSelected ( ) const
inline

Returns a copy of the selected option (a std::weak_ptr<U> if T = std::shared_ptr<U>).

If there is none selected, it will return the default. If there is none selected and no default, then it will return a value-initialized instance. For integer types, this is 0. For container types like std::string, this is an empty string.

Returns
The option selected

◆ InitSendable()

template<class T>
void wpi::SendableChooser< T >::InitSendable ( wpi::util::SendableBuilder & builder)
inlineoverridevirtual

Initializes this Sendable object.

Parameters
buildersendable builder

Implements wpi::util::Sendable.

◆ OnChange()

template<class T>
void wpi::SendableChooser< T >::OnChange ( std::function< void(T)> listener)
inline

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.

Parameters
listenerThe function to call that accepts the new value

◆ operator=()

template<class T>
SendableChooser & wpi::SendableChooser< T >::operator= ( SendableChooser< T > && rhs)
default

◆ SetDefaultOption()

template<class T>
void wpi::SendableChooser< T >::SetDefaultOption ( std::string_view name,
T object )
inline

Add the given object to the list of options and marks it as the default.

Functionally, this is very close to AddOption() except that it will use this as the default option if none other is explicitly selected.

Parameters
namethe name of the option
objectthe option

The documentation for this class was generated from the following file: