Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2011-2017. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <algorithm> |
| 11 | #include <memory> |
| 12 | #include <string> |
| 13 | #include <utility> |
| 14 | #include <vector> |
| 15 | |
| 16 | namespace frc { |
| 17 | |
| 18 | /** |
| 19 | * Adds the given object to the list of options. |
| 20 | * |
| 21 | * On the {@link SmartDashboard} on the desktop, the object will appear as the |
| 22 | * given name. |
| 23 | * |
| 24 | * @param name the name of the option |
| 25 | * @param object the option |
| 26 | */ |
| 27 | template <class T> |
| 28 | void SendableChooser<T>::AddObject(llvm::StringRef name, const T& object) { |
| 29 | m_choices[name] = object; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Add the given object to the list of options and marks it as the default. |
| 34 | * |
| 35 | * Functionally, this is very close to {@link SendableChooser#AddObject(const |
| 36 | * char *name, void *object) AddObject(...)} except that it will use this as |
| 37 | * the default option if none other is explicitly selected. |
| 38 | * |
| 39 | * @param name the name of the option |
| 40 | * @param object the option |
| 41 | */ |
| 42 | template <class T> |
| 43 | void SendableChooser<T>::AddDefault(llvm::StringRef name, const T& object) { |
| 44 | m_defaultChoice = name; |
| 45 | AddObject(name, object); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Returns the selected option. |
| 50 | * |
| 51 | * If there is none selected, it will return the default. If there is none |
| 52 | * selected and no default, then it will return {@code nullptr}. |
| 53 | * |
| 54 | * @return the option selected |
| 55 | */ |
| 56 | template <class T> |
| 57 | T SendableChooser<T>::GetSelected() { |
| 58 | std::string selected = m_table->GetString(kSelected, m_defaultChoice); |
| 59 | if (selected == "") { |
| 60 | return nullptr; |
| 61 | } else { |
| 62 | return m_choices[selected]; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | template <class T> |
| 67 | void SendableChooser<T>::InitTable(std::shared_ptr<ITable> subtable) { |
| 68 | std::vector<std::string> keys; |
| 69 | m_table = subtable; |
| 70 | if (m_table != nullptr) { |
| 71 | for (const auto& choice : m_choices) { |
| 72 | keys.push_back(choice.first()); |
| 73 | } |
| 74 | |
| 75 | // Unlike std::map, llvm::StringMap elements are not sorted |
| 76 | std::sort(keys.begin(), keys.end()); |
| 77 | |
| 78 | m_table->PutValue(kOptions, nt::Value::MakeStringArray(std::move(keys))); |
| 79 | m_table->PutString(kDefault, m_defaultChoice); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | } // namespace frc |