WPILibC++ 2023.4.3-108-ge5452e3
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 <memory>
9#include <string_view>
10
11#include <wpi/StringMap.h>
12
14
15namespace frc {
16
17/**
18 * The SendableChooser class is a useful tool for presenting a selection of
19 * options to the SmartDashboard.
20 *
21 * For instance, you may wish to be able to select between multiple autonomous
22 * modes. You can do this by putting every possible Command you want to run as
23 * an autonomous into a SendableChooser and then put it into the SmartDashboard
24 * to have a list of options appear on the laptop. Once autonomous starts,
25 * simply ask the SendableChooser what the selected value is.
26 *
27 * @tparam T The type of values to be stored
28 * @see SmartDashboard
29 */
30template <class T>
31 requires std::copy_constructible<T> && std::default_initializable<T>
33 wpi::StringMap<T> m_choices;
34
35 template <class U>
36 static U _unwrap_smart_ptr(const U& value);
37
38 template <class U>
39 static U* _unwrap_smart_ptr(const std::unique_ptr<U>& value);
40
41 template <class U>
42 static std::weak_ptr<U> _unwrap_smart_ptr(const std::shared_ptr<U>& value);
43
44 public:
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 raw pointer U* if T =
74 * std::unique_ptr<U> or a std::weak_ptr<U> if T = 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 auto GetSelected() -> decltype(_unwrap_smart_ptr(m_choices[""]));
84
85 void InitSendable(nt::NTSendableBuilder& builder) override;
86};
87
88} // namespace frc
89
This file defines the StringMap class.
This class is a non-template base class for SendableChooser.
Definition: SendableChooserBase.h:26
The SendableChooser class is a useful tool for presenting a selection of options to the SmartDashboar...
Definition: SendableChooser.h:32
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:35
SendableChooser(SendableChooser &&rhs)=default
void AddOption(std::string_view name, T object)
Adds the given object to the list of options.
Definition: SendableChooser.inc:22
~SendableChooser() override=default
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:28
SendableChooser()=default
void InitSendable(nt::NTSendableBuilder &builder) override
Initializes this Sendable object.
Definition: SendableChooser.inc:53
Definition: NTSendableBuilder.h:18
Definition: core.h:1240
basic_string_view< char > string_view
Definition: core.h:520
Definition: AprilTagPoseEstimator.h:15