SubZero Common
Common library components for an FRC CommandRobot
Loading...
Searching...
No Matches
ModifiableChooser.h
Go to the documentation of this file.
1// See
2// https://github.com/frc1675/frc1675-2024/blob/42d94881e7dd001fac9bb410892267b4d4dd8063/src/main/java/frc/robot/util/ChangableSendableChooser.java
3
4#pragma once
5
6#include <frc/smartdashboard/SendableChooser.h>
7#include <frc/smartdashboard/SmartDashboard.h>
8#include <wpi/sendable/Sendable.h>
9#include <wpi/sendable/SendableBuilder.h>
10#include <wpi/sendable/SendableRegistry.h>
11
12#include <atomic>
13#include <functional>
14#include <iostream>
15#include <map>
16#include <mutex>
17#include <ranges>
18#include <string>
19#include <vector>
20
21namespace subzero {
22
29template <typename T> class ModifiableChooser : public wpi::Sendable {
30private:
31 static inline const std::string kDefault = "default";
32 static inline const std::string kSelected = "selected";
33 static inline const std::string kActive = "active";
34 static inline const std::string kOptions = "options";
35 static inline const std::string kInstance = ".instance";
36
37 std::map<std::string, T> m_map;
38
39 std::string m_defaultChoice = "";
40 int m_instance;
41 std::string m_previousValue;
42 std::function<void(std::optional<T>)> m_listener;
43 std::atomic_int m_instances{0};
44 std::string m_selected;
45 std::recursive_mutex m_mutex;
46
47public:
49
51
58 void AddOption(std::string name, T object);
59
65 void RemoveOption(std::string name);
66
72
78 void SetOptions(std::map<std::string, T> options);
79
86 void SetDefaultOption(std::string name, T object);
87
95 void SetOptions(std::map<std::string, T> options, std::string defaultName,
96 T defaultObject);
97
104
110 std::string GetSelectedKey();
111
112 std::string GetNtSelected();
113
114 void SetNtSelected(std::string val);
115
122 void OnChange(std::function<void(std::optional<T>)> listener);
123
124 void InitSendable(wpi::SendableBuilder &builder) override;
125};
126} // namespace subzero
A SmartDashboard drop-down that can have its options dynamically change.
std::string GetSelectedKey()
Get the selected key rather than the value.
void SetOptions(std::map< std::string, T > options, std::string defaultName, T defaultObject)
Set the options from an existing set along with a default.
void RemoveOption(std::string name)
Removes the option and updates the default option accoringly.
void InitSendable(wpi::SendableBuilder &builder) override
void ClearOptions()
Clears all options.
void AddOption(std::string name, T object)
Add a new option to the chooser.
void SetNtSelected(std::string val)
T GetSelected()
Get the selected option.
void SetOptions(std::map< std::string, T > options)
Populates options from an existing set.
void SetDefaultOption(std::string name, T object)
Set the default option to return.
void OnChange(std::function< void(std::optional< T >)> listener)
Register a callback that gets executed whenever the selection changes.