blob: 255cc3af217639f6d2708b4915a373ea71312bc3 [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2011. All Rights Reserved.
3 */
4/* Open Source Software - may be modified and shared by FRC teams. The code */
5/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
6/*----------------------------------------------------------------------------*/
7
8#include "SmartDashboard/SendableChooser.h"
9
10#include <stdio.h>
11
12static const std::string kDefault = "default";
13static const std::string kOptions = "options";
14static const std::string kSelected = "selected";
15
16/**
17 * Adds the given object to the list of options. On the {@link SmartDashboard}
18 * on the desktop,
19 * the object will appear as the given name.
20 * @param name the name of the option
21 * @param object the option
22 */
23void SendableChooser::AddObject(const std::string &name, void *object) {
24 m_choices[name] = object;
25}
26
27/**
28 * Add the given object to the list of options and marks it as the default.
29 * Functionally, this is very close to {@link SendableChooser#AddObject(const
30 * char *name, void *object) AddObject(...)}
31 * except that it will use this as the default option if none other is
32 * explicitly selected.
33 * @param name the name of the option
34 * @param object the option
35 */
36void SendableChooser::AddDefault(const std::string &name, void *object) {
37 m_defaultChoice = name;
38 AddObject(name, object);
39}
40
41/**
42 * Returns the selected option. If there is none selected, it will return the
43 * default. If there is none selected
44 * and no default, then it will return {@code nullptr}.
45 * @return the option selected
46 */
47void *SendableChooser::GetSelected() {
48 std::string selected = m_table->GetString(kSelected, m_defaultChoice);
49 if (selected == "")
50 return nullptr;
51 else
52 return m_choices[selected];
53}
54
55void SendableChooser::InitTable(std::shared_ptr<ITable> subtable) {
56 std::vector<std::string> keys;
57 m_table = subtable;
58 if (m_table != nullptr) {
59 std::map<std::string, void *>::iterator iter;
60 for (iter = m_choices.begin(); iter != m_choices.end(); iter++) {
61 keys.push_back(iter->first);
62 }
63 m_table->PutValue(kOptions, nt::Value::MakeStringArray(std::move(keys)));
64 m_table->PutString(kDefault, m_defaultChoice);
65 }
66}
67
68std::shared_ptr<ITable> SendableChooser::GetTable() const { return m_table; }
69
70std::string SendableChooser::GetSmartDashboardType() const {
71 return "String Chooser";
72}