WPILibC++ 2023.4.3
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 <memory>
8#include <string_view>
9
10#include <wpi/StringMap.h>
11
13
14namespace frc {
15
16/**
17 * The SendableChooser class is a useful tool for presenting a selection of
18 * options to the SmartDashboard.
19 *
20 * For instance, you may wish to be able to select between multiple autonomous
21 * modes. You can do this by putting every possible Command you want to run as
22 * an autonomous into a SendableChooser and then put it into the SmartDashboard
23 * to have a list of options appear on the laptop. Once autonomous starts,
24 * simply ask the SendableChooser what the selected value is.
25 *
26 * @tparam T The type of values to be stored
27 * @see SmartDashboard
28 */
29template <class T>
31 wpi::StringMap<T> m_choices;
32 static_assert(std::is_copy_constructible_v<T>,
33 "T must be copy-constructible!");
34 static_assert(std::is_default_constructible_v<T>,
35 "T must be default-constructible!");
36
37 template <class U>
38 static U _unwrap_smart_ptr(const U& value);
39
40 template <class U>
41 static U* _unwrap_smart_ptr(const std::unique_ptr<U>& value);
42
43 template <class U>
44 static std::weak_ptr<U> _unwrap_smart_ptr(const std::shared_ptr<U>& value);
45
46 public:
47 SendableChooser() = default;
48 ~SendableChooser() override = default;
51
52 /**
53 * Adds the given object to the list of options.
54 *
55 * On the SmartDashboard on the desktop, the object will appear as the given
56 * name.
57 *
58 * @param name the name of the option
59 * @param object the option
60 */
61 void AddOption(std::string_view name, T object);
62
63 /**
64 * Add the given object to the list of options and marks it as the default.
65 *
66 * Functionally, this is very close to AddOption() except that it will use
67 * this as the default option if none other is explicitly selected.
68 *
69 * @param name the name of the option
70 * @param object the option
71 */
72 void SetDefaultOption(std::string_view name, T object);
73
74 /**
75 * Returns a copy of the selected option (a raw pointer U* if T =
76 * std::unique_ptr<U> or a std::weak_ptr<U> if T = std::shared_ptr<U>).
77 *
78 * If there is none selected, it will return the default. If there is none
79 * selected and no default, then it will return a value-initialized instance.
80 * For integer types, this is 0. For container types like std::string, this is
81 * an empty string.
82 *
83 * @return The option selected
84 */
85 auto GetSelected() -> decltype(_unwrap_smart_ptr(m_choices[""]));
86
87 void InitSendable(nt::NTSendableBuilder& builder) override;
88};
89
90} // namespace frc
91
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:30
SendableChooser & operator=(SendableChooser &&rhs)=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:26
SendableChooser(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:32
~SendableChooser() override=default
void AddOption(std::string_view name, T object)
Adds the given object to the list of options.
Definition: SendableChooser.inc:21
SendableChooser()=default
void InitSendable(nt::NTSendableBuilder &builder) override
Initializes this Sendable object.
Definition: SendableChooser.inc:49
Definition: NTSendableBuilder.h:18
Definition: core.h:1240
basic_string_view< char > string_view
Definition: core.h:520
Definition: AprilTagFieldLayout.h:22