WPILibC++ 2024.3.2
SendableChooser.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <concepts>
8#include <functional>
9#include <memory>
10#include <string_view>
11
12#include <wpi/StringMap.h>
13
15
16namespace frc {
17
18/**
19 * The SendableChooser class is a useful tool for presenting a selection of
20 * options to the SmartDashboard.
21 *
22 * For instance, you may wish to be able to select between multiple autonomous
23 * modes. You can do this by putting every possible Command you want to run as
24 * an autonomous into a SendableChooser and then put it into the SmartDashboard
25 * to have a list of options appear on the laptop. Once autonomous starts,
26 * simply ask the SendableChooser what the selected value is.
27 *
28 * @tparam T The type of values to be stored
29 * @see SmartDashboard
30 */
31template <class T>
32 requires std::copy_constructible<T> && std::default_initializable<T>
34 wpi::StringMap<T> m_choices;
35 std::function<void(T)> m_listener;
36 template <class U>
37 static U _unwrap_smart_ptr(const U& value);
38
39 template <class U>
40 static std::weak_ptr<U> _unwrap_smart_ptr(const std::shared_ptr<U>& value);
41
42 public:
43 using CopyType = decltype(_unwrap_smart_ptr(m_choices.lookup("")));
44
45 SendableChooser() = default;
46 ~SendableChooser() override = default;
49
50 /**
51 * Adds the given object to the list of options.
52 *
53 * On the SmartDashboard on the desktop, the object will appear as the given
54 * name.
55 *
56 * @param name the name of the option
57 * @param object the option
58 */
59 void AddOption(std::string_view name, T object);
60
61 /**
62 * Add the given object to the list of options and marks it as the default.
63 *
64 * Functionally, this is very close to AddOption() except that it will use
65 * this as the default option if none other is explicitly selected.
66 *
67 * @param name the name of the option
68 * @param object the option
69 */
70 void SetDefaultOption(std::string_view name, T object);
71
72 /**
73 * Returns a copy of the selected option (a std::weak_ptr<U> if T =
74 * std::shared_ptr<U>).
75 *
76 * If there is none selected, it will return the default. If there is none
77 * selected and no default, then it will return a value-initialized instance.
78 * For integer types, this is 0. For container types like std::string, this is
79 * an empty string.
80 *
81 * @return The option selected
82 */
83 CopyType GetSelected() const;
84
85 /**
86 * Bind a listener that's called when the selected value changes.
87 * Only one listener can be bound. Calling this function will replace the
88 * previous listener.
89 * @param listener The function to call that accepts the new value
90 */
91 void OnChange(std::function<void(T)>);
92
93 void InitSendable(wpi::SendableBuilder& builder) override;
94};
95
96} // namespace frc
97
This file defines the StringMap class.
This class is a non-template base class for SendableChooser.
Definition: SendableChooserBase.h:23
The SendableChooser class is a useful tool for presenting a selection of options to the SmartDashboar...
Definition: SendableChooser.h:33
SendableChooser & operator=(SendableChooser &&rhs)=default
CopyType GetSelected() const
Returns a copy of the selected option (a std::weak_ptr<U> if T = std::shared_ptr<U>).
Definition: SendableChooser.inc:36
SendableChooser(SendableChooser &&rhs)=default
void AddOption(std::string_view name, T object)
Adds the given object to the list of options.
Definition: SendableChooser.inc:23
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
Definition: SendableChooser.inc:60
~SendableChooser() override=default
void OnChange(std::function< void(T)>)
Bind a listener that's called when the selected value changes.
Definition: SendableChooser.inc:53
void SetDefaultOption(std::string_view name, T object)
Add the given object to the list of options and marks it as the default.
Definition: SendableChooser.inc:29
SendableChooser()=default
decltype(_unwrap_smart_ptr(m_choices.lookup(""))) CopyType
Definition: SendableChooser.h:43
Helper class for building Sendable dashboard representations.
Definition: SendableBuilder.h:21
ValueTy lookup(std::string_view Key) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: StringMap.h:238
basic_string_view< char > string_view
Definition: core.h:501
Definition: AprilTagPoseEstimator.h:15