WPILibC++ 2024.1.1-beta-4
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 U* _unwrap_smart_ptr(const std::unique_ptr<U>& value);
41
42 template <class U>
43 static std::weak_ptr<U> _unwrap_smart_ptr(const std::shared_ptr<U>& value);
44
45 public:
46 SendableChooser() = default;
47 ~SendableChooser() override = default;
50
51 /**
52 * Adds the given object to the list of options.
53 *
54 * On the SmartDashboard on the desktop, the object will appear as the given
55 * name.
56 *
57 * @param name the name of the option
58 * @param object the option
59 */
60 void AddOption(std::string_view name, T object);
61
62 /**
63 * Add the given object to the list of options and marks it as the default.
64 *
65 * Functionally, this is very close to AddOption() except that it will use
66 * this as the default option if none other is explicitly selected.
67 *
68 * @param name the name of the option
69 * @param object the option
70 */
71 void SetDefaultOption(std::string_view name, T object);
72
73 /**
74 * Returns a copy of the selected option (a raw pointer U* if T =
75 * std::unique_ptr<U> or a std::weak_ptr<U> if T = std::shared_ptr<U>).
76 *
77 * If there is none selected, it will return the default. If there is none
78 * selected and no default, then it will return a value-initialized instance.
79 * For integer types, this is 0. For container types like std::string, this is
80 * an empty string.
81 *
82 * @return The option selected
83 */
84 auto GetSelected() -> decltype(_unwrap_smart_ptr(m_choices[""]));
85
86 /**
87 * Bind a listener that's called when the selected value changes.
88 * Only one listener can be bound. Calling this function will replace the
89 * previous listener.
90 * @param listener The function to call that accepts the new value
91 */
92 void OnChange(std::function<void(T)>);
93
94 void InitSendable(wpi::SendableBuilder& builder) override;
95};
96
97} // namespace frc
98
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
auto GetSelected() -> decltype(_unwrap_smart_ptr(m_choices[""]))
Returns a copy of the selected option (a raw pointer U* if T = std::unique_ptr<U> or a std::weak_ptr<...
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:61
~SendableChooser() override=default
void OnChange(std::function< void(T)>)
Bind a listener that's called when the selected value changes.
Definition: SendableChooser.inc:54
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
Definition: SendableBuilder.h:18
basic_string_view< char > string_view
Definition: core.h:501
Definition: AprilTagPoseEstimator.h:15